(original) (raw)
On Fri, Mar 30, 2012 at 21:30, Benjamin Peterson <benjamin@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.
support.gc\_collect() is preferable
> +
> + � � � �# Test the shortest cycle: lst->element->lst
> + � � � �fl = \[\]
> + � � � �lst = \[ShowGC(fl)\]
> + � � � �lst.append(ET.Element('joe', attr=lst))
> + � � � �del lst
> + � � � �gc.collect()
> + � � � �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