按照示例和 numpy C-API ( http://docs.scipy.org/doc/numpy/reference/c-api.html ),我正在尝试访问 cpp 中的 numpy 数组数据,如下所示:
#include <Python.h>
#include <frameobject.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION // TOGGLE OR NOT
#include "numpy/ndarraytypes.h"
#include "numpy/arrayobject.h"
...
// here I have passed "some_python_object" to the C code
// .. and "some_python_object" has member "infobuf" that is a numpy array
//
unsigned long* fInfoBuffer;
PyObject* infobuffer = PyObject_GetAttrString(some_python_object, "infobuf");
PyObject* x_array = PyArray_FROM_OT(infobuffer, NPY_UINT32);
fInfoBuffer = (unsigned long*)PyArray_DATA(x_array); // DOES NOT WORK WHEN API DEPRECATION IS TOGGLED
当 API 弃用被切换时,我在编译时得到:
error: cannot convert ‘PyObject* {aka _object*}’ to ‘PyArrayObject* {aka tagPyArrayObject*}’ for argument ‘1’ to ‘void* PyArray_DATA(PyArrayObject*)’
在 numpy 1.7+ 中这样做的合法方式是什么?