Commit 4f38cb41 authored by Brett Cannon's avatar Brett Cannon

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.
parent e10d370a
......@@ -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:
......
......@@ -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
......
......@@ -76,6 +76,10 @@ Core and Builtins
Library
-------
- Issue #26367: importlib.__init__() raises RuntimeError like
builtins.__import__() when ``level`` is specified but without an accompanying
package specified.
- Issue #26309: In the "socketserver" module, shut down the request (closing
the connected socket) when verify_request() returns false. Patch by Aviv
Palivoda.
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment