From 5163c35881e614514c6191ab02b2cb13d86d61ad Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Fri, 10 Jun 2022 09:01:09 +0200 Subject: [PATCH] Don't include trailing '/.' to FileFinder path --- Lib/importlib/_bootstrap_external.py | 9 ++++++--- Lib/test/test_import/__init__.py | 14 +++++++------- .../2022-06-09-19-19-02.gh-issue-93461.5DqP1e.rst | 3 +++ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index 694891db4e8c43..8605c2a037d423 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -1569,9 +1569,12 @@ def __init__(self, path, *loader_details): loaders.extend((suffix, loader) for suffix in suffixes) self._loaders = loaders # Base (directory) path - self.path = path or '.' - if not _path_isabs(self.path): - self.path = _path_join(_os.getcwd(), self.path) + if not path or path == '.': + self.path = _os.getcwd() + elif not _path_isabs(path): + self.path = _path_join(_os.getcwd(), path) + else: + self.path = path self._path_mtime = -1 self._path_cache = set() self._relaxed_path_cache = set() diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index be2e91ddd9a9ab..8357586b57f075 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -927,7 +927,7 @@ def test_missing_source_legacy(self): m = __import__(TESTFN) try: self.assertEqual(m.__file__, - os.path.join(os.getcwd(), os.curdir, os.path.relpath(pyc_file))) + os.path.join(os.getcwd(), os.path.relpath(pyc_file))) finally: os.remove(pyc_file) @@ -935,7 +935,7 @@ def test___cached__(self): # Modules now also have an __cached__ that points to the pyc file. m = __import__(TESTFN) pyc_file = importlib.util.cache_from_source(TESTFN + '.py') - self.assertEqual(m.__cached__, os.path.join(os.getcwd(), os.curdir, pyc_file)) + self.assertEqual(m.__cached__, os.path.join(os.getcwd(), pyc_file)) @skip_if_dont_write_bytecode def test___cached___legacy_pyc(self): @@ -951,7 +951,7 @@ def test___cached___legacy_pyc(self): importlib.invalidate_caches() m = __import__(TESTFN) self.assertEqual(m.__cached__, - os.path.join(os.getcwd(), os.curdir, os.path.relpath(pyc_file))) + os.path.join(os.getcwd(), os.path.relpath(pyc_file))) @skip_if_dont_write_bytecode def test_package___cached__(self): @@ -971,10 +971,10 @@ def cleanup(): m = __import__('pep3147.foo') init_pyc = importlib.util.cache_from_source( os.path.join('pep3147', '__init__.py')) - self.assertEqual(m.__cached__, os.path.join(os.getcwd(), os.curdir, init_pyc)) + self.assertEqual(m.__cached__, os.path.join(os.getcwd(), init_pyc)) foo_pyc = importlib.util.cache_from_source(os.path.join('pep3147', 'foo.py')) self.assertEqual(sys.modules['pep3147.foo'].__cached__, - os.path.join(os.getcwd(), os.curdir, foo_pyc)) + os.path.join(os.getcwd(), foo_pyc)) def test_package___cached___from_pyc(self): # Like test___cached__ but ensuring __cached__ when imported from a @@ -998,10 +998,10 @@ def cleanup(): m = __import__('pep3147.foo') init_pyc = importlib.util.cache_from_source( os.path.join('pep3147', '__init__.py')) - self.assertEqual(m.__cached__, os.path.join(os.getcwd(), os.curdir, init_pyc)) + self.assertEqual(m.__cached__, os.path.join(os.getcwd(), init_pyc)) foo_pyc = importlib.util.cache_from_source(os.path.join('pep3147', 'foo.py')) self.assertEqual(sys.modules['pep3147.foo'].__cached__, - os.path.join(os.getcwd(), os.curdir, foo_pyc)) + os.path.join(os.getcwd(), foo_pyc)) def test_recompute_pyc_same_second(self): # Even when the source file doesn't change timestamp, a change in diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-06-09-19-19-02.gh-issue-93461.5DqP1e.rst b/Misc/NEWS.d/next/Core and Builtins/2022-06-09-19-19-02.gh-issue-93461.5DqP1e.rst index 7991e245fde8ef..f6ed14887eae1b 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2022-06-09-19-19-02.gh-issue-93461.5DqP1e.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2022-06-09-19-19-02.gh-issue-93461.5DqP1e.rst @@ -1,3 +1,6 @@ :func:`importlib.invalidate_caches` now drops entries from :data:`sys.path_importer_cache` with a relative path as name. This solves a caching issue when a process changes its current working directory. + +``FileFinder`` no longer inserts a dot in the path, e.g. +``/egg/./spam`` is now ``/egg/spam``.