From 4dc339b4d69195448207e1faecc3e258700daf33 Mon Sep 17 00:00:00 2001 From: AN Long Date: Fri, 7 Apr 2023 19:56:00 +0800 Subject: [PATCH] GH-88013: Fix TypeError raised by ntpath.realpath in some cases (GH-102813) --- Lib/ntpath.py | 2 +- Lib/test/test_ntpath.py | 7 +++++++ .../Windows/2023-03-18-21-38-00.gh-issue-88013.Z3loxC.rst | 2 ++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Windows/2023-03-18-21-38-00.gh-issue-88013.Z3loxC.rst diff --git a/Lib/ntpath.py b/Lib/ntpath.py index e93a5e696009..6e2da79c85d3 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -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) diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py index 08c8a7a1f94b..4e755d154039 100644 --- a/Lib/test/test_ntpath.py +++ b/Lib/test/test_ntpath.py @@ -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 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. -- 2.47.3