[pygobject] tests: add tests for GObject.Object.weak_ref



commit e6e052779a928a657b56c64526881e63feab6e32
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Thu Feb 14 12:35:11 2019 +0100

    tests: add tests for GObject.Object.weak_ref

 tests/test_gobject.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
---
diff --git a/tests/test_gobject.py b/tests/test_gobject.py
index 7816510c..bba12cb7 100644
--- a/tests/test_gobject.py
+++ b/tests/test_gobject.py
@@ -6,6 +6,8 @@ import sys
 import gc
 import unittest
 import warnings
+import weakref
+import platform
 
 import pytest
 
@@ -18,6 +20,53 @@ import testhelper
 from .helper import capture_glib_deprecation_warnings
 
 
+@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="crashes")
+def test_gobject_weak_ref():
+
+    called = []
+
+    def callback(*args):
+        called.extend(args)
+
+    # object gets finalized
+    obj = GObject.Object()
+    obj.weak_ref(callback, 1)
+    del obj
+    gc.collect()
+    gc.collect()
+    assert called == [1]
+    del called[:]
+
+    # wrapper gets finalized first
+    obj = GObject.Object()
+    pyref = weakref.ref(obj, lambda x: callback(-2))
+    value = GObject.Value(GObject.Object, obj)
+    ref = obj.weak_ref(callback, 2)
+    del obj
+    gc.collect()
+    assert called == [-2]
+    del pyref
+    value.unset()
+    gc.collect()
+    assert called == [-2, 2]
+    del called[:]
+
+    # weakref gets unregistered first
+    obj = GObject.Object()
+    ref = obj.weak_ref(callback, 3)
+    ref.unref()
+    del obj
+    gc.collect()
+    assert not called
+
+    # weakref gets GCed
+    obj = GObject.Object()
+    obj.weak_ref(callback, 4)
+    gc.collect()
+    del obj
+    assert called == [4]
+
+
 class TestGObjectAPI(unittest.TestCase):
 
     def test_call_method_uninitialized_instance(self):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]