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.
This commit is contained in:
Brett Cannon 2012-08-24 18:25:59 -04:00
parent 7a54d16dc5
commit 12c6bda4f0
4 changed files with 254 additions and 193 deletions

View file

@ -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