https://github.com/python/cpython/commit/90f9991abb2ef6422ec0ea8330533966a84d8c8f
commit: 90f9991abb2ef6422ec0ea8330533966a84d8c8f
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-03-23T12:17:54+01:00
summary:

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 <[email protected]>

files:
M Modules/_elementtree.c

diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index e0bc69c5fe22f8..721670ed99742e 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -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*)

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to