Author eryksun
Recipients Charles Machalow, amaury.forgeotdarc, belopolsky, eryksun, meador.inge
Date 2017-03-08.07:33:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1488958424.12.0.896797109172.issue29753@psf.upfronthosting.co.za>
In-reply-to
Content
To make it simpler to diagram the fields, I've rewritten your structure using field names A-J:

    import ctypes

    class MyStructure(ctypes.Structure):
        _pack_ = 1
        _fields_ = (('A', ctypes.c_uint16),     # 2 bytes
                    ('B', ctypes.c_uint16, 9),
                    ('C', ctypes.c_uint16, 1),
                    ('D', ctypes.c_uint16, 1),
                    ('E', ctypes.c_uint16, 1),
                    ('F', ctypes.c_uint16, 1),
                    ('G', ctypes.c_uint16, 3),  # 4 bytes
                    ('H', ctypes.c_uint32, 10),
                    ('I', ctypes.c_uint32, 20),
                    ('J', ctypes.c_uint32, 2))  # 8 bytes

ctypes is attempting to extend the bitfield for H by switching to a c_uint storage unit with an offset of 2 bytes and adding H field with a 16-bit offset:

    >>> MyStructure.H
    <Field type=c_uint, ofs=2:16, bits=10>

Here's the correct layout:

    |     uint16    |     uint16    |            uint32             
    |------16-------|------16-------|---10----|--------20---------|-
    A---------------B--------CDEFG--H---------I-------------------J-

and here's the layout that ctypes creates, which wastes 2 bytes:

                    |             uint32            |
    |     uint16    |     uint16    |               |             uint32            
    |------16-------|------16-------|---10----|--6--|--------20---------|-|---10----
    A---------------B--------CDEFG--H---------------I-------------------J-----------

The current strategy for extending bitfields to work like gcc on Linux is obviously failing us here. Hopefully someone can flesh out the exact rules to make this code accurate. Bitfields are such a pain.
History
Date User Action Args
2017-03-08 07:33:44eryksunsetrecipients: + eryksun, amaury.forgeotdarc, belopolsky, meador.inge, Charles Machalow
2017-03-08 07:33:44eryksunsetmessageid: <1488958424.12.0.896797109172.issue29753@psf.upfronthosting.co.za>
2017-03-08 07:33:44eryksunlinkissue29753 messages
2017-03-08 07:33:43eryksuncreate