python - PYCFunctionWithKeywords 从 python 中被错误调用

标签 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)

似乎解释器没有在 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/

相关文章:

python - Celery 升级 (3.1->4.1) - 由对等方重置连接

Python - 计算日期范围内的唯一标签

Python - 是否有更有效的方法来查找这些参数值?

python - 如何使用 python-docx 将存储在数据库中的二进制图像插入到 word 文档中?

python - PyUnicode字符串和C字符串之间的字符串转换是如何工作的?

python - asyncio 中的坏锁优化

python - 获取 ndArray/list 的所有其他索引

python-3.x - Python Unittest 在运行测试时不会隔离补丁

python - 如何在 C++ 中列出 Python 模块的所有函数名称?

python - 尝试写入 C 扩展中创建的 Numpy 数组时出现 SegFault