Find and load the module.
(name, import_)
| 1358 | |
| 1359 | |
| 1360 | def _find_and_load(name, import_): |
| 1361 | """Find and load the module.""" |
| 1362 | |
| 1363 | # Optimization: we avoid unneeded module locking if the module |
| 1364 | # already exists in sys.modules and is fully initialized. |
| 1365 | module = sys.modules.get(name, _NEEDS_LOADING) |
| 1366 | if (module is _NEEDS_LOADING or |
| 1367 | getattr(getattr(module, "__spec__", None), "_initializing", False)): |
| 1368 | with _ModuleLockManager(name): |
| 1369 | module = sys.modules.get(name, _NEEDS_LOADING) |
| 1370 | if module is _NEEDS_LOADING: |
| 1371 | return _find_and_load_unlocked(name, import_) |
| 1372 | |
| 1373 | # Optimization: only call _bootstrap._lock_unlock_module() if |
| 1374 | # module.__spec__._initializing is True. |
| 1375 | # NOTE: because of this, initializing must be set *before* |
| 1376 | # putting the new module in sys.modules. |
| 1377 | _lock_unlock_module(name) |
| 1378 | |
| 1379 | if module is None: |
| 1380 | message = f'import of {name} halted; None in sys.modules' |
| 1381 | raise ModuleNotFoundError(message, name=name) |
| 1382 | |
| 1383 | return module |
| 1384 | |
| 1385 | |
| 1386 | def _gcd_import(name, package=None, level=0): |
no test coverage detected