[pypy-svn] r47601 - in pypy/dist/pypy/module/_ffi: . test

fijal at codespeak.net fijal at codespeak.net
Fri Oct 19 18:05:10 CEST 2007


Author: fijal
Date: Fri Oct 19 18:05:10 2007
New Revision: 47601

Added:
   pypy/dist/pypy/module/_ffi/callback.py   (contents, props changed)
Modified:
   pypy/dist/pypy/module/_ffi/__init__.py
   pypy/dist/pypy/module/_ffi/interp_ffi.py
   pypy/dist/pypy/module/_ffi/test/test__ffi.py
Log:
* Begginings of callback - a test and some commented out code
* Really call a function even if it's returning Void, C is not
  functional enough


Modified: pypy/dist/pypy/module/_ffi/__init__.py
==============================================================================
--- pypy/dist/pypy/module/_ffi/__init__.py	(original)
+++ pypy/dist/pypy/module/_ffi/__init__.py	Fri Oct 19 18:05:10 2007
@@ -15,6 +15,7 @@
         'StructureInstance'  : 'structure.W_StructureInstance',
         'ArrayInstance'      : 'array.W_ArrayInstance',
         '_get_type'          : 'interp_ffi._w_get_type',
+        #'CallbackPtr'        : 'callback.W_CallbackPtr',
     }
 
     appleveldefs = {

Added: pypy/dist/pypy/module/_ffi/callback.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/_ffi/callback.py	Fri Oct 19 18:05:10 2007
@@ -0,0 +1,31 @@
+
+from pypy.interpreter.baseobjspace import W_Root, ObjSpace, Wrappable,\
+     Arguments
+from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.typedef import TypeDef, GetSetProperty
+from pypy.rpython.lltypesystem import lltype, rffi
+from pypy.module._ffi.structure import unpack_fields
+
+def stuff(a, b):
+    print "comparing"
+    return int(a > b)
+
+class W_CallbackPtr(Wrappable):
+    def __init__(self, space, w_callable, w_args, w_result):
+        self.w_callable = w_callable
+        self.args = [space.str_w(w_arg) for w_arg in space.unpackiterable(
+            w_args)]
+
+    def getllfuncptr(space, self):
+        TP = lltype.FuncType([lltype.Signed, lltype.Signed], lltype.Signed)
+        ptr = lltype.functionptr(TP, stuff)
+        return space.wrap(rffi.cast(rffi.INT, ptr))
+
+def descr_new_callbackptr(space, w_type, w_callable, w_args, w_result):
+    return W_CallbackPtr(space, w_callable, w_args, w_result)
+
+W_CallbackPtr.typedef = TypeDef(
+    'CallbackPtr',
+    buffer  = GetSetProperty(W_CallbackPtr.getllfuncptr),
+    __new__ = interp2app(descr_new_callbackptr),
+)

Modified: pypy/dist/pypy/module/_ffi/interp_ffi.py
==============================================================================
--- pypy/dist/pypy/module/_ffi/interp_ffi.py	(original)
+++ pypy/dist/pypy/module/_ffi/interp_ffi.py	Fri Oct 19 18:05:10 2007
@@ -51,7 +51,8 @@
     'f' : rffi.FLOAT,
     'd' : rffi.DOUBLE,
     's' : rffi.CCHARP,
-    'P' : rffi.VOIDP,    
+    'P' : rffi.VOIDP,
+    'v' : lltype.Void,
 }
 
 def _get_type(space, key):
@@ -194,10 +195,16 @@
             mod = space.getbuiltinmodule('_ffi')
             w_StructureInstance = space.getattr(mod, w('StructureInstance'))
             w_ArrayInstance = space.getattr(mod, w('ArrayInstance'))
+            #w_CallbackPtr = space.getattr(mod, w('CallbackPtr'))
             if space.is_true(space.isinstance(w_arg, w_StructureInstance)) or\
                    space.is_true(space.isinstance(w_arg, w_ArrayInstance)):
                 ptr = rffi.cast(rffi.VOIDP, space.int_w(space.getattr(w_arg, w('buffer'))))
                 push_func(add_arg, argdesc, ptr)
+            #elif space.is_true(space.isinstance(w_arg, w_CallbackPtr)):
+            #    TP = lltype.FuncType([lltype.Signed, lltype.Signed], lltype.Signed)
+            #    ptr = rffi.cast(TP, space.int_w(space.getattr(w_arg, w('buffer'))))
+            #    push_func(add_arg, argdesc, ptr)
+
             else:
                 raise OperationError(space.w_TypeError, w(
                     "Expected structure, array or simple type"))
@@ -243,6 +250,9 @@
                 if not res:
                     return space.w_None
                 return space.wrap(rffi.cast(rffi.INT, res))
+            elif c == 'v':
+                func(add_arg, argdesc, ll_type)
+                return space.w_None
             elif c == 'q' or c == 'Q' or c == 'L':
                 return space.wrap(func(add_arg, argdesc, ll_type))
             elif c == 'f' or c == 'd':
@@ -286,9 +296,7 @@
             unwrap_value(space, push, self.ptr, i, argtype, w_arg, to_free)
             i += 1
         try:
-            if self.restype != 'v':
-                return wrap_value(space, ptr_call, self.ptr, None, self.restype)
-            return space.w_None
+            return wrap_value(space, ptr_call, self.ptr, None, self.restype)
         finally:
             for elem in to_free:
                 lltype.free(elem, flavor='raw')

Modified: pypy/dist/pypy/module/_ffi/test/test__ffi.py
==============================================================================
--- pypy/dist/pypy/module/_ffi/test/test__ffi.py	(original)
+++ pypy/dist/pypy/module/_ffi/test/test__ffi.py	Fri Oct 19 18:05:10 2007
@@ -88,6 +88,7 @@
         {
            return x;
         }
+
         '''))
         compile_c_module([c_file], 'x')
         return str(udir.join('x.so'))
@@ -293,7 +294,7 @@
         assert pass_ll(1<<42) == 1<<42
     
     def test_callback(self):
-        skip("Segfaults")
+        skip("Not working")
         import _ffi
         libc = _ffi.CDLL('libc.so.6')
         to_sort = "kljhgfa"
@@ -302,5 +303,6 @@
         def compare(a, b):
             return a < b
         qsort(ll_to_sort, len(to_sort), 1,
-              CallbackPtr(compare, ['i', 'i'], 'i'))
-        
+              _ffi.CallbackPtr(compare, ['i', 'i'], 'i'))
+        res = [ll_to_sort[i] for i in range(len(to_sort))]
+        assert res == sorted(to_sort)



More information about the Pypy-commit mailing list