Commits

Armin Rigo committed ec27cf4 Draft

Test and fix

  • Participants
  • Parent commits e669e0d
  • Branches ffi-backend

Comments (0)

Files changed (2)

File pypy/module/_cffi_backend/cbuffer.py

 from pypy.interpreter.buffer import RWBuffer
 from pypy.interpreter.gateway import unwrap_spec
 from pypy.rpython.lltypesystem import rffi
-from pypy.module._cffi_backend import cdataobj, ctypeptr
+from pypy.module._cffi_backend import cdataobj, ctypeptr, ctypearray
 
 
 class LLBuffer(RWBuffer):
 
 @unwrap_spec(cdata=cdataobj.W_CData, size=int)
 def buffer(space, cdata, size=-1):
-    if not isinstance(cdata.ctype, ctypeptr.W_CTypePtrOrArray):
+    ctype = cdata.ctype
+    if isinstance(ctype, ctypeptr.W_CTypePointer):
+        if size < 0:
+            size = ctype.ctitem.size
+    elif isinstance(ctype, ctypearray.W_CTypeArray):
+        if size < 0:
+            size = cdata._sizeof()
+    else:
         raise operationerrfmt(space.w_TypeError,
                               "expected a pointer or array cdata, got '%s'",
-                              cdata.ctype.name)
+                              ctype.name)
     if size < 0:
-        size = cdata._sizeof()
-        if size < 0:
-            raise operationerrfmt(space.w_TypeError,
-                                  "don't know the size pointed to by '%s'",
-                                  cdata.ctype.name)
+        raise operationerrfmt(space.w_TypeError,
+                              "don't know the size pointed to by '%s'",
+                              ctype.name)
     return space.wrap(LLBuffer(cdata._cdata, size))

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

     assert (p < s) ^ (p > s)
 
 def test_buffer():
+    BShort = new_primitive_type("short")
+    s = newp(new_pointer_type(BShort), 100)
+    assert sizeof(s) == size_of_ptr()
+    assert sizeof(BShort) == 2
+    assert len(str(buffer(s))) == 2
+    #
     BChar = new_primitive_type("char")
     BCharArray = new_array_type(new_pointer_type(BChar), None)
     c = newp(BCharArray, "hi there")