internal static void InitializeModuleDef() { if (module_def == IntPtr.Zero) { module_def = ModuleDefOffset.AllocModuleDef("clr"); } }
internal static void ReleaseModuleDef() { if (module_def == IntPtr.Zero) { return; } ModuleDefOffset.FreeModuleDef(module_def); module_def = IntPtr.Zero; }
//=================================================================== // Cleanup resources upon shutdown of the Python runtime. //=================================================================== internal static void Shutdown() { #if (PYTHON32 || PYTHON33 || PYTHON34) Runtime.Decref(py_clr_module); Runtime.Decref(root.pyHandle); ModuleDefOffset.FreeModuleDef(module_def); #else Runtime.Decref(root.pyHandle); Runtime.Decref(root.pyHandle); #endif Runtime.Decref(py_import); }
//=================================================================== // Cleanup resources upon shutdown of the Python runtime. //=================================================================== internal static void Shutdown() { #if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35) if (0 != Runtime.Py_IsInitialized()) { Runtime.Decref(py_clr_module); Runtime.Decref(root.pyHandle); } ModuleDefOffset.FreeModuleDef(module_def); #else if (0 != Runtime.Py_IsInitialized()) { Runtime.Decref(root.pyHandle); Runtime.Decref(root.pyHandle); } #endif if (0 != Runtime.Py_IsInitialized()) { Runtime.Decref(py_import); } }
//=================================================================== // Initialization performed on startup of the Python runtime. //=================================================================== internal static void Initialize() { // Initialize the Python <--> CLR module hook. We replace the // built-in Python __import__ with our own. This isn't ideal, // but it provides the most "Pythonic" way of dealing with CLR // modules (Python doesn't provide a way to emulate packages). IntPtr dict = Runtime.PyImport_GetModuleDict(); #if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35) IntPtr mod = Runtime.PyImport_ImportModule("builtins"); py_import = Runtime.PyObject_GetAttrString(mod, "__import__"); #else IntPtr mod = Runtime.PyDict_GetItemString(dict, "__builtin__"); py_import = Runtime.PyObject_GetAttrString(mod, "__import__"); #endif hook = new MethodWrapper(typeof(ImportHook), "__import__", "TernaryFunc"); Runtime.PyObject_SetAttrString(mod, "__import__", hook.ptr); Runtime.Decref(hook.ptr); root = new CLRModule(); #if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35) // create a python module with the same methods as the clr module-like object module_def = ModuleDefOffset.AllocModuleDef("clr"); py_clr_module = Runtime.PyModule_Create2(module_def, 3); // both dicts are borrowed references IntPtr mod_dict = Runtime.PyModule_GetDict(py_clr_module); IntPtr clr_dict = Runtime._PyObject_GetDictPtr(root.pyHandle); // PyObject** clr_dict = (IntPtr)Marshal.PtrToStructure(clr_dict, typeof(IntPtr)); Runtime.PyDict_Update(mod_dict, clr_dict); Runtime.PyDict_SetItemString(dict, "CLR", py_clr_module); Runtime.PyDict_SetItemString(dict, "clr", py_clr_module); #else Runtime.Incref(root.pyHandle); // we are using the module two times Runtime.PyDict_SetItemString(dict, "CLR", root.pyHandle); Runtime.PyDict_SetItemString(dict, "clr", root.pyHandle); #endif }