@@ -182,6 +182,16 @@ def _path_isabs(path):
182182 return path .startswith (path_separators )
183183
184184
185+ def _path_abspath (path ):
186+ """Replacement for os.path.abspath."""
187+ if not _path_isabs (path ):
188+ for sep in path_separators :
189+ path = path .removeprefix (f".{ sep } " )
190+ return _path_join (_os .getcwd (), path )
191+ else :
192+ return path
193+
194+
185195def _write_atomic (path , data , mode = 0o666 ):
186196 """Best-effort function to write data to a path atomically.
187197 Be prepared to handle a FileExistsError if concurrent writing of the
@@ -494,8 +504,7 @@ def cache_from_source(path, debug_override=None, *, optimization=None):
494504 # make it absolute (`C:\Somewhere\Foo\Bar`), then make it root-relative
495505 # (`Somewhere\Foo\Bar`), so we end up placing the bytecode file in an
496506 # unambiguous `C:\Bytecode\Somewhere\Foo\Bar\`.
497- if not _path_isabs (head ):
498- head = _path_join (_os .getcwd (), head )
507+ head = _path_abspath (head )
499508
500509 # Strip initial drive from a Windows path. We know we have an absolute
501510 # path here, so the second part of the check rules out a POSIX path that
@@ -808,11 +817,10 @@ def spec_from_file_location(name, location=None, *, loader=None,
808817 pass
809818 else :
810819 location = _os .fspath (location )
811- if not _path_isabs (location ):
812- try :
813- location = _path_join (_os .getcwd (), location )
814- except OSError :
815- pass
820+ try :
821+ location = _path_abspath (location )
822+ except OSError :
823+ pass
816824
817825 # If the location is on the filesystem, but doesn't actually exist,
818826 # we could return None here, indicating that the location is not
@@ -1564,10 +1572,8 @@ def __init__(self, path, *loader_details):
15641572 # Base (directory) path
15651573 if not path or path == '.' :
15661574 self .path = _os .getcwd ()
1567- elif not _path_isabs (path ):
1568- self .path = _path_join (_os .getcwd (), path )
15691575 else :
1570- self .path = path
1576+ self .path = _path_abspath ( path )
15711577 self ._path_mtime = - 1
15721578 self ._path_cache = set ()
15731579 self ._relaxed_path_cache = set ()
@@ -1717,6 +1723,8 @@ def _fix_up_module(ns, name, pathname, cpathname=None):
17171723 loader = SourceFileLoader (name , pathname )
17181724 if not spec :
17191725 spec = spec_from_file_location (name , pathname , loader = loader )
1726+ if cpathname :
1727+ spec .cached = _path_abspath (cpathname )
17201728 try :
17211729 ns ['__spec__' ] = spec
17221730 ns ['__loader__' ] = loader
0 commit comments