0

Python and unsigned int

Hi I understand python does bit have data types strictly but I am in need of unsigned int Let me brief you about the story. I have a c++ api which takes unsigned int as an argument and updates that value. I have swig binding generated for this api which I will use into python. Now, as we have option of below: obj = myclass() Makes obj as myclass instance, how to do it for unsigned int Basically I want to declare a variable of unsigned int and laaa that variable to my api which will update value of unsigned int. Basically, I need a mechanism in python to generate unsigned int object

11th Mar 2025, 8:30 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
7 Réponses
+ 6
Though I am unfamiliar with using SWIG, I explored your question with ChatGPT. The primary answer was that it should work as long as you don't try to pass a negative integer. Taking it further, I asked how to make SWIG cast a negative int as unsigned. It gave me the SWIG setup below. I hope this helps: %module example %{ #include "example.h" %} // Custom typemap to convert negative Python integers to their unsigned representation %typemap(in) unsigned int { long temp = PyLong_AsLong($input); if (PyErr_Occurred()) return NULL; // Handle conversion errors $1 = static_cast<unsigned int>(temp); // Proper bitwise conversion } %include "example.h"
12th Mar 2025, 12:16 AM
Brian
Brian - avatar
+ 6
have you tried ctypes? import ctypes u32_1 = ctypes.c_uint32(-1).value print(u32_1) u8_1 = ctypes.c_uint8(0).value print(u8_1) u8_2 = ctypes.c_uint8(-1).value print(u8_2) https://docs.python.org/3/library/ctypes.html
12th Mar 2025, 1:10 AM
Bob_Li
Bob_Li - avatar
+ 5
Ketan Lalcheta perhaps go over the swig documentaion once again. specifially in the typemap section. Brian's example uses this. 31.8.1 What is a typemap? https://www.swig.org/Doc1.3/JUMP_LINK__&&__Python__&&__JUMP_LINK.html#Python_nn2 also, why choose Swig and not Cython? Swig is harder to work with because it's designed for multiple languages. Cython is for Python specifically.
12th Mar 2025, 4:40 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li tried with ctypes, but no success Not sure about Cython as I never heard or used it. Even though I am not in a position to use cython inplace of swig, it will be a good point got me to get overview of it. Thanks for bringing this up.
12th Mar 2025, 7:55 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Thanks Brian I was also only concerned about non negative values only. I just assigned as below: a = 0 And expected it to work , but it did not. type(a) is int and as python does not have strong type for unsigned int may be the reason. This made me think about what would happen if I have two api as below in C++. void display(int a); void display(unsigned int); Will above work with swig for python ? If it does not work, what to do. If it works and generate binding of python then a = 3 display(a) will call unsigned int or int version?
12th Mar 2025, 7:52 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Hi
13th Mar 2025, 1:54 PM
Sumit Sharma
Sumit Sharma - avatar
0
I started from below
13th Mar 2025, 1:55 PM
Sumit Sharma
Sumit Sharma - avatar