This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

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

Created on 2018-09-26 14:32 by scls, last changed 2022-04-11 14:59 by admin. This issue is now closed.

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

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 https://stackoverflow.com/questions/52475749/maximum-and-minimum-value-of-c-types-integers-from-python

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 https://pastebin.com/cvm95m1x
(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 

    ctypes.c_uint8(256)

will silently return c_ubyte(0)
msg326629 - (view) Author: Ammar Askar (ammar2) * (Python committer) 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?
msg414625 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-03-06 20:32
Closing because there was no reply to questions asking to clarify the use case. Please reopen if you would like to continue the discussion.
History
Date User Action Args
2022-04-11 14:59:06adminsetgithub: 78991
2022-03-06 20:32:17iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg414625

resolution: wont fix
stage: resolved
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