I'm looking for a way to get (using Python) the maximum and minimum values of C types integers (ie uint8
, int8
, uint16
, int16
, uint32
, int32
, uint64
, int64
...) from Python.
I was expecting to find this in ctypes
module
In [1]: import ctypesIn [2]: ctypes.c_uint8(256)Out[2]: c_ubyte(0)In [3]: ctypes.c_uint8(-1)Out[3]: c_ubyte(255)
but I couldn't find it.
Julia have great feature for this:
julia> typemax(UInt8)0xffjulia> typemin(UInt8)0x00julia> typemin(Int8)-128julia> typemax(Int8)127
I'm pretty sure Python have something quite similar.
Ideally I'm even looking for a way to ensure that a given Python integer (which is said to be unbounded) can be converted safely in a C type integer of a given size.When number is not in expected interval, it should raise an exception.
Currently overflow doesn't raise exception:
In [4]: ctypes.c_uint8(256)Out[4]: c_ubyte(0)
I saw this SO post Maximum and Minimum values for ints but it's a bit different as author is looking for min/max value of a Python integer... not a C integer (from Python)
I also noticed Detecting C types limits ("limits.h") in python? but, even if it's quite related, it doesn't really answer my question.