Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verifying ctypes type precision in Python

  1. If an API expects a 64 bit type, how can I check that a ctypes type has that many bits if sizeof returns the number of bytes?
  2. How do I know how many bits are in each byte on the current platform?
  3. Where is CHAR_BIT defined in Python?
like image 684
Matt Joiner Avatar asked Jun 03 '11 05:06

Matt Joiner


1 Answers

C/C++ function signatures are written with C/C++ types, like "int" or "double" or "uint32_t". All of these have corresponding ctypes equivalents, so normally you do not care about the number of bits.

That said...

import os
print os.sysconf('SC_CHAR_BIT')

...is about as close as you will get, I think. Does not work on non-Unix platforms. And as tMC points out in the comments, it does not even work on all Unix platforms; I believe it is a GNU extension.

[update]

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

like image 58
Nemo Avatar answered Nov 08 '22 00:11

Nemo