@@ -12,9 +12,9 @@ internal class ImportHook
12
12
static IntPtr py_import ;
13
13
static CLRModule root ;
14
14
static MethodWrapper hook ;
15
+ static IntPtr py_clr_module ;
15
16
16
17
#if PYTHON3
17
- static IntPtr py_clr_module ;
18
18
static IntPtr module_def = IntPtr . Zero ;
19
19
20
20
internal static void InitializeModuleDef ( )
@@ -36,11 +36,10 @@ internal static void Initialize()
36
36
IntPtr dict = Runtime . PyImport_GetModuleDict ( ) ;
37
37
#if PYTHON3
38
38
IntPtr mod = Runtime . PyImport_ImportModule ( "builtins" ) ;
39
- py_import = Runtime . PyObject_GetAttrString ( mod , "__import__" ) ;
40
39
#elif PYTHON2
41
40
IntPtr mod = Runtime . PyDict_GetItemString ( dict , "__builtin__" ) ;
42
- py_import = Runtime . PyObject_GetAttrString ( mod , "__import__" ) ;
43
41
#endif
42
+ py_import = Runtime . PyObject_GetAttrString ( mod , "__import__" ) ;
44
43
hook = new MethodWrapper ( typeof ( ImportHook ) , "__import__" , "TernaryFunc" ) ;
45
44
Runtime . PyObject_SetAttrString ( mod , "__import__" , hook . ptr ) ;
46
45
Runtime . XDecref ( hook . ptr ) ;
@@ -58,13 +57,12 @@ internal static void Initialize()
58
57
clr_dict = ( IntPtr ) Marshal . PtrToStructure ( clr_dict , typeof ( IntPtr ) ) ;
59
58
60
59
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 ) ;
63
60
#elif PYTHON2
64
61
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
67
63
#endif
64
+ Runtime . PyDict_SetItemString ( dict , "CLR" , py_clr_module ) ;
65
+ Runtime . PyDict_SetItemString ( dict , "clr" , py_clr_module ) ;
68
66
}
69
67
70
68
@@ -73,13 +71,9 @@ internal static void Initialize()
73
71
/// </summary>
74
72
internal static void Shutdown ( )
75
73
{
76
- if ( 0 != Runtime . Py_IsInitialized ( ) )
74
+ if ( Runtime . Py_IsInitialized ( ) != 0 )
77
75
{
78
- #if PYTHON3
79
76
Runtime . XDecref ( py_clr_module ) ;
80
- #elif PYTHON2
81
- Runtime . XDecref ( root . pyHandle ) ;
82
- #endif
83
77
Runtime . XDecref ( root . pyHandle ) ;
84
78
Runtime . XDecref ( py_import ) ;
85
79
}
@@ -117,11 +111,11 @@ public static IntPtr GetCLRModule(IntPtr? fromList = null)
117
111
continue ;
118
112
119
113
string s = item . AsManagedObject ( typeof ( string ) ) as string ;
120
- if ( null == s )
114
+ if ( s == null )
121
115
continue ;
122
116
123
117
ManagedType attr = root . GetAttribute ( s , true ) ;
124
- if ( null == attr )
118
+ if ( attr == null )
125
119
continue ;
126
120
127
121
Runtime . XIncref ( attr . pyHandle ) ;
@@ -134,13 +128,9 @@ public static IntPtr GetCLRModule(IntPtr? fromList = null)
134
128
}
135
129
}
136
130
}
137
-
131
+ #endif
138
132
Runtime . XIncref ( py_clr_module ) ;
139
133
return py_clr_module ;
140
- #elif PYTHON2
141
- Runtime . XIncref ( root . pyHandle ) ;
142
- return root . pyHandle ;
143
- #endif
144
134
}
145
135
146
136
/// <summary>
@@ -200,7 +190,7 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
200
190
}
201
191
if ( mod_name == "CLR" )
202
192
{
203
- Exceptions . deprecation ( "The CLR module is deprecated. " + " Please use 'clr'.") ;
193
+ Exceptions . deprecation ( "The CLR module is deprecated. Please use 'clr'." ) ;
204
194
IntPtr clr_module = GetCLRModule ( fromList ) ;
205
195
if ( clr_module != IntPtr . Zero )
206
196
{
@@ -315,9 +305,8 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
315
305
ModuleObject tail = root ;
316
306
root . InitializePreload ( ) ;
317
307
318
- for ( int i = 0 ; i < names . Length ; i ++ )
308
+ foreach ( string name in names )
319
309
{
320
- string name = names [ i ] ;
321
310
ManagedType mt = tail . GetAttribute ( name , true ) ;
322
311
if ( ! ( mt is ModuleObject ) )
323
312
{
0 commit comments