[pypy-svn] pypy out-of-line-guards: Implement passing weakrefs as ints in ll2ctypes

fijal commits-noreply at bitbucket.org
Sun Jan 2 12:31:48 CET 2011


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: out-of-line-guards
Changeset: r40320:145021409ab0
Date: 2011-01-02 13:31 +0200
http://bitbucket.org/pypy/pypy/changeset/145021409ab0/

Log:	Implement passing weakrefs as ints in ll2ctypes

diff --git a/pypy/rpython/lltypesystem/ll2ctypes.py b/pypy/rpython/lltypesystem/ll2ctypes.py
--- a/pypy/rpython/lltypesystem/ll2ctypes.py
+++ b/pypy/rpython/lltypesystem/ll2ctypes.py
@@ -106,7 +106,7 @@
         lltype.Bool:     ctypes.c_long, # XXX
         llmemory.Address:  ctypes.c_void_p,
         llmemory.GCREF:    ctypes.c_void_p,
-        llmemory.WeakRef:  ctypes.c_void_p, # XXX
+        llmemory.WeakRef:  ctypes.c_int,
         })
 
     # for unicode strings, do not use ctypes.c_wchar because ctypes
@@ -572,6 +572,10 @@
 _int2obj = {}
 _callback_exc_info = None
 
+_all_weakrefs_cache = [] # this is a cache of all weakrefs. They don't
+# keep alive stuff, but some more advanced scenario for cleaning them up
+# might be useful
+
 def get_rtyper():
     llinterp = LLInterpreter.current_interpreter
     if llinterp is not None:
@@ -611,6 +615,9 @@
             # the same valu
         else:
             container = llobj._obj
+            if T == llmemory.WeakRefPtr:
+                _all_weakrefs_cache.append(llobj)
+                return (ctypes.c_int * 1)(len(_all_weakrefs_cache) - 1)
         if isinstance(T.TO, lltype.FuncType):
             # XXX a temporary workaround for comparison of lltype.FuncType
             key = llobj._obj.__dict__.copy()
@@ -755,6 +762,8 @@
     if T is lltype.Void:
         return None
     if isinstance(T, lltype.Ptr):
+        if T == llmemory.WeakRefPtr:
+            return _all_weakrefs_cache[cobj[0]]
         if not cobj or not ctypes.cast(cobj, ctypes.c_void_p).value:   # NULL pointer
             # CFunctionType.__nonzero__ is broken before Python 2.6
             return lltype.nullptr(T.TO)

diff --git a/pypy/rpython/lltypesystem/test/test_ll2ctypes.py b/pypy/rpython/lltypesystem/test/test_ll2ctypes.py
--- a/pypy/rpython/lltypesystem/test/test_ll2ctypes.py
+++ b/pypy/rpython/lltypesystem/test/test_ll2ctypes.py
@@ -1328,3 +1328,10 @@
         f = rffi.llexternal('f', [rffi.INT, rffi.INT], rffi.INT,
                             compilation_info=eci)
         assert f(3, 4) == 7
+
+    def test_weakref(self):
+        TP = lltype.GcStruct('x')
+        x = lltype.malloc(TP)
+        wref = llmemory.weakref_create(x)
+        ct = lltype2ctypes(wref)
+        assert ctypes2lltype(llmemory.WeakRefPtr, ct) == wref


More information about the Pypy-commit mailing list