Python PyList_GetItem不是幂等元

Python PyList_GetItem不是幂等元,python,swig,idempotent,Python,Swig,Idempotent,我正在尝试Swig,并分别具有以下C代码和接口: // example.c #include <Python/Python.h> PyObject *test ( PyObject *self, int i) { PyObject **x; x = malloc(sizeof(PyObject *)); *x = PyList_GetItem(self, i); return *x; } // example.i %module example %{

我正在尝试Swig,并分别具有以下C代码和接口:

// example.c
#include <Python/Python.h>
PyObject *test ( PyObject *self, int i) {
    PyObject **x;
    x = malloc(sizeof(PyObject *));
    *x = PyList_GetItem(self, i);
    return *x;
}

// example.i
%module example
%{
/* Put header files here or function declarations like below */
extern PyObject* test(PyObject *self, int i);
%}

extern PyObject* test(PyObject *self, int i);
//example.c
#包括
PyObject*测试(PyObject*self,int i){
PyObject**x;
x=malloc(sizeof(PyObject*));
*x=PyList_GetItem(自我,i);
返回*x;
}
//例如,我
%模块示例
%{
/*将头文件放在此处或函数声明如下*/
外部PyObject*测试(PyObject*self,inti);
%}
外部PyObject*测试(PyObject*self,inti);
它编译正确,我可以很好地导入扩展模块。事实上,当我第一次将变量a定义为[{1:1},{2:2}]并执行example.test(a,0)时,它会正确地返回{1,1}。当我在pythonshell中输入a时,我得到了预期的[{1:1},{2:2}]。当我再次尝试example.test(a,0)时,我得到了一个分段错误。知道为什么会这样吗

PyObject*PyList\u GetItem(PyObject*列表,Py\u ssize\u t索引)
返回值:借用的引用

在返回对象之前递增该对象