[pypy-svn] r49986 - pypy/dist/pypy/translator/llvm/test

arigo at codespeak.net arigo at codespeak.net
Fri Dec 21 16:33:39 CET 2007


Author: arigo
Date: Fri Dec 21 16:33:39 2007
New Revision: 49986

Modified:
   pypy/dist/pypy/translator/llvm/test/test_new_gc.py
Log:
test_weakref passes out of the box.


Modified: pypy/dist/pypy/translator/llvm/test/test_new_gc.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/test/test_new_gc.py	(original)
+++ pypy/dist/pypy/translator/llvm/test/test_new_gc.py	Fri Dec 21 16:33:39 2007
@@ -41,3 +41,40 @@
 
     mod, f = compile_test(fn, [int], gcpolicy="semispace")
     assert f(5000) == fn(5000)
+
+def test_weakref():
+    import weakref
+    from pypy.rlib import rgc
+
+    class A:
+        pass
+
+    keepalive = []
+    def fn():
+        n = 7000
+        weakrefs = []
+        a = None
+        for i in range(n):
+            if i & 1 == 0:
+                a = A()
+                a.index = i
+            assert a is not None
+            weakrefs.append(weakref.ref(a))
+            if i % 7 == 6:
+                keepalive.append(a)
+        rgc.collect()
+        count_free = 0
+        for i in range(n):
+            a = weakrefs[i]()
+            if i % 7 == 6:
+                assert a is not None
+            if a is not None:
+                assert a.index == i & ~1
+            else:
+                count_free += 1
+        return count_free
+
+    mod, f = compile_test(fn, [], gcpolicy="semispace")
+    res = f()
+    # more than half of them should have been freed, ideally up to 6000
+    assert 3500 <= res <= 6000



More information about the Pypy-commit mailing list