@@ -1000,5 +1000,135 @@ def test_exc_info_restored(self):
1000
1000
self .assertEqual (sys .exc_info (), (None , None , None ))
1001
1001
1002
1002
1003
+ class TestExceptStarUnhashableLeafExceptions (ExceptStarTest ):
1004
+ class UnhashableExc (ValueError ):
1005
+ hash = None
1006
+
1007
+ def except_type (self , eg , type ):
1008
+ match , rest = None , None
1009
+ try :
1010
+ try :
1011
+ raise eg
1012
+ except* type as e :
1013
+ match = e
1014
+ except Exception as e :
1015
+ rest = e
1016
+ return match , rest
1017
+
1018
+ def test_catch_unhashable_leaf_exception (self ):
1019
+ Bad = self .UnhashableExc
1020
+ eg = ExceptionGroup ("eg" , [TypeError (1 ), Bad (2 )])
1021
+ match , rest = self .except_type (eg , Bad )
1022
+ self .assertExceptionIsLike (match , ExceptionGroup ("eg" , [Bad (2 )]))
1023
+ self .assertExceptionIsLike (rest , ExceptionGroup ("eg" , [TypeError (1 )]))
1024
+
1025
+ def test_propagate_unhashable_leaf (self ):
1026
+ Bad = self .UnhashableExc
1027
+ eg = ExceptionGroup ("eg" , [TypeError (1 ), Bad (2 )])
1028
+ match , rest = self .except_type (eg , TypeError )
1029
+ self .assertExceptionIsLike (match , ExceptionGroup ("eg" , [TypeError (1 )]))
1030
+ self .assertExceptionIsLike (rest , ExceptionGroup ("eg" , [Bad (2 )]))
1031
+
1032
+ def test_catch_nothing_unhashable_leaf (self ):
1033
+ Bad = self .UnhashableExc
1034
+ eg = ExceptionGroup ("eg" , [TypeError (1 ), Bad (2 )])
1035
+ match , rest = self .except_type (eg , OSError )
1036
+ self .assertIsNone (match )
1037
+ self .assertExceptionIsLike (rest , eg )
1038
+
1039
+ def test_catch_everything_unhashable_leaf (self ):
1040
+ Bad = self .UnhashableExc
1041
+ eg = ExceptionGroup ("eg" , [TypeError (1 ), Bad (2 )])
1042
+ match , rest = self .except_type (eg , Exception )
1043
+ self .assertExceptionIsLike (match , eg )
1044
+ self .assertIsNone (rest )
1045
+
1046
+ def test_reraise_unhashable_leaf (self ):
1047
+ Bad = self .UnhashableExc
1048
+ eg = ExceptionGroup ("eg" , [TypeError (1 ), Bad (2 ), ValueError (3 )])
1049
+ try :
1050
+ try :
1051
+ raise eg
1052
+ except* TypeError :
1053
+ pass
1054
+ except* Bad :
1055
+ raise
1056
+ except Exception as e :
1057
+ exc = e
1058
+
1059
+ self .assertExceptionIsLike (
1060
+ exc , ExceptionGroup ("eg" , [Bad (2 ), ValueError (3 )]))
1061
+
1062
+
1063
+ class TestExceptStarUnhashableExceptionGroupSubclass (ExceptStarTest ):
1064
+ class UnhashableEG (ExceptionGroup ):
1065
+ hash = None
1066
+
1067
+ def derive (self , excs ):
1068
+ return type (self )(self .message , excs )
1069
+
1070
+ def except_type (self , eg , type ):
1071
+ match , rest = None , None
1072
+ try :
1073
+ try :
1074
+ raise eg
1075
+ except* type as e :
1076
+ match = e
1077
+ except Exception as e :
1078
+ rest = e
1079
+ return match , rest
1080
+
1081
+ def test_catch_some_unhashable_exception_group_subclass (self ):
1082
+ BadEG = self .UnhashableEG
1083
+ eg = BadEG ("eg" ,
1084
+ [TypeError (1 ),
1085
+ BadEG ("nested" , [ValueError (2 )])])
1086
+
1087
+ match , rest = self .except_type (eg , TypeError )
1088
+ self .assertExceptionIsLike (match , BadEG ("eg" , [TypeError (1 )]))
1089
+ self .assertExceptionIsLike (rest ,
1090
+ BadEG ("eg" , [BadEG ("nested" , [ValueError (2 )])]))
1091
+
1092
+ def test_catch_none_unhashable_exception_group_subclass (self ):
1093
+ BadEG = self .UnhashableEG
1094
+ eg = BadEG ("eg" ,
1095
+ [TypeError (1 ),
1096
+ BadEG ("nested" , [ValueError (2 )])])
1097
+
1098
+ match , rest = self .except_type (eg , OSError )
1099
+ self .assertIsNone (match )
1100
+ self .assertExceptionIsLike (rest , eg )
1101
+
1102
+ def test_catch_all_unhashable_exception_group_subclass (self ):
1103
+ BadEG = self .UnhashableEG
1104
+ eg = BadEG ("eg" ,
1105
+ [TypeError (1 ),
1106
+ BadEG ("nested" , [ValueError (2 )])])
1107
+
1108
+ match , rest = self .except_type (eg , Exception )
1109
+ self .assertExceptionIsLike (match , eg )
1110
+ self .assertIsNone (rest )
1111
+
1112
+ def test_reraise_unhashable_eg (self ):
1113
+ BadEG = self .UnhashableEG
1114
+ eg = BadEG ("eg" ,
1115
+ [TypeError (1 ), ValueError (2 ),
1116
+ BadEG ("nested" , [ValueError (3 ), OSError (4 )])])
1117
+
1118
+ try :
1119
+ try :
1120
+ raise eg
1121
+ except* ValueError :
1122
+ pass
1123
+ except* OSError :
1124
+ raise
1125
+ except Exception as e :
1126
+ exc = e
1127
+
1128
+ self .assertExceptionIsLike (
1129
+ exc , BadEG ("eg" , [TypeError (1 ),
1130
+ BadEG ("nested" , [OSError (4 )])]))
1131
+
1132
+
1003
1133
if __name__ == '__main__' :
1004
1134
unittest .main ()
0 commit comments