我正在用 C 编写一个 python 模块,我需要让它调用一个 Python 函数,其中一个参数由“引用”传递。最终结果应该是 Python 函数对参数所做的事情被保存到原始 C 变量中。
int *resume; // this int is actually passed in to this body of code
PyObject *resumeInt; // which was originally a C callback func for libnids, but
PyObject *ret; // I removed/rewrote most of this code for clarity
resumeInt = Py_BuildValue("i",-1);
ret = PyObject_CallFunction(tcpResumeFunc, "(O)", resumeInt);
*resume = PyInt_AsLong(resumeInt);
Py_DECREF(ret);
Py_DECREF(resumeInt);
为了测试,我让 tcpResumeFunc 表示的 Python 函数将传入的整数修改为 = 5。但是,当我在这段代码的末尾打印出 *resume 时,它保留了它的初始值 -1。我知道我误解了 API 的工作原理。有什么建议么?