Find and load the module.
(name, import_)
| 1269 | |
| 1270 | |
| 1271 | def _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 | |
| 1305 | def _gcd_import(name, package=None, level=0): |
no test coverage detected
searching dependent graphs…