[Python-checkins] cpython: Issue #15111: When a module was imported using a 'from import'

brett.cannon python-checkins at python.org
Tue Jul 10 16:05:09 CEST 2012


http://hg.python.org/cpython/rev/dc18a2a66d16
changeset:   78051:dc18a2a66d16
user:        Brett Cannon <brett at python.org>
date:        Tue Jul 10 10:05:00 2012 -0400
summary:
  Issue #15111: When a module was imported using a 'from import'
statement (e.g. ``from distutils import msvc9compiler``) that triggers
an ImportError of its own (e.g. the non-existence of winreg), let that
exception propagate instead of raising a generic ImportError for the
module being requested (e.g. msvc9compiler).

files:
  Lib/importlib/_bootstrap.py                 |   10 +-
  Lib/importlib/test/import_/test_fromlist.py |   17 +-
  Misc/NEWS                                   |    4 +
  Python/importlib.h                          |  801 ++++-----
  4 files changed, 410 insertions(+), 422 deletions(-)


diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -1459,16 +1459,14 @@
     # The hell that is fromlist ...
     # If a package was imported, try to import stuff from fromlist.
     if hasattr(module, '__path__'):
-        if '*' in fromlist and hasattr(module, '__all__'):
+        if '*' in fromlist:
             fromlist = list(fromlist)
             fromlist.remove('*')
-            fromlist.extend(module.__all__)
+            if hasattr(module, '__all__'):
+                fromlist.extend(module.__all__)
         for x in fromlist:
             if not hasattr(module, x):
-                try:
-                    import_('{}.{}'.format(module.__name__, x))
-                except ImportError:
-                    pass
+                import_('{}.{}'.format(module.__name__, x))
     return module
 
 
diff --git a/Lib/importlib/test/import_/test_fromlist.py b/Lib/importlib/test/import_/test_fromlist.py
--- a/Lib/importlib/test/import_/test_fromlist.py
+++ b/Lib/importlib/test/import_/test_fromlist.py
@@ -39,11 +39,9 @@
     [object case]. This is even true if the object does not exist [bad object].
 
     If a package is being imported, then what is listed in fromlist may be
-    treated as a module to be imported [module]. But once again, even if
-    something in fromlist does not exist as a module, no error is thrown
-    [no module]. And this extends to what is contained in __all__ when '*' is
-    imported [using *]. And '*' does not need to be the only name in the
-    fromlist [using * with others].
+    treated as a module to be imported [module]. And this extends to what is
+    contained in __all__ when '*' is imported [using *]. And '*' does not need
+    to be the only name in the fromlist [using * with others].
 
     """
 
@@ -71,15 +69,6 @@
                 self.assertTrue(hasattr(module, 'module'))
                 self.assertEqual(module.module.__name__, 'pkg.module')
 
-    def test_no_module_from_package(self):
-        # [no module]
-        with util.mock_modules('pkg.__init__') as importer:
-            with util.import_state(meta_path=[importer],
-                                   path_hooks=[imp.NullImporter]):
-                module = import_util.import_('pkg', fromlist='non_existent')
-                self.assertEqual(module.__name__, 'pkg')
-                self.assertTrue(not hasattr(module, 'non_existent'))
-
     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
@@ -10,6 +10,10 @@
 Core and Builtins
 -----------------
 
+- Issue #15111: When a module imported using 'from import' has an ImportError
+  inside itself, don't mask that fact behind a generic ImportError for the
+  module itself.
+
 - Issue #15293: Add GC support to the AST base node type.
 
 - Issue #15291: Fix a memory leak where AST nodes where not properly
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