Closes issue #15111: Calling __import__ with a module specified in

fromlist which causes its own ImportError (e.g. the module tries to
import a non-existent module) should have that exception propagate.
This commit is contained in:
Brett Cannon 2012-10-10 19:03:46 -04:00
parent 3fa8c59024
commit a6ce4fd426
4 changed files with 498 additions and 477 deletions

View file

@ -1602,19 +1602,19 @@ def _handle_fromlist(module, fromlist, import_):
fromlist.extend(module.__all__)
for x in fromlist:
if not hasattr(module, x):
from_name = '{}.{}'.format(module.__name__, x)
try:
_call_with_frames_removed(import_,
'{}.{}'.format(module.__name__, x))
_call_with_frames_removed(import_, from_name)
except ImportError as exc:
# 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 hasattr(exc, '_not_found') and exc._not_found:
pass
else:
raise
if getattr(exc, '_not_found', False):
if exc.name == from_name:
continue
raise
return module