aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/ntpath.py
diff options
context:
space:
mode:
authorNice Zombies <nineteendo19d0@gmail.com>2024-11-12 22:18:03 +0100
committerGitHub <noreply@github.com>2024-11-12 21:18:03 +0000
commit4b00aba42e4d9440d22e399ec2122fe8601bbe54 (patch)
tree4de6297865b11706f73f1ed22351542822396a65 /Lib/ntpath.py
parent5610860840aa71b186fc5639211dd268b817d65f (diff)
downloadcpython-4b00aba42e4d9440d22e399ec2122fe8601bbe54.tar.gz
cpython-4b00aba42e4d9440d22e399ec2122fe8601bbe54.zip
gh-119826: Improved fallback for ntpath.abspath() on Windows (GH-119938)
Diffstat (limited to 'Lib/ntpath.py')
-rw-r--r--Lib/ntpath.py49
1 files changed, 31 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