[pypy-commit] pypy ffistruct: small refactor, and add a failing test

antocuni noreply at buildbot.pypy.org
Wed Nov 9 13:51:05 CET 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: ffistruct
Changeset: r48995:0d79a558d6fc
Date: 2011-11-08 12:11 +0100
http://bitbucket.org/pypy/pypy/changeset/0d79a558d6fc/

Log:	small refactor, and add a failing test

diff --git a/pypy/module/_ffi/interp_struct.py b/pypy/module/_ffi/interp_struct.py
--- a/pypy/module/_ffi/interp_struct.py
+++ b/pypy/module/_ffi/interp_struct.py
@@ -62,21 +62,26 @@
 
 @unwrap_spec(name=str)
 def descr_new_structdescr(space, w_type, name, w_fields):
+    fields_w = space.fixedview(w_fields)
+    # note that the fields_w returned by compute_size_and_alignemnt has a
+    # different annotation than the original: list(W_Root) vs list(W_Field)
+    size, alignment, fields_w = compute_size_and_alignemnt(space, fields_w)
+    field_types = [] # clibffi's types
+    for w_field in fields_w:
+        field_types.append(w_field.w_ffitype.ffitype)
+    ffistruct = clibffi.make_struct_ffitype_e(size, alignment, field_types)
+    return W__StructDescr(space, name, fields_w, ffistruct)
+
+def compute_size_and_alignemnt(space, fields_w):
     size = 0
     alignment = 0 # XXX
-    fields_w = space.fixedview(w_fields)
-    fields_w2 = [] # its items are annotated as W_Field
-    field_types = []
+    fields_w2 = []
     for w_field in fields_w:
         w_field = space.interp_w(W_Field, w_field)
         w_field.offset = size # XXX: alignment!
         size += w_field.w_ffitype.sizeof()
         fields_w2.append(w_field)
-        field_types.append(w_field.w_ffitype.ffitype)
-    #
-    ffistruct = clibffi.make_struct_ffitype_e(size, alignment, field_types)
-    return W__StructDescr(space, name, fields_w2, ffistruct)
-
+    return size, alignment, fields_w2
 
 
 W__StructDescr.typedef = TypeDef(
diff --git a/pypy/module/_ffi/test/test_struct.py b/pypy/module/_ffi/test/test_struct.py
--- a/pypy/module/_ffi/test/test_struct.py
+++ b/pypy/module/_ffi/test/test_struct.py
@@ -27,6 +27,19 @@
         assert descr.ffitype.sizeof() == longsize*2
         assert descr.ffitype.name == 'struct foo'
 
+    def test_alignment(self):
+        from _ffi import _StructDescr, Field, types
+        longsize = types.slong.sizeof()
+        fields = [
+            Field('x', types.sbyte),
+            Field('y', types.slong),
+            ]
+        descr = _StructDescr('foo', fields)
+        assert descr.ffitype.sizeof() == longsize*2
+        assert fields[0].offset == 0
+        assert fields[1].offset == longsize # aligned to WORD
+
+
     def test_getfield_setfield(self):
         from _ffi import _StructDescr, Field, types
         longsize = types.slong.sizeof()


More information about the pypy-commit mailing list