Commits

Armin Rigo committed ec27cf4 Draft

Test and fix

Comments (0)

Files changed (2)

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))

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")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.