@@ -12,9 +12,9 @@ internal class ImportHook
1212 static IntPtr py_import ;
1313 static CLRModule root ;
1414 static MethodWrapper hook ;
15+ static IntPtr py_clr_module ;
1516
1617#if PYTHON3
17- static IntPtr py_clr_module ;
1818 static IntPtr module_def = IntPtr . Zero ;
1919
2020 internal static void InitializeModuleDef ( )
@@ -36,11 +36,10 @@ internal static void Initialize()
3636 IntPtr dict = Runtime . PyImport_GetModuleDict ( ) ;
3737#if PYTHON3
3838 IntPtr mod = Runtime . PyImport_ImportModule ( "builtins" ) ;
39- py_import = Runtime . PyObject_GetAttrString ( mod , "__import__" ) ;
4039#elif PYTHON2
4140 IntPtr mod = Runtime . PyDict_GetItemString ( dict , "__builtin__" ) ;
42- py_import = Runtime . PyObject_GetAttrString ( mod , "__import__" ) ;
4341#endif
42+ py_import = Runtime . PyObject_GetAttrString ( mod , "__import__" ) ;
4443 hook = new MethodWrapper ( typeof ( ImportHook ) , "__import__" , "TernaryFunc" ) ;
4544 Runtime . PyObject_SetAttrString ( mod , "__import__" , hook . ptr ) ;
4645 Runtime . XDecref ( hook . ptr ) ;
@@ -58,13 +57,12 @@ internal static void Initialize()
5857 clr_dict = ( IntPtr ) Marshal . PtrToStructure ( clr_dict , typeof ( IntPtr ) ) ;
5958
6059 Runtime . PyDict_Update ( mod_dict , clr_dict ) ;
61- Runtime . PyDict_SetItemString ( dict , "CLR" , py_clr_module ) ;
62- Runtime . PyDict_SetItemString ( dict , "clr" , py_clr_module ) ;
6360#elif PYTHON2
6461 Runtime . XIncref ( root . pyHandle ) ; // we are using the module two times
65- Runtime . PyDict_SetItemString ( dict , "CLR" , root . pyHandle ) ;
66- Runtime . PyDict_SetItemString ( dict , "clr" , root . pyHandle ) ;
62+ py_clr_module = root . pyHandle ; // Alias handle for PY2/PY3
6763#endif
64+ Runtime . PyDict_SetItemString ( dict , "CLR" , py_clr_module ) ;
65+ Runtime . PyDict_SetItemString ( dict , "clr" , py_clr_module ) ;
6866 }
6967
7068
@@ -75,11 +73,7 @@ internal static void Shutdown()
7573 {
7674 if ( 0 != Runtime . Py_IsInitialized ( ) )
7775 {
78- #if PYTHON3
7976 Runtime . XDecref ( py_clr_module ) ;
80- #elif PYTHON2
81- Runtime . XDecref ( root . pyHandle ) ;
82- #endif
8377 Runtime . XDecref ( root . pyHandle ) ;
8478 Runtime . XDecref ( py_import ) ;
8579 }
@@ -134,13 +128,9 @@ public static IntPtr GetCLRModule(IntPtr? fromList = null)
134128 }
135129 }
136130 }
137-
131+ #endif
138132 Runtime . XIncref ( py_clr_module ) ;
139133 return py_clr_module ;
140- #elif PYTHON2
141- Runtime . XIncref ( root . pyHandle ) ;
142- return root . pyHandle ;
143- #endif
144134 }
145135
146136 /// <summary>
0 commit comments