[pypy-svn] r50859 - in pypy/dist/pypy/module/_rawffi: . test

fijal at codespeak.net fijal at codespeak.net
Tue Jan 22 10:15:58 CET 2008


Author: fijal
Date: Tue Jan 22 10:15:57 2008
New Revision: 50859

Modified:
   pypy/dist/pypy/module/_rawffi/callback.py
   pypy/dist/pypy/module/_rawffi/test/test__rawffi.py
Log:
* use proper interface for results of callbacks
* fix issues (we don't really care about having an address of such stuff)


Modified: pypy/dist/pypy/module/_rawffi/callback.py
==============================================================================
--- pypy/dist/pypy/module/_rawffi/callback.py	(original)
+++ pypy/dist/pypy/module/_rawffi/callback.py	Tue Jan 22 10:15:57 2008
@@ -5,9 +5,9 @@
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
 from pypy.rpython.lltypesystem import lltype, rffi
 from pypy.module._rawffi.structure import unpack_fields
-from pypy.module._rawffi.array import get_elem
+from pypy.module._rawffi.array import get_elem, push_elem
 from pypy.module._rawffi.interp_rawffi import W_DataInstance, _get_type_,\
-     wrap_value, unwrap_value, unwrap_truncate_int
+     wrap_value, unwrap_value, unwrap_truncate_int, letter2tp
 from pypy.rlib.libffi import USERDATA_P, CallbackFuncPtr
 
 def callback(ll_args, ll_res, ll_userdata):
@@ -18,17 +18,11 @@
     argtypes = callback_ptr.args
     space = callback_ptr.space
     w_args = space.newlist([wrap_value(space, get_elem, ll_args[i], 0,
-                                       (argtypes[i], 0, 0))
+                                       letter2tp(space, argtypes[i]))
                             for i in range(len(argtypes))])
     w_res = space.call(w_callable, w_args)
-    if space.is_w(w_res, space.w_None):
-        res[0] = lltype.nullptr(rffi.VOIDP.TO)
-    else:
-        instance = space.interpclass_w(w_res)
-        if isinstance(instance, W_DataInstance):
-            res[0] = instance.ll_buffer
-        else:
-            res[0] = unwrap_truncate_int(rffi.VOIDP, space, w_res)
+    unwrap_value(space, push_elem, ll_res, 0,
+                 letter2tp(space, callback_ptr.result), w_res)
 
 class W_CallbackPtr(W_DataInstance):
     # XXX some weird hackery to be able to recover W_CallbackPtr object
@@ -38,7 +32,7 @@
     
     def __init__(self, space, w_callable, w_args, w_result):
         number = self.CallbackPtr_id
-        self.CallbackPtr_id += 1
+        self.__class__.CallbackPtr_id += 1
         self.CallbackPtr_by_number[number] = self
         self.space = space
         self.w_callable = w_callable

Modified: pypy/dist/pypy/module/_rawffi/test/test__rawffi.py
==============================================================================
--- pypy/dist/pypy/module/_rawffi/test/test__rawffi.py	(original)
+++ pypy/dist/pypy/module/_rawffi/test/test__rawffi.py	Tue Jan 22 10:15:57 2008
@@ -104,6 +104,11 @@
             return prebuilt_array1;
         }
 
+        long long runcallback(long long(*callback)())
+        {
+            return callback();
+        }
+
         '''))
         return compile_c_module([c_file], 'x', ExternalCompilationInfo())
     prepare_c_example = staticmethod(prepare_c_example)
@@ -371,10 +376,9 @@
             a1 = _rawffi.Array('i').fromaddress(a, 1)
             a2 = _rawffi.Array('i').fromaddress(b, 1)
             if a1[0] > a2[0]:
-                res = 1
-            res = -1
-            resarray[0] = res
-            return resarray
+                res = -1
+            res = 1
+            return res
         a1 = ll_to_sort.byptr()
         a2 = _rawffi.Array('i')(1)
         a2[0] = len(ll_to_sort)
@@ -385,6 +389,27 @@
         qsort(a1, a2, a3, a4)
         res = [ll_to_sort[i] for i in range(len(ll_to_sort))]
         assert res == [1,2,3,4]
+        a1.free()
+        a2.free()
+        a3.free()
+        a4.free()
+        ll_to_sort.free()
+        del cb
+
+    def test_another_callback(self):
+        import _rawffi
+        lib = _rawffi.CDLL(self.lib_name)
+        runcallback = lib.ptr('runcallback', ['P'], 'q')
+        def callback():
+            return 1<<42
+
+        cb = _rawffi.CallbackPtr(callback, [], 'q')
+        a1 = cb.byptr()
+        res = runcallback(a1)
+        assert res[0] == 1<<42
+        res.free()
+        a1.free()
+        del cb
 
     def test_setattr_struct(self):
         import _rawffi



More information about the Pypy-commit mailing list