Issue #15715: Ignore failed imports triggered by the use of fromlist.

When the fromlist argument is specified for __import__() and the
attribute doesn't already exist, an import is attempted. If that fails
(e.g. module doesn't exist), the ImportError will now be silenced (for
backwards-compatibility). This *does not* affect
``from ... import ...`` statements.

Thanks to Eric Snow for the patch and Simon Feltman for reporting the
regression.
This commit is contained in:
Brett Cannon 2012-08-17 13:21:16 -04:00
parent b391b24efe
commit 7385adc84c
4 changed files with 474 additions and 458 deletions

View file

@ -1573,8 +1573,13 @@ def _handle_fromlist(module, fromlist, import_):
fromlist.extend(module.__all__)
for x in fromlist:
if not hasattr(module, x):
_call_with_frames_removed(import_,
'{}.{}'.format(module.__name__, x))
try:
_call_with_frames_removed(import_,
'{}.{}'.format(module.__name__, x))
except ImportError:
# Backwards-compatibility dictates we ignore failed
# imports triggered by fromlist.
pass
return module