gh-146056: Rework ref counting in treebuilder_handle_end() (#146167)

Use more regular code to handle reference counting in
treebuilder_handle_end().

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Victor Stinner 2026-03-23 12:17:54 +01:00 committed by GitHub
parent fb8d8d9c9f
commit 90f9991abb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2840,8 +2840,6 @@ treebuilder_handle_data(TreeBuilderObject* self, PyObject* data)
LOCAL(PyObject*)
treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)
{
PyObject* item;
if (treebuilder_flush_data(self) < 0) {
return NULL;
}
@ -2854,17 +2852,22 @@ treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)
return NULL;
}
item = self->last;
self->last = Py_NewRef(self->this);
Py_XSETREF(self->last_for_tail, self->last);
PyObject *last = self->last;
PyObject *last_for_tail = self->last_for_tail;
PyObject *this = self->this;
self->last = Py_NewRef(this);
self->last_for_tail = Py_NewRef(this);
self->index--;
self->this = Py_NewRef(PyList_GET_ITEM(self->stack, self->index));
Py_DECREF(item);
Py_DECREF(last);
Py_XDECREF(last_for_tail);
if (treebuilder_append_event(self, self->end_event_obj, self->last) < 0)
if (treebuilder_append_event(self, self->end_event_obj, self->last) < 0) {
Py_DECREF(this);
return NULL;
}
return Py_NewRef(self->last);
return this;
}
LOCAL(PyObject*)