Issues with using C code in Python | Set 1
Prerequisite: Using C codes in Python | Set 1, Set 2
Issue #1 : If using ctypes then there is a problem that the original C code may use language that don’t map cleanly to Python.
Let’s take the example of divide() function as it returns a value through one of its arguments. It is a common C technique but it is often not clear how it is supposed to work in Python.
divide = _mod.divide divide.argtypes = (ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_int)) x = 0divide(10, 3, x) |
Output :
Traceback (most recent call last): File "", line 1, in ctypes.ArgumentError: argument 3: : expected LP_c_int instance instead of int
Issue #2 :
Even if it works, then there is a violation of Python’s immutability of integers. For arguments involving pointers, a compatible ctypes object has to be constructed and is pass as in the given code below.
x = ctypes.c_int() print ("divide(10, 3, x) : ", divide(10, 3, x)) print ("x.value : ", x.value) |
Output :
divide(10, 3, x) : 3 x.value : 1
Issue #3 :
In the code below, an instance of a ctypes.c_int is created and is passed in as the pointer object .c_int object can be mutated unlike a normal Python integer. To either retrieve or change the value, .value attribute can be used.
# int divide(int, int, int *) _divide = _mod.divide _divide.argtypes = (ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_int)) _divide.restype = ctypes.c_int def divide(x, y): rem = ctypes.c_int() quot = _divide(x, y, rem) return quot, rem.value |
Issue #4 : The avg() function is a challenge.
The underlying C code expects a length representing an array and to receive a pointer. However, from the Python side in practice, a Python “array” can take many different forms, it can be array, list, tuple or numpy array and maybe you would like to support multiple possibilities.
Recommended Posts:
- Issues with using C code in Python | Set 2
- Python code to convert SOP to POS
- Code introspection in Python
- Morse Code Translator In Python
- Execute a String of Code in Python
- Packaging and Publishing Python code
- How to run Python code on Google Colaboratory
- Different Python IDEs and Code Editors
- Debugging Python code using breakpoint() and pdb
- Optimization Tips for Python Code
- Understanding Code Reuse and Modularity in Python 3
- Performing Google Search using Python code
- How to generate byte code file in python ?
- Python | Generate QR Code using pyqrcode module
- Python Code for time Complexity plot of Heap Sort
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



