Author: Armin Rigo <[email protected]>
Branch: c99-array
Changeset: r1387:1737e2a72c02
Date: 2013-11-08 19:58 +0100
http://bitbucket.org/cffi/cffi/changeset/1737e2a72c02/

Log:    Add a test, now passing. Fixes in the front-end.

diff --git a/cffi/model.py b/cffi/model.py
--- a/cffi/model.py
+++ b/cffi/model.py
@@ -327,17 +327,21 @@
                             "field '%s.%s' has a bogus size?" % (
                             self.name, self.fldnames[i] or '{}'))
                     ftype = ftype.resolve_length(nlen)
+                    fsize = 0
                     self.fldtypes = (self.fldtypes[:i] + (ftype,) +
                                      self.fldtypes[i+1:])
                 #
                 BFieldType = ftype.get_cached_btype(ffi, finishlist)
-                bitemsize = ffi.sizeof(BFieldType)
-                if bitemsize != fsize:
-                    self._verification_error(
-                        "field '%s.%s' is declared as %d bytes, but is "
-                        "really %d bytes" % (self.name,
-                                             self.fldnames[i] or '{}',
-                                             bitemsize, fsize))
+                if isinstance(ftype, ArrayType) and ftype.length is None:
+                    assert fsize == 0
+                else:
+                    bitemsize = ffi.sizeof(BFieldType)
+                    if bitemsize != fsize:
+                        self._verification_error(
+                            "field '%s.%s' is declared as %d bytes, but is "
+                            "really %d bytes" % (self.name,
+                                                 self.fldnames[i] or '{}',
+                                                 bitemsize, fsize))
                 fldtypes.append(BFieldType)
             #
             lst = list(zip(self.fldnames, fldtypes, self.fldbitsize, fieldofs))
diff --git a/cffi/vengine_cpy.py b/cffi/vengine_cpy.py
--- a/cffi/vengine_cpy.py
+++ b/cffi/vengine_cpy.py
@@ -464,11 +464,14 @@
         prnt('  static Py_ssize_t nums[] = {')
         prnt('    sizeof(%s),' % cname)
         prnt('    offsetof(struct _cffi_aligncheck, y),')
-        for fname, _, fbitsize in tp.enumfields():
+        for fname, ftype, fbitsize in tp.enumfields():
             if fbitsize >= 0:
                 continue      # xxx ignore fbitsize for now
             prnt('    offsetof(%s, %s),' % (cname, fname))
-            prnt('    sizeof(((%s *)0)->%s),' % (cname, fname))
+            if isinstance(ftype, model.ArrayType) and ftype.length is None:
+                prnt('    0,  /* %s */' % ftype._get_c_name())
+            else:
+                prnt('    sizeof(((%s *)0)->%s),' % (cname, fname))
         prnt('    -1')
         prnt('  };')
         prnt('  return _cffi_get_struct_layout(nums);')
diff --git a/testing/test_ffi_backend.py b/testing/test_ffi_backend.py
--- a/testing/test_ffi_backend.py
+++ b/testing/test_ffi_backend.py
@@ -184,3 +184,13 @@
         ffi.cdef("typedef struct { float x; } foo_t;")
         p = ffi.new("foo_t *", [5.2])
         assert repr(p).startswith("<cdata 'foo_t *' ")
+
+    def test_struct_array_no_length(self):
+        ffi = FFI()
+        ffi.cdef("struct foo_s { int x; int a[]; };")
+        p = ffi.new("struct foo_s *", [100, [200, 300, 400]])
+        assert p.x == 100
+        assert ffi.typeof(p.a) is ffi.typeof("int *")   # no length available
+        assert p.a[0] == 200
+        assert p.a[1] == 300
+        assert p.a[2] == 400
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to