[New-bugs-announce] [issue11920] ctypes: Strange bitfield structure sizing issue

Steve Thompson report at bugs.python.org
Mon Apr 25 18:52:56 CEST 2011


New submission from Steve Thompson <steve.f.thompson at gmail.com>:

Consider the following:
import ctypes

class struct1( ctypes.Structure ):
    _pack_ = 1
    _fields_ =  [
                    ( "first",  ctypes.c_uint8,  1  ),
                    ( "second", ctypes.c_uint8,  1  ),
                    ( "third",  ctypes.c_uint8,  1  ),
                    ( "fourth", ctypes.c_uint8,  1  ),
                    ( "fifth",  ctypes.c_uint8,  1  ),
                    ( "pad",    ctypes.c_uint16, 11 ),
                ]
                
s1 = struct1()
print ctypes.sizeof( s1 )

class struct2( ctypes.Structure ):
    _pack_ = 1
    _fields_ =  [
                    ( "first",  ctypes.c_uint16, 1  ),
                    ( "second", ctypes.c_uint16, 1  ),
                    ( "third",  ctypes.c_uint16, 1  ),
                    ( "fourth", ctypes.c_uint16, 1  ),
                    ( "fifth",  ctypes.c_uint16, 1  ),
                    ( "pad",    ctypes.c_uint16, 11 ),
                ]
                
s2 = struct2()
print ctypes.sizeof( s2 )

The output is:
3
2

I'm generating python code from real c code.  The compiler I'm using for the real c code packs both of these structures into two bytes.  I need a way to make the first example work in python like the compiler without having to modify the source code.

Is this possible?

----------
components: ctypes
messages: 134393
nosy: Steve.Thompson
priority: normal
severity: normal
status: open
title: ctypes: Strange bitfield structure sizing issue
versions: Python 2.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11920>
_______________________________________


More information about the New-bugs-announce mailing list