[pypy-svn] r27548 - in pypy/dist/pypy: rpython/memory/test translator/c/test

cfbolz at codespeak.net cfbolz at codespeak.net
Sun May 21 21:05:51 CEST 2006


Author: cfbolz
Date: Sun May 21 21:05:49 2006
New Revision: 27548

Modified:
   pypy/dist/pypy/rpython/memory/test/test_transformed_gc.py
   pypy/dist/pypy/translator/c/test/test_newgc.py
Log:
add weakref tests for framework. seems to work fine :-)


Modified: pypy/dist/pypy/rpython/memory/test/test_transformed_gc.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/test/test_transformed_gc.py	(original)
+++ pypy/dist/pypy/rpython/memory/test/test_transformed_gc.py	Sun May 21 21:05:49 2006
@@ -336,7 +336,6 @@
                 b.num_deleted += 1
         def f(x, y):
             a = A()
-            c = C()
             i = 0
             while i < x:
                 i += 1
@@ -346,7 +345,7 @@
             return b.num_deleted
         run = self.runner(f, nbargs=2)
         res = run([5, 42]) #XXX pure lazyness here too
-        assert res == 13
+        assert res == 12
 
     def test_cloning(self):
         B = lltype.GcStruct('B', ('x', lltype.Signed))

Modified: pypy/dist/pypy/translator/c/test/test_newgc.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_newgc.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_newgc.py	Sun May 21 21:05:49 2006
@@ -598,6 +598,100 @@
         res = fn()
         assert res == -70
 
+    def test_framework_finalizer(self):
+        class B(object):
+            pass
+        b = B()
+        b.nextid = 0
+        b.num_deleted = 0
+        class A(object):
+            def __init__(self):
+                self.id = b.nextid
+                b.nextid += 1
+            def __del__(self):
+                b.num_deleted += 1
+        def f():
+            a = A()
+            i = 0
+            while i < 5:
+                i += 1
+                a = A()
+            llop.gc__collect(lltype.Void)
+            llop.gc__collect(lltype.Void)
+            return b.num_deleted
+        run = self.getcompiled(f)
+        res = run()
+        assert res == 6
+
+    def test_framework_weakref_alive(self):
+        def func():
+            f = Weakrefable()
+            f.x = 32
+            ref1 = get_weakref(f)
+            ref2 = get_weakref(f)
+            return f.x + ref2.ref().x
+        assert func() == 64
+        f = self.getcompiled(func)
+        assert f() == 64
+
+    def test_framework_weakref_dying(self):
+        def g():
+            f = Weakrefable()
+            f.x = 32
+            return get_weakref(f)
+        def func():
+            ref = g()
+            llop.gc__collect(lltype.Void)
+            llop.gc__collect(lltype.Void)
+            return ref.ref() is None
+        f = self.getcompiled(func)
+        assert f()
+
+    def test_framework_weakref_callback(self):
+        global_w = Weakrefable()
+        global_w.x = 31
+        def g(ref):
+            global_w.x = 32
+        def h():
+            f = Weakrefable()
+            f.x = 32
+            ref = get_weakref(f, g)
+            return ref
+        def func():
+            ref = h()
+            llop.gc__collect(lltype.Void)
+            llop.gc__collect(lltype.Void)
+            return (ref.ref() is None and ref.callback is not None and
+                    global_w.x == 32)
+        f = self.getcompiled(func)
+        assert f()
+
+    def test_framework_weakref_dont_always_callback(self):
+        global_w1 = Weakrefable()
+        global_w1.x = 30
+        global_w2 = Weakrefable()
+        global_w2.x = 30
+        def g1(ref):
+            global_w1.x = 32
+        def g2(ref):
+            global_w2.x = 32
+        def h():
+            f = Weakrefable()
+            f.x = 32
+            ref1 = get_weakref(f, g1)
+            ref2 = get_weakref(f, g2)
+            if f.x % 2 == 0: # start a new block to make sure ref2 dies before f
+                ref2 = None
+            f.x = 12
+            llop.gc__collect(lltype.Void)
+            llop.gc__collect(lltype.Void)
+            return ref1
+        def func():
+            ref = h()
+            return global_w1.x == 32 and global_w2.x == 30
+        f = compile_func(func, [])
+        assert f()
+
     def test_framework_malloc_raw(self):
         A = lltype.Struct('A', ('value', lltype.Signed))
 



More information about the Pypy-commit mailing list