Skip to content

Commit 268a721

Browse files
committed
add tests
1 parent 909ef27 commit 268a721

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Lib/test/test_capi/test_exceptions.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,30 @@ def _check_no_crash(self, exc):
429429
# ensure that the __str__() method does not crash
430430
_ = str(exc)
431431

432+
def test_unicode_encode_error_custom_str(self):
433+
434+
class Evil(str):
435+
def __str__(self):
436+
del exc.object
437+
return self
438+
439+
for reason, encoding in [
440+
("reason", Evil("encoding")),
441+
(Evil("reason"), "encoding"),
442+
(Evil("reason"), Evil("encoding")),
443+
]:
444+
with self.subTest(encoding=encoding, reason=reason):
445+
with self.subTest(UnicodeEncodeError):
446+
exc = UnicodeEncodeError(encoding, "x", 0, 1, reason)
447+
self.assertRaises(TypeError, str, exc)
448+
with self.subTest(UnicodeDecodeError):
449+
exc = UnicodeDecodeError(encoding, b"x", 0, 1, reason)
450+
self.assertRaises(TypeError, str, exc)
451+
452+
with self.subTest(UnicodeTranslateError):
453+
exc = UnicodeTranslateError("x", 0, 1, Evil("reason"))
454+
self.assertRaises(TypeError, str, exc)
455+
432456
def test_unicode_encode_error_get_start(self):
433457
get_start = _testcapi.unicode_encode_get_start
434458
self._test_unicode_error_get_start('x', UnicodeEncodeError, get_start)

0 commit comments

Comments
 (0)