I try to do a python wrapper to bind some C++ functions and types to python. My issue is when I try to convert a custom matrix type to a numpy ndarray. The most convincing solution is to use PyArray_SimpleNewFromData.
To test its behaviour, as I didn't manage to do what I wanted I tried to implement a simple test:
PyObject* ConvertToPython(...) {
uint8_t test[10] = {12, 15, 82, 254, 10, 32, 0, 8, 127, 54};
int32_t ndims = 1;
npy_intp dims[1];
dims[0] = 10;
int32_t typenum = (int32_t)NPY_UBYTE;
PyObject* python_object = PyArray_SimpleNewFromData(ndims, dims, typenum, (void*)test);
Py_XINCREF(python_object);
return python_object;
}
And then I got in python these results:
type(test) = <type 'numpy.ndarray'>
test.ndim = 1
test.dtype = uint8
test.shape = (10,)
But the values inside the array are:
test.values = [ 1 0 0 0 0 0 0 0 80 8]
I cannot figure out, what am I doing wrong ? And I am not very experienced doing a python Wrapper so any help would be appreciable !
PyArray_SimpleNewFromDatasteals a reference to the array so you need to handle yourself the reference to the input array. You must be sure it won't be deleted during its use in Python. In my example, thetestarray was deleted at the end of the function which was making the python array to wrap an unallocated chunk of memory. Check the accepted answer but to be short, I think this function cannot work with statically allocated arrays.