@@ -1092,21 +1092,16 @@ print_exception_suggestions(struct exception_print_context *ctx,
10921092}
10931093
10941094static int
1095- print_exception_notes (struct exception_print_context * ctx , PyObject * value )
1095+ print_exception_notes (struct exception_print_context * ctx , PyObject * notes )
10961096{
10971097 PyObject * f = ctx -> file ;
10981098
1099- if (! PyExceptionInstance_Check ( value ) ) {
1099+ if (notes == NULL ) {
11001100 return 0 ;
11011101 }
11021102
1103- PyObject * notes ;
1104- int res = _PyObject_LookupAttr (value , & _Py_ID (__notes__ ), & notes );
1105- if (res <= 0 ) {
1106- return res ;
1107- }
11081103 if (!PySequence_Check (notes ) || PyUnicode_Check (notes ) || PyBytes_Check (notes )) {
1109- res = 0 ;
1104+ int res = 0 ;
11101105 if (write_indented_margin (ctx , f ) < 0 ) {
11111106 res = -1 ;
11121107 }
@@ -1119,7 +1114,6 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value)
11191114 res = PyFile_WriteObject (s , f , Py_PRINT_RAW );
11201115 Py_DECREF (s );
11211116 }
1122- Py_DECREF (notes );
11231117 if (PyFile_WriteString ("\n" , f ) < 0 ) {
11241118 res = -1 ;
11251119 }
@@ -1164,17 +1158,16 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value)
11641158 }
11651159 }
11661160
1167- Py_DECREF (notes );
11681161 return 0 ;
11691162error :
11701163 Py_XDECREF (lines );
1171- Py_DECREF (notes );
11721164 return -1 ;
11731165}
11741166
11751167static int
11761168print_exception (struct exception_print_context * ctx , PyObject * value )
11771169{
1170+ PyObject * notes = NULL ;
11781171 PyObject * f = ctx -> file ;
11791172
11801173 if (!PyExceptionInstance_Check (value )) {
@@ -1188,8 +1181,11 @@ print_exception(struct exception_print_context *ctx, PyObject *value)
11881181 goto error ;
11891182 }
11901183
1191- /* grab the type now because value can change below */
1184+ /* grab the type and notes now because value can change below */
11921185 PyObject * type = (PyObject * ) Py_TYPE (value );
1186+ if (_PyObject_LookupAttr (value , & _Py_ID (__notes__ ), & notes ) < 0 ) {
1187+ goto error ;
1188+ }
11931189
11941190 if (print_exception_file_and_line (ctx , & value ) < 0 ) {
11951191 goto error ;
@@ -1203,14 +1199,16 @@ print_exception(struct exception_print_context *ctx, PyObject *value)
12031199 if (PyFile_WriteString ("\n" , f ) < 0 ) {
12041200 goto error ;
12051201 }
1206- if (print_exception_notes (ctx , value ) < 0 ) {
1202+ if (print_exception_notes (ctx , notes ) < 0 ) {
12071203 goto error ;
12081204 }
12091205
1206+ Py_XDECREF (notes );
12101207 Py_DECREF (value );
12111208 assert (!PyErr_Occurred ());
12121209 return 0 ;
12131210error :
1211+ Py_XDECREF (notes );
12141212 Py_DECREF (value );
12151213 return -1 ;
12161214}
0 commit comments