[Python-Dev] [Python-checkins] cpython: Issue #14065: Added cyclic GC support to ET.Element
Eli Bendersky
eliben at gmail.com
Sat Mar 31 06:33:27 CEST 2012
On Fri, Mar 30, 2012 at 21:30, Benjamin Peterson <benjamin at python.org>wrote:
> > + def test_cyclic_gc(self):
> > + class ShowGC:
> > + def __init__(self, flaglist):
> > + self.flaglist = flaglist
> > + def __del__(self):
> > + self.flaglist.append(1)
>
>
> I think a nicer way to check for cyclic collection is to take a
> weakref to an object, call the GC, then check to make sure the weakref
> is broken.
>
> > +
> > + # Test the shortest cycle: lst->element->lst
> > + fl = []
> > + lst = [ShowGC(fl)]
> > + lst.append(ET.Element('joe', attr=lst))
> > + del lst
> > + gc.collect()
>
> support.gc_collect() is preferable
>
> > + self.assertEqual(fl, [1])
> > +
> > + # A longer cycle: lst->e->e2->lst
> > + fl = []
> > + e = ET.Element('joe')
> > + lst = [ShowGC(fl), e]
> > + e2 = ET.SubElement(e, 'foo', attr=lst)
> > + del lst, e, e2
> > + gc.collect()
> > + self.assertEqual(fl, [1])
>
>
Thanks for the insights, Benjamin. I'll explore these alternatives and will
submit a fix.
Eli
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20120331/e51ff62b/attachment.html>
More information about the Python-Dev
mailing list