From 2ce0330b84d5ef5759dd22a233a607609ce318c5 Mon Sep 17 00:00:00 2001 From: AN Long Date: Fri, 7 Apr 2023 19:56:00 +0800 Subject: [PATCH 1/2] GH-88013: Fix TypeError raised by ntpath.realpath in some cases (GH-102813) (cherry picked from commit 4dc339b4d69195448207e1faecc3e258700daf33) Co-authored-by: AN Long --- 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 1cfb15b77102c9..0444b0f65d10ba 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -644,7 +644,7 @@ def _getfinalpathname_nonstrict(path): # 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 c26c74cdd61950..555ee646bbd767 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,12 @@ def test_realpath_basic(self): 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 00000000000000..4ca3185ea1f65e --- /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. From 9757c623d07d060a26d7e161adf109a58fd67c33 Mon Sep 17 00:00:00 2001 From: Barney Gale Date: Fri, 7 Apr 2023 19:42:09 +0100 Subject: [PATCH 2/2] Update test case to not use `os.listdrives()` --- Lib/test/test_ntpath.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py index 555ee646bbd767..646e81d1e2fa9b 100644 --- a/Lib/test/test_ntpath.py +++ b/Lib/test/test_ntpath.py @@ -324,8 +324,12 @@ def test_realpath_basic(self): # 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() + 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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy