diff options
author | Brett Cannon <brett@python.org> | 2012-07-02 18:53:10 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2012-07-02 18:53:10 (GMT) |
commit | 8e2f5564b3cce162aa1427dcd26092f24f418418 (patch) | |
tree | 383e0f0f2e286c85af805422a71e48d798d07ffe | |
parent | 1e331560eea62c78ec189f2b72b59864ee315ddc (diff) | |
download | cpython-8e2f5564b3cce162aa1427dcd26092f24f418418.zip cpython-8e2f5564b3cce162aa1427dcd26092f24f418418.tar.gz cpython-8e2f5564b3cce162aa1427dcd26092f24f418418.tar.bz2 |
Issue #15210: If _frozen_importlib is not found in sys.modules by
importlib.__init__, then catch the KeyError raised, not ImportError.
-rw-r--r-- | Lib/importlib/__init__.py | 2 | ||||
-rw-r--r-- | Lib/importlib/test/test_api.py | 23 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
3 files changed, 26 insertions, 2 deletions
diff --git a/Lib/importlib/__init__.py b/Lib/importlib/__init__.py index e62bdb1..dad0833 100644 --- a/Lib/importlib/__init__.py +++ b/Lib/importlib/__init__.py @@ -7,7 +7,7 @@ import sys try: _bootstrap = sys.modules['_frozen_importlib'] -except ImportError: +except KeyError: from . import _bootstrap _bootstrap._setup(sys, imp) else: diff --git a/Lib/importlib/test/test_api.py b/Lib/importlib/test/test_api.py index b7d6cb4..8431eab 100644 --- a/Lib/importlib/test/test_api.py +++ b/Lib/importlib/test/test_api.py @@ -160,9 +160,30 @@ class InvalidateCacheTests(unittest.TestCase): importlib.invalidate_caches() # Shouldn't trigger an exception. +class FrozenImportlibTests(unittest.TestCase): + + def test_no_frozen_importlib(self): + # Should be able to import w/o _frozen_importlib being defined. + modules = {} + for name in ('importlib', 'importlib.__init__', 'importlib._bootstrap', + '_frozen_importlib'): + try: + modules[name] = sys.modules[name] + del sys.modules[name] + except KeyError: + continue + modules['_frozen_importlib'] = None + import importlib + for name, module in modules.items(): + sys.modules[name] = module + + def test_main(): from test.support import run_unittest - run_unittest(ImportModuleTests, FindLoaderTests, InvalidateCacheTests) + run_unittest(ImportModuleTests, + FindLoaderTests, + InvalidateCacheTests, + FrozenImportlibTests) if __name__ == '__main__': @@ -17,6 +17,9 @@ Core and Builtins Library ------- +- Issue #15210: Catch KeyError when imprortlib.__init__ can't find + _frozen_importlib in sys.modules, not ImportError. + - Issue #15030: importlib.abc.PyPycLoader now supports the new source size header field in .pyc files. |