[Python-checkins] bpo-35502: Fix reference leaks in ElementTree.TreeBuilder. (GH-11170)

Miss Islington (bot) webhook-mailer at python.org
Tue Dec 18 16:40:29 EST 2018


https://github.com/python/cpython/commit/60c919b58bd3cf8730947a00ddc6a527d6922ff1
commit: 60c919b58bd3cf8730947a00ddc6a527d6922ff1
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-12-18T13:40:23-08:00
summary:

bpo-35502: Fix reference leaks in ElementTree.TreeBuilder. (GH-11170)

(cherry picked from commit d2a75c67830d7c9f59e4e9b60f36974234c829ef)

Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>

files:
A Misc/NEWS.d/next/Library/2018-12-14-23-56-48.bpo-35502.gLHuFS.rst
M Lib/test/test_xml_etree_c.py
M Modules/_elementtree.c

diff --git a/Lib/test/test_xml_etree_c.py b/Lib/test/test_xml_etree_c.py
index e87de6094441..2144d203e1e9 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 @@ def test_setstate_leaks(self):
         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 000000000000..0fcea8d5a41d
--- /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 ff3a240f2ec3..79f1ccd68565 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -2429,6 +2429,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);



More information about the Python-checkins mailing list