@@ -183,15 +183,31 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw) {
183
183
// do the Incref()ed return here, since we've already found
184
184
// the module.
185
185
if ( mod_name == "clr" ) {
186
- return GetCLRModule ( fromList ) ;
186
+ IntPtr clr_module = GetCLRModule ( fromList ) ;
187
+ if ( clr_module != IntPtr . Zero ) {
188
+ IntPtr sys_modules = Runtime . PyImport_GetModuleDict ( ) ;
189
+ if ( sys_modules != IntPtr . Zero ) {
190
+ Runtime . PyDict_SetItemString ( sys_modules , "clr" , clr_module ) ;
191
+ }
192
+ }
193
+ return clr_module ;
187
194
}
188
195
if ( mod_name == "CLR" ) {
189
196
Exceptions . deprecation ( "The CLR module is deprecated. " +
190
197
"Please use 'clr'." ) ;
191
- return GetCLRModule ( fromList ) ;
198
+ IntPtr clr_module = GetCLRModule ( fromList ) ;
199
+ if ( clr_module != IntPtr . Zero ) {
200
+ IntPtr sys_modules = Runtime . PyImport_GetModuleDict ( ) ;
201
+ if ( sys_modules != IntPtr . Zero ) {
202
+ Runtime . PyDict_SetItemString ( sys_modules , "clr" , clr_module ) ;
203
+ }
204
+ }
205
+ return clr_module ;
192
206
}
193
207
string realname = mod_name ;
208
+ string clr_prefix = null ;
194
209
if ( mod_name . StartsWith ( "CLR." ) ) {
210
+ clr_prefix = "CLR." ; // prepend when adding the module to sys.modules
195
211
realname = mod_name . Substring ( 4 ) ;
196
212
string msg = String . Format ( "Importing from the CLR.* namespace " +
197
213
"is deprecated. Please import '{0}' directly." , realname ) ;
@@ -251,6 +267,9 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw) {
251
267
Runtime . Incref ( module ) ;
252
268
return module ;
253
269
}
270
+ if ( clr_prefix != null ) {
271
+ return GetCLRModule ( fromList ) ;
272
+ }
254
273
module = Runtime . PyDict_GetItemString ( modules , names [ 0 ] ) ;
255
274
Runtime . Incref ( module ) ;
256
275
return module ;
@@ -286,9 +305,18 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw) {
286
305
if ( CLRModule . preload ) {
287
306
tail . LoadNames ( ) ;
288
307
}
289
- Runtime . PyDict_SetItemString ( modules , tail . moduleName ,
290
- tail . pyHandle
291
- ) ;
308
+
309
+ // Add the module to sys.modules
310
+ Runtime . PyDict_SetItemString ( modules ,
311
+ tail . moduleName ,
312
+ tail . pyHandle ) ;
313
+
314
+ // If imported from CLR add CLR.<modulename> to sys.modules as well
315
+ if ( clr_prefix != null ) {
316
+ Runtime . PyDict_SetItemString ( modules ,
317
+ clr_prefix + tail . moduleName ,
318
+ tail . pyHandle ) ;
319
+ }
292
320
}
293
321
294
322
ModuleObject mod = fromlist ? tail : head ;
0 commit comments