@@ -207,22 +207,21 @@ BaseException_add_note(PyObject *self, PyObject *note)
207207 return NULL ;
208208 }
209209
210- if (!PyObject_HasAttr (self , & _Py_ID (__notes__ ))) {
211- PyObject * new_notes = PyList_New (0 );
212- if (new_notes == NULL ) {
210+ PyObject * notes ;
211+ if (_PyObject_LookupAttr (self , & _Py_ID (__notes__ ), & notes ) < 0 ) {
212+ return NULL ;
213+ }
214+ if (notes == NULL ) {
215+ notes = PyList_New (0 );
216+ if (notes == NULL ) {
213217 return NULL ;
214218 }
215- if (PyObject_SetAttr (self , & _Py_ID (__notes__ ), new_notes ) < 0 ) {
216- Py_DECREF (new_notes );
219+ if (PyObject_SetAttr (self , & _Py_ID (__notes__ ), notes ) < 0 ) {
220+ Py_DECREF (notes );
217221 return NULL ;
218222 }
219- Py_DECREF (new_notes );
220223 }
221- PyObject * notes = PyObject_GetAttr (self , & _Py_ID (__notes__ ));
222- if (notes == NULL ) {
223- return NULL ;
224- }
225- if (!PyList_Check (notes )) {
224+ else if (!PyList_Check (notes )) {
226225 Py_DECREF (notes );
227226 PyErr_SetString (PyExc_TypeError , "Cannot add note: __notes__ is not a list" );
228227 return NULL ;
@@ -941,11 +940,11 @@ exceptiongroup_subset(
941940 PyException_SetContext (eg , PyException_GetContext (orig ));
942941 PyException_SetCause (eg , PyException_GetCause (orig ));
943942
944- if ( PyObject_HasAttr ( orig , & _Py_ID ( __notes__ ))) {
945- PyObject * notes = PyObject_GetAttr ( orig , & _Py_ID (__notes__ ));
946- if ( notes == NULL ) {
947- goto error ;
948- }
943+ PyObject * notes ;
944+ if ( _PyObject_LookupAttr ( orig , & _Py_ID (__notes__ ), & notes ) < 0 ) {
945+ goto error ;
946+ }
947+ if ( notes ) {
949948 if (PySequence_Check (notes )) {
950949 /* Make a copy so the parts have independent notes lists. */
951950 PyObject * notes_copy = PySequence_List (notes );
0 commit comments