From e7d7435ca1fc8e4018b30afd438fd1b5bb2e2b44 Mon Sep 17 00:00:00 2001 From: tonghuaroot Date: Sat, 11 Jul 2026 11:49:27 +0800 Subject: [PATCH 1/2] gh-153537: Fix Element re-init memory leak in the C accelerator element_init allocated a new extra without releasing the previous one, leaking the element's children and attributes on a second __init__ call. Clear the old extra first, so re-init discards the previous children and attributes, matching the pure-Python implementation. --- Lib/test/test_xml_etree.py | 13 +++++++++++++ .../2026-07-11-11-36-15.gh-issue-153537.2KySPQ.rst | 3 +++ Modules/_elementtree.c | 3 +++ 3 files changed, 19 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-07-11-11-36-15.gh-issue-153537.2KySPQ.rst diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index acec4ec2ca257c4..78ccf4581f7c812 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -2654,6 +2654,19 @@ def f(): class BasicElementTest(ElementTestCase, unittest.TestCase): + def test_reinit_releases_children(self): + # gh-153537: re-init should release the previous children. + e = ET.Element('a', {'k': 'v'}) + children = [ET.SubElement(e, 'b') for _ in range(3)] + refs = [weakref.ref(c) for c in children] + del children + self.assertEqual(len(e), 3) + e.__init__('a') + gc_collect() + self.assertEqual(len(e), 0) + self.assertEqual(e.attrib, {}) + self.assertTrue(all(ref() is None for ref in refs)) + def test___init__(self): tag = "foo" attrib = { "zix": "wyp" } diff --git a/Misc/NEWS.d/next/Library/2026-07-11-11-36-15.gh-issue-153537.2KySPQ.rst b/Misc/NEWS.d/next/Library/2026-07-11-11-36-15.gh-issue-153537.2KySPQ.rst new file mode 100644 index 000000000000000..5fa070be5f7f1c1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-11-11-36-15.gh-issue-153537.2KySPQ.rst @@ -0,0 +1,3 @@ +Re-initializing an :class:`xml.etree.ElementTree.Element` in the C accelerator +now discards the previous children and attributes instead of leaking them, +matching the pure-Python implementation. Patch by tonghuaroot. diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index f827274eeffba83..fdd31925604bb29 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -449,6 +449,9 @@ element_init(PyObject *self, PyObject *args, PyObject *kwds) self_elem = (ElementObject *)self; + /* On re-init, drop any children and attrib from a previous init. */ + clear_extra(self_elem); + if (attrib != NULL && !is_empty_dict(attrib)) { if (create_extra(self_elem, attrib) < 0) { Py_DECREF(attrib); From dc9a6ba84169c2104abb286a48409ccd608fb696 Mon Sep 17 00:00:00 2001 From: tonghuaroot Date: Wed, 18 Nov 2026 23:21:41 +0800 Subject: [PATCH 2/2] gh-114795: Make Element re-initialization atomic Build the replacement state before releasing the previous state. This keeps destructor callbacks from leaving a partially updated element when they reenter Element.__init__. --- Lib/test/test_xml_etree.py | 18 +++++- ...07-11-11-36-15.gh-issue-114795.2KySPQ.rst} | 0 Modules/_elementtree.c | 58 +++++++++++++------ 3 files changed, 56 insertions(+), 20 deletions(-) rename Misc/NEWS.d/next/Library/{2026-07-11-11-36-15.gh-issue-153537.2KySPQ.rst => 2026-07-11-11-36-15.gh-issue-114795.2KySPQ.rst} (100%) diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 78ccf4581f7c812..e75b573b280f319 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -2655,7 +2655,7 @@ def f(): class BasicElementTest(ElementTestCase, unittest.TestCase): def test_reinit_releases_children(self): - # gh-153537: re-init should release the previous children. + # gh-114795: re-init should release the previous children. e = ET.Element('a', {'k': 'v'}) children = [ET.SubElement(e, 'b') for _ in range(3)] refs = [weakref.ref(c) for c in children] @@ -2667,6 +2667,22 @@ def test_reinit_releases_children(self): self.assertEqual(e.attrib, {}) self.assertTrue(all(ref() is None for ref in refs)) + def test_reinit_reentrant(self): + e = ET.Element('outer') + + class Child(ET.Element): + def __del__(self): + e.__init__('inner', {'k': 'v'}) + e.text = 'text' + e.tail = 'tail' + + e.append(Child('child')) + e.__init__('outer') + self.assertEqual(e.tag, 'inner') + self.assertEqual(e.attrib, {'k': 'v'}) + self.assertEqual(e.text, 'text') + self.assertEqual(e.tail, 'tail') + def test___init__(self): tag = "foo" attrib = { "zix": "wyp" } diff --git a/Misc/NEWS.d/next/Library/2026-07-11-11-36-15.gh-issue-153537.2KySPQ.rst b/Misc/NEWS.d/next/Library/2026-07-11-11-36-15.gh-issue-114795.2KySPQ.rst similarity index 100% rename from Misc/NEWS.d/next/Library/2026-07-11-11-36-15.gh-issue-153537.2KySPQ.rst rename to Misc/NEWS.d/next/Library/2026-07-11-11-36-15.gh-issue-114795.2KySPQ.rst diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index fdd31925604bb29..10ccb9712719ef9 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -268,20 +268,31 @@ typedef struct { /* -------------------------------------------------------------------- */ /* Element constructors and destructor */ -LOCAL(int) -create_extra(ElementObject* self, PyObject* attrib) +LOCAL(ElementObjectExtra *) +allocate_extra(PyObject *attrib) { - self->extra = PyMem_Malloc(sizeof(ElementObjectExtra)); - if (!self->extra) { + ElementObjectExtra *extra = PyMem_Malloc(sizeof(ElementObjectExtra)); + if (extra == NULL) { PyErr_NoMemory(); - return -1; + return NULL; } - self->extra->attrib = Py_XNewRef(attrib); + extra->attrib = Py_XNewRef(attrib); - self->extra->length = 0; - self->extra->allocated = STATIC_CHILDREN; - self->extra->children = self->extra->_children; + extra->length = 0; + extra->allocated = STATIC_CHILDREN; + extra->children = extra->_children; + + return extra; +} + +LOCAL(int) +create_extra(ElementObject* self, PyObject* attrib) +{ + self->extra = allocate_extra(attrib); + if (self->extra == NULL) { + return -1; + } return 0; } @@ -418,7 +429,9 @@ element_init(PyObject *self, PyObject *args, PyObject *kwds) { PyObject *tag; PyObject *attrib = NULL; + PyObject *oldtag, *oldtext, *oldtail; ElementObject *self_elem; + ElementObjectExtra *newextra = NULL, *oldextra; if (!PyArg_ParseTuple(args, "O|O:Element", &tag, &attrib)) return -1; @@ -449,24 +462,31 @@ element_init(PyObject *self, PyObject *args, PyObject *kwds) self_elem = (ElementObject *)self; - /* On re-init, drop any children and attrib from a previous init. */ - clear_extra(self_elem); - if (attrib != NULL && !is_empty_dict(attrib)) { - if (create_extra(self_elem, attrib) < 0) { + newextra = allocate_extra(attrib); + if (newextra == NULL) { Py_DECREF(attrib); return -1; } } - /* We own a reference to attrib here and it's no longer needed. */ - Py_XDECREF(attrib); + oldtag = self_elem->tag; + oldtext = JOIN_OBJ(self_elem->text); + oldtail = JOIN_OBJ(self_elem->tail); + oldextra = self_elem->extra; - /* Replace the objects already pointed to by tag, text and tail. */ - Py_XSETREF(self_elem->tag, Py_NewRef(tag)); + /* Replace the entire state before dropping old references, since a + destructor can call element_init() again. */ + self_elem->tag = Py_NewRef(tag); + self_elem->text = Py_NewRef(Py_None); + self_elem->tail = Py_NewRef(Py_None); + self_elem->extra = newextra; - _set_joined_ptr(&self_elem->text, Py_NewRef(Py_None)); - _set_joined_ptr(&self_elem->tail, Py_NewRef(Py_None)); + Py_XDECREF(attrib); + Py_XDECREF(oldtag); + Py_XDECREF(oldtext); + Py_XDECREF(oldtail); + dealloc_extra(oldextra); return 0; }