@@ -952,6 +952,49 @@ def derive(self, excs):
952952 self .assertExceptionIsLike (tes , FalsyEG ("eg" , [TypeError (1 )]))
953953 self .assertExceptionIsLike (ves , FalsyEG ("eg" , [ValueError (2 )]))
954954
955+ def test_exception_group_subclass_with_bad_split_func (self ):
956+ # see gh-128049.
957+ class BadEG1 (ExceptionGroup ):
958+ def split (self , * args ):
959+ return "NOT A 2-TUPLE!"
960+
961+ class BadEG2 (ExceptionGroup ):
962+ def split (self , * args ):
963+ return ("NOT A 2-TUPLE!" ,)
964+
965+ eg_list = [
966+ (BadEG1 ("eg" , [OSError (123 ), ValueError (456 )]),
967+ r"split must return a tuple, not str" ),
968+ (BadEG2 ("eg" , [OSError (123 ), ValueError (456 )]),
969+ r"split must return a 2-tuple, got tuple of size 1" )
970+ ]
971+
972+ for eg_class , msg in eg_list :
973+ with self .assertRaisesRegex (TypeError , msg ) as m :
974+ try :
975+ raise eg_class
976+ except* ValueError :
977+ pass
978+ except* OSError :
979+ pass
980+
981+ self .assertExceptionIsLike (m .exception .__context__ , eg_class )
982+
983+ # we allow tuples of length > 2 for backwards compatibility
984+ class WeirdEG (ExceptionGroup ):
985+ def split (self , * args ):
986+ return super ().split (* args ) + ("anything" , 123456 , None )
987+
988+ try :
989+ raise WeirdEG ("eg" , [OSError (123 ), ValueError (456 )])
990+ except* OSError as e :
991+ oeg = e
992+ except* ValueError as e :
993+ veg = e
994+
995+ self .assertExceptionIsLike (oeg , WeirdEG ("eg" , [OSError (123 )]))
996+ self .assertExceptionIsLike (veg , WeirdEG ("eg" , [ValueError (456 )]))
997+
955998
956999class TestExceptStarCleanup (ExceptStarTest ):
9571000 def test_sys_exception_restored (self ):
0 commit comments