Issue #18416: Have importlib.machinery.PathFinder treat '' as the cwd

and stop importlib.machinery.FileFinder treating '' as '.'.

Previous PathFinder transformed '' into '.' which led to __file__ for
modules imported from the cwd to always be relative paths. This meant
the values of the attribute were wrong as soon as the cwd changed.
This change now means that as long as the site module is run (which
makes all entries in sys.path absolute) then all values for __file__
will also be absolute unless it's for __main__ when specified by file
path in a relative way (modules imported by runpy will have an
absolute path).

Now that PathFinder is no longer treating '' as '.' it only makes
sense for FileFinder to stop doing so as well. Now no transformation
is performed for the directory given to the __init__ method.

Thanks to Madison May for the initial patch.
This commit is contained in:
Brett Cannon 2013-10-18 11:39:04 -04:00
parent 40b22d0661
commit 27e27f7ee1
6 changed files with 3471 additions and 3223 deletions

View file

@ -1302,7 +1302,7 @@ class PathFinder:
"""
if path == '':
path = '.'
path = _os.getcwd()
try:
finder = sys.path_importer_cache[path]
except KeyError:
@ -1373,7 +1373,7 @@ class FileFinder:
loaders.extend((suffix, loader) for suffix in suffixes)
self._loaders = loaders
# Base (directory) path
self.path = path or '.'
self.path = path
self._path_mtime = -1
self._path_cache = set()
self._relaxed_path_cache = set()