<div dir="ltr"><div class="gmail_quote">On Fri, Mar 30, 2012 at 21:30, Benjamin Peterson <span dir="ltr"><<a href="mailto:benjamin@python.org">benjamin@python.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div class="h5">
> + def test_cyclic_gc(self):<br>
> + class ShowGC:<br>
> + def __init__(self, flaglist):<br>
> + self.flaglist = flaglist<br>
> + def __del__(self):<br>
> + self.flaglist.append(1)<br>
<br>
</div></div><br>I think a nicer way to check for cyclic collection is to take a<br>
weakref to an object, call the GC, then check to make sure the weakref<br>
is broken.<br>
<div class="im"><br>
> +<br>
> + # Test the shortest cycle: lst->element->lst<br>
> + fl = []<br>
> + lst = [ShowGC(fl)]<br>
> + lst.append(ET.Element('joe', attr=lst))<br>
> + del lst<br>
> + gc.collect()<br>
<br>
</div>support.gc_collect() is preferable<br>
<div class="HOEnZb"><div class="h5"><br>
> + self.assertEqual(fl, [1])<br>
> +<br>
> + # A longer cycle: lst->e->e2->lst<br>
> + fl = []<br>
> + e = ET.Element('joe')<br>
> + lst = [ShowGC(fl), e]<br>
> + e2 = ET.SubElement(e, 'foo', attr=lst)<br>
> + del lst, e, e2<br>
> + gc.collect()<br>
> + self.assertEqual(fl, [1])<br>
<br></div></div></blockquote></div><br>Thanks for the insights, Benjamin. I'll explore these alternatives and will submit a fix.<br><br>Eli<br><br><br><br><br></div>