@@ -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
@@ -73,13 +71,9 @@ internal static void Initialize()
7371 /// </summary>
7472 internal static void Shutdown ( )
7573 {
76- if ( 0 != Runtime . Py_IsInitialized ( ) )
74+ if ( Runtime . Py_IsInitialized ( ) != 0 )
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 }
@@ -117,11 +111,11 @@ public static IntPtr GetCLRModule(IntPtr? fromList = null)
117111 continue ;
118112
119113 string s = item . AsManagedObject ( typeof ( string ) ) as string ;
120- if ( null == s )
114+ if ( s == null )
121115 continue ;
122116
123117 ManagedType attr = root . GetAttribute ( s , true ) ;
124- if ( null == attr )
118+ if ( attr == null )
125119 continue ;
126120
127121 Runtime . XIncref ( attr . pyHandle ) ;
@@ -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>
@@ -200,7 +190,7 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
200190 }
201191 if ( mod_name == "CLR" )
202192 {
203- Exceptions . deprecation ( "The CLR module is deprecated. " + " Please use 'clr'.") ;
193+ Exceptions . deprecation ( "The CLR module is deprecated. Please use 'clr'." ) ;
204194 IntPtr clr_module = GetCLRModule ( fromList ) ;
205195 if ( clr_module != IntPtr . Zero )
206196 {
@@ -315,9 +305,8 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
315305 ModuleObject tail = root ;
316306 root . InitializePreload ( ) ;
317307
318- for ( int i = 0 ; i < names . Length ; i ++ )
308+ foreach ( string name in names )
319309 {
320- string name = names [ i ] ;
321310 ManagedType mt = tail . GetAttribute ( name , true ) ;
322311 if ( ! ( mt is ModuleObject ) )
323312 {
0 commit comments