mirror of
https://github.com/python/cpython.git
synced 2025-10-18 04:38:07 +00:00
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.
This commit is contained in:
parent
0e314548c2
commit
8593a75688
2 changed files with 23 additions and 3 deletions
|
@ -661,9 +661,10 @@ class PathFinder:
|
||||||
finder = cls._path_importer_cache(entry)
|
finder = cls._path_importer_cache(entry)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
continue
|
continue
|
||||||
loader = finder.find_module(fullname)
|
if finder:
|
||||||
if loader:
|
loader = finder.find_module(fullname)
|
||||||
return loader
|
if loader:
|
||||||
|
return loader
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
|
@ -55,6 +55,25 @@ class FinderTests(unittest.TestCase):
|
||||||
self.assert_(path in sys.path_importer_cache)
|
self.assert_(path in sys.path_importer_cache)
|
||||||
self.assert_(sys.path_importer_cache[path] is importer)
|
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):
|
class DefaultPathFinderTests(unittest.TestCase):
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue