Commit 49b1e732 authored by Brett Cannon's avatar Brett Cannon

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.
parent aa29f998
...@@ -1536,7 +1536,8 @@ def _sanity_check(name, package, level): ...@@ -1536,7 +1536,8 @@ def _sanity_check(name, package, level):
raise ValueError("Empty module name") raise ValueError("Empty module name")
_ERR_MSG = 'No module named {!r}' _ERR_MSG_PREFIX = 'No module named '
_ERR_MSG = _ERR_MSG_PREFIX + '{!r}'
def _find_and_load_unlocked(name, import_): def _find_and_load_unlocked(name, import_):
path = None path = None
...@@ -1556,11 +1557,7 @@ def _find_and_load_unlocked(name, import_): ...@@ -1556,11 +1557,7 @@ def _find_and_load_unlocked(name, import_):
raise ImportError(msg, name=name) raise ImportError(msg, name=name)
loader = _find_module(name, path) loader = _find_module(name, path)
if loader is None: if loader is None:
exc = ImportError(_ERR_MSG.format(name), name=name) raise ImportError(_ERR_MSG.format(name), name=name)
# TODO(brett): switch to a proper ModuleNotFound exception in Python
# 3.4.
exc._not_found = True
raise exc
elif name not in sys.modules: elif name not in sys.modules:
# The parent import may have already imported this module. # The parent import may have already imported this module.
loader.load_module(name) loader.load_module(name)
...@@ -1650,9 +1647,7 @@ def _handle_fromlist(module, fromlist, import_): ...@@ -1650,9 +1647,7 @@ def _handle_fromlist(module, fromlist, import_):
# Backwards-compatibility dictates we ignore failed # Backwards-compatibility dictates we ignore failed
# imports triggered by fromlist for modules that don't # imports triggered by fromlist for modules that don't
# exist. # exist.
# TODO(brett): In Python 3.4, have import raise if str(exc).startswith(_ERR_MSG_PREFIX):
# ModuleNotFound and catch that.
if getattr(exc, '_not_found', False):
if exc.name == from_name: if exc.name == from_name:
continue continue
raise raise
......
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