summaryrefslogtreecommitdiffstats
path: root/Lib/ntpath.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2018-07-29 15:42:18 (GMT)
committerGitHub <noreply@github.com>2018-07-29 15:42:18 (GMT)
commit5753b13cb949b939b2b29cec5e2d646f9a30db44 (patch)
tree4231d1271dec3cd31aee247eb417478a598b897e /Lib/ntpath.py
parent6332de10c7ff703d7f9cef62969aa68e7d28d075 (diff)
downloadcpython-5753b13cb949b939b2b29cec5e2d646f9a30db44.zip
cpython-5753b13cb949b939b2b29cec5e2d646f9a30db44.tar.gz
cpython-5753b13cb949b939b2b29cec5e2d646f9a30db44.tar.bz2
bpo-31047: Fix ntpath.abspath for invalid paths (GH-8544)
(cherry picked from commit d2e902e4fb304f27e4a72356efbc1fc26be3935d) Co-authored-by: Franz Wöllert <franz.woellert@gmail.com>
Diffstat (limited to 'Lib/ntpath.py')
-rw-r--r--Lib/ntpath.py42
1 files changed, 20 insertions, 22 deletions
diff --git a/Lib/ntpath.py b/Lib/ntpath.py
index 2182ec7..f0e03a2 100644
--- a/Lib/ntpath.py
+++ b/Lib/ntpath.py
@@ -496,38 +496,36 @@ def normpath(path):
comps.append(curdir)
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
- 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)
+ abspath = _abspath_fallback
else: # use native Windows method on Windows
def abspath(path):
"""Return the absolute version of a path."""
-
- if path: # Empty path must return current working directory.
- path = os.fspath(path)
- try:
- path = _getfullpathname(path)
- except OSError:
- pass # Bad path - return unchanged.
- elif isinstance(path, bytes):
- path = os.getcwdb()
- else:
- path = os.getcwd()
- return normpath(path)
+ try:
+ return _getfullpathname(path)
+ except OSError:
+ return _abspath_fallback(path)
# realpath is a no-op on systems without islink support
realpath = abspath