예제 #1
0
    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)
예제 #2
0
    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)
예제 #3
0
    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)
예제 #4
0
    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)