raise ImportError("sys.meta_path is None, Python is likely "
"shutting down")
+ # gh-130094: Copy sys.meta_path so that we have a consistent view of the
+ # list while iterating over it.
+ meta_path = list(meta_path)
if not meta_path:
_warnings.warn('sys.meta_path is empty', ImportWarning)
_ERR_MSG_PREFIX = 'No module named '
-_ERR_MSG = _ERR_MSG_PREFIX + '{!r}'
def _find_and_load_unlocked(name, import_):
path = None
if parent not in sys.modules:
_call_with_frames_removed(import_, parent)
# Crazy side-effects!
- if name in sys.modules:
- return sys.modules[name]
+ module = sys.modules.get(name)
+ if module is not None:
+ return module
parent_module = sys.modules[parent]
try:
path = parent_module.__path__
msg = f'{_ERR_MSG_PREFIX}{name!r}; {parent!r} is not a package'
raise ModuleNotFoundError(msg, name=name) from None
parent_spec = parent_module.__spec__
+ if getattr(parent_spec, '_initializing', False):
+ _call_with_frames_removed(import_, parent)
+ # Crazy side-effects (again)!
+ module = sys.modules.get(name)
+ if module is not None:
+ return module
child = name.rpartition('.')[2]
spec = _find_spec(name, path)
if spec is None: