0

I am trying to replicate C program behaviours using Python3 and repeatedly casting all numbers to ctypes.c_int32 get ugly real fast.

Is there any way I could default Python's int to be an instance of ctypes.c_int32?

Edit: I would like to perform arithmetic operations between multiple c_int32 objects (e.g. xor-ing and and-ing), but I can't do this without adding .value behind every c_type int object. I want to see if I can somehow override the default python int object to behave more similarly to c_int32 in ctypes.

narwhal
  • 43
  • 3
  • It isn't really even clear what you mean, but no, no there isn't. Operations involving `ctypes` objects should return the same kind of object, so it what exactly is the problem? – juanpa.arrivillaga Jun 09 '22 at 08:01
  • @juanpa.arrivillaga I have edited the question to be clearer. – narwhal Jun 09 '22 at 08:05
  • If your real purpose is only to simplify the way you do arithmetic with `c_int32` objects, you could create your own class inheriting from `ctypes.c_int32` that overloads its `__xor__` and `__and__` operators. – vultkayn Jun 09 '22 at 08:20
  • @vultkayn How can I make what you said the default behavior of Python numbers? – narwhal Jun 09 '22 at 08:38
  • You can't, at most you can add method to builtin types (such as int), but it is impossible to overload an existing method. I have myself tried to, but without any success. See this post for more details https://stackoverflow.com/questions/6738987/extension-method-for-python-built-in-types Since you are already using explicit `c_int32` objects instead of `int`, it shouldn't be much of an issue to replace it by your own, let's say, `my_cint32` type – vultkayn Jun 09 '22 at 08:55
  • There are too few details, but looks like an *XY Problem*. Why would one need that? It's easier to write in *C* than in *CTypes* (and it will also be faster). Also what do you want to do with the numbers? what if you'd subclass *CTypes* types that you use and add appropriate magic methods (*\_\_add\_\_*, ...)? – CristiFati Jun 09 '22 at 15:10
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jun 09 '22 at 15:50
  • 1
    Show example code of whatever you’re trying to do. Don’t make us guess. – Mark Tolonen Jun 09 '22 at 15:51

1 Answers1

0

You can't do what you want in Python 3. int in Python 2.3 and earlier (before int/long integration) worked they way I think you want.

If you just want to constrain math results to signed int32 results ctypes isn't needed. The following truncates n to the lower 32 bits, then adjusts for the sign bit:

>>> def int32(n):
...   n &= (1<<32)-1
...   return n - (1<<32) if n & (1<<31) else n
...
>>> int32(-1)
-1
>>> int32(0x80000000)  # sign bit set (bit 31)
-2147483648
>>> int32(0x7fffffff)  # max positive int32
2147483647
>>> int32(0x7fffffff + 1)  # overflow to sign bit
-2147483648
>>> 1<<31         # without constraint
2147483648
>>> int32(1<<31)
-2147483648
>>> 1<<32         # without constraint
4294967296
>>> int32(1<<32)
0
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251