Skip to content

BUG: np.dtype(ctypes.Structure) does not respect _pack_ field #10532

@eric-wieser

Description

@eric-wieser
class Foo(ctypes.Structure):
    _fields_ = [('one', ctypes.c_uint8), ('two', ctypes.c_uint32)]
    _pack_ = 2
>>> ctypes.sizeof(Foo())  # packed to alignment 2
6
>>> np.dtype(Foo).itemsize  # default alignment incorrectly used
8

Fix should be fairly straightforward by changing:

def _from_ctypes_structure(t):
# TODO: gh-10533, gh-10532
fields = []
for item in t._fields_:
if len(item) > 2:
raise TypeError(
"ctypes bitfields have no dtype equivalent")
fname, ftyp = item
fields.append((fname, dtype_from_ctypes_type(ftyp)))
# by default, ctypes structs are aligned
return np.dtype(fields, align=True)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions