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

Skip to content

Commit 0075110

Browse files
committed
Issue #18364: Stop using the ImportError._not_found hack.
The private attribute was leaking out of importlib and led to at least one person noticing it. Switch to another hack which won't leak outside of importlib and is nearly as robust.
1 parent e0a39de commit 0075110

2 files changed

Lines changed: 3551 additions & 3556 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,8 @@ def _sanity_check(name, package, level):
15361536
raise ValueError("Empty module name")
15371537

15381538

1539-
_ERR_MSG = 'No module named {!r}'
1539+
_ERR_MSG_PREFIX = 'No module named '
1540+
_ERR_MSG = _ERR_MSG_PREFIX + '{!r}'
15401541

15411542
def _find_and_load_unlocked(name, import_):
15421543
path = None
@@ -1556,11 +1557,7 @@ def _find_and_load_unlocked(name, import_):
15561557
raise ImportError(msg, name=name)
15571558
loader = _find_module(name, path)
15581559
if loader is None:
1559-
exc = ImportError(_ERR_MSG.format(name), name=name)
1560-
# TODO(brett): switch to a proper ModuleNotFound exception in Python
1561-
# 3.4.
1562-
exc._not_found = True
1563-
raise exc
1560+
raise ImportError(_ERR_MSG.format(name), name=name)
15641561
elif name not in sys.modules:
15651562
# The parent import may have already imported this module.
15661563
loader.load_module(name)
@@ -1650,9 +1647,7 @@ def _handle_fromlist(module, fromlist, import_):
16501647
# Backwards-compatibility dictates we ignore failed
16511648
# imports triggered by fromlist for modules that don't
16521649
# exist.
1653-
# TODO(brett): In Python 3.4, have import raise
1654-
# ModuleNotFound and catch that.
1655-
if getattr(exc, '_not_found', False):
1650+
if str(exc).startswith(_ERR_MSG_PREFIX):
16561651
if exc.name == from_name:
16571652
continue
16581653
raise

0 commit comments

Comments
 (0)