diff options
author | Franz Wöllert <franz.woellert@gmail.com> | 2018-07-29 12:47:09 (GMT) |
---|---|---|
committer | Steve Dower <steve.dower@microsoft.com> | 2018-07-29 12:47:09 (GMT) |
commit | d2e902e4fb304f27e4a72356efbc1fc26be3935d (patch) | |
tree | 04a6fcc9f9e2e02a9ea6412834d05e1d4610d42a /Lib | |
parent | b7fd73896db5666020e382c010b8da900260ede4 (diff) | |
download | cpython-d2e902e4fb304f27e4a72356efbc1fc26be3935d.zip cpython-d2e902e4fb304f27e4a72356efbc1fc26be3935d.tar.gz cpython-d2e902e4fb304f27e4a72356efbc1fc26be3935d.tar.bz2 |
bpo-31047: Fix ntpath.abspath for invalid paths (GH-8544)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/ntpath.py | 42 | ||||
-rw-r--r-- | Lib/test/test_ntpath.py | 4 |
2 files changed, 24 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 diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py index 1e85ad5..f37a994 100644 --- a/Lib/test/test_ntpath.py +++ b/Lib/test/test_ntpath.py @@ -280,6 +280,10 @@ class TestNtpath(unittest.TestCase): @unittest.skipUnless(nt, "abspath requires 'nt' module") def test_abspath(self): 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 + "\\?") def test_relpath(self): tester('ntpath.relpath("a")', 'a') |