我正在尝试为 numpy 编写一个 C 扩展来对大数组进行一些计算,但是当我使用 "PyArray_SimpleNew" 或 "PyArray_FromDims" 来获取 PyArrayObject 时,我遇到了一个大问题。这是我的代码:
#include<stdio.h>
#include "Python.h"
#include"arrayobject.h"
static PyObject *MyExtGFV(PyObject *self, PyObject *args)
{
npy_intp dims = 1;
PyArray_SimpleNew(1, &dims, PyArray_FLOAT32);
retuurn Py_BuildValue("i", 1);
}
static PyMethodDef my_ext_methods[] =
{
{"GFV", MyExtGFV, METH_VARARGS, "used to generate feature vectors"},
{NULL, NULL}
}
PyMODINIT_FUNC initMyExt(void)
{
Py_InitModule("MyExt", my_ext_methods);
}
为了调试它,我删除了函数 MyExtGFV() 中的大部分代码,只留下
PyArray_SimpleNew(1, &dims, PyArray_FLOAT32);
在其中,但是当我导入它并在我的 python 代码中使用它时,它说“python 已停止工作”。这个问题我google了很久,但好像没有人有同样的问题,这几乎让我发疯,任何帮助将不胜感激!!!