我需要让我的Python程序访问我的C函数。所以我用example from the documentation开始
不幸的是,它非常慢,占用了我所有的记忆
我使用的是64位windows7、python3.4和gcc(rubenvb-4.8.0)4.8.0
为完整起见,以下是文档中的代码:
#include <Python.h>
static int numargs=0;
static PyObject*
emb_numargs(PyObject *self, PyObject *args)
{
if(!PyArg_ParseTuple(args, ":numargs"))
return NULL;
return PyLong_FromLong(numargs);
}
static PyMethodDef EmbMethods[] = {
{"numargs", emb_numargs, METH_VARARGS,
"Return the number of arguments received by the process."},
{NULL, NULL, 0, NULL}
};
static PyModuleDef EmbModule = {
PyModuleDef_HEAD_INIT, "emb", NULL, -1, EmbMethods,
NULL, NULL, NULL, NULL
};
static PyObject*
PyInit_emb(void)
{
return PyModule_Create(&EmbModule);
}
int main()
{
[...]
numargs = argc;
PyImport_AppendInittab("emb", &PyInit_emb);
Py_Initialize();
pModule = PyImport_Import(pName);
[...]
}
python程序具有以下代码:
import emb
print("Number of arguments", emb.numargs())
我花了3个小时解决这个问题。我在this page找到了答案
显然,您需要定义以下内容:
The page说“这确保了类型Py\u ssize\u t变成了\uuuu int64而不是int。”
相关问题 更多 >
编程相关推荐