In an embedded python scenario we are using PyArg_ParseTupleAndKeywords to receive data from Python (version >=3 .x) and use it in a C++ application.
At the moment we have a similar setup:
PyObject* whatever(PyObject *self, PyObject *args, PyObject *keywds) {
....
static char* kwlist[] = { "foo", "bar", NULL };
...
if(!PyArg_ParseTupleAndKeywords(args, keywds, ..., kwlist, ...))
{
...bail out
however, if we pass more parameters than the two expected (e.g. issuing a python call like whatever(foo="a", bar="b", baz="c")) the whole thing crashes (not really, it returns an error, but that’s beyond the scope here).
We would like to avoid such scenarios; it would be great if we could parse only the parameters in the kwlist and ignore everthing else. What’s the best way to do it?
One solution we were thinking about was to convert kwlist into a dict, then manipulate it with PyDict_Merge or the like.
In the end we solved it like below:
(I’m answering my own question since nobody answered, and I think it might be valuable to somebody else in the future).