diff options
author | Brett Cannon <bcannon@gmail.com> | 2009-03-30 19:57:15 (GMT) |
---|---|---|
committer | Brett Cannon <bcannon@gmail.com> | 2009-03-30 19:57:15 (GMT) |
commit | 8593a7568817bf36039460e34826822bec9c3904 (patch) | |
tree | a9d149001ca961556ea5d7c41b3a5f869a77fcbe | |
parent | 0e314548c2e8dac3fa714bdf75b3ea7554c1368d (diff) | |
download | cpython-8593a7568817bf36039460e34826822bec9c3904.zip cpython-8593a7568817bf36039460e34826822bec9c3904.tar.gz cpython-8593a7568817bf36039460e34826822bec9c3904.tar.bz2 |
Fix importlib.machinery.PathFinder.find_module() to essentially skip over None
entries in sys.path_importer_cache. While this differs from semantics in how
__import__ works, it prevents any implicit semantics from taking hold with
users.
-rw-r--r-- | Lib/importlib/_bootstrap.py | 7 | ||||
-rw-r--r-- | Lib/importlib/test/import_/test_path.py | 19 |
2 files changed, 23 insertions, 3 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index d5b909e..949a612 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -661,9 +661,10 @@ class PathFinder: finder = cls._path_importer_cache(entry) except ImportError: continue - loader = finder.find_module(fullname) - if loader: - return loader + if finder: + loader = finder.find_module(fullname) + if loader: + return loader else: return None diff --git a/Lib/importlib/test/import_/test_path.py b/Lib/importlib/test/import_/test_path.py index 3d9b1a1..ede9b5f 100644 --- a/Lib/importlib/test/import_/test_path.py +++ b/Lib/importlib/test/import_/test_path.py @@ -55,6 +55,25 @@ class FinderTests(unittest.TestCase): self.assert_(path in sys.path_importer_cache) self.assert_(sys.path_importer_cache[path] is importer) + def test_path_importer_cache_has_None(self): + # Test that if sys.path_importer_cache has None that None is returned. + clear_cache = {path: None for path in sys.path} + with util.import_state(path_importer_cache=clear_cache): + for name in ('asynchat', 'sys', '<test module>'): + self.assert_(machinery.PathFinder.find_module(name) is None) + + def test_path_importer_cache_has_None_continues(self): + # Test that having None in sys.path_importer_cache causes the search to + # continue. + path = '<test path>' + module = '<test module>' + importer = util.mock_modules(module) + with util.import_state(path=['1', '2'], + path_importer_cache={'1': None, '2': importer}): + loader = machinery.PathFinder.find_module(module) + self.assert_(loader is importer) + + class DefaultPathFinderTests(unittest.TestCase): |