Skip to content
Closed
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
18 changes: 18 additions & 0 deletions Lib/test/test_xml_etree_c.py
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
# xml.etree test for cElementTree
import gc
import io
import struct
from test import support
Expand DownExpand Up@@ -203,6 +204,23 @@ def test_disallow_instantiation(self):
iter_type = type(root.iter())
support.check_disallow_instantiation(self, iter_type)

def test_tree_builder_stack(self):
builder = cET.TreeBuilder()
for x in range(10):
builder.start("a", {})
for x in range(10):
builder.end("a")
root = builder.close()

# Find the internal TreeBuilder stack list using gc.get_referrers()
for ref in gc.get_referrers(root[0]):
if isinstance(ref, list):
# Check that the list doesn't contain NULL items (gh-146056)
repr(ref)
break
else:
self.fail("failed to find TreeBuilder stack list")


@unittest.skipUnless(cET, 'requires _elementtree')
class TestAliasWorking(unittest.TestCase):
Expand Down
6 changes: 3 additions & 3 deletions Modules/_elementtree.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -2431,7 +2431,7 @@ treebuilder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
t->element_factory = NULL;
t->comment_factory = NULL;
t->pi_factory = NULL;
t->stack = PyList_New(20);
t->stack = PyList_New(0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can have performance impact.

if (!t->stack) {
Py_DECREF(t->this);
Py_DECREF(t->last);
Expand DownExpand Up@@ -2856,9 +2856,9 @@ treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)

item = self->last;
self->last = Py_NewRef(self->this);
Py_XSETREF(self->last_for_tail, self->last);
Py_XSETREF(self->last_for_tail, Py_NewRef(self->last));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated.

If rewrite this code to be more explicit or safe, I would write something like

PyObject*last=self->last;
PyObject*last_for_tail=self->last_for_tail;
PyObject*this=self->this;
self->index--;
self->this=Py_NewRef(PyList_GET_ITEM(self->stack, self->index));
self->last=Py_NewRef(this);
self->last_for_tail=Py_NewRef(this);
Py_DECREF(last);
Py_XDECREF(last_for_tail);
if (treebuilder_append_event(self, self->end_event_obj, this) <0) {
Py_DECREF(this);
returnNULL;
}
returnthis;

But we should also look a the other ends -- how these attributes are set in other code in this file. This is a separate issue.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote #146167 which uses your suggestion.

self->index--;
self->this = Py_NewRef(PyList_GET_ITEM(self->stack, self->index));
Py_SETREF(self->this, Py_NewRef(PyList_GET_ITEM(self->stack, self->index)));
Py_DECREF(item);

if (treebuilder_append_event(self, self->end_event_obj, self->last) < 0)
Expand Down
Loading