@@ -839,38 +839,33 @@ def test_codec_ignore_errors_handler(self):
839
839
840
840
def test_codec_replace_errors_handler (self ):
841
841
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 )
844
843
845
844
def test_codec_xmlcharrefreplace_errors_handler (self ):
846
845
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 )
849
847
850
848
def test_codec_backslashreplace_errors_handler (self ):
851
849
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 )
854
851
855
852
def test_codec_namereplace_errors_handler (self ):
856
853
handler = _testlimitedcapi .codec_namereplace_errors
857
854
self .do_test_codec_errors_handler (handler , self .unicode_encode_errors )
858
855
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 )
861
858
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
868
859
with self .subTest (handler = handler , exc = exc ):
869
860
# 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 ))
874
869
875
870
for bad_exc in (
876
871
self .bad_unicode_errors
@@ -879,30 +874,6 @@ def do_test_codec_errors_handler(self, handler, exceptions, *, safe=False):
879
874
with self .subTest ('bad type' , handler = handler , exc = bad_exc ):
880
875
self .assertRaises (TypeError , handler , bad_exc )
881
876
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
-
906
877
907
878
if __name__ == "__main__" :
908
879
unittest .main ()
0 commit comments