Skip to content

Commit 70bc8c9

Browse files
miss-islingtonaiskbarneygale
authored
[3.11] GH-88013: Fix TypeError raised by ntpath.realpath in some cases (GH-102813, GH-103343)
(cherry picked from commit 4dc339b) Co-authored-by: AN Long <[email protected]> Co-authored-by: Barney Gale <[email protected]>
1 parent b8d1623 commit 70bc8c9

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

Lib/ntpath.py

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

645645
# Non-strict algorithm is to find as much of the target directory
646646
# as we can and join the rest.
647-
tail = ''
647+
tail = path[:0]
648648
while path:
649649
try:
650650
path = _getfinalpathname(path)

Lib/test/test_ntpath.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ntpath
22
import os
3+
import string
34
import sys
45
import unittest
56
import warnings
@@ -321,6 +322,16 @@ def test_realpath_basic(self):
321322
self.assertPathEqual(ntpath.realpath(os.fsencode(ABSTFN + "1")),
322323
os.fsencode(ABSTFN))
323324

325+
# gh-88013: call ntpath.realpath with binary drive name may raise a
326+
# TypeError. The drive should not exist to reproduce the bug.
327+
for c in string.ascii_uppercase:
328+
d = f"{c}:\\"
329+
if not ntpath.exists(d):
330+
break
331+
else:
332+
raise OSError("No free drive letters available")
333+
self.assertEqual(ntpath.realpath(d), d)
334+
324335
@os_helper.skip_unless_symlink
325336
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
326337
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)