1. cbjadwani
  2. pypy

Commits

Armin Rigo  committed 57a1071

Update the test to hg/cffi/c/test_c, and fix.

  • Participants
  • Parent commits 2e0907c
  • Branches ffi-backend

Comments (0)

Files changed (3)

File pypy/module/_cffi_backend/ccallback.py

View file
 
 from pypy.module._cffi_backend.cdataobj import W_CData, W_CDataApplevelOwning
 from pypy.module._cffi_backend.ctypefunc import SIZE_OF_FFI_ARG, BIG_ENDIAN
-from pypy.module._cffi_backend import cerrno
+from pypy.module._cffi_backend.ctypeprim import W_CTypePrimitiveSigned
+from pypy.module._cffi_backend.ctypevoid import W_CTypeVoid
+from pypy.module._cffi_backend import cerrno, misc
 
 # ____________________________________________________________
 
         #
         w_res = space.call(self.w_callable, space.newtuple(args_w))
         #
-        if fresult.size > 0:
-            convert_from_object_fficallback(fresult, ll_res, w_res)
+        convert_from_object_fficallback(fresult, ll_res, w_res)
 
     def print_error(self, operr):
         space = self.space
 
 
 def convert_from_object_fficallback(fresult, ll_res, w_res):
-    if fresult.is_primitive_integer and fresult.size < SIZE_OF_FFI_ARG:
+    space = fresult.space
+    small_result = fresult.size < SIZE_OF_FFI_ARG
+    if small_result and isinstance(fresult, W_CTypeVoid):
+        if not space.is_w(w_res, space.w_None):
+            raise OperationError(space.w_TypeError,
+                    space.wrap("callback with the return type 'void'"
+                               " must return None"))
+        return
+    #
+    if small_result and fresult.is_primitive_integer:
         # work work work around a libffi irregularity: for integer return
         # types we have to fill at least a complete 'ffi_arg'-sized result
         # buffer.
-        from pypy.module._cffi_backend.ctypeprim import W_CTypePrimitiveSigned
         if type(fresult) is W_CTypePrimitiveSigned:
             # It's probably fine to always zero-extend, but you never
             # know: maybe some code somewhere expects a negative
             # manual inlining and tweaking of
             # W_CTypePrimitiveSigned.convert_from_object() in order
             # to write a whole 'ffi_arg'.
-            from pypy.module._cffi_backend import misc
-            value = misc.as_long_long(fresult.space, w_res)
+            value = misc.as_long_long(space, w_res)
             value = r_ulonglong(value)
             misc.write_raw_integer_data(ll_res, value, SIZE_OF_FFI_ARG)
             return

File pypy/module/_cffi_backend/ctypeobj.py

View file
 
     def newp(self, w_init):
         space = self.space
-        raise OperationError(space.w_TypeError,
-                             space.wrap("expected a pointer or array ctype"))
+        raise operationerrfmt(space.w_TypeError,
+                              "expected a pointer or array ctype, got '%s'",
+                              self.name)
 
     def cast(self, w_ob):
         space = self.space

File pypy/module/_cffi_backend/test/_backend_test_c.py

View file
     assert s.a == -10
     assert s.b == 1E-42
 
+def test_callback_returning_void():
+    BVoid = new_void_type()
+    BFunc = new_function_type((), BVoid, False)
+    def cb():
+        seen.append(42)
+    f = callback(BFunc, cb)
+    seen = []
+    f()
+    assert seen == [42]
+    py.test.raises(TypeError, callback, BFunc, cb, -42)
+
 def test_enum_type():
     BEnum = new_enum_type("foo", (), ())
     assert repr(BEnum) == "<ctype 'enum foo'>"
     assert f(0) == unichr(0)
     assert f(255) == unichr(255)
     assert f(0x1234) == u'\u1234'
-    assert f(-1) == u'\U00012345'
+    if sizeof(BWChar) == 4:
+        assert f(-1) == u'\U00012345'
 
 def test_struct_with_bitfields():
     BLong = new_primitive_type("long")
         s.a1 = u'\ud807\udf44'
         assert s.a1 == u'\U00011f44'
     else:
-        py.test.raises(ValueError, "s.a1 = u'\U00012345'")
+        py.test.raises(TypeError, "s.a1 = u'\U00012345'")
     #
     BWCharArray = new_array_type(BWCharP, None)
     a = newp(BWCharArray, u'hello \u1234 world')