Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-146056: Rework ref counting in treebuilder_handle_end()
Use more regular code to handle reference counting in
treebuilder_handle_end().

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
  • Loading branch information
vstinner and serhiy-storchaka committed Mar 19, 2026
commit e00b6122a53fff4b6cca2e05be67c134cd49fdfb
19 changes: 11 additions & 8 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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*)
Expand Down
Loading