ctypes and misaligned doubles

Thomas Heller theller at python.net
Thu Dec 11 08:18:06 EST 2008


Jan Roelens schrieb:
> Dear python experts,
> 
> How can I change the alignment of types in the ctypes package? I have
> a library that was built with gcc using the -malign-double option. I
> also have python code that can create ctypes wrapper code from the
> include files for that library. The problem is that structs that
> contain fields of type double are not correctly wrapped half of the
> time. This is because ctypes assumes an alignment of 4 (on a 32-bit
> platform) for the double type, while the library I'm wrapping uses an
> alignment of 8 for these doubles.

To force an alignment that is smaller than the native alignment, you
can use the _pack_ attribute in your Structure definitions.

http://docs.python.org/library/ctypes.html?highlight=_pack_#ctypes.Structure._pack_

To force an alignment that is larger than the native alignment, you
must use padding.

> Is there a way to change the alignment of a ctypes type without
> recompiling the whole ctypes package? If I can't change it, is there a
> way to define my own type based on c_double but with a different
> alignment?

No.

Thomas



More information about the Python-list mailing list