3

Is it possible to detect an integer overflow with the Python ctypes module?

The docs suggest that none of the primitives support overflow checking, but I'd like to write my own so I can use native c types for my CPU emulator.

1 Answer 1

1

ctypes isn’t for doing operations on integers, it’s for calling functions in shareable libraries. Any relevant integer arithmetic would still have to be done in Python.

Unless the sharable library is itself providing integer-arithmetic functions, in which case it would have to do the overflow detection.

Sign up to request clarification or add additional context in comments.

2 Comments

Well, the question is, what would happen if we have a python integer that is too big for the C type in question? Say we have an FFI function with argtype c_double, and call it with a python float that is bigger than what c_double can hold. The docs suggest to me this would silently cause an overflow.
The next problem is, how should the caller check against overflows in a portable manner (given that max sizes may differ across platforms)? And then, do we have to remember doing this individually on each FFI function that may receive a previously-unchecked python int? Or should we wrap the c_* types to generally do overflow checking on parameter conversion?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.