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)
| 1318 | |
| 1319 | |
| 1320 | def _handle_fromlist(module, fromlist, import_, *, recursive=False): |
| 1321 | """Figure out what __import__ should return. |
| 1322 | |
| 1323 | The import_ parameter is a callable which takes the name of module to |
| 1324 | import. It is required to decouple the function from assuming importlib's |
| 1325 | import implementation is desired. |
| 1326 | |
| 1327 | """ |
| 1328 | # The hell that is fromlist ... |
| 1329 | # If a package was imported, try to import stuff from fromlist. |
| 1330 | for x in fromlist: |
| 1331 | if not isinstance(x, str): |
| 1332 | if recursive: |
| 1333 | where = module.__name__ + '.__all__' |
| 1334 | else: |
| 1335 | where = "``from list''" |
| 1336 | raise TypeError(f"Item in {where} must be str, " |
| 1337 | f"not {type(x).__name__}") |
| 1338 | elif x == '*': |
| 1339 | if not recursive and hasattr(module, '__all__'): |
| 1340 | _handle_fromlist(module, module.__all__, import_, |
| 1341 | recursive=True) |
| 1342 | elif not hasattr(module, x): |
| 1343 | from_name = f'{module.__name__}.{x}' |
| 1344 | try: |
| 1345 | _call_with_frames_removed(import_, from_name) |
| 1346 | except ModuleNotFoundError as exc: |
| 1347 | # Backwards-compatibility dictates we ignore failed |
| 1348 | # imports triggered by fromlist for modules that don't |
| 1349 | # exist. |
| 1350 | if (exc.name == from_name and |
| 1351 | sys.modules.get(from_name, _NEEDS_LOADING) is not None): |
| 1352 | continue |
| 1353 | raise |
| 1354 | return module |
| 1355 | |
| 1356 | |
| 1357 | def _calc___package__(globals): |
no test coverage detected
searching dependent graphs…