[pypy-svn] r47368 - in pypy/dist/pypy/rlib: . test

fijal at codespeak.net fijal at codespeak.net
Wed Oct 10 09:19:51 CEST 2007


Author: fijal
Date: Wed Oct 10 09:19:50 2007
New Revision: 47368

Modified:
   pypy/dist/pypy/rlib/libffi.py
   pypy/dist/pypy/rlib/test/test_libffi.py
Log:
Another approach - more rpythonic, still complaining about bad cast
from <* Array of Char> to <* Array of Float>


Modified: pypy/dist/pypy/rlib/libffi.py
==============================================================================
--- pypy/dist/pypy/rlib/libffi.py	(original)
+++ pypy/dist/pypy/rlib/libffi.py	Wed Oct 10 09:19:50 2007
@@ -5,6 +5,7 @@
 from pypy.rpython.tool import rffi_platform
 from pypy.rpython.lltypesystem import lltype, rffi
 from pypy.rlib.unroll import unrolling_iterable
+from pypy.rlib.rarithmetic import intmask
 
 includes = ['dlfcn.h', 'ffi.h']
 
@@ -137,7 +138,7 @@
 def push_arg_as_ffiptr(ffitp, TP, arg, ll_buf):
     # this is for primitive types. For structures and arrays
     # would be something different (more dynamic)
-    TP_P = rffi.CArray(TP)
+    TP_P = rffi.CArrayPtr(TP)
     rffi.cast(TP_P, ll_buf)[0] = arg
 push_arg_as_ffiptr._annspecialcase_ = 'specialize:argtype(1)'
 
@@ -155,20 +156,24 @@
         self.ll_argtypes = lltype.malloc(FFI_TYPE_PP.TO, argnum, flavor='raw')
         for i in range(argnum):
             self.ll_argtypes[i] = argtypes[i]
+        # XXX why cast to FFI_TYPE_PP is needed? ll2ctypes bug?
         res = c_ffi_prep_cif(self.ll_cif, FFI_DEFAULT_ABI,
                              rffi.cast(rffi.UINT, argnum), restype,
-                             self.ll_argtypes)
+                             rffi.cast(FFI_TYPE_PP, self.ll_argtypes))
         if not res == FFI_OK:
             raise OSError(-1, "Wrong typedef")
         for i in range(argnum):
             # space for each argument
-            self.ll_args[i] = lltype.malloc(rffi.VOIDP.TO, argtypes[i].c_size,
+            self.ll_args[i] = lltype.malloc(rffi.VOIDP.TO,
+                                            intmask(argtypes[i].c_size),
                                             flavor='raw')
-        self.ll_result = lltype.malloc(rffi.VOIDP.TO, restype.c_size,
+        self.ll_result = lltype.malloc(rffi.VOIDP.TO, intmask(restype.c_size),
                                        flavor='raw')
 
-    def push_arg(self, num, TP, value):
-        push_arg_as_ffiptr(self.argtypes[i], TP, value, self.ll_args[i])
+    # XXX some rpython trick to get rid of TP here?
+    def push_arg(self, num, value):
+        TP = lltype.typeOf(value)
+        push_arg_as_ffiptr(self.argtypes[num], TP, value, self.ll_args[num])
         self.ready_args[num] = 1
 
     def _check_args(self):
@@ -182,11 +187,14 @@
 
     def call(self, RES_TP):
         self._check_args()
-        c_ffi_call(self.ll_cif, self.func_sym,
-                   rffi.cast(rffi.VOIDP, self.restype),
+        c_ffi_call(self.ll_cif, self.funcsym,
+                   rffi.cast(rffi.VOIDP, self.ll_result),
                    rffi.cast(VOIDPP, self.ll_args))
         if self.restype != ffi_type_void:
-            res = rffi.cast(lltype.Ptr(CArray(RES_TP)), self.ll_result)[0]
+            TP = rffi.CArrayPtr(RES_TP)
+            res = rffi.cast(TP, self.ll_result)[0]
+        else:
+            res = None
         self._clean_args()
         return res
     call._annspecialcase_ = 'specialize:argtype(1)'

Modified: pypy/dist/pypy/rlib/test/test_libffi.py
==============================================================================
--- pypy/dist/pypy/rlib/test/test_libffi.py	(original)
+++ pypy/dist/pypy/rlib/test/test_libffi.py	Wed Oct 10 09:19:50 2007
@@ -43,7 +43,8 @@
         ptr = lib.getpointer('rand', [], ffi_type_sint)
         zeroes = 0
         for i in range(100):
-            res = ptr.call([])
+            res = ptr.call(rffi.INT)
+            print res
             if not res:
                 zeroes += 1
         assert not zeroes
@@ -53,18 +54,22 @@
         libm = CDLL('libm.so')
         pow = libm.getpointer('pow', [ffi_type_double, ffi_type_double],
                               ffi_type_double)
-        pow.push_arg(0, rffi.DOUBLE, 2.0)
-        pow.push_arg(1, rffi.DOUBLE, 2.0)
-        assert pow.call() == 4.0
-        pow.push_arg(0, rffi.DOUBLE, 3.0)
-        pow.push_arg(1, rffi.DOUBLE, 3.0)
-        assert pow.call() == 27.0
+        pow.push_arg(0, 2.0)
+        pow.push_arg(1, 2.0)
+        res = pow.call(rffi.DOUBLE)
+        assert res == 4.0
+        pow.push_arg(0, 3.0)
+        pow.push_arg(1, 3.0)
+        res = pow.call(rffi.DOUBLE)
+        assert res == 27.0
 
     def test_compile(self):
-        py.test.skip("in-progress")
         def f(x, y):
             libm = CDLL('libm.so')
-            c_pow = libm.getpointer('pow', (ffi_type_double, ffi_type_double), ffi_type_double)
-            return c_pow.call((x, y))
+            c_pow = libm.getpointer('pow', [ffi_type_double, ffi_type_double], ffi_type_double)
+            c_pow.push_arg(0, x)
+            c_pow.push_arg(1, y)
+            return c_pow.call(rffi.DOUBLE)
 
         interpret(f, [2.0, 4.0])
+        



More information about the Pypy-commit mailing list