Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4f38cb4

Browse files
committed
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.
1 parent e10d370 commit 4f38cb4

4 files changed

Lines changed: 381 additions & 371 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ def _sanity_check(name, package, level):
922922
raise TypeError('module name must be str, not {}'.format(type(name)))
923923
if level < 0:
924924
raise ValueError('level must be >= 0')
925-
if package:
925+
if level > 0:
926926
if not isinstance(package, str):
927927
raise TypeError('__package__ not set to a string')
928928
elif package not in sys.modules:

Lib/test/test_importlib/import_/test_relative_imports.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ def test_relative_import_no_globals(self):
207207
with self.assertRaises(KeyError):
208208
self.__import__('sys', level=1)
209209

210+
def test_relative_import_no_package_exists_absolute(self):
211+
with self.assertRaises(SystemError):
212+
self.__import__('sys', {'__package__': '', '__spec__': None},
213+
level=1)
214+
210215

211216
(Frozen_RelativeImports,
212217
Source_RelativeImports

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ Core and Builtins
7676
Library
7777
-------
7878

79+
- Issue #26367: importlib.__init__() raises RuntimeError like
80+
builtins.__import__() when ``level`` is specified but without an accompanying
81+
package specified.
82+
7983
- Issue #26309: In the "socketserver" module, shut down the request (closing
8084
the connected socket) when verify_request() returns false. Patch by Aviv
8185
Palivoda.

0 commit comments

Comments
 (0)