diff options
author | Brett Cannon <brett@python.org> | 2016-02-20 20:52:06 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2016-02-20 20:52:06 (GMT) |
commit | 4f38cb41fe022c94bb5569c72d8b48020d8c13d4 (patch) | |
tree | 6d6e1763c211a140c82462c2c9530030e1c292b8 /Lib | |
parent | e10d370a929a80bc0708daea4bf4bc0715da9706 (diff) | |
download | cpython-4f38cb41fe022c94bb5569c72d8b48020d8c13d4.zip cpython-4f38cb41fe022c94bb5569c72d8b48020d8c13d4.tar.gz cpython-4f38cb41fe022c94bb5569c72d8b48020d8c13d4.tar.bz2 |
Issue #26367: Have importlib.__init__() raise RuntimeError when
'level' is specified but no __package__.
This brings the function inline with builtins.__import__(). Thanks to
Manuel Jacob for the patch.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/importlib/_bootstrap.py | 2 | ||||
-rw-r--r-- | Lib/test/test_importlib/import_/test_relative_imports.py | 5 |
2 files changed, 6 insertions, 1 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index 6f62bb3..1b3b430 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -922,7 +922,7 @@ def _sanity_check(name, package, level): raise TypeError('module name must be str, not {}'.format(type(name))) if level < 0: raise ValueError('level must be >= 0') - if package: + if level > 0: if not isinstance(package, str): raise TypeError('__package__ not set to a string') elif package not in sys.modules: diff --git a/Lib/test/test_importlib/import_/test_relative_imports.py b/Lib/test/test_importlib/import_/test_relative_imports.py index 28bb6f7..3bb819f 100644 --- a/Lib/test/test_importlib/import_/test_relative_imports.py +++ b/Lib/test/test_importlib/import_/test_relative_imports.py @@ -207,6 +207,11 @@ class RelativeImports: with self.assertRaises(KeyError): self.__import__('sys', level=1) + def test_relative_import_no_package_exists_absolute(self): + with self.assertRaises(SystemError): + self.__import__('sys', {'__package__': '', '__spec__': None}, + level=1) + (Frozen_RelativeImports, Source_RelativeImports |