summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-10-10 23:18:37 (GMT)
committerBrett Cannon <brett@python.org>2012-10-10 23:18:37 (GMT)
commit9407d502085d0e3d281eb5274149ebca4567034c (patch)
tree8d18519d9b6189cde4c68010b1e94f8f6bc4be55 /Lib
parentb36a05094d16bfb86c1b113bcab769ca6dbeee7a (diff)
parenta6ce4fd426cb9df0e4816a627c79aff0fc6ecf63 (diff)
downloadcpython-9407d502085d0e3d281eb5274149ebca4567034c.zip
cpython-9407d502085d0e3d281eb5274149ebca4567034c.tar.gz
cpython-9407d502085d0e3d281eb5274149ebca4567034c.tar.bz2
Merge fix for issue #15111.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/importlib/_bootstrap.py12
-rw-r--r--Lib/test/test_importlib/import_/test_fromlist.py15
2 files changed, 20 insertions, 7 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index fd86737..e26cd26 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -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
diff --git a/Lib/test/test_importlib/import_/test_fromlist.py b/Lib/test/test_importlib/import_/test_fromlist.py
index 281961d..27b2270 100644
--- a/Lib/test/test_importlib/import_/test_fromlist.py
+++ b/Lib/test/test_importlib/import_/test_fromlist.py
@@ -52,7 +52,7 @@ class HandlingFromlist(unittest.TestCase):
module = import_util.import_('module', fromlist=['attr'])
self.assertEqual(module.__name__, 'module')
- def test_unexistent_object(self):
+ def test_nonexistent_object(self):
# [bad object]
with util.mock_modules('module') as importer:
with util.import_state(meta_path=[importer]):
@@ -69,6 +69,19 @@ class HandlingFromlist(unittest.TestCase):
self.assertTrue(hasattr(module, 'module'))
self.assertEqual(module.module.__name__, 'pkg.module')
+ def test_module_from_package_triggers_ImportError(self):
+ # If a submodule causes an ImportError because it tries to import
+ # a module which doesn't exist, that should let the ImportError
+ # propagate.
+ def module_code():
+ import i_do_not_exist
+ with util.mock_modules('pkg.__init__', 'pkg.mod',
+ module_code={'pkg.mod': module_code}) as importer:
+ with util.import_state(meta_path=[importer]):
+ with self.assertRaises(ImportError) as exc:
+ import_util.import_('pkg', fromlist=['mod'])
+ self.assertEquals('i_do_not_exist', exc.exception.name)
+
def test_empty_string(self):
with util.mock_modules('pkg.__init__', 'pkg.mod') as importer:
with util.import_state(meta_path=[importer]):