bpo-29753: fix merging packed bitfields in ctypes struct/union by FFY00 · Pull Request #19850 · python/cpython (original) (raw)

from ctypes import LittleEndianStructure, c_uint8, sizeof

class Point(LittleEndianStructure): pack = 1 fields = [ ("x", c_uint8, 4), ("y", c_uint8, 8), ("z", c_uint8, 4), ]

data = Point(2, 2, 2) print(f"size of data: {sizeof(data)}") print(f"x: {data.x}, y: {data.y}, z: {data.z}")

I executed this script with version 3.10.0b3 on Linux, but got this output:

size of data: 2
x: 2, y: 0, z: 0

However, with version 3.9.5, I got:

size of data: 3
x: 2, y: 2, z: 2

It seems the size of packing bitfields in ctypes structure is now the same as what GCC does, but still unexpected behavor here.