]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-88013: Fix TypeError raised by ntpath.realpath in some cases (GH-102813)
authorAN Long <aisk@users.noreply.github.com>
Fri, 7 Apr 2023 11:56:00 +0000 (19:56 +0800)
committerGitHub <noreply@github.com>
Fri, 7 Apr 2023 11:56:00 +0000 (12:56 +0100)
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 e93a5e69600973e1ddac83bf92c94c26f9cfffee..6e2da79c85d3f086549de2b6daaa5c9718410726 100644 (file)
@@ -670,7 +670,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 08c8a7a1f94b956c5c100ef6ed33dfa18df765e6..4e755d15403916a8286f1ac919465178eb1ed4fb 100644 (file)
@@ -1,6 +1,7 @@
 import inspect
 import ntpath
 import os
+import string
 import sys
 import unittest
 import warnings
@@ -374,6 +375,12 @@ 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.
+        drives = {f"{c}:\\" for c in string.ascii_uppercase} - set(os.listdrives())
+        d = drives.pop().encode()
+        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.