[Python-checkins] cpython: Issue #18364: Stop using the ImportError._not_found hack.

brett.cannon python-checkins at python.org
Sat Jul 6 20:48:29 CEST 2013


http://hg.python.org/cpython/rev/0d590683c8d1
changeset:   84464:0d590683c8d1
user:        Brett Cannon <brett at python.org>
date:        Sat Jul 06 14:48:18 2013 -0400
summary:
  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.

files:
  Lib/importlib/_bootstrap.py |    13 +-
  Python/importlib.h          |  7136 +++++++++++-----------
  2 files changed, 3572 insertions(+), 3577 deletions(-)


diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -1536,7 +1536,8 @@
         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_):
     path = None
@@ -1556,11 +1557,7 @@
             raise ImportError(msg, name=name)
     loader = _find_module(name, path)
     if loader is None:
-        exc = 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
+        raise ImportError(_ERR_MSG.format(name), name=name)
     elif name not in sys.modules:
         # The parent import may have already imported this module.
         loader.load_module(name)
@@ -1650,9 +1647,7 @@
                     # Backwards-compatibility dictates we ignore failed
                     # imports triggered by fromlist for modules that don't
                     # exist.
-                    # TODO(brett): In Python 3.4, have import raise
-                    #   ModuleNotFound and catch that.
-                    if getattr(exc, '_not_found', False):
+                    if str(exc).startswith(_ERR_MSG_PREFIX):
                         if exc.name == from_name:
                             continue
                     raise
diff --git a/Python/importlib.h b/Python/importlib.h
--- a/Python/importlib.h
+++ b/Python/importlib.h
[stripped]

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list