Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f58d45c

Browse files
committed
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.
1 parent 22e7c88 commit f58d45c

2 files changed

Lines changed: 4 additions & 2 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,10 +713,12 @@ def _path_importer_cache(cls, path, default=None):
713713
the default hook, for which ImportError is raised.
714714
715715
"""
716+
if path == '':
717+
path = _os.getcwd()
716718
try:
717719
finder = sys.path_importer_cache[path]
718720
except KeyError:
719-
finder = cls._path_hooks(path if path != '' else _os.getcwd())
721+
finder = cls._path_hooks(path)
720722
sys.path_importer_cache[path] = finder
721723
else:
722724
if finder is None and default:

Lib/importlib/test/import_/test_path.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_path_importer_cache_empty_string(self):
8282
with util.import_state(path=[path], path_hooks=[hook]):
8383
loader = machinery.PathFinder.find_module(module)
8484
self.assertIs(loader, importer)
85-
self.assertIn('', sys.path_importer_cache)
85+
self.assertIn(os.getcwd(), sys.path_importer_cache)
8686

8787

8888
class DefaultPathFinderTests(unittest.TestCase):

0 commit comments

Comments
 (0)