From 8a1a59f0eced3327ccf095a3de05701cdf379ab5 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Sun, 30 Aug 2009 04:29:47 +0000 Subject: [PATCH] Merged revisions 74584 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r74584 | brett.cannon | 2009-08-29 20:47:36 -0700 (Sat, 29 Aug 2009) | 3 lines Have importlib raise ImportError if None is found in sys.modules. This matches current import semantics. ........ --- Lib/importlib/_bootstrap.py | 7 ++++++- Lib/importlib/test/import_/test_caching.py | 18 ++++++++++++++---- Misc/NEWS | 3 +++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index ee3f1e6bf2..24bcff2f26 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -860,7 +860,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] diff --git a/Lib/importlib/test/import_/test_caching.py b/Lib/importlib/test/import_/test_caching.py index cf65b233b6..530b1a06df 100644 --- a/Lib/importlib/test/import_/test_caching.py +++ b/Lib/importlib/test/import_/test_caching.py @@ -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) diff --git a/Misc/NEWS b/Misc/NEWS index b60d8e57bc..02ce31e1f0 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -50,6 +50,9 @@ C-API Library ------- +- Have importlib raise ImportError if None is found in sys.modules for a + module. + - Issue #6794: Fix Decimal.compare_total and Decimal.compare_total_mag: NaN payloads are now ordered by integer value rather than lexicographically. -- 2.40.0