[pypy-commit] cffi cffi-1.0: test and fix

arigo noreply at buildbot.pypy.org
Sat Apr 18 12:53:26 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r1752:ec0e49dc60d1
Date: 2015-04-18 12:54 +0200
http://bitbucket.org/cffi/cffi/changeset/ec0e49dc60d1/

Log:	test and fix

diff --git a/new/recompiler.py b/new/recompiler.py
--- a/new/recompiler.py
+++ b/new/recompiler.py
@@ -374,7 +374,7 @@
             ptr_struct_name = tp_struct.get_c_name('*')
             actual_length = '_cffi_array_len(((%s)0)->%s)' % (
                 ptr_struct_name, field_name)
-            tp_field = model.ArrayType(tp_field.item, actual_length)
+            tp_field = tp_field.resolve_length(actual_length)
         return tp_field
 
     def _generate_cpy_struct_collecttype(self, tp, name):
@@ -400,11 +400,15 @@
             for fldname, fldtype in zip(tp.fldnames, tp.fldtypes):
                 fldtype = self._field_type(tp, fldname, fldtype)
                 spaces = " " * len(fldname)
+                if (isinstance(fldtype, model.ArrayType) and
+                       fldtype.length is None):
+                    size = '-1'
+                else:
+                    size = 'sizeof(((%s)0)->%s)' % (tp.get_c_name('*'), fldname)
                 c_field.append(
                     '  { "%s", offsetof(%s, %s),\n' % (
                             fldname, tp.get_c_name(''), fldname) +
-                    '     %s   sizeof(((%s)0)->%s),\n' % (
-                            spaces, tp.get_c_name('*'), fldname) +
+                    '     %s   %s,\n' % (spaces, size) +
                     '     %s   _CFFI_OP(_CFFI_OP_NOOP, %s) },' % (
                             spaces, self._typesdict[fldtype]))
             self._lsts["field"].append('\n'.join(c_field))
@@ -495,7 +499,7 @@
     def _global_type(self, tp, global_name):
         if isinstance(tp, model.ArrayType) and tp.length == '...':
             actual_length = '_cffi_array_len(%s)' % (global_name,)
-            tp = model.ArrayType(tp.item, actual_length)
+            tp = tp.resolve_length(actual_length)
         return tp
 
     def _generate_cpy_variable_collecttype(self, tp, name):
diff --git a/new/test_recompiler.py b/new/test_recompiler.py
--- a/new/test_recompiler.py
+++ b/new/test_recompiler.py
@@ -252,3 +252,12 @@
     e = py.test.raises(ffi.error, "p.a")     # lazily build the fields and boom
     assert str(e.value) == ("struct foo_s field 'a' was declared in the "
                             "cdef to be 20 bytes, but is actually 24 bytes")
+
+def test_open_array_in_struct():
+    ffi = FFI()
+    ffi.cdef("struct foo_s { int b; int a[]; };")
+    verify(ffi, 'test_open_array_in_struct',
+           "struct foo_s { int b; int a[]; };")
+    assert ffi.sizeof("struct foo_s") == 4
+    p = ffi.new("struct foo_s *", [5, [10, 20, 30]])
+    assert p.a[2] == 30


More information about the pypy-commit mailing list