@@ -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