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.
This commit is contained in:
Brett Cannon 2016-02-20 12:52:06 -08:00
parent e10d370a92
commit 4f38cb41fe
4 changed files with 381 additions and 371 deletions

View file

@ -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:

View file

@ -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