[Python-checkins] bpo-31047: Fix ntpath.abspath for invalid paths (GH-8544)

Miss Islington (bot) webhook-mailer at python.org
Sun Jul 29 11:42:21 EDT 2018


https://github.com/python/cpython/commit/5753b13cb949b939b2b29cec5e2d646f9a30db44
commit: 5753b13cb949b939b2b29cec5e2d646f9a30db44
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-07-29T08:42:18-07:00
summary:

bpo-31047: Fix ntpath.abspath for invalid paths (GH-8544)

(cherry picked from commit d2e902e4fb304f27e4a72356efbc1fc26be3935d)

Co-authored-by: Franz Wöllert <franz.woellert at gmail.com>

files:
A Misc/NEWS.d/next/Library/2018-07-29-14-12-23.bpo-31047.FSarLs.rst
M Lib/ntpath.py
M Lib/test/test_ntpath.py

diff --git a/Lib/ntpath.py b/Lib/ntpath.py
index 2182ec776cc5..f0e03a2f496a 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 2d48be844242..907ca7edbead 100644
--- a/Lib/test/test_ntpath.py
+++ b/Lib/test/test_ntpath.py
@@ -280,6 +280,10 @@ def test_expanduser(self):
     @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')
diff --git a/Misc/NEWS.d/next/Library/2018-07-29-14-12-23.bpo-31047.FSarLs.rst b/Misc/NEWS.d/next/Library/2018-07-29-14-12-23.bpo-31047.FSarLs.rst
new file mode 100644
index 000000000000..6415d4a95aef
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-07-29-14-12-23.bpo-31047.FSarLs.rst
@@ -0,0 +1,2 @@
+Fix ``ntpath.abspath`` for invalid paths on windows. Patch by Franz
+Woellert.



More information about the Python-checkins mailing list