I am trying to write a C-extension module for python3, say foo
and I am trying to define method that can take keyword arguments.
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;
}
When I try this within python, I get the following error
>>> 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)
Seems like the interpreter is not registering the METH_KEYWORDS
flag in the PyMethodDef
. Is there someother way to add PyCFunctionWithKeywords
methods to C-extension in Python3 . The only source I found was this stackoverflow post that goes back to the Python documentation here
Any help is deeply appreciated