8000 gh-126004: Remove redundant safeguards for codecs handlers tests (#12… · python/cpython@cf0b2da · GitHub
[go: up one dir, main page]

Skip to content

Commit cf0b2da

Browse files
authored
gh-126004: Remove redundant safeguards for codecs handlers tests (#127680)
We remove the safeguards that were added in `Lib/test/test_capi/test_codecs.py` since they are now redundant (see 32e07fd for additional context). Indeed, the codecs handlers now correctly handle the `start` and `end` positions of `UnicodeError` objects and thus should not crash.
1 parent b23b27b commit cf0b2da

File tree

1 file changed

+13
-42
lines changed

1 file changed

+13
-42
lines changed

Lib/test/test_capi/test_codecs.py

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -839,38 +839,33 @@ def test_codec_ignore_errors_handler(self):
839839

840840
def test_codec_replace_errors_handler(self):
841841
handler = _testcapi.codec_replace_errors
842-
self.do_test_codec_errors_handler(handler, self.all_unicode_errors,
843-
safe=True)
842+
self.do_test_codec_errors_handler(handler, self.all_unicode_errors)
844843

845844
def test_codec_xmlcharrefreplace_errors_handler(self):
846845
handler = _testcapi.codec_xmlcharrefreplace_errors
847-
self.do_test_codec_errors_handler(handler, self.unicode_encode_errors,
848-
safe=True)
846+
self.do_test_codec_errors_handler(handler, self.unicode_encode_errors)
849847

850848
def test_codec_backslashreplace_errors_handler(self):
851849
handler = _testcapi.codec_backslashreplace_errors
852-
self.do_test_codec_errors_handler(handler, self.all_unicode_errors,
853-
safe=True)
850+
self.do_test_codec_errors_handler(handler, self.all_unicode_errors)
854851

855852
def test_codec_namereplace_errors_handler(self):
856853
handler = _testlimitedcapi.codec_namereplace_errors
857854
self.do_test_codec_errors_handler(handler, self.unicode_encode_errors)
858855

859-
def do_test_codec_errors_handler(self, handler, exceptions, *, safe=False):
860-
at_least_one = False
856+
def do_test_codec_errors_handler(self, handler, exceptions):
857+
self.assertNotEqual(len(exceptions), 0)
861858
for exc in exceptions:
862-
# See https://github.com/python/cpython/issues/123378 and related
863-
# discussion and issues for details.
864-
if not safe and self._exception_may_crash(exc):
865-
continue
866-
867-
at_least_one = True
868859
with self.subTest(handler=handler, exc=exc):
869860
# test that the handler does not crash
870-
self.assertIsInstance(handler(exc), tuple)
871-
872-
if exceptions:
873-
self.assertTrue(at_least_one, "all exceptions are crashing")
861+
res = handler(exc)
862+
self.assertIsInstance(res, tuple)
863+
self.assertEqual(len(res), 2)
864+
replacement, continue_from = res
865+
self.assertIsInstance(replacement, str)
866+
self.assertIsInstance(continue_from, int)
867+
self.assertGreaterEqual(continue_from, 0)
868+
self.assertLessEqual(continue_from, len(exc.object))
874869

875870
for bad_exc in (
876871
self.bad_unicode_errors
@@ -879,30 +874,6 @@ def do_test_codec_errors_handler(self, handler, exceptions, *, safe=False):
879874
with self.subTest('bad type', handler=handler, exc=bad_exc):
880875
self.assertRaises(TypeError, handler, bad_exc)
881876

882-
@classmethod
883-
def _exception_may_crash(cls, exc):
884-
"""Indicate whether a Unicode exception might currently crash
885-
the interpreter when used by a built-in codecs error handler.
886-
887-
Until gh-123378 is fixed, we skip the tests for these exceptions.
888-
889-
This should only be used by "do_test_codec_errors_handler".
890-
"""
891-
message, start, end = exc.object, exc.start, exc.end
892-
match exc:
893-
case UnicodeEncodeError():
894-
return end < start or (end - start) >= len(message)
895-
case UnicodeDecodeError():
896-
# The case "end - start >= len(message)" does not crash.
897-
return end < start
898-
case UnicodeTranslateError():
899-
# Test "end <= start" because PyCodec_ReplaceErrors checks
900-
# the Unicode kind of a 0-length string which by convention
901-
# is PyUnicode_1BYTE_KIND and not PyUnicode_2BYTE_KIND as
902-
# the handler currently expects.
903-
return end <= start or (end - start) >= len(message)
904-
return False
905-
906877

907878
if __name__ == "__main__":
908879
unittest.main()

0 commit comments

Comments
 (0)
0