我正在编写代码,试图习惯 NumPy 数组的 C API。
#include <Python.h>
#include "numpy/arrayobject.h"
#include <stdio.h>
#include <stdbool.h>
static char doc[] =
"Document";
static PyArrayObject *
trace(PyObject *self, PyObject *args){
PyArrayObject *matin;
if (!PyArg_ParseTuple(args, "O!",&PyArray_Type, &matin))
return NULL;
printf("a");
return matin;
}
static PyMethodDef TraceMethods[] = {
{"trace", trace, METH_VARARGS, doc},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
inittrace(void)
{
(void) Py_InitModule("trace", TraceMethods);
import_array();
}
这是一个精简版。我只想能够获得一个类型的对象PyArrayObject
并将其返回。不幸的是,这也给出了 SegFault。
Linux,64 位,Python 2.7.1