GH-88013: Fix TypeError raised by ntpath.realpath in some cases (GH-102813)

This commit is contained in:
AN Long 2023-04-07 19:56:00 +08:00 committed by GitHub
parent 995386071f
commit 4dc339b4d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 1 deletions

View 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)

View 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):

View File

@ -0,0 +1,2 @@
Fixed a bug where :exc:`TypeError` was raised when calling
:func:`ntpath.realpath` with a bytes parameter in some cases.