Commit 0911c0d2 authored by Brett Cannon's avatar Brett Cannon

Merge for issue #26367

parents d9dc5302 4f38cb41
......@@ -922,9 +922,12 @@ 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 not package:
raise ImportError('attempted relative import with no known parent '
'package')
elif package not in sys.modules:
msg = ('Parent module {!r} not loaded, cannot perform relative '
'import')
......
......@@ -218,6 +218,11 @@ class RelativeImports:
self.__import__('a', {'__package__': '', '__spec__': None},
level=1)
def test_relative_import_no_package_exists_absolute(self):
with self.assertRaises(ImportError):
self.__import__('sys', {'__package__': '', '__spec__': None},
level=1)
(Frozen_RelativeImports,
Source_RelativeImports
......
......@@ -185,6 +185,9 @@ Core and Builtins
Library
-------
- Issue #26367: importlib.__init__() raises ImportError 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
......
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