[pypy-svn] r46459 - in pypy/dist/pypy/rpython/lltypesystem: . test

arigo at codespeak.net arigo at codespeak.net
Tue Sep 11 10:44:22 CEST 2007


Author: arigo
Date: Tue Sep 11 10:44:21 2007
New Revision: 46459

Modified:
   pypy/dist/pypy/rpython/lltypesystem/llmemory.py
   pypy/dist/pypy/rpython/lltypesystem/test/test_llmemory.py
Log:
A more direct approach to weakrefs, without using opaques.


Modified: pypy/dist/pypy/rpython/lltypesystem/llmemory.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/llmemory.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/llmemory.py	Tue Sep 11 10:44:21 2007
@@ -540,24 +540,30 @@
 
 # ____________________________________________________________
 
-WeakGcRefOpaque = lltype.OpaqueType('WeakGcRef')
+class fakeweakref(fakeweakaddress):
+    pass        # inheriting only to copy all methods
 
-def weakgcref_init(wropaque, obj):
+WeakRef = lltype.Primitive("WeakRef", fakeweakref(None))
+
+def weakref_create(obj):
     PTRTYPE = lltype.typeOf(obj)
     assert isinstance(PTRTYPE, lltype.Ptr)
     assert PTRTYPE.TO._gckind == 'gc'
-    wropaque._obj.ref = weakref.ref(lltype.normalizeptr(obj))
+    return fakeweakref(lltype.normalizeptr(obj))
 
-def weakgcref_get(PTRTYPE, wropaque):
+def weakref_deref(PTRTYPE, wref):
     assert isinstance(PTRTYPE, lltype.Ptr)
     assert PTRTYPE.TO._gckind == 'gc'
-    assert lltype.typeOf(wropaque) == lltype.Ptr(WeakGcRefOpaque)
-    p = wropaque._obj.ref()
+    assert lltype.typeOf(wref) == WeakRef
+    p = wref.get()
     if p is None:
         return lltype.nullptr(PTRTYPE.TO)
     else:
         return lltype.cast_pointer(PTRTYPE, p)
 
+fakeweakref._TYPE = WeakRef
+WEAKREFNULL = fakeweakref(None)
+
 # ____________________________________________________________
 
 def raw_malloc(size):

Modified: pypy/dist/pypy/rpython/lltypesystem/test/test_llmemory.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/test/test_llmemory.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/test/test_llmemory.py	Tue Sep 11 10:44:21 2007
@@ -576,15 +576,12 @@
 def test_weakref():
     S = lltype.GcStruct('S', ('x',lltype.Signed))
     s = lltype.malloc(S)
-    o = lltype.malloc(WeakGcRefOpaque, flavor='raw')
-    weakgcref_init(o, s)
-    assert weakgcref_get(lltype.Ptr(S), o) == s
-    assert weakgcref_get(lltype.Ptr(S), o) == s
+    w = weakref_create(s)
+    assert weakref_deref(lltype.Ptr(S), w) == s
+    assert weakref_deref(lltype.Ptr(S), w) == s
     del s
     import gc; gc.collect()
-    assert weakgcref_get(lltype.Ptr(S), o) == lltype.nullptr(S)
-    lltype.free(o, flavor='raw')
-    py.test.raises(RuntimeError, weakgcref_get, lltype.Ptr(S), o)
+    assert weakref_deref(lltype.Ptr(S), w) == lltype.nullptr(S)
 
 class TestWeakAddressLLinterp(object):
     def test_null(self):



More information about the Pypy-commit mailing list