diff options
author | Brett Cannon <brett@python.org> | 2012-08-24 22:25:59 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2012-08-24 22:25:59 (GMT) |
commit | 12c6bda4f0e96c4bea285d8c664044753ea81bf1 (patch) | |
tree | 8f7f0d38129542f4c8388c4ad20863ee9c457df9 /Lib/importlib | |
parent | 7a54d16dc5684a2279bf3bd1e6bbb10c74c09850 (diff) | |
download | cpython-12c6bda4f0e96c4bea285d8c664044753ea81bf1.zip cpython-12c6bda4f0e96c4bea285d8c664044753ea81bf1.tar.gz cpython-12c6bda4f0e96c4bea285d8c664044753ea81bf1.tar.bz2 |
Issue #15316: Let exceptions raised during imports triggered by the
fromlist of __import__ propagate.
The problem previously was that if something listed in fromlist didn't
exist then that's okay. The fix for that was too broad in terms of
catching ImportError.
The trick with the solution to this issue is that the proper
refactoring of import thanks to importlib doesn't allow for a way to
distinguish (portably) between an ImportError because finders couldn't
find a loader, or a loader raised the exception. In Python 3.4 the
hope is to introduce a new exception (e.g. ModuleNotFound) to make it
clean to differentiate why ImportError was raised.
Diffstat (limited to 'Lib/importlib')
-rw-r--r-- | Lib/importlib/_bootstrap.py | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index 861900c..171adc5 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -1513,7 +1513,11 @@ def _find_and_load_unlocked(name, import_): raise ImportError(msg, name=name) loader = _find_module(name, path) if loader is None: - raise ImportError(_ERR_MSG.format(name), name=name) + 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 elif name not in sys.modules: # The parent import may have already imported this module. loader.load_module(name) @@ -1599,10 +1603,16 @@ def _handle_fromlist(module, fromlist, import_): try: _call_with_frames_removed(import_, '{}.{}'.format(module.__name__, x)) - except ImportError: + except ImportError as exc: # Backwards-compatibility dictates we ignore failed - # imports triggered by fromlist. - pass + # imports triggered by fromlist for modules that don't + # exist. + # TODO(brett): In Python 3.4, have import raise + # ModuleNotFound and catch that. + if hasattr(exc, '_not_found') and exc._not_found: + pass + else: + raise return module |