[Python-checkins] bpo-38081: Fixes ntpath.realpath('NUL') (GH-15899)

Miss Islington (bot) webhook-mailer at python.org
Wed Sep 11 06:43:34 EDT 2019


https://github.com/python/cpython/commit/57491de7c33c5886c4cae2f468b474d9b4e6fed2
commit: 57491de7c33c5886c4cae2f468b474d9b4e6fed2
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-09-11T03:43:30-07:00
summary:

bpo-38081: Fixes ntpath.realpath('NUL') (GH-15899)

(cherry picked from commit 92521fea5d0d4aeb9b6a3c3fdd4654af700ad5c8)

Co-authored-by: Steve Dower <steve.dower at python.org>

files:
A Misc/NEWS.d/next/Windows/2019-09-11-10-22-01.bpo-38081.8JhzjD.rst
M Lib/ntpath.py
M Lib/test/test_ntpath.py

diff --git a/Lib/ntpath.py b/Lib/ntpath.py
index 1d22d5f1dc2a..1b5e16f95f16 100644
--- a/Lib/ntpath.py
+++ b/Lib/ntpath.py
@@ -535,9 +535,10 @@ def _readlink_deep(path, seen=None):
             try:
                 path = _nt_readlink(path)
             except OSError as ex:
-                # Stop on file (2) or directory (3) not found, or
-                # paths that are not reparse points (4390)
-                if ex.winerror in (2, 3, 4390):
+                # Stop on incorrect function (1), file (2) or
+                # directory (3) not found, or paths that are
+                # not reparse points (4390)
+                if ex.winerror in (1, 2, 3, 4390):
                     break
                 raise
             except ValueError:
@@ -553,9 +554,9 @@ def _getfinalpathname_nonstrict(path):
         except OSError:
             pass
 
-        # Allow file (2) or directory (3) not found, invalid syntax (123),
-        # and symlinks that cannot be followed (1921)
-        allowed_winerror = 2, 3, 123, 1921
+        # Allow file (2) or directory (3) not found, incorrect parameter (87),
+        # invalid syntax (123), and symlinks that cannot be followed (1921)
+        allowed_winerror = 2, 3, 87, 123, 1921
 
         # Non-strict algorithm is to find as much of the target directory
         # as we can and join the rest.
diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py
index c5c96e32d747..2f0faf94726f 100644
--- a/Lib/test/test_ntpath.py
+++ b/Lib/test/test_ntpath.py
@@ -400,6 +400,10 @@ def test_realpath_symlink_prefix(self):
         self.assertPathEqual(ntpath.realpath("\\\\?\\" + ABSTFN + "3.link"),
                              "\\\\?\\" + ABSTFN + "3.")
 
+    @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
+    def test_realpath_nul(self):
+        tester("ntpath.realpath('NUL')", r'\\.\NUL')
+
     def test_expandvars(self):
         with support.EnvironmentVarGuard() as env:
             env.clear()
diff --git a/Misc/NEWS.d/next/Windows/2019-09-11-10-22-01.bpo-38081.8JhzjD.rst b/Misc/NEWS.d/next/Windows/2019-09-11-10-22-01.bpo-38081.8JhzjD.rst
new file mode 100644
index 000000000000..e9d7a30eed9f
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2019-09-11-10-22-01.bpo-38081.8JhzjD.rst
@@ -0,0 +1 @@
+Prevent error calling :func:`os.path.realpath` on ``'NUL'``.



More information about the Python-checkins mailing list