我想在Python中集成C模块,因此我选择了接口Python.h
。一切编译没有错误和警告,所以我不明白是什么问题。你知道吗
C侧:
#include <python3.5m/Python.h>
...
#define PyInt_AsLong(x) (PyLong_AsLong((x)))
typedef PyObject* Py;
static Py getSumma(Py self, Py args){
Py nums;
if (!PyArg_ParseTuple(args, "O", &nums)){
return NULL;
}
size_t numsAmount = PyList_Size(args);
int32_t summa = 0;
for (size_t i = 0; i < numsAmount; i++){
Py temp = PyList_GetItem(nums, i);
int32_t num = PyInt_AsLong(temp);
summa += num;
}
return Py_BuildValue("l", summa);
}
static PyMethodDef moduleMethods[] = {
{"getSumma", (PyCFunction)getSumma, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL}
};
static PyModuleDef SummaLogic = {
PyModuleDef_HEAD_INIT,
"SummaLogic",
"",
-1,
moduleMethods
};
PyMODINIT_FUNC PyInit_SummaLogic(void){
return PyModule_Create(&SummaLogic);
}
你知道吗设置.py地址:
from distutils.core import setup, Extension
SummaLogic = Extension("SummaLogic", sources=['SummaLogic.c'])
setup(ext_modules=[SummaLogic])
Python方面:
from SummaLogic import getSumma
if __name__ == "__main__":
a = [1, 2, 3]
b = getSumma(a)
print(b)
这似乎是对的,但当我在终端启动它-什么都没有发生,只是挂着没有任何活动。我会错过什么?你知道吗
它归结为^{} ,您不需要在那里检查错误。你知道吗
您可能想在
nums
上使用它,而不是将args
用作参数。但是你用在args
上,发生了一件非常有趣的事情:args
是tuple
PyList_Size
失败并返回^{-1
转换成一个无符号size_t
的-1
可能会产生一个非常大的数字,可能2**64-1
2**64-1
项需要相当长的时间(除了所有的越界内存访问)。你知道吗快速解决方法是使用:
但是,您应该检查错误条件是什么,并在每次pythoncapi函数调用之后测试它们,否则您将得到许多未定义的行为。另外,我可能会坚持使用已定义的返回类型,而不是
int32_t
(PyInt_AsLong
返回long
,因此您可能会在那里得到奇怪的转换错误!),size_t
。。。对于经常编写C扩展的人来说,typedef PyObject* Py;
使事情变得非常棘手。你知道吗相关问题 更多 >
编程相关推荐