[pypy-svn] r46524 - in pypy/dist/pypy/translator/c: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Thu Sep 13 00:39:26 CEST 2007


Author: cfbolz
Date: Thu Sep 13 00:39:26 2007
New Revision: 46524

Modified:
   pypy/dist/pypy/translator/c/gc.py
   pypy/dist/pypy/translator/c/test/test_newgc.py
Log:
make weakrefs work with genc and mark-n-sweep too. gives an assertion error
when using the framework GC for some reason :-(


Modified: pypy/dist/pypy/translator/c/gc.py
==============================================================================
--- pypy/dist/pypy/translator/c/gc.py	(original)
+++ pypy/dist/pypy/translator/c/gc.py	Thu Sep 13 00:39:26 2007
@@ -358,6 +358,9 @@
         fnptr = self.db.gctransformer.frameworkgc_setup_ptr.value
         yield '%s();' % (self.db.get(fnptr),)
 
+    def get_real_weakref_type(self):
+        return framework.WEAKREF
+
     def OP_GC_RELOAD_POSSIBLY_MOVED(self, funcgen, op):
         args = [funcgen.expr(v) for v in op.args]
         # XXX this more or less assumes mark-and-sweep gc

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	Thu Sep 13 00:39:26 2007
@@ -566,6 +566,41 @@
         # does not crash
         fn()
 
+    def test_weakref(self):
+        import weakref
+        from pypy.rlib import rgc
+
+        class A:
+            pass
+
+        def fn():
+            n = 7000
+            keepalive = []
+            weakrefs = []
+            a = None
+            for i in range(n):
+                if i & 1 == 0:
+                    a = A()
+                    a.index = i
+                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
+        c_fn = self.getcompiled(fn)
+        res = c_fn()
+        # more than half of them should have been freed, ideally up to 6000
+        assert 3500 <= res <= 6000
+
     def test_framework_malloc_raw(self):
         A = lltype.Struct('A', ('value', lltype.Signed))
 
@@ -722,3 +757,6 @@
             else:
                 return res
         return compiled
+
+    def test_weakref(self):
+        py.test.skip("fails for some reason I couldn't figure out yet :-(")



More information about the Pypy-commit mailing list