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

arigo at codespeak.net arigo at codespeak.net
Wed Jul 18 16:40:46 CEST 2007


Author: arigo
Date: Wed Jul 18 16:40:46 2007
New Revision: 45185

Modified:
   pypy/dist/pypy/rpython/lltypesystem/ll2ctypes.py
   pypy/dist/pypy/rpython/lltypesystem/test/test_rffi.py
Log:
Support for compiling calls to rffi.cast().
If more cases arise, they can be added in the supposedly-generic
rbuiltin.gen_cast().


Modified: pypy/dist/pypy/rpython/lltypesystem/ll2ctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/ll2ctypes.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/ll2ctypes.py	Wed Jul 18 16:40:46 2007
@@ -2,9 +2,12 @@
 import ctypes
 import ctypes.util
 from pypy.rpython.lltypesystem import lltype, llmemory
+from pypy.rpython.extfunc import ExtRegistryEntry
 from pypy.rlib.objectmodel import Symbolic
 from pypy.tool.uid import fixid
 from pypy.rlib.rarithmetic import r_uint
+from pypy.annotation import model as annmodel
+from pypy.rpython.rbuiltin import gen_cast
 
 
 def uaddressof(obj):
@@ -463,3 +466,20 @@
     else:
         cvalue = cresulttype(cvalue).value   # mask high bits off if needed
     return ctypes2lltype(RESTYPE, cvalue)
+
+class ForceCastEntry(ExtRegistryEntry):
+    _about_ = force_cast
+
+    def compute_result_annotation(self, s_RESTYPE, s_value):
+        assert s_RESTYPE.is_constant()
+        RESTYPE = s_RESTYPE.const
+        return annmodel.lltype_to_annotation(RESTYPE)
+
+    def specialize_call(self, hop):
+        hop.exception_cannot_occur()
+        s_RESTYPE = hop.args_s[0]
+        assert s_RESTYPE.is_constant()
+        RESTYPE = s_RESTYPE.const
+        v_arg = hop.inputarg(hop.args_r[1], arg=1)
+        TYPE1 = v_arg.concretetype
+        return gen_cast(hop.llops, RESTYPE, v_arg)

Modified: pypy/dist/pypy/rpython/lltypesystem/test/test_rffi.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/test/test_rffi.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/test/test_rffi.py	Wed Jul 18 16:40:46 2007
@@ -202,3 +202,11 @@
     res = cast(SIZE_T, -1)
     assert type(res) is r_size_t
     assert res == r_size_t(-1)
+
+def test_compile_cast():
+    def f(n):
+        return cast(SIZE_T, n)
+
+    f1 = compile(f, [int])
+    res = f1(-1)
+    assert res == r_size_t(-1)



More information about the Pypy-commit mailing list