Reputation: 1733
I have been working with the numerical python / SWIG / C++ combination, using the celebrated "numpy.i" SWIG template file (ARGOUTVIEW, INPLACE, etc. arrays, see: http://docs.scipy.org/doc/numpy-1.10.0/reference/swig.interface-file.html).
However, in order to actually learn something, I'd like to create my very own typemap for numpy arrays. First, just something very simple:
We have a byte buffer numpy array in python, we want to pass it to C(++), in the most simple way: the C(++) obtains the pointer to the bytebuffer (yes I know .. garbage collection.. dangerous).
I have tried this with the following swig code (none of the commented-out nor active lines did the trick):
%typemap(in) (uint8_t* bytebuffer) {
PyObject* x_array = PyArray_FROM_OTF($input, NPY_UINT8, NPY_ARRAY_IN_ARRAY);
// PyArray* x_array = PyArray_FROM_OTF($input, NPY_UINT8, NPY_ARRAY_IN_ARRAY);
// PyObject* x_array = PyArray_FROM_O($input);
// $1 = (uint8_t*)PyArray_DATA(x_array);
// $1 = (uint8_t*)PyArray_DATA($input);
$1 = static_cast<uint8_t*>(PyArray_DATA($input));
}
I am getting for each case, things like:
error: cannot convert ‘PyObject* {aka _object*}’ to ‘PyArrayObject* {aka tagPyArrayObject*}’ for argument ‘1’ to ‘void* PyArray_DATA(PyArrayObject*)’
I have read some of the numpy api (see: http://docs.scipy.org/doc/numpy-1.10.0/reference/c-api.array.html#array-structure-and-data-access) but I seem to misunderstand something.. there is no freaking way to make this #%@ work! Help!
Upvotes: 0
Views: 518
Reputation: 1733
OK. After analyzing what "numpy.i" does, I found the silver bullet:
$1=(uint8_t*)(PyArray_DATA((PyArrayObject*)$input));
Sorry for the spam.
Upvotes: 2