[pypy-svn] pypy out-of-line-guards-2: Finish casting back and forth opaque pointers to tagged pointers

fijal commits-noreply at bitbucket.org
Thu Apr 14 21:44:28 CEST 2011


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: out-of-line-guards-2
Changeset: r43363:6a3590c4fc0d
Date: 2011-04-14 21:44 +0200
http://bitbucket.org/pypy/pypy/changeset/6a3590c4fc0d/

Log:	Finish casting back and forth opaque pointers to tagged pointers

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
@@ -768,6 +768,9 @@
             # CFunctionType.__nonzero__ is broken before Python 2.6
             return lltype.nullptr(T.TO)
         if isinstance(T.TO, lltype.Struct):
+            if ctypes.addressof(cobj[0]) & 1: # a tagged pointer
+                gcref = _opaque_objs[ctypes.addressof(cobj[0]) // 2].hide()
+                return lltype.cast_opaque_ptr(T, gcref)
             REAL_TYPE = T.TO
             if T.TO._arrayfld is not None:
                 carray = getattr(cobj.contents, T.TO._arrayfld)

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
@@ -1286,6 +1286,28 @@
         rffi.cast(SP, p).x = 0
         lltype.free(chunk, flavor='raw')
 
+    def test_opaque_tagged_pointers(self):
+        from pypy.rpython.annlowlevel import cast_base_ptr_to_instance
+        from pypy.rpython.annlowlevel import cast_instance_to_base_ptr
+        from pypy.rpython.lltypesystem import rclass
+        
+        class Opaque(object):
+            llopaque = True
+
+            def hide(self):
+                ptr = cast_instance_to_base_ptr(self)
+                return lltype.cast_opaque_ptr(llmemory.GCREF, ptr)
+
+            @staticmethod
+            def show(gcref):
+                ptr = lltype.cast_opaque_ptr(lltype.Ptr(rclass.OBJECT), gcref)
+                return cast_base_ptr_to_instance(Opaque, ptr)
+
+        opaque = Opaque()
+        round = ctypes2lltype(llmemory.GCREF, lltype2ctypes(opaque.hide()))
+        assert Opaque.show(round) is opaque
+
+
 class TestPlatform(object):
     def test_lib_on_libpaths(self):
         from pypy.translator.platform import platform


More information about the Pypy-commit mailing list