MCPcopy Index your code
hub / github.com/python/cpython / _find_and_load_unlocked

Function _find_and_load_unlocked

Lib/importlib/_bootstrap.py:1205–1265  ·  view source on GitHub ↗
(name, import_)

Source from the content-addressed store, hash-verified

1203_ERR_MSG_PREFIX = 'No module named '
1204
1205def _find_and_load_unlocked(name, import_):
1206 path = None
1207 sys.audit(
1208 "import",
1209 name,
1210 path,
1211 getattr(sys, "path", None),
1212 getattr(sys, "meta_path", None),
1213 getattr(sys, "path_hooks", None)
1214 )
1215 parent = name.rpartition('.')[0]
1216 parent_spec = None
1217 if parent:
1218 if parent not in sys.modules:
1219 _call_with_frames_removed(import_, parent)
1220 # Crazy side-effects!
1221 module = sys.modules.get(name)
1222 if module is not None:
1223 return module
1224 parent_module = sys.modules[parent]
1225 try:
1226 path = parent_module.__path__
1227 except AttributeError:
1228 msg = f'{_ERR_MSG_PREFIX}{name!r}; {parent!r} is not a package'
1229 raise ModuleNotFoundError(msg, name=name) from None
1230 parent_spec = parent_module.__spec__
1231 if getattr(parent_spec, '_initializing', False):
1232 _call_with_frames_removed(import_, parent)
1233 # Crazy side-effects (again)!
1234 module = sys.modules.get(name)
1235 if module is not None:
1236 return module
1237 child = name.rpartition('.')[2]
1238 spec = _find_spec(name, path)
1239 if spec is None:
1240 raise ModuleNotFoundError(f'{_ERR_MSG_PREFIX}{name!r}', name=name)
1241 else:
1242 if parent_spec:
1243 # Temporarily add child we are currently importing to parent's
1244 # _uninitialized_submodules for circular import tracking.
1245 parent_spec._uninitialized_submodules.append(child)
1246 try:
1247 module = _load_unlocked(spec)
1248 finally:
1249 if parent_spec:
1250 parent_spec._uninitialized_submodules.pop()
1251 if parent:
1252 # Set the module as an attribute on its parent.
1253 parent_module = sys.modules[parent]
1254 try:
1255 setattr(parent_module, child, module)
1256 except AttributeError:
1257 msg = f"Cannot set an attribute on {parent!r} for child module {child!r}"
1258 _warnings.warn(msg, ImportWarning)
1259 # Set attributes to lazy submodules on the module.
1260 try:
1261 _imp._set_lazy_attributes(module, name)
1262 except Exception as e:

Callers 1

_find_and_loadFunction · 0.85

Calls 8

_find_specFunction · 0.85
_load_unlockedFunction · 0.85
rpartitionMethod · 0.45
getMethod · 0.45
appendMethod · 0.45
popMethod · 0.45
warnMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…