From 4488509a40a0521bb4705903870f00787bd041bc Mon Sep 17 00:00:00 2001 From: LimpingNinja Date: Sun, 25 Jul 2021 12:05:21 +0200 Subject: [PATCH 1/2] Added Create, Find, Get methods to PyModule to support what I need until the functionality is crystallized in pythonnet:master --- src/runtime/pymodule.cs | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/runtime/pymodule.cs b/src/runtime/pymodule.cs index 800edb686..44a96a7e0 100644 --- a/src/runtime/pymodule.cs +++ b/src/runtime/pymodule.cs @@ -37,5 +37,53 @@ public static PyModule FromString(string name, string code) PythonException.ThrowIfIsNull(m); return new PyModule(ref m); } + + public static bool Exists(string name) + { + // first check if there is an existing module + BorrowedReference modules = Runtime.PyImport_GetModuleDict(); + BorrowedReference m = Runtime.PyDict_GetItemString(modules, name); + return !m.IsNull; + } + + public static PyModule Get(string name) + { + NewReference op; + + // first check if there is an existing module + BorrowedReference modules = Runtime.PyImport_GetModuleDict(); + BorrowedReference m = Runtime.PyDict_GetItemString(modules, name); + // there is not a module by this name + if (m.IsNull || !Runtime.PyObject_TypeCheck(m, new BorrowedReference(Runtime.PyModuleType))) return null; + // return module + op = new NewReference(m); + return new PyModule(ref op); + } + + public static PyModule Create(string name, string filename = "none") + { + if (Exists(name)) return null; + BorrowedReference modules = Runtime.PyImport_GetModuleDict(); + // create a new module + var op = Runtime.PyModule_New(name); + PythonException.ThrowIfIsNull(op); + + // setup the module basics (__builtins__ and __file__) + BorrowedReference globals = Runtime.PyModule_GetDict(new BorrowedReference(op.DangerousGetAddress())); + PythonException.ThrowIfIsNull(globals); + BorrowedReference __builtins__ = Runtime.PyEval_GetBuiltins(); + PythonException.ThrowIfIsNull(__builtins__); + int rc = Runtime.PyDict_SetItemString(globals, "__builtins__", __builtins__); + PythonException.ThrowIfIsNotZero(rc); + rc = Runtime.PyDict_SetItemString(globals, "__file__", + new BorrowedReference(filename.ToPython().Handle)); + PythonException.ThrowIfIsNotZero(rc); + + // add to sys.modules + rc = Runtime.PyDict_SetItemString(modules, name, op); + PythonException.ThrowIfIsNotZero(rc); + + return new PyModule(ref op); + } } } From bf2843297a94355ba42d0ca2316c04fb8209b89a Mon Sep 17 00:00:00 2001 From: LimpingNinja Date: Sun, 25 Jul 2021 12:11:47 +0200 Subject: [PATCH 2/2] Removed Get for now. YAGNI. --- src/runtime/pymodule.cs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/runtime/pymodule.cs b/src/runtime/pymodule.cs index 44a96a7e0..d361c3f3f 100644 --- a/src/runtime/pymodule.cs +++ b/src/runtime/pymodule.cs @@ -46,20 +46,6 @@ public static bool Exists(string name) return !m.IsNull; } - public static PyModule Get(string name) - { - NewReference op; - - // first check if there is an existing module - BorrowedReference modules = Runtime.PyImport_GetModuleDict(); - BorrowedReference m = Runtime.PyDict_GetItemString(modules, name); - // there is not a module by this name - if (m.IsNull || !Runtime.PyObject_TypeCheck(m, new BorrowedReference(Runtime.PyModuleType))) return null; - // return module - op = new NewReference(m); - return new PyModule(ref op); - } - public static PyModule Create(string name, string filename = "none") { if (Exists(name)) return null;