changeset: 105764:957091874ea0 branch: 3.5 parent: 105759:2f004cc84153 user: Serhiy Storchaka date: Wed Dec 21 12:32:56 2016 +0200 files: Lib/test/test_xml_etree_c.py Misc/NEWS Modules/_elementtree.c description: Issue #28871: Fixed a crash when deallocate deep ElementTree. diff -r 2f004cc84153 -r 957091874ea0 Lib/test/test_xml_etree_c.py --- a/Lib/test/test_xml_etree_c.py Mon Dec 19 23:54:25 2016 -0800 +++ b/Lib/test/test_xml_etree_c.py Wed Dec 21 12:32:56 2016 +0200 @@ -11,6 +11,7 @@ fresh=['_elementtree', 'xml.etree']) +@unittest.skipUnless(cET, 'requires _elementtree') class MiscTests(unittest.TestCase): # Issue #8651. @support.bigmemtest(size=support._2G + 100, memuse=1, dry_run=False) @@ -54,6 +55,15 @@ del element.attrib self.assertEqual(element.attrib, {'A': 'B', 'C': 'D'}) + def test_trashcan(self): + # If this test fails, it will most likely die via segfault. + e = root = cET.Element('root') + for i in range(200000): + e = cET.SubElement(e, 'x') + del e + del root + support.gc_collect() + @unittest.skipUnless(cET, 'requires _elementtree') class TestAliasWorking(unittest.TestCase): diff -r 2f004cc84153 -r 957091874ea0 Misc/NEWS --- a/Misc/NEWS Mon Dec 19 23:54:25 2016 -0800 +++ b/Misc/NEWS Wed Dec 21 12:32:56 2016 +0200 @@ -138,6 +138,8 @@ Library ------- +- Issue #28871: Fixed a crash when deallocate deep ElementTree. + - Issue #19542: Fix bugs in WeakValueDictionary.setdefault() and WeakValueDictionary.pop() when a GC collection happens in another thread. diff -r 2f004cc84153 -r 957091874ea0 Modules/_elementtree.c --- a/Modules/_elementtree.c Mon Dec 19 23:54:25 2016 -0800 +++ b/Modules/_elementtree.c Wed Dec 21 12:32:56 2016 +0200 @@ -652,6 +652,7 @@ element_dealloc(ElementObject* self) { PyObject_GC_UnTrack(self); + Py_TRASHCAN_SAFE_BEGIN(self) if (self->weakreflist != NULL) PyObject_ClearWeakRefs((PyObject *) self); @@ -662,6 +663,7 @@ RELEASE(sizeof(ElementObject), "destroy element"); Py_TYPE(self)->tp_free((PyObject *)self); + Py_TRASHCAN_SAFE_END(self) } /* -------------------------------------------------------------------- */