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

Function _find_and_load

Lib/importlib/_bootstrap.py:1271–1302  ·  view source on GitHub ↗

Find and load the module.

(name, import_)

Source from the content-addressed store, hash-verified

1269
1270
1271def _find_and_load(name, import_):
1272 """Find and load the module."""
1273
1274 # Optimization: we avoid unneeded module locking if the module
1275 # already exists in sys.modules and is fully initialized.
1276 module = sys.modules.get(name, _NEEDS_LOADING)
1277 if (module is _NEEDS_LOADING or
1278 getattr(getattr(module, "__spec__", None), "_initializing", False)):
1279 with _ModuleLockManager(name):
1280 module = sys.modules.get(name, _NEEDS_LOADING)
1281 if module is _NEEDS_LOADING:
1282 return _find_and_load_unlocked(name, import_)
1283
1284 # Optimization: only call _bootstrap._lock_unlock_module() if
1285 # module.__spec__._initializing is True.
1286 # NOTE: because of this, initializing must be set *before*
1287 # putting the new module in sys.modules.
1288 _lock_unlock_module(name)
1289 else:
1290 # Verify the module is still in sys.modules. Another thread may have
1291 # removed it (due to import failure) between our sys.modules.get()
1292 # above and the _initializing check. If removed, we retry the import
1293 # to preserve normal semantics: the caller gets the exception from
1294 # the actual import failure rather than a synthetic error.
1295 if sys.modules.get(name) is not module:
1296 return _find_and_load(name, import_)
1297
1298 if module is None:
1299 message = f'import of {name} halted; None in sys.modules'
1300 raise ModuleNotFoundError(message, name=name)
1301
1302 return module
1303
1304
1305def _gcd_import(name, package=None, level=0):

Callers 1

_gcd_importFunction · 0.85

Calls 4

_ModuleLockManagerClass · 0.85
_find_and_load_unlockedFunction · 0.85
_lock_unlock_moduleFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…