Figure out what __import__ should return. The import_ parameter is a callable which takes the name of module to import. It is required to decouple the function from assuming importlib's import implementation is desired.
(module, fromlist, import_, *, recursive=False)
| 1399 | |
| 1400 | |
| 1401 | def _handle_fromlist(module, fromlist, import_, *, recursive=False): |
| 1402 | """Figure out what __import__ should return. |
| 1403 | |
| 1404 | The import_ parameter is a callable which takes the name of module to |
| 1405 | import. It is required to decouple the function from assuming importlib's |
| 1406 | import implementation is desired. |
| 1407 | |
| 1408 | """ |
| 1409 | # The hell that is fromlist ... |
| 1410 | # If a package was imported, try to import stuff from fromlist. |
| 1411 | for x in fromlist: |
| 1412 | if not isinstance(x, str): |
| 1413 | if recursive: |
| 1414 | where = module.__name__ + '.__all__' |
| 1415 | else: |
| 1416 | where = "``from list''" |
| 1417 | raise TypeError(f"Item in {where} must be str, " |
| 1418 | f"not {type(x).__name__}") |
| 1419 | elif x == '*': |
| 1420 | if not recursive and hasattr(module, '__all__'): |
| 1421 | _handle_fromlist(module, module.__all__, import_, |
| 1422 | recursive=True) |
| 1423 | elif not hasattr(module, x): |
| 1424 | from_name = f'{module.__name__}.{x}' |
| 1425 | try: |
| 1426 | _call_with_frames_removed(import_, from_name) |
| 1427 | except ModuleNotFoundError as exc: |
| 1428 | # Backwards-compatibility dictates we ignore failed |
| 1429 | # imports triggered by fromlist for modules that don't |
| 1430 | # exist. |
| 1431 | if (exc.name == from_name and |
| 1432 | sys.modules.get(from_name, _NEEDS_LOADING) is not None): |
| 1433 | continue |
| 1434 | raise |
| 1435 | return module |
| 1436 | |
| 1437 | |
| 1438 | def _calc___package__(globals): |
no test coverage detected