@@ -86,10 +86,10 @@ public static object ImportFrom(CodeContext/*!*/ context, object from, string na
8686
8787 if ( scope . __dict__ . _storage . TryGetPath ( out object path ) ) {
8888 if ( path is PythonList listPath ) {
89- return ImportNestedModule ( context , scope , new [ ] { name } , 0 , listPath ) ;
89+ return ImportNestedModule ( context , scope , new ArraySegment < string > ( new [ ] { name } ) , listPath , scope . GetName ( ) ) ;
9090 }
9191 if ( path is string stringPath ) {
92- return ImportNestedModule ( context , scope , new [ ] { name } , 0 , PythonList . FromArrayNoCopy ( stringPath ) ) ;
92+ return ImportNestedModule ( context , scope , new ArraySegment < string > ( new [ ] { name } ) , PythonList . FromArrayNoCopy ( stringPath ) , scope . GetName ( ) ) ;
9393 }
9494 }
9595 } else if ( from is PythonType pt ) {
@@ -112,25 +112,26 @@ public static object ImportFrom(CodeContext/*!*/ context, object from, string na
112112 throw PythonOps . ImportError ( "Cannot import name {0}" , name ) ;
113113 }
114114
115- private static object ImportModuleFrom ( CodeContext /*!*/ context , object from , string [ ] parts , int current ) {
115+ private static object ImportModuleFrom ( CodeContext /*!*/ context , object from , ArraySegment < string > parts , object root ) {
116116 if ( from is PythonModule scope ) {
117117 if ( scope . __dict__ . _storage . TryGetPath ( out object path ) || DynamicHelpers . GetPythonType ( scope ) . TryGetMember ( context , scope , "__path__" , out path ) ) {
118118 if ( path is PythonList listPath ) {
119- return ImportNestedModule ( context , scope , parts , current , listPath ) ;
119+ return ImportNestedModule ( context , scope , parts , listPath , ( root as PythonModule ) ? . GetName ( ) ) ;
120120 }
121121 if ( path is string stringPath ) {
122- return ImportNestedModule ( context , scope , parts , current , PythonList . FromArrayNoCopy ( stringPath ) ) ;
122+ return ImportNestedModule ( context , scope , parts , PythonList . FromArrayNoCopy ( stringPath ) , ( root as PythonModule ) ? . GetName ( ) ) ;
123123 }
124124 }
125125 }
126126
127+ string name = parts . Array [ parts . Offset + parts . Count - 1 ] ;
127128 if ( from is NamespaceTracker ns ) {
128- if ( ns . TryGetValue ( parts [ current ] , out object val ) ) {
129+ if ( ns . TryGetValue ( name , out object val ) ) {
129130 return MemberTrackerToPython ( context , val ) ;
130131 }
131132 }
132133
133- throw PythonOps . ImportError ( "No module named {0}" , parts [ current ] ) ;
134+ throw PythonOps . ImportError ( "No module named {0}" , name ) ;
134135 }
135136
136137 /// <summary>
@@ -262,7 +263,7 @@ public static object ImportModule(CodeContext/*!*/ context, object globals, stri
262263 }
263264 } else if ( i != 0 ) {
264265 // child module isn't loaded yet, import it.
265- next = ImportModuleFrom ( context , next , parts , i ) ;
266+ next = ImportModuleFrom ( context , next , new ArraySegment < string > ( parts , 1 , i ) , newmod ) ;
266267 } else {
267268 // top-level module doesn't exist in sys.modules, probably
268269 // came from some weird meta path hook.
@@ -603,17 +604,21 @@ private static bool TryGetNestedModule(CodeContext/*!*/ context, PythonModule/*!
603604 }
604605
605606 private static object ImportNestedModule ( CodeContext /*!*/ context , PythonModule /*!*/ module ,
606- string [ ] parts , int current , PythonList /*!*/ path ) {
607+ ArraySegment < string > parts , PythonList /*!*/ path , string scopeModuleName ) {
608+ Debug . Assert ( parts . Array is not null ) ;
609+ Debug . Assert ( parts . Count > 0 ) ;
610+
607611 object ret ;
608- string name = parts [ current ] ;
609- string fullName = CreateFullName ( module . GetName ( ) as string , name ) ;
612+ int current = parts . Offset + parts . Count - 1 ;
613+ string name = parts . Array [ current ] ;
614+ string fullName = CreateFullName ( scopeModuleName , parts ) ;
610615
611616 if ( TryGetExistingOrMetaPathModule ( context , fullName , path , out ret ) ) {
612617 module . __dict__ [ name ] = ret ;
613618 return ret ;
614619 }
615620
616- if ( TryGetNestedModule ( context , module , parts , current , out ret ) ) {
621+ if ( TryGetNestedModule ( context , module , parts . Array , current , out ret ) ) {
617622 return ret ;
618623 }
619624
@@ -762,11 +767,11 @@ private static bool IsReflected(object module) {
762767 || module is BuiltinFunction ;
763768 }
764769
765- private static string CreateFullName ( string /*!*/ baseName , string name ) {
770+ private static string CreateFullName ( string /*!*/ baseName , ArraySegment < string > parts ) {
766771 if ( baseName == null || baseName . Length == 0 || baseName == "__main__" ) {
767- return name ;
772+ return string . Join ( "." , parts ) ;
768773 }
769- return baseName + "." + name ;
774+ return baseName + "." + string . Join ( "." , parts ) ;
770775 }
771776
772777 #endregion
0 commit comments