Skip to content
Open
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2654,6 +2654,35 @@ def f():

class BasicElementTest(ElementTestCase, unittest.TestCase):

def test_reinit_releases_children(self):
# 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]
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_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" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Re-initializing an :class:`xml.etree.ElementTree.Element` in the C accelerator
Comment thread
tonghuaroot marked this conversation as resolved.
Resolved
now discards the previous children and attributes instead of leaking them,
matching the pure-Python implementation. Patch by tonghuaroot.
55 changes: 39 additions & 16 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

extra->length = 0;
extra->allocated = STATIC_CHILDREN;
extra->children = extra->_children;

return extra;
}

self->extra->length = 0;
self->extra->allocated = STATIC_CHILDREN;
self->extra->children = self->extra->_children;
LOCAL(int)
create_extra(ElementObject* self, PyObject* attrib)
{
self->extra = allocate_extra(attrib);
if (self->extra == NULL) {
return -1;
}

return 0;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -450,20 +463,30 @@ element_init(PyObject *self, PyObject *args, PyObject *kwds)
self_elem = (ElementObject *)self;

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;
}
Expand Down
Loading