Skip to content

Commit

Permalink
GH-88013: Fix TypeError raised by ntpath.realpath in some cases (GH-1…
Browse files Browse the repository at this point in the history
  • Loading branch information
aisk authored Apr 7, 2023
1 parent 9953860 commit 4dc339b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,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)
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import inspect
import ntpath
import os
import string
import sys
import unittest
import warnings
Expand Down Expand Up @@ -374,6 +375,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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a bug where :exc:`TypeError` was raised when calling
:func:`ntpath.realpath` with a bytes parameter in some cases.

0 comments on commit 4dc339b

Please sign in to comment.