0
0
mirror of https://github.com/python/cpython.git synced 2024-11-24 08:52:25 +01:00

gh-119826: Improved fallback for ntpath.abspath() on Windows (GH-119938)

This commit is contained in:
Nice Zombies 2024-11-12 22:18:03 +01:00 committed by GitHub
parent 5610860840
commit 4b00aba42e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 18 deletions

View File

@ -553,13 +553,13 @@ except ImportError:
return prefix + sep.join(comps)
def _abspath_fallback(path):
"""Return the absolute version of a path as a fallback function in case
`nt._getfullpathname` is not available or raises OSError. See bpo-31047 for
more.
"""
# Return an absolute path.
try:
from nt import _getfullpathname
except ImportError: # not running on Windows - mock up something sensible
def abspath(path):
"""Return the absolute version of a path."""
path = os.fspath(path)
if not isabs(path):
if isinstance(path, bytes):
@ -569,20 +569,33 @@ def _abspath_fallback(path):
path = join(cwd, path)
return normpath(path)
# Return an absolute path.
try:
from nt import _getfullpathname
except ImportError: # not running on Windows - mock up something sensible
abspath = _abspath_fallback
else: # use native Windows method on Windows
def abspath(path):
"""Return the absolute version of a path."""
try:
return _getfullpathname(normpath(path))
except (OSError, ValueError):
return _abspath_fallback(path)
# See gh-75230, handle outside for cleaner traceback
pass
path = os.fspath(path)
if not isabs(path):
if isinstance(path, bytes):
sep = b'\\'
getcwd = os.getcwdb
else:
sep = '\\'
getcwd = os.getcwd
drive, root, path = splitroot(path)
# Either drive or root can be nonempty, but not both.
if drive or root:
try:
path = join(_getfullpathname(drive + root), path)
except (OSError, ValueError):
# Drive "\0:" cannot exist; use the root directory.
path = drive + sep + path
else:
path = join(getcwd(), path)
return normpath(path)
try:
from nt import _findfirstfile, _getfinalpathname, readlink as _nt_readlink

View File

@ -806,6 +806,9 @@ class TestNtpath(NtpathTestCase):
tester('ntpath.abspath("C:\\spam. . .")', "C:\\spam")
tester('ntpath.abspath("C:/nul")', "\\\\.\\nul")
tester('ntpath.abspath("C:\\nul")', "\\\\.\\nul")
self.assertTrue(ntpath.isabs(ntpath.abspath("C:spam")))
self.assertEqual(ntpath.abspath("C:\x00"), ntpath.join(ntpath.abspath("C:"), "\x00"))
self.assertEqual(ntpath.abspath("\x00:spam"), "\x00:\\spam")
tester('ntpath.abspath("//..")', "\\\\")
tester('ntpath.abspath("//../")', "\\\\..\\")
tester('ntpath.abspath("//../..")', "\\\\..\\")

View File

@ -0,0 +1 @@
Always return an absolute path for :func:`os.path.abspath` on Windows.