我正在尝试为 python3 编写一个 C 扩展模块,例如 foo
,并且我正在尝试定义可以采用关键字参数的方法。
static PyObject* fooImpl(PyObject*, PyObject*, PyObject*);
static PyObject* fooImpl2(PyObject, PyObject*);
static PyMethodDef fooMethods[] = {
{"foo_impl", (PyCFunction) fooImpl, METH_VARARGS | METH_KEYWORDS, "Some description"},
{"foo_impl2", fooImpl2, METH_VARARGS, "Some description"},
{NULL, NULL, 0, NULL}
};
PyObject* fooImpl(PyObject* self, PyObject* args, PyObject* kwds) {
static const char *keywordList[] = { "kw1", "kw2", NULL};
PyObject *input = nullptr;
PyObject *kw1Val = nullptr;
PyObject *kw2Val = nullptr;
PyObject *returnVal = nullptr;
int err = PyArg_ParseTupleAndKeywords(args, kwds, "O|OO",
const_cast<char**>(keywordList),
&input, &kw1Val, &kw2Val);
if (!err) {
return NULL;
}
//// Do something with args to compute returnVal
return returnVal;
}
当我在 python 中尝试此操作时,出现以下错误
>>> import foo as fp
>>> arg1 = ...
>>> arg2 = ...
>>> arg3 = ...
>>> a = fp.foo_impl(arg1, kw1 = arg2, kw2 = arg3);
TypeError: function takes at most 2 arguments (3 given)
似乎解释器没有在 PyMethodDef
中注册 METH_KEYWORDS
标志。是否有其他方法可以将 PyCFunctionWithKeywords 方法添加到 Python3 中的 C 扩展。我找到的唯一来源是 this stackoverflow 帖子可追溯到 Python 文档 here
非常感谢任何帮助
最佳答案
您没有定义所有关键字。即使参数是非可选的,它仍然需要定义一个名称,因此可以通过关键字或位置传递(因此 PyArg_ParseTupleAndKeywords
可以将位置与关键字匹配,以防可选参数按位置传递)。基本上,关键字名称的数量必须始终与要解析的参数的最大数量相匹配。
更改:
static const char *keywordList[] = { "kw1", "kw2", NULL};
至:
static const char *keywordList[] = { "input", "kw1", "kw2", NULL};
显然,您可以随意命名第一个参数;我只是匹配了 C 变量名称。
关于python - PYCFunctionWithKeywords 从 python 中被错误调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34645136/