From: Nitro <ni...@dr...> - 2008-03-26 21:20:57
|
Am 26.03.2008, 20:49 Uhr, schrieb Donovan Parks <don...@gm...>: > Hello, > > I have a simple C++ function that I am wrapping with swig: > > void getImage(int frameNum, int* imageData); > > I have successfully wrapped this function. Using a simple helper > function, I can create a C int array in python and pass it into this > function. Now, I would like to be able to access the elements of this > C int array in python. Clearly I could write a simple C helper > function that took in the array points and the desired element and > would return the value of the array at the element. Is there a way to > access the elements of this C array directly? Can I convert the C Yes. Instead of using a helper function you can also use a typemap. Here http://www.swig.org/Doc1.3/Typemaps.html#Typemaps_nn39 is some more information. Specifically the "out" typemap is interesting here: %typemap(out) float [ANY] { int i; $result = PyList_New($1_dim0); for (i = 0; i < $1_dim0; i++) { PyObject *o = PyFloat_FromDouble((double) $1[i]); PyList_SetItem($result,i,o); } } You can translate this to something like typemap(out) int* { int i; int length = XXX; $result = PyList_New( length ); for (i = 0; i < length; i++) { PyObject *o = PyInt_FromLong( $1[i] ); PyList_SetItem($result,i,o); } } Note that XXX is undefined. It really depends on the size of your array. Maybe you are passing a fixed size array or it is zero-terminated. This is something you need to fill in yourself. -Matthias |