Skip to content

Commit af2d24b

Browse files
gh-119826: Improved fallback for ntpath.abspath() on Windows (GH-119938)
(cherry picked from commit 4b00aba) Co-authored-by: Nice Zombies <[email protected]>
1 parent 8fffbb0 commit af2d24b

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

Lib/ntpath.py

+31-18
Original file line numberDiff line numberDiff line change
@@ -561,36 +561,49 @@ def normpath(path):
561561
return prefix + sep.join(comps)
562562

563563

564-
def _abspath_fallback(path):
565-
"""Return the absolute version of a path as a fallback function in case
566-
`nt._getfullpathname` is not available or raises OSError. See bpo-31047 for
567-
more.
568-
569-
"""
570-
571-
path = os.fspath(path)
572-
if not isabs(path):
573-
if isinstance(path, bytes):
574-
cwd = os.getcwdb()
575-
else:
576-
cwd = os.getcwd()
577-
path = join(cwd, path)
578-
return normpath(path)
579-
580564
# Return an absolute path.
581565
try:
582566
from nt import _getfullpathname
583567

584568
except ImportError: # not running on Windows - mock up something sensible
585-
abspath = _abspath_fallback
569+
def abspath(path):
570+
"""Return the absolute version of a path."""
571+
path = os.fspath(path)
572+
if not isabs(path):
573+
if isinstance(path, bytes):
574+
cwd = os.getcwdb()
575+
else:
576+
cwd = os.getcwd()
577+
path = join(cwd, path)
578+
return normpath(path)
586579

587580
else: # use native Windows method on Windows
588581
def abspath(path):
589582
"""Return the absolute version of a path."""
590583
try:
591584
return _getfullpathname(normpath(path))
592585
except (OSError, ValueError):
593-
return _abspath_fallback(path)
586+
# See gh-75230, handle outside for cleaner traceback
587+
pass
588+
path = os.fspath(path)
589+
if not isabs(path):
590+
if isinstance(path, bytes):
591+
sep = b'\\'
592+
getcwd = os.getcwdb
593+
else:
594+
sep = '\\'
595+
getcwd = os.getcwd
596+
drive, root, path = splitroot(path)
597+
# Either drive or root can be nonempty, but not both.
598+
if drive or root:
599+
try:
600+
path = join(_getfullpathname(drive + root), path)
601+
except (OSError, ValueError):
602+
# Drive "\0:" cannot exist; use the root directory.
603+
path = drive + sep + path
604+
else:
605+
path = join(getcwd(), path)
606+
return normpath(path)
594607

595608
try:
596609
from nt import _getfinalpathname, readlink as _nt_readlink

Lib/test/test_ntpath.py

+3
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,9 @@ def test_abspath(self):
743743
tester('ntpath.abspath("C:\\spam. . .")', "C:\\spam")
744744
tester('ntpath.abspath("C:/nul")', "\\\\.\\nul")
745745
tester('ntpath.abspath("C:\\nul")', "\\\\.\\nul")
746+
self.assertTrue(ntpath.isabs(ntpath.abspath("C:spam")))
747+
self.assertEqual(ntpath.abspath("C:\x00"), ntpath.join(ntpath.abspath("C:"), "\x00"))
748+
self.assertEqual(ntpath.abspath("\x00:spam"), "\x00:\\spam")
746749
tester('ntpath.abspath("//..")', "\\\\")
747750
tester('ntpath.abspath("//../")', "\\\\..\\")
748751
tester('ntpath.abspath("//../..")', "\\\\..\\")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Always return an absolute path for :func:`os.path.abspath` on Windows.

0 commit comments

Comments
 (0)