Commits

Armin Rigo committed 42c0d16

Update to cffi/12a797fcf465.

Comments (0)

Files changed (3)

pypy/module/_cffi_backend/ctypeptr.py

 from pypy.rlib import rposix
 
 from pypy.module._cffi_backend.ctypeobj import W_CType
-from pypy.module._cffi_backend import cdataobj, misc, ctypeprim
+from pypy.module._cffi_backend import cdataobj, misc, ctypeprim, ctypevoid
 
 
 class W_CTypePtrOrArray(W_CType):
             for i in range(len(lst_w)):
                 ctitem.convert_from_object(cdata, lst_w[i])
                 cdata = rffi.ptradd(cdata, ctitem.size)
-        elif (self.ctitem.is_primitive_integer and
-              self.ctitem.size == rffi.sizeof(lltype.Char)):
+        elif (self.can_cast_anything or
+              (self.ctitem.is_primitive_integer and
+               self.ctitem.size == rffi.sizeof(lltype.Char))):
             if not space.isinstance_w(w_ob, space.w_str):
                 raise self._convert_error("str or list or tuple", w_ob)
             s = space.str_w(w_ob)
             return 0
         else:
             return 0
-        if self.ctitem.size <= 0:
-            return 0
+        itemsize = self.ctitem.size
+        if itemsize <= 0:
+            if isinstance(self.ctitem, ctypevoid.W_CTypeVoid):
+                itemsize = 1
+            else:
+                return 0
         try:
-            datasize = ovfcheck(length * self.ctitem.size)
+            datasize = ovfcheck(length * itemsize)
         except OverflowError:
             raise OperationError(space.w_OverflowError,
                 space.wrap("array size would overflow a ssize_t"))

pypy/module/_cffi_backend/test/_backend_test_c.py

     for i in range(10):
         assert res.a[i] == p1.a[i] - p2.a[i]
 
+def test_call_function_23():
+    BVoid = new_void_type()          # declaring the function as int(void*)
+    BVoidP = new_pointer_type(BVoid)
+    BInt = new_primitive_type("int")
+    BFunc23 = new_function_type((BVoidP,), BInt, False)
+    f = cast(BFunc23, _testfunc(23))
+    res = f(b"foo")
+    assert res == 1000 * ord(b'f')
+
+def test_call_function_23_bis():
+    # declaring the function as int(unsigned char*)
+    BUChar = new_primitive_type("unsigned char")
+    BUCharP = new_pointer_type(BUChar)
+    BInt = new_primitive_type("int")
+    BFunc23 = new_function_type((BUCharP,), BInt, False)
+    f = cast(BFunc23, _testfunc(23))
+    res = f(b"foo")
+    assert res == 1000 * ord(b'f')
+
 def test_cannot_pass_struct_with_array_of_length_0():
     BInt = new_primitive_type("int")
     BArray0 = new_array_type(new_pointer_type(BInt), 0)

pypy/module/_cffi_backend/test/_test_lib.c

     return result;
 }
 
+static int _testfunc23(char *p)
+{
+    return 1000 * p[0];
+}
+
 DLLEXPORT void *gettestfunc(int num)
 {
     void *f;
     case 20: f = &_testfunc20; break;
     case 21: f = &_testfunc21; break;
     case 22: f = &_testfunc22; break;
+    case 23: f = &_testfunc23; break;
     default:
         return NULL;
     }