[issue24859] ctypes.Structure bit order is reversed - counts from right

Martin Panter report at bugs.python.org
Fri Aug 14 04:38:21 CEST 2015


Martin Panter added the comment:

It would be helpful if you could trim down your example code a bit. Without studying the whole file, it is hard to see exactly what order you are seeing and what order you expect, since there are two versions with different orders in the code.

My understanding of the “ctypes” module is that it is for interacting with the local OS, ABI, compiler, etc, which could use various layouts depending on the platform. According to the Linux x86-64 ABI <http:/www.x86-64.org/documentation/abi.pdf>, page 14, “bit-fields are allocated from right to left”, which I interpret to mean from least-significant to most-significant bit. Not so sure about Windows, but <https://msdn.microsoft.com/en-us/library/yszfawxh.aspx> suggests a similar story (LSB first). This behaviour agrees with my experiments on Linux and Wine:

>>> class Bitfield(Structure):
...     _fields_ = (("a", c_uint8, 4), ("b", c_uint8, 4))
... 
>>> bytes(Bitfield(0xA, 0xB))
b'\xba'

Does this agree with what you expect? Otherwise, what leads you to expect something different?

Also:
* bytes(saej1939_message_id) should copy the bytes directly; no need for a union.
* struct.unpack() should also accept a “ctypes” object directly; no need for the copy.

----------
nosy: +martin.panter

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


More information about the Python-bugs-list mailing list