Currently I'm in the process of moving a performance bottleneck in my python code to c, to investigate peformance effects. This code will run a simulation, and report back the results to python via ctypes. However, I'm having problems getting my types to match up correctly.
Although I'm looking to solve this particular problem, I'm also on the lookout for more general advice on working with ctypes, as the documentation and procedure seems a bit thin.
I have the following c function:
extern "C" {
void f( int* array, int arraylen ) {
for(int i = 0; i < arraylen; i++) {
array[i] = g() // mutate the value array[i];
}
}
}
And the following code in python:
import ctypes
plib = ctypes.cdll.LoadLibrary('./mylib.so')
_f = plib.f
_f.restype = None
_f.argtypes = [ ctypes.POINTER(ctypes.c_int), ctypes.c_int ]
seqlen = 50
buffer = ctypes.c_int * seqlen
_f( buffer, seqlen )
However, this snippet dies with the following traceback:
Traceback (most recent call last):
File "particle.py", line 9, in <module>
_f( buffer, seqlen )
ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: expected LP_c_int instance instead of _ctypes.ArrayType