[Python-checkins] cpython (merge 3.3 -> default): Merge fix for issue #15111.

brett.cannon python-checkins at python.org
Thu Oct 11 01:18:51 CEST 2012


http://hg.python.org/cpython/rev/31db8e3272f1
changeset:   79653:31db8e3272f1
parent:      79651:73e7bebb3aa4
parent:      79652:09b5158d5284
user:        Brett Cannon <brett at python.org>
date:        Wed Oct 10 19:18:37 2012 -0400
summary:
  Merge fix for issue #15111.

files:
  Lib/importlib/_bootstrap.py                      |   12 +-
  Lib/test/test_importlib/import_/test_fromlist.py |   15 +-
  Misc/NEWS                                        |    3 +
  Python/importlib.h                               |  924 +++++----
  4 files changed, 487 insertions(+), 467 deletions(-)


diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -1602,19 +1602,19 @@
                 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
--- a/Lib/test/test_importlib/import_/test_fromlist.py
+++ b/Lib/test/test_importlib/import_/test_fromlist.py
@@ -52,7 +52,7 @@
                 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 @@
                 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]):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -39,6 +39,9 @@
 - Issue #15801: Make sure mappings passed to '%' formatting are actually
   subscriptable.
 
+- Issue #15111: __import__ should propagate ImportError when raised as a
+  side-effect of a module triggered from using fromlist.
+
 Library
 -------
 
diff --git a/Python/importlib.h b/Python/importlib.h
--- a/Python/importlib.h
+++ b/Python/importlib.h
[stripped]

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list