Python C 扩展关键字参数

Python C Extension Keyword arguments

问题描述:

当我调用这样定义的方法时:

static PyMethodDef Parser_methods[] = {
    {"read", (PyCFunction)Parser_read, METH_KEYWORDS, "read from input source"},
    {NULL, NULL, 0, NULL} 
};

static PyObject *
Parser_read(Parser * const self, PyObject * unused0, PyObject * unused1)
{
...
}

我得到:

SystemError: Bad call flags in PyCFunction_Call. METH_OLDARGS is no longer supported!

代码在 Python2 上运行良好,但在 Python3

上崩溃

Parser_read应该是

static PyObject* Parser_read(PyObject *self, PyObject *args)

可能是这个错误...

http://bugs.python.org/issue11587

这意味着这是一个 python 版本问题。一种解决方法似乎是使用 METH_KEYWORDS | METH_VARARGS.