mirror of
https://github.com/python/cpython.git
synced 2025-11-10 02:21:40 +00:00
Tweak the handling of the empty string in sys.path for importlib.
It seems better to cache the finder for the cwd under its full path insetad of '' in case the cwd changes. Otherwise FileFinder needs to dynamically change itself based on whether it is given '' instead of caching a finder for every change to the cwd.
This commit is contained in:
parent
22e7c88057
commit
f58d45c649
2 changed files with 4 additions and 2 deletions
|
|
@ -713,10 +713,12 @@ def _path_importer_cache(cls, path, default=None):
|
||||||
the default hook, for which ImportError is raised.
|
the default hook, for which ImportError is raised.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
if path == '':
|
||||||
|
path = _os.getcwd()
|
||||||
try:
|
try:
|
||||||
finder = sys.path_importer_cache[path]
|
finder = sys.path_importer_cache[path]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
finder = cls._path_hooks(path if path != '' else _os.getcwd())
|
finder = cls._path_hooks(path)
|
||||||
sys.path_importer_cache[path] = finder
|
sys.path_importer_cache[path] = finder
|
||||||
else:
|
else:
|
||||||
if finder is None and default:
|
if finder is None and default:
|
||||||
|
|
|
||||||
|
|
@ -82,7 +82,7 @@ def test_path_importer_cache_empty_string(self):
|
||||||
with util.import_state(path=[path], path_hooks=[hook]):
|
with util.import_state(path=[path], path_hooks=[hook]):
|
||||||
loader = machinery.PathFinder.find_module(module)
|
loader = machinery.PathFinder.find_module(module)
|
||||||
self.assertIs(loader, importer)
|
self.assertIs(loader, importer)
|
||||||
self.assertIn('', sys.path_importer_cache)
|
self.assertIn(os.getcwd(), sys.path_importer_cache)
|
||||||
|
|
||||||
|
|
||||||
class DefaultPathFinderTests(unittest.TestCase):
|
class DefaultPathFinderTests(unittest.TestCase):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue