Commit 72a7b23a authored by Eli Bendersky's avatar Eli Bendersky

Fix the tests of GC collection in ET.Element according to Benjamin's recommendations

parent faf52edb
...@@ -14,14 +14,14 @@ ...@@ -14,14 +14,14 @@
# Don't re-import "xml.etree.ElementTree" module in the docstring, # Don't re-import "xml.etree.ElementTree" module in the docstring,
# except if the test is specific to the Python implementation. # except if the test is specific to the Python implementation.
import gc
import html import html
import io import io
import sys import sys
import unittest import unittest
import weakref
from test import support from test import support
from test.support import findfile, import_fresh_module from test.support import findfile, import_fresh_module, gc_collect
pyET = import_fresh_module('xml.etree.ElementTree', blocked=['_elementtree']) pyET = import_fresh_module('xml.etree.ElementTree', blocked=['_elementtree'])
...@@ -1848,28 +1848,26 @@ class BasicElementTest(unittest.TestCase): ...@@ -1848,28 +1848,26 @@ class BasicElementTest(unittest.TestCase):
self.assertRaises(TypeError, e.insert, 0, 'foo') self.assertRaises(TypeError, e.insert, 0, 'foo')
def test_cyclic_gc(self): def test_cyclic_gc(self):
class ShowGC: class Dummy:
def __init__(self, flaglist): pass
self.flaglist = flaglist
def __del__(self): # Test the shortest cycle: d->element->d
self.flaglist.append(1) d = Dummy()
d.dummyref = ET.Element('joe', attr=d)
# Test the shortest cycle: lst->element->lst wref = weakref.ref(d)
fl = [] del d
lst = [ShowGC(fl)] gc_collect()
lst.append(ET.Element('joe', attr=lst)) self.assertIsNone(wref())
del lst
gc.collect() # A longer cycle: d->e->e2->d
self.assertEqual(fl, [1])
# A longer cycle: lst->e->e2->lst
fl = []
e = ET.Element('joe') e = ET.Element('joe')
lst = [ShowGC(fl), e] d = Dummy()
e2 = ET.SubElement(e, 'foo', attr=lst) d.dummyref = e
del lst, e, e2 wref = weakref.ref(d)
gc.collect() e2 = ET.SubElement(e, 'foo', attr=d)
self.assertEqual(fl, [1]) del d, e, e2
gc_collect()
self.assertIsNone(wref())
class ElementTreeTest(unittest.TestCase): class ElementTreeTest(unittest.TestCase):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment