def test_raw_forced_delayed(self): comments = [] class Test_JavaAbortFinalizable(Object): def __init__(self, name, toAbort): self.name = name self.toAbort = toAbort def __repr__(self): return "<"+self.name+">" def finalize(self): gc.notifyPreFinalization() comments.append("del "+self.name) gc.abortDelayedFinalization(self.toAbort) # We manually restore weak references: gc.restoreWeakReferences(self.toAbort) gc.notifyPostFinalization() 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) def callback_a(obj): comments.append("callback_a") def callback_b(obj): comments.append("callback_b") a = Test_Finalizable("a") wa = weakref.ref(a, callback_a) b = Test_JavaAbortFinalizable("b", a) wb = weakref.ref(b, callback_b) gc.addJythonGCFlags(gc.FORCE_DELAYED_FINALIZATION) gc.addJythonGCFlags(gc.FORCE_DELAYED_WEAKREF_CALLBACKS) self.assertTrue(gc.delayedFinalizationEnabled()) self.assertTrue(gc.delayedWeakrefCallbacksEnabled()) self.assertEqual(len(comments), 0) del a del b System.gc() time.sleep(2) self.assertIsNotNone(wa()) self.assertIsNone(wb()) self.assertIn('del b', comments) self.assertNotIn('callback_a', comments) self.assertIn('callback_b', comments) self.assertNotIn('del a', comments) self.assertEqual(2, len(comments)) gc.removeJythonGCFlags(gc.FORCE_DELAYED_FINALIZATION) gc.removeJythonGCFlags(gc.FORCE_DELAYED_WEAKREF_CALLBACKS)
def test_raw_forced_delayed(self): comments = [] class Test_JavaAbortFinalizable(Object): def __init__(self, name, toAbort): self.name = name self.toAbort = toAbort def __repr__(self): return "<" + self.name + ">" def finalize(self): gc.notifyPreFinalization() comments.append("del " + self.name) gc.abortDelayedFinalization(self.toAbort) # We manually restore weak references: gc.restoreWeakReferences(self.toAbort) gc.notifyPostFinalization() 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) def callback_a(obj): comments.append("callback_a") def callback_b(obj): comments.append("callback_b") a = Test_Finalizable("a") wa = weakref.ref(a, callback_a) b = Test_JavaAbortFinalizable("b", a) wb = weakref.ref(b, callback_b) gc.addJythonGCFlags(gc.FORCE_DELAYED_FINALIZATION) gc.addJythonGCFlags(gc.FORCE_DELAYED_WEAKREF_CALLBACKS) self.assertTrue(gc.delayedFinalizationEnabled()) self.assertTrue(gc.delayedWeakrefCallbacksEnabled()) self.assertEqual(len(comments), 0) del a del b System.gc() time.sleep(2) self.assertIsNotNone(wa()) self.assertIsNone(wb()) self.assertIn('del b', comments) self.assertNotIn('callback_a', comments) self.assertIn('callback_b', comments) self.assertNotIn('del a', comments) self.assertEqual(2, len(comments)) gc.removeJythonGCFlags(gc.FORCE_DELAYED_FINALIZATION) gc.removeJythonGCFlags(gc.FORCE_DELAYED_WEAKREF_CALLBACKS)
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 test_raw_forced_delayedWeakrefCallback(self): comments = [] resurrected = [] class Test_JavaResurrectFinalizable(Object): def __init__(self, name, toResurrect): self.name = name self.toResurrect = toResurrect def __repr__(self): return "<" + self.name + ">" # Note that this type of finalizer is usually not recommended # as it gets lost in case of resurrection. def finalize(self): gc.notifyPreFinalization() comments.append("del " + self.name) resurrected.append(self.toResurrect) # We manually restore weak references: gc.restoreWeakReferences(self.toResurrect) gc.notifyPostFinalization() 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) def callback(obj): comments.append("callback") a = Test_Finalizable("a") b = Test_JavaResurrectFinalizable("b", a) wa = weakref.ref(a, callback) gc.removeJythonGCFlags(gc.FORCE_DELAYED_FINALIZATION) gc.addJythonGCFlags(gc.FORCE_DELAYED_WEAKREF_CALLBACKS) self.assertFalse(gc.delayedFinalizationEnabled()) self.assertTrue(gc.delayedWeakrefCallbacksEnabled()) self.assertEqual(len(comments), 0) aStr = str(a) del a del b System.gc() time.sleep(2) self.assertIn("del a", comments) self.assertIn("del b", comments) self.assertEqual(1, len(resurrected)) self.assertEqual(str(resurrected[0]), aStr) self.assertIsNotNone(wa()) self.assertEqual(resurrected[0], wa()) self.assertNotIn("callback", comments) self.assertEqual(2, len(comments)) gc.removeJythonGCFlags(gc.FORCE_DELAYED_WEAKREF_CALLBACKS)
def test_raw_forced_delayedWeakrefCallback(self): comments = [] resurrected = [] class Test_JavaResurrectFinalizable(Object): def __init__(self, name, toResurrect): self.name = name self.toResurrect = toResurrect def __repr__(self): return "<"+self.name+">" # Note that this type of finalizer is usually not recommended # as it gets lost in case of resurrection. def finalize(self): gc.notifyPreFinalization() comments.append("del "+self.name) resurrected.append(self.toResurrect) # We manually restore weak references: gc.restoreWeakReferences(self.toResurrect) gc.notifyPostFinalization() 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) def callback(obj): comments.append("callback") a = Test_Finalizable("a") b = Test_JavaResurrectFinalizable("b", a) wa = weakref.ref(a, callback) gc.removeJythonGCFlags(gc.FORCE_DELAYED_FINALIZATION) gc.addJythonGCFlags(gc.FORCE_DELAYED_WEAKREF_CALLBACKS) self.assertFalse(gc.delayedFinalizationEnabled()) self.assertTrue(gc.delayedWeakrefCallbacksEnabled()) self.assertEqual(len(comments), 0) aStr = str(a) del a del b System.gc() time.sleep(2) self.assertIn("del a", comments) self.assertIn("del b", comments) self.assertEqual(1, len(resurrected)) self.assertEqual(str(resurrected[0]), aStr) self.assertIsNotNone(wa()) self.assertEqual(resurrected[0], wa()) self.assertNotIn("callback", comments) self.assertEqual(2, len(comments)) gc.removeJythonGCFlags(gc.FORCE_DELAYED_WEAKREF_CALLBACKS)
def setUpClass(cls): #Jython-specific block: try: cls.savedJythonGCFlags = gc.getJythonGCFlags() gc.removeJythonGCFlags(gc.DONT_TRAVERSE_BY_REFLECTION) # i.e. enable ... gc.addJythonGCFlags(gc.SUPPRESS_TRAVERSE_BY_REFLECTION_WARNING) gc.setMonitorGlobal(True) except Exception: pass
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.removeJythonGCFlags( gc.DONT_TRAVERSE_BY_REFLECTION) # i.e. enable ... gc.addJythonGCFlags(gc.SUPPRESS_TRAVERSE_BY_REFLECTION_WARNING) gc.setMonitorGlobal(True) except Exception: pass