Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a6ce4fd

Browse files
committed
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.
1 parent 3fa8c59 commit a6ce4fd

4 files changed

Lines changed: 495 additions & 474 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,19 +1602,19 @@ def _handle_fromlist(module, fromlist, import_):
16021602
fromlist.extend(module.__all__)
16031603
for x in fromlist:
16041604
if not hasattr(module, x):
1605+
from_name = '{}.{}'.format(module.__name__, x)
16051606
try:
1606-
_call_with_frames_removed(import_,
1607-
'{}.{}'.format(module.__name__, x))
1607+
_call_with_frames_removed(import_, from_name)
16081608
except ImportError as exc:
16091609
# Backwards-compatibility dictates we ignore failed
16101610
# imports triggered by fromlist for modules that don't
16111611
# exist.
16121612
# TODO(brett): In Python 3.4, have import raise
16131613
# ModuleNotFound and catch that.
1614-
if hasattr(exc, '_not_found') and exc._not_found:
1615-
pass
1616-
else:
1617-
raise
1614+
if getattr(exc, '_not_found', False):
1615+
if exc.name == from_name:
1616+
continue
1617+
raise
16181618
return module
16191619

16201620

Lib/test/test_importlib/import_/test_fromlist.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_object(self):
5252
module = import_util.import_('module', fromlist=['attr'])
5353
self.assertEqual(module.__name__, 'module')
5454

55-
def test_unexistent_object(self):
55+
def test_nonexistent_object(self):
5656
# [bad object]
5757
with util.mock_modules('module') as importer:
5858
with util.import_state(meta_path=[importer]):
@@ -69,6 +69,19 @@ def test_module_from_package(self):
6969
self.assertTrue(hasattr(module, 'module'))
7070
self.assertEqual(module.module.__name__, 'pkg.module')
7171

72+
def test_module_from_package_triggers_ImportError(self):
73+
# If a submodule causes an ImportError because it tries to import
74+
# a module which doesn't exist, that should let the ImportError
75+
# propagate.
76+
def module_code():
77+
import i_do_not_exist
78+
with util.mock_modules('pkg.__init__', 'pkg.mod',
79+
module_code={'pkg.mod': module_code}) as importer:
80+
with util.import_state(meta_path=[importer]):
81+
with self.assertRaises(ImportError) as exc:
82+
import_util.import_('pkg', fromlist=['mod'])
83+
self.assertEquals('i_do_not_exist', exc.exception.name)
84+
7285
def test_empty_string(self):
7386
with util.mock_modules('pkg.__init__', 'pkg.mod') as importer:
7487
with util.import_state(meta_path=[importer]):

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ Core and Builtins
3333
- Issue #15801: Make sure mappings passed to '%' formatting are actually
3434
subscriptable.
3535

36+
- Issue #15111: __import__ should let ImportError propagate when a module that
37+
is imported as a side-effect of using fromlist tries to import a module
38+
that cannot be found.
39+
3640

3741
Library
3842
-------

0 commit comments

Comments
 (0)