From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Fri, 7 Apr 2023 20:38:56 +0000 (-0700) Subject: [3.11] GH-88013: Fix TypeError raised by ntpath.realpath in some cases (GH-102813... X-Git-Tag: v3.11.4~217 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=70bc8c936dfbd936959efd32f0cddc2597598592;p=thirdparty%2FPython%2Fcpython.git [3.11] GH-88013: Fix TypeError raised by ntpath.realpath in some cases (GH-102813, GH-103343) (cherry picked from commit 4dc339b4d69195448207e1faecc3e258700daf33) Co-authored-by: AN Long Co-authored-by: Barney Gale --- diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 1cfb15b77102..0444b0f65d10 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -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) diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py index c26c74cdd619..646e81d1e2fa 100644 --- a/Lib/test/test_ntpath.py +++ b/Lib/test/test_ntpath.py @@ -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 index 000000000000..4ca3185ea1f6 --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2023-03-18-21-38-00.gh-issue-88013.Z3loxC.rst @@ -0,0 +1,2 @@ +Fixed a bug where :exc:`TypeError` was raised when calling +:func:`ntpath.realpath` with a bytes parameter in some cases.