@@ -1125,21 +1125,16 @@ print_exception_suggestions(struct exception_print_context *ctx,
11251125}
11261126
11271127static int
1128- print_exception_notes (struct exception_print_context * ctx , PyObject * value )
1128+ print_exception_notes (struct exception_print_context * ctx , PyObject * notes )
11291129{
11301130 PyObject * f = ctx -> file ;
11311131
1132- if (! PyExceptionInstance_Check ( value ) ) {
1132+ if (notes == NULL ) {
11331133 return 0 ;
11341134 }
11351135
1136- PyObject * notes ;
1137- int res = PyObject_GetOptionalAttr (value , & _Py_ID (__notes__ ), & notes );
1138- if (res <= 0 ) {
1139- return res ;
1140- }
11411136 if (!PySequence_Check (notes ) || PyUnicode_Check (notes ) || PyBytes_Check (notes )) {
1142- res = 0 ;
1137+ int res = 0 ;
11431138 if (write_indented_margin (ctx , f ) < 0 ) {
11441139 res = -1 ;
11451140 }
@@ -1152,7 +1147,6 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value)
11521147 res = PyFile_WriteObject (s , f , Py_PRINT_RAW );
11531148 Py_DECREF (s );
11541149 }
1155- Py_DECREF (notes );
11561150 if (PyFile_WriteString ("\n" , f ) < 0 ) {
11571151 res = -1 ;
11581152 }
@@ -1197,17 +1191,16 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value)
11971191 }
11981192 }
11991193
1200- Py_DECREF (notes );
12011194 return 0 ;
12021195error :
12031196 Py_XDECREF (lines );
1204- Py_DECREF (notes );
12051197 return -1 ;
12061198}
12071199
12081200static int
12091201print_exception (struct exception_print_context * ctx , PyObject * value )
12101202{
1203+ PyObject * notes = NULL ;
12111204 PyObject * f = ctx -> file ;
12121205
12131206 if (!PyExceptionInstance_Check (value )) {
@@ -1221,8 +1214,11 @@ print_exception(struct exception_print_context *ctx, PyObject *value)
12211214 goto error ;
12221215 }
12231216
1224- /* grab the type now because value can change below */
1217+ /* grab the type and notes now because value can change below */
12251218 PyObject * type = (PyObject * ) Py_TYPE (value );
1219+ if (PyObject_GetOptionalAttr (value , & _Py_ID (__notes__ ), & notes ) < 0 ) {
1220+ goto error ;
1221+ }
12261222
12271223 if (print_exception_file_and_line (ctx , & value ) < 0 ) {
12281224 goto error ;
@@ -1236,14 +1232,16 @@ print_exception(struct exception_print_context *ctx, PyObject *value)
12361232 if (PyFile_WriteString ("\n" , f ) < 0 ) {
12371233 goto error ;
12381234 }
1239- if (print_exception_notes (ctx , value ) < 0 ) {
1235+ if (print_exception_notes (ctx , notes ) < 0 ) {
12401236 goto error ;
12411237 }
12421238
1239+ Py_XDECREF (notes );
12431240 Py_DECREF (value );
12441241 assert (!PyErr_Occurred ());
12451242 return 0 ;
12461243error :
1244+ Py_XDECREF (notes );
12471245 Py_DECREF (value );
12481246 return -1 ;
12491247}
0 commit comments