]> granicus.if.org Git - python/commitdiff
Fix issue #3547: ctypes is confused by bitfields of varying integer types
authorThomas Heller <theller@ctypes.org>
Wed, 24 Sep 2008 19:00:21 +0000 (19:00 +0000)
committerThomas Heller <theller@ctypes.org>
Wed, 24 Sep 2008 19:00:21 +0000 (19:00 +0000)
Reviewed by Fredrik Lundh and Skip Montanaro.

Backport from trunk.

Lib/ctypes/test/test_bitfields.py
Misc/NEWS
Modules/_ctypes/cfield.c

index 2867cbf93e6c05855e9b6e2c1673c42295e96d18..ddd753ea09b0c835a55654ea825cdbfca790948a 100644 (file)
@@ -215,6 +215,21 @@ class BitFieldTest(unittest.TestCase):
                         ("b", c_ubyte, 4)]
         self.failUnlessEqual(sizeof(X), sizeof(c_byte))
 
+    def test_mixed_4(self):
+        class X(Structure):
+            _fields_ = [("a", c_short, 4),
+                        ("b", c_short, 4),
+                        ("c", c_int, 24),
+                        ("d", c_short, 4),
+                        ("e", c_short, 4),
+                        ("f", c_int, 24)]
+        # MS compilers do NOT combine c_short and c_int into
+        # one field, gcc does.
+        if os.name in ("nt", "ce"):
+            self.failUnlessEqual(sizeof(X), sizeof(c_int) * 4)
+        else:
+            self.failUnlessEqual(sizeof(X), sizeof(c_int) * 2)
+
     def test_anon_bitfields(self):
         # anonymous bit-fields gave a strange error message
         class X(Structure):
index a4c2ef5709395ffe7a0c10ffeda4828e0d66d157..1a58dba1e11d7be76cebdb13042e17bcb843064f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -86,6 +86,9 @@ Core and builtins
 Library
 -------
 
+- Issue #3547: Fixed ctypes structures bitfields of varying integer
+  sizes.
+
 - Issue #3762: platform.architecture() fails if python is lanched via
   its symbolic link.
 
index d310ac54828ce13bf2f109ba59693c9b91bca591..adf85971e495092b6666b4defa3396ce0e66752b 100644 (file)
@@ -163,7 +163,7 @@ CField_FromDesc(PyObject *desc, int index,
                break;
 
        case EXPAND_BITFIELD:
-               /* XXX needs more */
+               *poffset += dict->size - *pfield_size/8;
                *psize += dict->size - *pfield_size/8;
 
                *pfield_size = dict->size * 8;