Title: Maximum and minimum value of C types integers from Python
Type: Stage:
Components: Versions:
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ammar2, scls, serhiy.storchaka
Priority: normal Keywords:

Created on 2018-09-26 14:32 by scls, last changed 2018-09-28 12:15 by ammar2.

Messages (5)
msg326467 - (view) Author: Sébastien Celles (scls) Date: 2018-09-26 14:32

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 asked this question on StackOverflow and get a nice answer

but I wonder if this kind of feature couldn't be add directly in `ctypes.c_` as property.

Maybe we could have the following properties:
- `issigned`
- `max`
- `min`

Moreover being able to convert a number into a C type integer raising exception when input data is out of range could also be considered
Maybe using code like
(instead of silently overflow)

Kind regards
msg326470 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-09-26 14:39
Maximum values for uint8, int8, uint16, int16, uint32, int32, uint64, int64 are 2**8-1, 2**7-1, 2**16-1, 2**15-1, 2**32-1, 2**31-1, 2**64-1, 2**63-1 by definition. Minimum values are 0, -2**7, 0, -2**15, 0, -2**31, 0, -2**63.
msg326472 - (view) Author: Sébastien Celles (scls) Date: 2018-09-26 14:54
Thanks @serhiy.storchaka I'm aware of this... but I think it could be returned programmatically without much difficulty.
msg326473 - (view) Author: Sébastien Celles (scls) Date: 2018-09-26 14:58
About raising exception with converting integer value that should overflow maybe we should have a parameter

    ctypes.c_uint8(256, raise=True)

will raise an exception but 


will silently return c_ubyte(0)
msg326629 - (view) Author: Ammar Askar (ammar2) * (Python triager) Date: 2018-09-28 12:15
What is the use case for getting the maximum and minimum of the fixed bit integers like `uint8`? It's a trivial formula to calculate those.

Now, if you're talking about platform dependent types like `int` and `long` then it might makes more sense to add such an api. Or is your end goal to just have an exception be raised when trying to fit a large number in a small type like in your example?
Date User Action Args
2018-09-28 12:15:36ammar2setnosy: + ammar2
messages: + msg326629
2018-09-26 14:58:25sclssetmessages: + msg326473
2018-09-26 14:54:22sclssetmessages: + msg326472
2018-09-26 14:39:01serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg326470
2018-09-26 14:32:05sclscreate