diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-07-23 06:44:05 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-23 06:44:05 (GMT) |
commit | c824cc8426a16dd9f3949a3ed135523d37787bae (patch) | |
tree | 2653e36c69b6e77eafc4d0b8346f0800d9a80a06 /Lib/importlib | |
parent | e78dc0aaf03cd98373910150c2d35418cf938254 (diff) | |
download | cpython-c824cc8426a16dd9f3949a3ed135523d37787bae.zip cpython-c824cc8426a16dd9f3949a3ed135523d37787bae.tar.gz cpython-c824cc8426a16dd9f3949a3ed135523d37787bae.tar.bz2 |
[3.5] Backport bpo-30876 (GH-2639), bpo-18018 and bpo-26367. (#2677)
* bpo-30876: Relative import from unloaded package now reimports the package
instead of failing with SystemError.
Relative import from non-package now fails with ImportError rather than
SystemError.
(cherry picked from commit 8a9cd20edca7d01b68292036029ae3735ce65edd)
* bpo-18018: Import raises ImportError instead of SystemError if a relative
import is attempted without a known parent package.
* bpo-26367: importlib.__init__() raises ImportError like
builtins.__import__() when ``level`` is specified but without an accompanying
package specified.
Diffstat (limited to 'Lib/importlib')
-rw-r--r-- | Lib/importlib/_bootstrap.py | 7 |
1 files changed, 3 insertions, 4 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index 9eecbfe..02fe05b 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -925,10 +925,9 @@ def _sanity_check(name, package, level): if level > 0: if not isinstance(package, str): raise TypeError('__package__ not set to a string') - elif package not in sys.modules: - msg = ('Parent module {!r} not loaded, cannot perform relative ' - 'import') - raise SystemError(msg.format(package)) + elif not package: + raise ImportError('attempted relative import with no known parent ' + 'package') if not name and level == 0: raise ValueError('Empty module name') |