Python ctypes type precision check

  • If the API expects a 64-bit type, how can I verify that the ctypes type has so many bits if sizeof returns the number of bytes?
  • How to find out how many bits in each byte on the current platform?
  • Where is the CHAR_BIT defined in Python?
+7
source share
1 answer

C / C ++ function signatures are written with C / C ++ types, such as "int" or "double" or "uint32_t". All of them have corresponding ctypes equivalents, so usually you are not interested in the number of bits.

That said ...

 import os print os.sysconf('SC_CHAR_BIT') 

... about as close as you get, I think. Does not work on platforms other than Unix. And, as tMC points out in the comments, it doesn't even work on all Unix platforms; I believe this is a GNU extension.

[update]

In fact, the POSIX spec mandate appears CHAR_BIT == 8. So, on any system that supports the sysconf selector SC_CHAR_BIT, you actually do not use it :-).

+6
source

All Articles