diff options
author | AN Long <aisk@users.noreply.github.com> | 2023-04-07 11:56:00 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-07 11:56:00 (GMT) |
commit | 4dc339b4d69195448207e1faecc3e258700daf33 (patch) | |
tree | 252c06708b64e198c6beaf2b060ed629b4a3dcd9 | |
parent | 995386071f96e4cfebfa027a71ca9134e4651d2a (diff) | |
download | cpython-4dc339b4d69195448207e1faecc3e258700daf33.zip cpython-4dc339b4d69195448207e1faecc3e258700daf33.tar.gz cpython-4dc339b4d69195448207e1faecc3e258700daf33.tar.bz2 |
GH-88013: Fix TypeError raised by ntpath.realpath in some cases (GH-102813)
-rw-r--r-- | Lib/ntpath.py | 2 | ||||
-rw-r--r-- | Lib/test/test_ntpath.py | 7 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Windows/2023-03-18-21-38-00.gh-issue-88013.Z3loxC.rst | 2 |
3 files changed, 10 insertions, 1 deletions
diff --git a/Lib/ntpath.py b/Lib/ntpath.py index e93a5e6..6e2da79 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 08c8a7a..4e755d1 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 0000000..4ca3185 --- /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. |