I want to return array from c++ and receive it in python code as a numpy array. I used PyArray_SimpleNewFromData and Py_BuildValue but my program is just quit without any error/warning message where 'PyArray_SimpleNewFromData' should run, so it's bit hard to understand what is the problem.
Here is a part of my code where I return the array.
#include <Python.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "numpy\ndarraytypes.h"
#include "numpy\ndarrayobject.h"
#include "numpy\arrayobject.h"
static PyObject* cfunc(PyObject* self, PyObject* args)
{
PyArrayObject* arr;
double* carr;
if (!PyArg_ParseTuple(args, "O", &arr))
{
return NULL;
}
carr = (double*)PyArray_DATA(arr);
...// do some job here to change the values of carr elements.
npy_intp dims[1];
dims[0] = 1; // Will be a 1D numpy array
int length_arr = 512 * 512;
PyObject* arr_return = PyArray_SimpleNewFromData(length_arr, dims, NPY_DOUBLE,
(void*)carr);
return Py_BuildValue("O", arr_return); // Python code will receive the array as numpy array.
}
Please let me know if you have any ideas, or need further information.