Skip to content

Commit df0b37f

Browse files
committed
ntpath.abspath() always return absolute path
1 parent f79ffc8 commit df0b37f

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

Lib/ntpath.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -554,36 +554,48 @@ def normpath(path):
554554
return prefix + sep.join(comps)
555555

556556

557-
def _abspath_fallback(path):
558-
"""Return the absolute version of a path as a fallback function in case
559-
`nt._getfullpathname` is not available or raises OSError. See bpo-31047 for
560-
more.
561-
562-
"""
563-
564-
path = os.fspath(path)
565-
if not isabs(path):
566-
if isinstance(path, bytes):
567-
cwd = os.getcwdb()
568-
else:
569-
cwd = os.getcwd()
570-
path = join(cwd, path)
571-
return normpath(path)
572-
573557
# Return an absolute path.
574558
try:
575559
from nt import _getfullpathname
576560

577561
except ImportError: # not running on Windows - mock up something sensible
578-
abspath = _abspath_fallback
562+
def abspath(path):
563+
"""Return the absolute version of a path."""
564+
path = os.fspath(path)
565+
if not isabs(path):
566+
if isinstance(path, bytes):
567+
cwd = os.getcwdb()
568+
else:
569+
cwd = os.getcwd()
570+
path = join(cwd, path)
571+
return normpath(path)
579572

580573
else: # use native Windows method on Windows
581574
def abspath(path):
582575
"""Return the absolute version of a path."""
583576
try:
584577
return _getfullpathname(normpath(path))
585578
except (OSError, ValueError):
586-
return _abspath_fallback(path)
579+
# See gh-75230, handle outside for cleaner traceback
580+
pass
581+
path = os.fspath(path)
582+
if not isabs(path):
583+
if isinstance(path, bytes):
584+
sep = b'/'
585+
cwd = os.getcwdb()
586+
else:
587+
sep = '/'
588+
cwd = os.getcwd()
589+
drive, root, path = splitroot(path)
590+
if drive and drive != splitroot(cwd)[0]:
591+
try:
592+
path = join(_getfullpathname(drive), path)
593+
except (OSError, ValueError):
594+
# Invalid drive \x00: on Windows; assume root directory
595+
path = drive + sep + path
596+
else:
597+
path = join(cwd, root + path)
598+
return normpath(path)
587599

588600
try:
589601
from nt import _findfirstfile, _getfinalpathname, readlink as _nt_readlink

Lib/test/test_ntpath.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,9 @@ def test_abspath(self):
806806
tester('ntpath.abspath("C:\\spam. . .")', "C:\\spam")
807807
tester('ntpath.abspath("C:/nul")', "\\\\.\\nul")
808808
tester('ntpath.abspath("C:\\nul")', "\\\\.\\nul")
809+
self.assertTrue(ntpath.isabs(ntpath.abspath("C:spam")))
810+
self.assertTrue(ntpath.isabs(ntpath.abspath("C:\x00")))
811+
self.assertTrue(ntpath.isabs(ntpath.abspath("\x00:spam")))
809812
tester('ntpath.abspath("//..")', "\\\\")
810813
tester('ntpath.abspath("//../")', "\\\\..\\")
811814
tester('ntpath.abspath("//../..")', "\\\\..\\")

0 commit comments

Comments
 (0)