From 26ce7d8badb50f5dbee937c26070fd3855977fd4 Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Wed, 1 Feb 2017 11:17:47 -0700 Subject: [PATCH 1/2] Alias root.pyHandle to py_clr_module --- src/runtime/importhook.cs | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/runtime/importhook.cs b/src/runtime/importhook.cs index 68d81ac14..255be0d5a 100644 --- a/src/runtime/importhook.cs +++ b/src/runtime/importhook.cs @@ -12,9 +12,9 @@ internal class ImportHook static IntPtr py_import; static CLRModule root; static MethodWrapper hook; + static IntPtr py_clr_module; #if PYTHON3 - static IntPtr py_clr_module; static IntPtr module_def = IntPtr.Zero; internal static void InitializeModuleDef() @@ -36,11 +36,10 @@ internal static void Initialize() IntPtr dict = Runtime.PyImport_GetModuleDict(); #if PYTHON3 IntPtr mod = Runtime.PyImport_ImportModule("builtins"); - py_import = Runtime.PyObject_GetAttrString(mod, "__import__"); #elif PYTHON2 IntPtr mod = Runtime.PyDict_GetItemString(dict, "__builtin__"); - py_import = Runtime.PyObject_GetAttrString(mod, "__import__"); #endif + py_import = Runtime.PyObject_GetAttrString(mod, "__import__"); hook = new MethodWrapper(typeof(ImportHook), "__import__", "TernaryFunc"); Runtime.PyObject_SetAttrString(mod, "__import__", hook.ptr); Runtime.XDecref(hook.ptr); @@ -58,13 +57,12 @@ internal static void Initialize() 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); #elif PYTHON2 Runtime.XIncref(root.pyHandle); // we are using the module two times - Runtime.PyDict_SetItemString(dict, "CLR", root.pyHandle); - Runtime.PyDict_SetItemString(dict, "clr", root.pyHandle); + py_clr_module = root.pyHandle; // Alias handle for PY2/PY3 #endif + Runtime.PyDict_SetItemString(dict, "CLR", py_clr_module); + Runtime.PyDict_SetItemString(dict, "clr", py_clr_module); } @@ -75,11 +73,7 @@ internal static void Shutdown() { if (0 != Runtime.Py_IsInitialized()) { -#if PYTHON3 Runtime.XDecref(py_clr_module); -#elif PYTHON2 - Runtime.XDecref(root.pyHandle); -#endif Runtime.XDecref(root.pyHandle); Runtime.XDecref(py_import); } @@ -134,13 +128,9 @@ public static IntPtr GetCLRModule(IntPtr? fromList = null) } } } - +#endif Runtime.XIncref(py_clr_module); return py_clr_module; -#elif PYTHON2 - Runtime.XIncref(root.pyHandle); - return root.pyHandle; -#endif } /// From 0e74f869ad1e6423b3680858f9e43144fb75271a Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Wed, 1 Feb 2017 11:57:51 -0700 Subject: [PATCH 2/2] Syntax cleanup --- src/runtime/importhook.cs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/runtime/importhook.cs b/src/runtime/importhook.cs index 255be0d5a..e5843d436 100644 --- a/src/runtime/importhook.cs +++ b/src/runtime/importhook.cs @@ -71,7 +71,7 @@ internal static void Initialize() /// internal static void Shutdown() { - if (0 != Runtime.Py_IsInitialized()) + if (Runtime.Py_IsInitialized() != 0) { Runtime.XDecref(py_clr_module); Runtime.XDecref(root.pyHandle); @@ -111,11 +111,11 @@ public static IntPtr GetCLRModule(IntPtr? fromList = null) continue; string s = item.AsManagedObject(typeof(string)) as string; - if (null == s) + if (s == null) continue; ManagedType attr = root.GetAttribute(s, true); - if (null == attr) + if (attr == null) continue; Runtime.XIncref(attr.pyHandle); @@ -190,7 +190,7 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw) } if (mod_name == "CLR") { - Exceptions.deprecation("The CLR module is deprecated. " + "Please use 'clr'."); + Exceptions.deprecation("The CLR module is deprecated. Please use 'clr'."); IntPtr clr_module = GetCLRModule(fromList); if (clr_module != IntPtr.Zero) { @@ -305,9 +305,8 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw) ModuleObject tail = root; root.InitializePreload(); - for (int i = 0; i < names.Length; i++) + foreach (string name in names) { - string name = names[i]; ManagedType mt = tail.GetAttribute(name, true); if (!(mt is ModuleObject)) {