@@ -947,6 +947,7 @@ print_exception(struct exception_print_context *ctx, PyObject *value)
947
947
Py_DECREF (filename );
948
948
if (line != NULL ) {
949
949
err += WRITE_INDENTED_MARGIN (ctx , f );
950
+ PyErr_Clear ();
950
951
PyFile_WriteObject (line , f , Py_PRINT_RAW );
951
952
Py_DECREF (line );
952
953
}
@@ -997,6 +998,7 @@ print_exception(struct exception_print_context *ctx, PyObject *value)
997
998
if (!_PyUnicode_EqualToASCIIId (modulename , & PyId_builtins ) &&
998
999
!_PyUnicode_EqualToASCIIId (modulename , & PyId___main__ ))
999
1000
{
1001
+ PyErr_Clear ();
1000
1002
err += PyFile_WriteObject (modulename , f , Py_PRINT_RAW );
1001
1003
err += PyFile_WriteString ("." , f );
1002
1004
}
@@ -1010,6 +1012,7 @@ print_exception(struct exception_print_context *ctx, PyObject *value)
1010
1012
err = PyFile_WriteString ("<unknown>" , f );
1011
1013
}
1012
1014
else {
1015
+ PyErr_Clear ();
1013
1016
err = PyFile_WriteObject (qualname , f , Py_PRINT_RAW );
1014
1017
Py_DECREF (qualname );
1015
1018
}
@@ -1072,7 +1075,6 @@ static int
1072
1075
print_chained (struct exception_print_context * ctx , PyObject * value ,
1073
1076
const char * message , const char * tag ) {
1074
1077
PyObject * f = ctx -> file ;
1075
- int err = 0 ;
1076
1078
PyObject * parent_label = ctx -> parent_label ;
1077
1079
PyObject * label = NULL ;
1078
1080
int need_close = ctx -> need_close ;
@@ -1084,16 +1086,25 @@ print_chained(struct exception_print_context* ctx, PyObject *value,
1084
1086
}
1085
1087
ctx -> parent_label = label ;
1086
1088
1087
- print_exception_recursive (ctx , value );
1088
- err |= WRITE_INDENTED_MARGIN (ctx , f );
1089
- err |= PyFile_WriteString ("\n" , f );
1090
- err |= WRITE_INDENTED_MARGIN (ctx , f );
1091
- err |= PyFile_WriteString (message , f );
1092
- err |= WRITE_INDENTED_MARGIN (ctx , f );
1093
- err |= PyFile_WriteString ("\n" , f );
1089
+ int err = Py_EnterRecursiveCall (" in print_chained" );
1090
+ if (!err ) {
1091
+ print_exception_recursive (ctx , value );
1092
+ Py_LeaveRecursiveCall ();
1093
+
1094
+ err |= WRITE_INDENTED_MARGIN (ctx , f );
1095
+ err |= PyFile_WriteString ("\n" , f );
1096
+ err |= WRITE_INDENTED_MARGIN (ctx , f );
1097
+ err |= PyFile_WriteString (message , f );
1098
+ err |= WRITE_INDENTED_MARGIN (ctx , f );
1099
+ err |= PyFile_WriteString ("\n" , f );
1100
+ }
1101
+ else {
1102
+ PyErr_Clear ();
1103
+ }
1094
1104
1095
1105
ctx -> need_close = need_close ;
1096
1106
ctx -> parent_label = parent_label ;
1107
+
1097
1108
Py_XDECREF (label );
1098
1109
return err ;
1099
1110
}
@@ -1156,7 +1167,6 @@ print_exception_recursive(struct exception_print_context* ctx, PyObject *value)
1156
1167
1157
1168
/* TODO: add arg to limit number of exceptions printed? */
1158
1169
1159
-
1160
1170
if (ctx -> exception_group_depth == 0 ) {
1161
1171
ctx -> exception_group_depth += 1 ;
1162
1172
}
@@ -1190,12 +1200,21 @@ print_exception_recursive(struct exception_print_context* ctx, PyObject *value)
1190
1200
"%s+---------------- %U ----------------\n" ,
1191
1201
(i == 0 ) ? "+-" : " " , label );
1192
1202
ctx -> exception_group_depth += 1 ;
1203
+ PyErr_Clear ();
1193
1204
err |= PyFile_WriteObject (line , f , Py_PRINT_RAW );
1194
1205
Py_XDECREF (line );
1195
1206
1196
1207
ctx -> parent_label = label ;
1197
1208
PyObject * exc = PyTuple_GetItem (excs , i );
1198
- print_exception_recursive (ctx , exc );
1209
+
1210
+ if (!Py_EnterRecursiveCall (" in print_exception_recursive" )) {
1211
+ print_exception_recursive (ctx , exc );
1212
+ Py_LeaveRecursiveCall ();
1213
+ }
1214
+ else {
1215
+ err = -1 ;
1216
+ PyErr_Clear ();
1217
+ }
1199
1218
ctx -> parent_label = parent_label ;
1200
1219
Py_XDECREF (label );
1201
1220
0 commit comments