[pypy-svn] pypy fast-forward: ctypes: Add support for Structure._pack_

amauryfa commits-noreply at bitbucket.org
Fri Jan 14 18:24:47 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: fast-forward
Changeset: r40682:8e57802fbd9c
Date: 2011-01-14 12:54 +0100
http://bitbucket.org/pypy/pypy/changeset/8e57802fbd9c/

Log:	ctypes: Add support for Structure._pack_

diff --git a/lib_pypy/_ctypes/structure.py b/lib_pypy/_ctypes/structure.py
--- a/lib_pypy/_ctypes/structure.py
+++ b/lib_pypy/_ctypes/structure.py
@@ -71,7 +71,8 @@
 # ________________________________________________________________
 
 def _set_shape(tp, rawfields, is_union=False):
-    tp._ffistruct = _rawffi.Structure(rawfields, is_union)
+    tp._ffistruct = _rawffi.Structure(rawfields, is_union,
+                                      getattr(self, '_pack_', 0))
     tp._ffiargshape = tp._ffishape = (tp._ffistruct, 1)
     tp._fficompositesize = tp._ffistruct.size
 

diff --git a/pypy/module/_rawffi/test/test__rawffi.py b/pypy/module/_rawffi/test/test__rawffi.py
--- a/pypy/module/_rawffi/test/test__rawffi.py
+++ b/pypy/module/_rawffi/test/test__rawffi.py
@@ -489,6 +489,12 @@
         raises(ValueError, _rawffi.Structure, [('A', 'I', 129)])
         raises(ValueError, _rawffi.Structure, [('A', 'I', -1)])
 
+    def test_packed_structure(self):
+        import _rawffi
+        Y = _rawffi.Structure([('a', 'c'),
+                               ('b', 'i')], pack=1)
+        assert Y.size == 5
+
     def test_array(self):
         import _rawffi
         lib = _rawffi.CDLL(self.lib_name)

diff --git a/pypy/module/_rawffi/structure.py b/pypy/module/_rawffi/structure.py
--- a/pypy/module/_rawffi/structure.py
+++ b/pypy/module/_rawffi/structure.py
@@ -134,7 +134,7 @@
 
 
 class W_Structure(W_DataShape):
-    def __init__(self, space, fields, size, alignment, is_union=False):
+    def __init__(self, space, fields, size, alignment, is_union=False, pack=0):
         name_to_index = {}
         if fields is not None:
             for i in range(len(fields)):
@@ -144,7 +144,7 @@
                         "duplicate field name %s", name)
                 name_to_index[name] = i
             size, alignment, pos, bitsizes = size_alignment_pos(
-                fields, is_union)
+                fields, is_union, pack)
         else: # opaque case
             fields = []
             pos = []
@@ -227,7 +227,11 @@
     
 
 
-def descr_new_structure(space, w_type, w_shapeinfo, union=0):
+def descr_new_structure(space, w_type, w_shapeinfo, union=0, pack=0):
+    if pack < 0:
+        raise OperationError(space.w_ValueError, space.wrap(
+            "_pack_ must be a non-negative integer"))
+
     is_union = bool(union)
     if space.is_true(space.isinstance(w_shapeinfo, space.w_tuple)):
         w_size, w_alignment = space.fixedview(w_shapeinfo, expected_length=2)
@@ -235,9 +239,9 @@
                                      space.int_w(w_alignment), is_union)
     else:
         fields = unpack_fields(space, w_shapeinfo)
-        S = W_Structure(space, fields, 0, 0, is_union)
+        S = W_Structure(space, fields, 0, 0, is_union, pack)
     return space.wrap(S)
-descr_new_structure.unwrap_spec = [ObjSpace, W_Root, W_Root, int]
+descr_new_structure.unwrap_spec = [ObjSpace, W_Root, W_Root, int, int]
 
 W_Structure.typedef = TypeDef(
     'Structure',


More information about the Pypy-commit mailing list