ctypes and misaligned doubles

J

Jan Roelens

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.

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?

Regards,
Jan
 
T

Thomas Heller

Jan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top