[pypy-commit] cffi cffi-1.0: in-progress

arigo noreply at buildbot.pypy.org
Thu Apr 16 15:17:37 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r1730:d6e79e8e8194
Date: 2015-04-16 14:26 +0200
http://bitbucket.org/cffi/cffi/changeset/d6e79e8e8194/

Log:	in-progress

diff --git a/new/manual.c b/new/manual.c
--- a/new/manual.c
+++ b/new/manual.c
@@ -15,6 +15,10 @@
     return ~a;
 }
 
+struct foo_s {
+    int a;
+};
+
 /************************************************************/
 
 static void *_cffi_types[] = {
@@ -97,22 +101,35 @@
 static const struct _cffi_global_s _cffi_globals[] = {
     { "AA",    &_cffi_const_AA, _CFFI_OP(_CFFI_OP_CONSTANT_INT, 0) },
     { "BB",    &_cffi_const_BB, _CFFI_OP(_CFFI_OP_CONSTANT, 2) },
-    { "bb",    &bb, _CFFI_OP(_CFFI_OP_VARIABLE, 2) },
+    { "bb",    &bb, _CFFI_OP(_CFFI_OP_GLOBAL_VAR, 1) },
     { "foo42", &_cffi_f_foo42, _CFFI_OP(_CFFI_OP_CPYTHON_BLTN_V, 0) },
     { "foo64", &_cffi_f_foo64, _CFFI_OP(_CFFI_OP_CPYTHON_BLTN_O, 4) },
 };
 
+struct _cffi_align_foo_s { char x; struct foo_s y; };
+
+static const struct _cffi_struct_union_s _cffi_struct_unions[] = {
+    { "foo_s",
+      sizeof(struct foo_s),
+      offsetof(struct _cffi_align_foo_s, y),
+      0,
+      1, 0 },
+};
+
+static const struct _cffi_field_s _cffi_fields[] = {
+    { "a", offsetof(struct foo_s, a), sizeof(((struct foo_s *)0)->a),
+      _CFFI_OP(_CFFI_OP_NOOP, 1) },
+};
+
 static const struct _cffi_type_context_s _cffi_type_context = {
     _cffi_types,
     _cffi_globals,
-    NULL,  /* no constants */
-    NULL,
-    NULL,
+    _cffi_struct_unions,
+    _cffi_fields,
     NULL,
     NULL,
     5,  /* num_globals */
-    0,
-    0,
+    1,  /* num_struct_unions */
     0,
     0,
 };
diff --git a/new/recompiler.py b/new/recompiler.py
--- a/new/recompiler.py
+++ b/new/recompiler.py
@@ -369,6 +369,13 @@
         for tp1 in tp.fldtypes:
             self._do_collect_type(tp1)
 
+    def _generate_cpy_struct_decl(self, tp, name):
+        pass
+
+    def _generate_cpy_struct_ctx(self, tp, name):
+        self._lsts["struct_union"].append(
+            '  { "%s",  ')
+
     _generate_cpy_union_collecttype = _generate_cpy_struct_collecttype
 
     # ----------
diff --git a/new/test_recompiler.py b/new/test_recompiler.py
--- a/new/test_recompiler.py
+++ b/new/test_recompiler.py
@@ -152,3 +152,14 @@
     """)
     lib.aa = 5
     assert dir(lib) == ['aa', 'ff', 'my_constant']
+
+def test_verify_struct():
+    ffi = FFI()
+    ffi.cdef("struct foo_s { int b; short a; };")
+    lib = verify(ffi, 'test_verify_struct',
+                 "struct foo_s { short a; int b; };")
+    p = ffi.new("struct foo_s *", {'a': -32768, 'b': -2147483648})
+    assert p.a == -32768
+    assert p.b == -2147483648
+    py.test.raises(OverflowError, "p.a -= 1")
+    py.test.raises(OverflowError, "p.b -= 1")


More information about the pypy-commit mailing list