diff options
-rw-r--r-- | Lib/test/test_xml_etree_c.py | 21 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2018-12-14-23-56-48.bpo-35502.gLHuFS.rst | 3 | ||||
-rw-r--r-- | Modules/_elementtree.c | 5 |
3 files changed, 29 insertions, 0 deletions
diff --git a/Lib/test/test_xml_etree_c.py b/Lib/test/test_xml_etree_c.py index e87de60..2144d20 100644 --- a/Lib/test/test_xml_etree_c.py +++ b/Lib/test/test_xml_etree_c.py @@ -1,4 +1,5 @@ # xml.etree test for cElementTree +import io import struct from test import support from test.support import import_fresh_module @@ -133,6 +134,26 @@ class MiscTests(unittest.TestCase): self.assertEqual(len(elem), 1) self.assertEqual(elem[0].tag, 'child') + def test_iterparse_leaks(self): + # Test reference leaks in TreeBuilder (issue #35502). + # The test is written to be executed in the hunting reference leaks + # mode. + XML = '<a></a></b>' + parser = cET.iterparse(io.StringIO(XML)) + next(parser) + del parser + support.gc_collect() + + def test_xmlpullparser_leaks(self): + # Test reference leaks in TreeBuilder (issue #35502). + # The test is written to be executed in the hunting reference leaks + # mode. + XML = '<a></a></b>' + parser = cET.XMLPullParser() + parser.feed(XML) + del parser + support.gc_collect() + @unittest.skipUnless(cET, 'requires _elementtree') class TestAliasWorking(unittest.TestCase): diff --git a/Misc/NEWS.d/next/Library/2018-12-14-23-56-48.bpo-35502.gLHuFS.rst b/Misc/NEWS.d/next/Library/2018-12-14-23-56-48.bpo-35502.gLHuFS.rst new file mode 100644 index 0000000..0fcea8d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-12-14-23-56-48.bpo-35502.gLHuFS.rst @@ -0,0 +1,3 @@ +Fixed reference leaks in :class:`xml.etree.ElementTree.TreeBuilder` in case +of unfinished building of the tree (in particular when an error was raised +during parsing XML). diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 12e418d..b1a9629 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -2447,6 +2447,11 @@ _elementtree_TreeBuilder___init___impl(TreeBuilderObject *self, static int treebuilder_gc_traverse(TreeBuilderObject *self, visitproc visit, void *arg) { + Py_VISIT(self->end_ns_event_obj); + Py_VISIT(self->start_ns_event_obj); + Py_VISIT(self->end_event_obj); + Py_VISIT(self->start_event_obj); + Py_VISIT(self->events_append); Py_VISIT(self->root); Py_VISIT(self->this); Py_VISIT(self->last); |