diff --git a/src/runtime/importhook.cs b/src/runtime/importhook.cs index 68d81ac14..e5843d436 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); } @@ -73,13 +71,9 @@ internal static void Initialize() /// internal static void Shutdown() { - if (0 != Runtime.Py_IsInitialized()) + if (Runtime.Py_IsInitialized() != 0) { -#if PYTHON3 Runtime.XDecref(py_clr_module); -#elif PYTHON2 - Runtime.XDecref(root.pyHandle); -#endif Runtime.XDecref(root.pyHandle); Runtime.XDecref(py_import); } @@ -117,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); @@ -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 } /// @@ -200,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) { @@ -315,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)) {