summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrett Cannon <bcannon@gmail.com>2009-08-30 03:47:36 +0000
committerBrett Cannon <bcannon@gmail.com>2009-08-30 03:47:36 +0000
commit4d75fc1ce97bd15126442c16faab8931aaa7ade2 (patch)
tree4558c370a531f7f3a82b128361ea9d1236b99211
parentccd686a47336bfbd70614824465f5793fd69a685 (diff)
downloadcpython-git-4d75fc1ce97bd15126442c16faab8931aaa7ade2.tar.gz
Have importlib raise ImportError if None is found in sys.modules. This matches
current import semantics.
-rw-r--r--Lib/importlib/_bootstrap.py7
-rw-r--r--Lib/importlib/test/import_/test_caching.py18
-rw-r--r--Misc/NEWS2
3 files changed, 22 insertions, 5 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 2c5a1cfc13..079a9b275f 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -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]
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 e35f5eeab2..ea061a7864 100644
--- 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