从python

时间:2016-01-06 23:59:36

标签: python python-3.x python-c-api python-c-extension

我正在尝试为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)

似乎解释器没有在METH_KEYWORDS中注册PyMethodDef标志。是否有其他方法可以在Python3中向C-extension添加PyCFunctionWithKeywords方法。我找到的唯一来源是this stackoverflow帖子,可以追溯到Python文档here

非常感谢任何帮助

1 个答案:

答案 0 :(得分:2)

您没有定义所有关键字。即使参数是非可选的,它仍然需要定义一个名称,因此它可以通过关键字或位置传递(因此PyArg_ParseTupleAndKeywords可以匹配位置和关键字,以防可选参数按位置传递)。基本上,关键字名称的数量必须始终与要解析的最大参数数量相匹配。

变化:

static const char *keywordList[] = { "kw1", "kw2", NULL};

为:

static const char *keywordList[] = { "input", "kw1", "kw2", NULL};

显然,无论你想要什么,你都可以命名第一个参数;我只是匹配了C变量名。