Skip to content

Commit 4dc339b

Browse files
authored
GH-88013: Fix TypeError raised by ntpath.realpath in some cases (GH-102813)
1 parent 9953860 commit 4dc339b

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

Lib/ntpath.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ def _getfinalpathname_nonstrict(path):
670670

671671
# Non-strict algorithm is to find as much of the target directory
672672
# as we can and join the rest.
673-
tail = ''
673+
tail = path[:0]
674674
while path:
675675
try:
676676
path = _getfinalpathname(path)

Lib/test/test_ntpath.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import inspect
22
import ntpath
33
import os
4+
import string
45
import sys
56
import unittest
67
import warnings
@@ -374,6 +375,12 @@ def test_realpath_basic(self):
374375
self.assertPathEqual(ntpath.realpath(os.fsencode(ABSTFN + "1")),
375376
os.fsencode(ABSTFN))
376377

378+
# gh-88013: call ntpath.realpath with binary drive name may raise a
379+
# TypeError. The drive should not exist to reproduce the bug.
380+
drives = {f"{c}:\\" for c in string.ascii_uppercase} - set(os.listdrives())
381+
d = drives.pop().encode()
382+
self.assertEqual(ntpath.realpath(d), d)
383+
377384
@os_helper.skip_unless_symlink
378385
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
379386
def test_realpath_strict(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed a bug where :exc:`TypeError` was raised when calling
2+
:func:`ntpath.realpath` with a bytes parameter in some cases.

0 commit comments

Comments
 (0)