(name, import_)
| 1306 | _ERR_MSG_PREFIX = 'No module named ' |
| 1307 | |
| 1308 | def _find_and_load_unlocked(name, import_): |
| 1309 | path = None |
| 1310 | parent = name.rpartition('.')[0] |
| 1311 | parent_spec = None |
| 1312 | if parent: |
| 1313 | if parent not in sys.modules: |
| 1314 | _call_with_frames_removed(import_, parent) |
| 1315 | # Crazy side-effects! |
| 1316 | module = sys.modules.get(name) |
| 1317 | if module is not None: |
| 1318 | return module |
| 1319 | parent_module = sys.modules[parent] |
| 1320 | try: |
| 1321 | path = parent_module.__path__ |
| 1322 | except AttributeError: |
| 1323 | msg = f'{_ERR_MSG_PREFIX}{name!r}; {parent!r} is not a package' |
| 1324 | raise ModuleNotFoundError(msg, name=name) from None |
| 1325 | parent_spec = parent_module.__spec__ |
| 1326 | if getattr(parent_spec, '_initializing', False): |
| 1327 | _call_with_frames_removed(import_, parent) |
| 1328 | # Crazy side-effects (again)! |
| 1329 | module = sys.modules.get(name) |
| 1330 | if module is not None: |
| 1331 | return module |
| 1332 | child = name.rpartition('.')[2] |
| 1333 | spec = _find_spec(name, path) |
| 1334 | if spec is None: |
| 1335 | raise ModuleNotFoundError(f'{_ERR_MSG_PREFIX}{name!r}', name=name) |
| 1336 | else: |
| 1337 | if parent_spec: |
| 1338 | # Temporarily add child we are currently importing to parent's |
| 1339 | # _uninitialized_submodules for circular import tracking. |
| 1340 | parent_spec._uninitialized_submodules.append(child) |
| 1341 | try: |
| 1342 | module = _load_unlocked(spec) |
| 1343 | finally: |
| 1344 | if parent_spec: |
| 1345 | parent_spec._uninitialized_submodules.pop() |
| 1346 | if parent: |
| 1347 | # Set the module as an attribute on its parent. |
| 1348 | parent_module = sys.modules[parent] |
| 1349 | try: |
| 1350 | setattr(parent_module, child, module) |
| 1351 | except AttributeError: |
| 1352 | msg = f"Cannot set an attribute on {parent!r} for child module {child!r}" |
| 1353 | _warnings.warn(msg, ImportWarning) |
| 1354 | return module |
| 1355 | |
| 1356 | |
| 1357 | _NEEDS_LOADING = object() |
no test coverage detected