def test_queue(self): class X(object): def __init__(self, q): self.q = q x = X(Queue()) gc.monitorObject(x) gc.collect()
def test_weakref_after_resurrection_and_delayed_finalize(self): resurrect = [] comments = [] class Test_Finalizable(object): def __init__(self, name): self.name = name def __repr__(self): return "<"+self.name+">" def __del__(self): comments.append("del "+self.name) class Test_Resurrection(object): def __init__(self, name): self.name = name def __repr__(self): return "<"+self.name+">" def __del__(self): comments.append("del "+self.name) if hasattr(self, "toResurrect"): resurrect.append(self) def clb(ref): comments.append("clb") def clb2(ref): comments.append("clb2 "+str(comments)) a = Test_Finalizable("a") wa = weakref.ref(a, clb) self.assertEqual(wa(), a) c = Test_Resurrection("c") c.toResurrect = a wc = weakref.ref(c, clb2) try: gc.monitorObject(c) gc.addJythonGCFlags(gc.DONT_FINALIZE_RESURRECTED_OBJECTS) except Exception: pass del a del c gc.collect() self.assertIn('del c', comments) self.assertNotIn('del a', comments) self.assertIn('clb2 []', comments) self.assertNotIn("clb", comments) self.assertEqual(str(resurrect), "[<c>]") self.assertEqual(str(wa()), "<a>") self.assertEqual(wc(), None) try: gc.removeJythonGCFlags(gc.DONT_FINALIZE_RESURRECTED_OBJECTS) except Exception: pass
def setUpClass(cls): #Jython-specific block: try: cls.savedJythonGCFlags = gc.getJythonGCFlags() gc.setMonitorGlobal(True) #since gc module already exists, it would not be caught by monitorGlobal. #so we have to monitor it manually: gc.monitorObject(gc) #the finalizer-related tests need this flag to pass in Jython: gc.addJythonGCFlags(gc.DONT_FINALIZE_CYCLIC_GARBAGE) except Exception: pass
def test_manual_monitoring(self): # since tuples are immutable we close the loop with a list l = [] t = (l, ) l.append(t) gc.monitorObject(l) #gc.monitorObject(t) <- intentionally only monitor one of them gc.collect() del t del l # Note that usually two collected objects would be expected - l and t. # But we intentionally only monitored one of them, so only one should # be counted. self.assertEqual(gc.collect(), 1)
def test_manual_monitoring(self): # since tuples are immutable we close the loop with a list l = [] t = (l,) l.append(t) gc.monitorObject(l) #gc.monitorObject(t) <- intentionally only monitor one of them gc.collect() del t del l #Note that usually two collected objects would be expected - l and t. #But we intentionally only monitored one of them, so only one should #be counted. self.assertEqual(gc.collect(), 1)
def test_weakref_after_resurrection(self): resurrect = [] comments = [] class Test_Finalizable(object): def __init__(self, name): self.name = name def __repr__(self): return "<" + self.name + ">" def __del__(self): comments.append("del " + self.name) class Test_Resurrection(object): def __init__(self, name): self.name = name def __repr__(self): return "<" + self.name + ">" def __del__(self): comments.append("del " + self.name) if hasattr(self, "toResurrect"): resurrect.append(self) def clb(ref): comments.append("clb") def clb2(ref): comments.append("clb2 " + str(comments)) a = Test_Finalizable("a") wa = weakref.ref(a, clb) self.assertEqual(wa(), a) c = Test_Resurrection("c") c.toResurrect = a wc = weakref.ref(c, clb2) try: gc.monitorObject(c) except Exception: pass del a del c gc.collect() self.assertIn('clb2 []', comments) self.assertNotIn("clb", comments) self.assertEqual(str(resurrect), "[<c>]") self.assertEqual(str(wa()), "<a>") self.assertEqual(wc(), None)