Issue 1610575: C99 _Bool support for struct (original) (raw)
C99 adds the fundamental _Bool integer type (fundamental in the sense that it is not equivalent to or a composite of any other C type). Its size can vary from platform to platform; the only restriction imposed by the C standard is that it must be able to hold the values 0 or 1. Typically, sizeof _Bool is 1 or 4.
A struct module user trying to parse a native C structure that contains a _Bool member faces a problem: struct does not have a format character for _Bool. One is forced to hardcode a size for bool (use a char or an int instead).
This patch adds support for a new format character, 't', representing the fundamental type _Bool. It is handled sementically as representing pure booleans -- when packing a structure the truth value of the argument to be packed is used and when unpacking either True or False is always returned.
For platforms that don't support _Bool, as well as in non-native mode, 't' packs as a single byte.
Test cases are included, as well as a small change to the struct documentation. The patch modifies configure.in to check for _Bool support, and the patch includes the autogenerated configure and pyconfig.h.in files as well.
I have tested the module on Mac OS X x86 (uses 1 byte for _Bool) and Mac OS X ppc (uses 4 bytes for _Bool). Ran regression suite.
Oops!
I didn't intend for there to be any ctypes content in this patch (as indicated by the subject), but apparently I forgot to remove part of the ctypes section. I have uploaded a new patch without that part.
Once this has been integrated, I'll upload a complete ctypes patch for consideration. File Added: bool struct patch-2.diff