]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] GH-88013: Fix TypeError raised by ntpath.realpath in some cases (GH-102813...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 7 Apr 2023 20:38:56 +0000 (13:38 -0700)
committerGitHub <noreply@github.com>
Fri, 7 Apr 2023 20:38:56 +0000 (21:38 +0100)
(cherry picked from commit 4dc339b4d69195448207e1faecc3e258700daf33)

Co-authored-by: AN Long <aisk@users.noreply.github.com>
Co-authored-by: Barney Gale <barney.gale@gmail.com>
Lib/ntpath.py
Lib/test/test_ntpath.py
Misc/NEWS.d/next/Windows/2023-03-18-21-38-00.gh-issue-88013.Z3loxC.rst [new file with mode: 0644]

index 1cfb15b77102c9ef34ac978a83e850a2d22ccfe5..0444b0f65d10ba52356fdece1d05e07a8286da0b 100644 (file)
@@ -644,7 +644,7 @@ else:
 
         # Non-strict algorithm is to find as much of the target directory
         # as we can and join the rest.
-        tail = ''
+        tail = path[:0]
         while path:
             try:
                 path = _getfinalpathname(path)
index c26c74cdd61950a91272c191fa27e8b4d7993451..646e81d1e2fa9be49c83c6d7c304cebaa6f66b0a 100644 (file)
@@ -1,5 +1,6 @@
 import ntpath
 import os
+import string
 import sys
 import unittest
 import warnings
@@ -321,6 +322,16 @@ class TestNtpath(NtpathTestCase):
         self.assertPathEqual(ntpath.realpath(os.fsencode(ABSTFN + "1")),
                          os.fsencode(ABSTFN))
 
+        # gh-88013: call ntpath.realpath with binary drive name may raise a
+        # TypeError. The drive should not exist to reproduce the bug.
+        for c in string.ascii_uppercase:
+            d = f"{c}:\\"
+            if not ntpath.exists(d):
+                break
+        else:
+            raise OSError("No free drive letters available")
+        self.assertEqual(ntpath.realpath(d), d)
+
     @os_helper.skip_unless_symlink
     @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
     def test_realpath_strict(self):
diff --git a/Misc/NEWS.d/next/Windows/2023-03-18-21-38-00.gh-issue-88013.Z3loxC.rst b/Misc/NEWS.d/next/Windows/2023-03-18-21-38-00.gh-issue-88013.Z3loxC.rst
new file mode 100644 (file)
index 0000000..4ca3185
--- /dev/null
@@ -0,0 +1,2 @@
+Fixed a bug where :exc:`TypeError` was raised when calling
+:func:`ntpath.realpath` with a bytes parameter in some cases.