aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--Lib/ntpath.py49
-rw-r--r--Lib/test/test_ntpath.py3
-rw-r--r--Misc/NEWS.d/next/Library/2024-06-02-11-48-19.gh-issue-119826.N1obGa.rst1
3 files changed, 35 insertions, 18 deletions
diff --git a/Lib/ntpath.py b/Lib/ntpath.py
index 1b1873f08b6..5481bb8888e 100644
--- a/Lib/ntpath.py
+++ b/Lib/ntpath.py
@@ -553,28 +553,21 @@ 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.
-
- """
-
- path = os.fspath(path)
- if not isabs(path):
- if isinstance(path, bytes):
- cwd = os.getcwdb()
- else:
- cwd = os.getcwd()
- 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
+ def abspath(path):
+ """Return the absolute version of a path."""
+ path = os.fspath(path)
+ if not isabs(path):
+ if isinstance(path, bytes):
+ cwd = os.getcwdb()
+ else:
+ cwd = os.getcwd()
+ path = join(cwd, path)
+ return normpath(path)
else: # use native Windows method on Windows
def abspath(path):
@@ -582,7 +575,27 @@ else: # use native Windows method on Windows
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
diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py
index 64cbfaaaaa0..4f59184dfcf 100644
--- a/Lib/test/test_ntpath.py
+++ b/Lib/test/test_ntpath.py
@@ -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("//../..")', "\\\\..\\")
diff --git a/Misc/NEWS.d/next/Library/2024-06-02-11-48-19.gh-issue-119826.N1obGa.rst b/Misc/NEWS.d/next/Library/2024-06-02-11-48-19.gh-issue-119826.N1obGa.rst
new file mode 100644
index 00000000000..6901e7475dd
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-06-02-11-48-19.gh-issue-119826.N1obGa.rst
@@ -0,0 +1 @@
+Always return an absolute path for :func:`os.path.abspath` on Windows.