]> granicus.if.org Git - python/commitdiff
Have importlib raise ImportError if None is found in sys.modules. This matches
authorBrett Cannon <bcannon@gmail.com>
Sun, 30 Aug 2009 03:47:36 +0000 (03:47 +0000)
committerBrett Cannon <bcannon@gmail.com>
Sun, 30 Aug 2009 03:47:36 +0000 (03:47 +0000)
current import semantics.

Lib/importlib/_bootstrap.py
Lib/importlib/test/import_/test_caching.py
Misc/NEWS

index 2c5a1cfc13c626a6d6044c5eefc2602c648657d4..079a9b275f7ef71e86ee11cc68dc43312755e974 100644 (file)
@@ -864,7 +864,12 @@ def _gcd_import(name, package=None, level=0):
             name = package[:dot]
     with _ImportLockContext():
         try:
-            return sys.modules[name]
+            module = sys.modules[name]
+            if module is None:
+                message = ("import of {} halted; "
+                            "None in sys.modules".format(name))
+                raise ImportError(message)
+            return module
         except KeyError:
             pass
         parent = name.rpartition('.')[0]
index cf65b233b63c12ee18ee5469174e2d7e7f286596..530b1a06dfb508b268aa89e0a5a11c609091a79e 100644 (file)
@@ -17,15 +17,25 @@ class UseCache(unittest.TestCase):
     loader returns) [from cache on return]. This also applies to imports of
     things contained within a package and thus get assigned as an attribute
     [from cache to attribute] or pulled in thanks to a fromlist import
-    [from cache for fromlist].
+    [from cache for fromlist]. But if sys.modules contains None then
+    ImportError is raised [None in cache].
 
     """
     def test_using_cache(self):
         # [use cache]
         module_to_use = "some module found!"
-        sys.modules['some_module'] = module_to_use
-        module = import_util.import_('some_module')
-        self.assertEqual(id(module_to_use), id(module))
+        with util.uncache(module_to_use):
+            sys.modules['some_module'] = module_to_use
+            module = import_util.import_('some_module')
+            self.assertEqual(id(module_to_use), id(module))
+
+    def test_None_in_cache(self):
+        #[None in cache]
+        name = 'using_None'
+        with util.uncache(name):
+            sys.modules[name] = None
+            with self.assertRaises(ImportError):
+                import_util.import_(name)
 
     def create_mock(self, *names, return_=None):
         mock = util.mock_modules(*names)
index e35f5eeab2bda0e42f6a26a177223fba0a55c828..ea061a7864993dc85cf62160cc574c70b163d4c1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -68,6 +68,8 @@ C-API
 Library
 -------
 
+- Have importlib raise ImportError if None is found in sys.modules.
+
 - Issue #6054: Do not normalize stored pathnames in tarfile.
 
 - Issue #6794: Fix Decimal.compare_total and Decimal.compare_total_mag: NaN