def setUpClass(cls):
     #Jython-specific block:
     try:
         cls.savedJythonGCFlags = gc.getJythonGCFlags()
         gc.addJythonGCFlags(gc.PRESERVE_WEAKREFS_ON_RESURRECTION)
     except Exception:
         pass
Beispiel #2
0
 def setUpClass(cls):
     #Jython-specific block:
     try:
         cls.savedJythonGCFlags = gc.getJythonGCFlags()
         gc.addJythonGCFlags(gc.PRESERVE_WEAKREFS_ON_RESURRECTION)
     except Exception:
         pass
Beispiel #3
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)
Beispiel #4
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)
Beispiel #5
0
 def setUpClass(cls):
     #Jython-specific block:
     try:
         cls.savedJythonGCFlags = gc.getJythonGCFlags()
         gc.addJythonGCFlags(gc.SUPPRESS_TRAVERSE_BY_REFLECTION_WARNING)
         gc.setMonitorGlobal(True)
     except Exception:
         pass
Beispiel #6
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)
Beispiel #7
0
 def setUpClass(cls):
     #Jython-specific block:
     try:
         cls.savedJythonGCFlags = gc.getJythonGCFlags()
         #the finalizer-related tests need this flag to pass in Jython:
         gc.addJythonGCFlags(gc.DONT_FINALIZE_CYCLIC_GARBAGE)
         gc.stopMonitoring()
     except Exception:
         pass
 def setUpClass(cls):
     #Jython-specific block:
     try:
         cls.savedJythonGCFlags = gc.getJythonGCFlags()
         #the finalizer-related tests need this flag to pass in Jython:
         gc.addJythonGCFlags(gc.DONT_FINALIZE_CYCLIC_GARBAGE)
         gc.stopMonitoring()
     except Exception:
         pass
Beispiel #9
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)
Beispiel #10
0
    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_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
Beispiel #12
0
 def setUpClass(cls):
     #Jython-specific block:
     try:
         cls.savedJythonGCFlags = gc.getJythonGCFlags()
         #the finalizer-related tests need this flag to pass in Jython:
         gc.addJythonGCFlags(gc.DONT_FINALIZE_RESURRECTED_OBJECTS)
         gc.addJythonGCFlags(gc.FORCE_DELAYED_FINALIZATION)
         gc.stopMonitoring()
     except Exception:
         pass
Beispiel #13
0
 def setUpClass(cls):
     #Jython-specific block:
     try:
         cls.savedJythonGCFlags = gc.getJythonGCFlags()
         #the finalizer-related tests need this flag to pass in Jython:
         gc.addJythonGCFlags(gc.DONT_FINALIZE_RESURRECTED_OBJECTS)
         gc.addJythonGCFlags(gc.FORCE_DELAYED_FINALIZATION)
         gc.stopMonitoring()
     except Exception:
         pass
Beispiel #14
0
 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
Beispiel #15
0
 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_weakref_after_resurrection_threadsafe(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)

        a = Test_Finalizable("a")
        wa = weakref.ref(a)
        c = Test_Resurrection("c")
        c.toResurrect = a
        wc = weakref.ref(c)
        del a
        del c
        try:
            gc.addJythonGCFlags(gc.PRESERVE_WEAKREFS_ON_RESURRECTION)
            System.gc()
            # We intentionally don't wait here, but want to observe
            # the situation with gc unfinnished. Note that wa() preserves
            # its result right away, due to thread-safe implementation.
            # Technically, the weak reference breaks and is restored after
            # gc-run finishes. However wa() blocks until the referent is
            # restored or the deletion is confirmed.
        except Exception:
            pass
        self.assertEqual(comments, [])
        self.assertEqual(resurrect, [])
        while comments == [] or resurrect == []:
            self.assertEqual(str(wa()), '<a>')
            self.assertEqual(wc(), None)
        self.assertEqual(str(wa()), '<a>')
        self.assertEqual(wc(), None)
Beispiel #17
0
    def test_weakref_after_resurrection_threadsafe(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)

        a = Test_Finalizable("a")
        wa = weakref.ref(a)
        c = Test_Resurrection("c")
        c.toResurrect = a
        wc = weakref.ref(c)
        del a
        del c
        try:
            gc.addJythonGCFlags(gc.PRESERVE_WEAKREFS_ON_RESURRECTION)
            System.gc()
            # We intentionally don't wait here, but want to observe
            # the situation with gc unfinnished. Note that wa() preserves
            # its result right away, due to thread-safe implementation.
            # Technically, the weak reference breaks and is restored after
            # gc-run finishes. However wa() blocks until the referent is
            # restored or the deletion is confirmed.
        except Exception:
            pass
        self.assertEqual(comments, [])
        self.assertEqual(resurrect, [])
        while comments == [] or resurrect == []:
            self.assertEqual(str(wa()), '<a>')
            self.assertEqual(wc(), None)
        self.assertEqual(str(wa()), '<a>')
        self.assertEqual(wc(), None)