@@ -183,15 +183,31 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw) {
183183 // do the Incref()ed return here, since we've already found
184184 // the module.
185185 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 ;
187194 }
188195 if ( mod_name == "CLR" ) {
189196 Exceptions . deprecation ( "The CLR module is deprecated. " +
190197 "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 ;
192206 }
193207 string realname = mod_name ;
208+ string clr_prefix = null ;
194209 if ( mod_name . StartsWith ( "CLR." ) ) {
210+ clr_prefix = "CLR." ; // prepend when adding the module to sys.modules
195211 realname = mod_name . Substring ( 4 ) ;
196212 string msg = String . Format ( "Importing from the CLR.* namespace " +
197213 "is deprecated. Please import '{0}' directly." , realname ) ;
@@ -251,6 +267,9 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw) {
251267 Runtime . Incref ( module ) ;
252268 return module ;
253269 }
270+ if ( clr_prefix != null ) {
271+ return GetCLRModule ( fromList ) ;
272+ }
254273 module = Runtime . PyDict_GetItemString ( modules , names [ 0 ] ) ;
255274 Runtime . Incref ( module ) ;
256275 return module ;
@@ -286,9 +305,18 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw) {
286305 if ( CLRModule . preload ) {
287306 tail . LoadNames ( ) ;
288307 }
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+ }
292320 }
293321
294322 ModuleObject mod = fromlist ? tail : head ;
0 commit comments