[pypy-commit] stmgc weakref: A weakref-kept-alive test

arigo noreply at buildbot.pypy.org
Mon Jul 15 16:32:06 CEST 2013


Author: Armin Rigo <arigo at tunes.org>
Branch: weakref
Changeset: r397:e2f68af5cc4e
Date: 2013-07-15 16:31 +0200
http://bitbucket.org/pypy/stmgc/changeset/e2f68af5cc4e/

Log:	A weakref-kept-alive test

diff --git a/c4/nursery.c b/c4/nursery.c
--- a/c4/nursery.c
+++ b/c4/nursery.c
@@ -448,7 +448,13 @@
 
         weakref = (gcptr)weakref->h_revision;
         size_t size = stmgc_size(weakref);
-        WEAKREF_PTR(weakref, size) = NULL;   /* XXX */
+        gcptr obj = WEAKREF_PTR(weakref, size);
+
+        if (obj->h_tid & GCFLAG_NURSERY_MOVED)
+            obj = obj->h_revision;
+        else
+            obj = NULL;
+        WEAKREF_PTR(weakref, size) = obj;
     }
 }
 
diff --git a/c4/test/test_nursery.py b/c4/test/test_nursery.py
--- a/c4/test/test_nursery.py
+++ b/c4/test/test_nursery.py
@@ -330,3 +330,21 @@
     minor_collect()
     p1 = lib.stm_pop_root()
     assert lib.rawgetptr(p1, 0) == ffi.NULL
+
+def test_weakref_itself_dies():
+    p2 = nalloc(HDR)
+    p1 = lib.stm_weakref_allocate(WEAKREF_SIZE, WEAKREF_TID, p2)
+    minor_collect()
+
+def test_weakref_keep():
+    p2 = nalloc(HDR)
+    p1 = lib.stm_weakref_allocate(WEAKREF_SIZE, WEAKREF_TID, p2)
+    assert p1.h_tid == WEAKREF_TID   # no GC flags
+    assert p1.h_revision == lib.get_private_rev_num()
+    assert lib.rawgetptr(p1, 0) == p2
+    lib.stm_push_root(p1)
+    lib.stm_push_root(p2)
+    minor_collect()
+    p2 = lib.stm_pop_root()
+    p1 = lib.stm_pop_root()
+    assert lib.rawgetptr(p1, 0) == p2


More information about the pypy-commit mailing list