Python CApi中的PyArg_语法元组SegFaults
我正在写一段代码,试图习惯NumPy数组的C APIPython CApi中的PyArg_语法元组SegFaults,python,numpy,python-c-api,Python,Numpy,Python C Api,我正在写一段代码,试图习惯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){ PyArrayO
#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();
}
#包括
#包括“numpy/arrayobject.h”
#包括
#包括
静态字符doc[]=
“文件”;
静态PyArrayObject*
跟踪(PyObject*self,PyObject*args){
Pyarayobject*matin;
if(!PyArg_ParseTuple(args,“O!”,&PyArray_Type,&matin))
返回NULL;
printf(“a”);
回归模型;
}
静态PyMethodDef TraceMethods[]={
{“trace”,trace,METH_VARARGS,doc},
{NULL,NULL,0,NULL}
};
PyMODINIT_FUNC
初始跟踪(无效)
{
(void)Py_InitModule(“trace”,TraceMethods);
导入数组();
}
这是一个精简版。我只希望能够获取类型为PyArrayObject
的对象并将其返回。不幸的是,这也给出了一个错误
Linux,64位,Python 2.7.1,来源:
O
(对象)[PyObject*]将Python对象(无任何转换)存储在C对象指针中。因此,C程序接收传递的实际对象对象的引用计数不增加。存储的指针不为空
O代码>(对象)[typeobject,PyObject*]
将Python对象存储在C对象指针中。这类似于O
,但
你在退回一份偷来的参考资料。首先增加它。我不熟悉C语言的软件架构,但我猜当我不使用PYINCREF(python对象)增加引用时代码>然后垃圾收集器发现其引用为0,并释放该内存。(如果我错了,请纠正我)基本上是的,但调用GC的是refcount下降到0,而不是相反。