@@ -1245,6 +1245,9 @@ def _find_spec(name, path, target=None):
12451245 raise ImportError ("sys.meta_path is None, Python is likely "
12461246 "shutting down" )
12471247
1248+ # gh-130094: Copy sys.meta_path so that we have a consistent view of the
1249+ # list while iterating over it.
1250+ meta_path = list (meta_path )
12481251 if not meta_path :
12491252 _warnings .warn ('sys.meta_path is empty' , ImportWarning )
12501253
@@ -1299,7 +1302,6 @@ def _sanity_check(name, package, level):
12991302
13001303
13011304_ERR_MSG_PREFIX = 'No module named '
1302- _ERR_MSG = _ERR_MSG_PREFIX + '{!r}'
13031305
13041306def _find_and_load_unlocked (name , import_ ):
13051307 path = None
@@ -1309,15 +1311,22 @@ def _find_and_load_unlocked(name, import_):
13091311 if parent not in sys .modules :
13101312 _call_with_frames_removed (import_ , parent )
13111313 # Crazy side-effects!
1312- if name in sys .modules :
1313- return sys .modules [name ]
1314+ module = sys .modules .get (name )
1315+ if module is not None :
1316+ return module
13141317 parent_module = sys .modules [parent ]
13151318 try :
13161319 path = parent_module .__path__
13171320 except AttributeError :
13181321 msg = f'{ _ERR_MSG_PREFIX } { name !r} ; { parent !r} is not a package'
13191322 raise ModuleNotFoundError (msg , name = name ) from None
13201323 parent_spec = parent_module .__spec__
1324+ if getattr (parent_spec , '_initializing' , False ):
1325+ _call_with_frames_removed (import_ , parent )
1326+ # Crazy side-effects (again)!
1327+ module = sys .modules .get (name )
1328+ if module is not None :
1329+ return module
13211330 child = name .rpartition ('.' )[2 ]
13221331 spec = _find_spec (name , path )
13231332 if spec is None :
0 commit comments