@@ -182,6 +182,16 @@ def _path_isabs(path):
182
182
return path .startswith (path_separators )
183
183
184
184
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
+
185
195
def _write_atomic (path , data , mode = 0o666 ):
186
196
"""Best-effort function to write data to a path atomically.
187
197
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):
494
504
# make it absolute (`C:\Somewhere\Foo\Bar`), then make it root-relative
495
505
# (`Somewhere\Foo\Bar`), so we end up placing the bytecode file in an
496
506
# unambiguous `C:\Bytecode\Somewhere\Foo\Bar\`.
497
- if not _path_isabs (head ):
498
- head = _path_join (_os .getcwd (), head )
507
+ head = _path_abspath (head )
499
508
500
509
# Strip initial drive from a Windows path. We know we have an absolute
501
510
# 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,
808
817
pass
809
818
else :
810
819
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
816
824
817
825
# If the location is on the filesystem, but doesn't actually exist,
818
826
# we could return None here, indicating that the location is not
@@ -1564,10 +1572,8 @@ def __init__(self, path, *loader_details):
1564
1572
# Base (directory) path
1565
1573
if not path or path == '.' :
1566
1574
self .path = _os .getcwd ()
1567
- elif not _path_isabs (path ):
1568
- self .path = _path_join (_os .getcwd (), path )
1569
1575
else :
1570
- self .path = path
1576
+ self .path = _path_abspath ( path )
1571
1577
self ._path_mtime = - 1
1572
1578
self ._path_cache = set ()
1573
1579
self ._relaxed_path_cache = set ()
@@ -1717,6 +1723,8 @@ def _fix_up_module(ns, name, pathname, cpathname=None):
1717
1723
loader = SourceFileLoader (name , pathname )
1718
1724
if not spec :
1719
1725
spec = spec_from_file_location (name , pathname , loader = loader )
1726
+ if cpathname :
1727
+ spec .cached = _path_abspath (cpathname )
1720
1728
try :
1721
1729
ns ['__spec__' ] = spec
1722
1730
ns ['__loader__' ] = loader
0 commit comments