]> granicus.if.org Git - python/commitdiff
Issue #28871: Fixed a crash when deallocate deep ElementTree.
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 21 Dec 2016 10:32:56 +0000 (12:32 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 21 Dec 2016 10:32:56 +0000 (12:32 +0200)
Lib/test/test_xml_etree_c.py
Misc/NEWS
Modules/_elementtree.c

index 96b446e32ead61327374ed04948425edd135c5c0..bfced1225e0faeccde684ea1f85725b6e5ad9506 100644 (file)
@@ -11,6 +11,7 @@ cET_alias = import_fresh_module('xml.etree.cElementTree',
                                 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 @@ class MiscTests(unittest.TestCase):
             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):
index 1286d7b3511a3d3b909065b0142b2a06a43a4540..74266cd1255215a7d26e01dff5af893bfbc52cf0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -138,6 +138,8 @@ Core and Builtins
 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.
index 1cdddaf0fbed000ef158ece66d44a9a7b16506fb..7d50dd07a961f2e9d85bd1d09647094f8b5876cb 100644 (file)
@@ -652,6 +652,7 @@ static void
 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 @@ element_dealloc(ElementObject* self)
 
     RELEASE(sizeof(ElementObject), "destroy element");
     Py_TYPE(self)->tp_free((PyObject *)self);
+    Py_TRASHCAN_SAFE_END(self)
 }
 
 /* -------------------------------------------------------------------- */