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
15 changes: 15 additions & 0 deletions Lib/test/test_xml_etree_c.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# xml.etree test for cElementTree
import io
import struct
import sys
from test import support
from test.support.import_helper import import_fresh_module
import types
Expand Down Expand Up @@ -171,6 +172,20 @@ def test_xmlpullparser_leaks(self):
del parser
support.gc_collect()

@support.refcount_test
def test_xmlparser_refleaks_in___init__(self):
gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount')
parser = cET.XMLParser()
lastrc = gettotalrefcount()
for i in range(20):
support.gc_collect()
rc = gettotalrefcount()
for _ in range(100):
parser.__init__()
delta = rc - lastrc
lastrc = rc
self.assertLess(delta, 3)

def test_dict_disappearing_during_get_item(self):
# test fix for seg fault reported in issue 27946
class X:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix reference leak in :class:`xml.etree.ElementTree.XMLParser` when
:meth:`~object.__init__` is called many times. Patch by Maurycy
Pawłowski-Wieroński.
6 changes: 6 additions & 0 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -3699,6 +3699,8 @@ ignore_attribute_error(PyObject *value)
return 0;
}

static int xmlparser_gc_clear(PyObject *op);

/*[clinic input]
_elementtree.XMLParser.__init__

Expand All @@ -3713,6 +3715,10 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *target,
const char *encoding)
/*[clinic end generated code: output=3ae45ec6cdf344e4 input=7e716dd6e4f3e439]*/
{
if (self->parser != NULL) {
(void)xmlparser_gc_clear((PyObject *)self);
Comment on lines +3718 to +3719
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.

I would prefer rejecting this rather than fixing it. If there is no crash issues, I would rather not fix it at all.

}

self->entity = PyDict_New();
if (!self->entity)
return -1;
Expand Down
Loading