mirror of https://github.com/python/cpython
bpo-31047: Fix ntpath.abspath for invalid paths (GH-8544)
This commit is contained in:
parent
3da5c5c76d
commit
b0bf51b322
|
@ -518,38 +518,36 @@ def normpath(path):
|
||||||
comps.append(curdir)
|
comps.append(curdir)
|
||||||
return prefix + sep.join(comps)
|
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.
|
# Return an absolute path.
|
||||||
try:
|
try:
|
||||||
from nt import _getfullpathname
|
from nt import _getfullpathname
|
||||||
|
|
||||||
except ImportError: # not running on Windows - mock up something sensible
|
except ImportError: # not running on Windows - mock up something sensible
|
||||||
def abspath(path):
|
abspath = _abspath_fallback
|
||||||
"""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
|
else: # use native Windows method on Windows
|
||||||
def abspath(path):
|
def abspath(path):
|
||||||
"""Return the absolute version of a path."""
|
"""Return the absolute version of a path."""
|
||||||
|
try:
|
||||||
if path: # Empty path must return current working directory.
|
return _getfullpathname(path)
|
||||||
path = os.fspath(path)
|
except OSError:
|
||||||
try:
|
return _abspath_fallback(path)
|
||||||
path = _getfullpathname(path)
|
|
||||||
except OSError:
|
|
||||||
pass # Bad path - return unchanged.
|
|
||||||
elif isinstance(path, bytes):
|
|
||||||
path = os.getcwdb()
|
|
||||||
else:
|
|
||||||
path = os.getcwd()
|
|
||||||
return normpath(path)
|
|
||||||
|
|
||||||
# realpath is a no-op on systems without islink support
|
# realpath is a no-op on systems without islink support
|
||||||
realpath = abspath
|
realpath = abspath
|
||||||
|
|
|
@ -303,6 +303,10 @@ class TestNtpath(unittest.TestCase):
|
||||||
try:
|
try:
|
||||||
import nt
|
import nt
|
||||||
tester('ntpath.abspath("C:\\")', "C:\\")
|
tester('ntpath.abspath("C:\\")', "C:\\")
|
||||||
|
with support.temp_cwd(support.TESTFN) as cwd_dir: # bpo-31047
|
||||||
|
tester('ntpath.abspath("")', cwd_dir)
|
||||||
|
tester('ntpath.abspath(" ")', cwd_dir + "\\ ")
|
||||||
|
tester('ntpath.abspath("?")', cwd_dir + "\\?")
|
||||||
except ImportError:
|
except ImportError:
|
||||||
self.skipTest('nt module not available')
|
self.skipTest('nt module not available')
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix ``ntpath.abspath`` for invalid paths on windows. Patch by Franz
|
||||||
|
Woellert.
|
Loading…
Reference in New Issue