]> granicus.if.org Git - python/commitdiff
Trying to import a submodule from another module and not a package was raising
authorBrett Cannon <bcannon@gmail.com>
Sun, 30 Aug 2009 20:22:21 +0000 (20:22 +0000)
committerBrett Cannon <bcannon@gmail.com>
Sun, 30 Aug 2009 20:22:21 +0000 (20:22 +0000)
AttributeError in importlib when it should be an ImportError.

Found when running importlib against test_runpy.

Lib/importlib/_bootstrap.py
Lib/importlib/test/import_/test_packages.py
Lib/importlib/test/regrtest.py
Misc/NEWS

index bd62c3616b47379eacd7a5e691c15e0b802f08db..466b287c4d2eee1abd38cd5268d439dcdb61109e 100644 (file)
@@ -879,7 +879,11 @@ def _gcd_import(name, package=None, level=0):
                 _gcd_import(parent)
             # Backwards-compatibility; be nicer to skip the dict lookup.
             parent_module = sys.modules[parent]
-            path = parent_module.__path__
+            try:
+                path = parent_module.__path__
+            except AttributeError:
+                raise ImportError("no module named {}; "
+                                    "{} is not a package".format(name, parent))
         meta_path = sys.meta_path + _IMPLICIT_META_PATH
         for finder in meta_path:
             loader = finder.find_module(name, path)
index 1a15ebbd3ad3dbbd7342b2fd3ad3c22f407802f4..faadc32172bfa40b0f3c817a9c8d6d4598c2830b 100644 (file)
@@ -21,6 +21,12 @@ class ParentModuleTests(unittest.TestCase):
                 with self.assertRaises(ImportError):
                     import_util.import_('pkg.module')
 
+    def test_module_not_package(self):
+        # Try to import a submodule from a non-package should raise ImportError.
+        assert not hasattr(sys, '__path__')
+        with self.assertRaises(ImportError):
+            import_util.import_('sys.no_submodules_here')
+
 
 def test_main():
     from test.support import run_unittest
index ca274892af98c565efb319ccd64122c8360966eb..f9721c06d490925bb59de77b0f4a59378e3d8acb 100644 (file)
@@ -8,9 +8,6 @@ this script.
 XXX FAILING
     test_import  # execution bit, exception name differing, file name differing
                     between code and module (?)
-    test_runpy  # Importing sys.imp.eric raises AttributeError instead of
-                    ImportError (as does any attempt to import a sub-module
-                    from a non-package, e.g. tokenize.menotreal)
 
 """
 import importlib
index 608e9a89c88d07889ea542d2eb975b80baa92839..bc7f5b06fddd32eca61ec2b704f939a73d83491a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -68,6 +68,9 @@ C-API
 Library
 -------
 
+- Trying to import a submodule from a module that is not a package, ImportError
+  should be raised, not AttributeError.
+
 - When the globals past to importlib.__import__() has __package__ set to None,
   fall back to computing what __package__ should be instead of giving up.