diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-10-26 09:02:54 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-26 09:02:54 (GMT) |
commit | 2b5cbbb13c6c9138d04c3ca4eb7431f8c65d8e65 (patch) | |
tree | 24d62b53398b6cc121835eeb54fdbfeb62180fbb /Lib/importlib | |
parent | 614ea48986f80d361043510ac3945f3dcd666c31 (diff) | |
download | cpython-2b5cbbb13c6c9138d04c3ca4eb7431f8c65d8e65.zip cpython-2b5cbbb13c6c9138d04c3ca4eb7431f8c65d8e65.tar.gz cpython-2b5cbbb13c6c9138d04c3ca4eb7431f8c65d8e65.tar.bz2 |
[3.6] bpo-21720: Restore the Python 2.7 logic in handling a fromlist. (GH-4118) (#4128)
BytesWarning no longer emitted when the fromlist argument of
__import__() or the __all__ attribute of the module contain bytes
instances..
(cherry picked from commit 41c56940c6edf3ea169332a6b039b6c8796f0475)
Diffstat (limited to 'Lib/importlib')
-rw-r--r-- | Lib/importlib/_bootstrap.py | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index 4fc5f02..e2343dd 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -994,7 +994,7 @@ def _gcd_import(name, package=None, level=0): return _find_and_load(name, _gcd_import) -def _handle_fromlist(module, fromlist, import_): +def _handle_fromlist(module, fromlist, import_, *, recursive=False): """Figure out what __import__ should return. The import_ parameter is a callable which takes the name of module to @@ -1005,13 +1005,19 @@ def _handle_fromlist(module, fromlist, import_): # The hell that is fromlist ... # If a package was imported, try to import stuff from fromlist. if hasattr(module, '__path__'): - if '*' in fromlist: - fromlist = list(fromlist) - fromlist.remove('*') - if hasattr(module, '__all__'): - fromlist.extend(module.__all__) for x in fromlist: - if not hasattr(module, x): + if not isinstance(x, str): + if recursive: + where = module.__name__ + '.__all__' + else: + where = "``from list''" + raise TypeError(f"Item in {where} must be str, " + f"not {type(x).__name__}") + elif x == '*': + if not recursive and hasattr(module, '__all__'): + _handle_fromlist(module, module.__all__, import_, + recursive=True) + elif not hasattr(module, x): from_name = '{}.{}'.format(module.__name__, x) try: _call_with_frames_removed(import_, from_name) |