mirror of
https://github.com/python/cpython.git
synced 2025-08-09 19:38:42 +00:00
gh-119826: Improved fallback for ntpath.abspath() on Windows (GH-119938)
(cherry picked from commit 4b00aba42e
)
Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
This commit is contained in:
parent
8fffbb0982
commit
af2d24b5ad
3 changed files with 35 additions and 18 deletions
|
@ -561,28 +561,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):
|
||||
|
@ -590,7 +583,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 _getfinalpathname, readlink as _nt_readlink
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue