I'm writing some C level function, it should accept a callable, *args
and **kwargs
, print something and call the callable with arguments. It's signature should be reflecting this one from Python:
def my_function(callable, *args, **kwargs): ...
Here's the C code that I have:
static PyObject *call_c(PyObject *self, PyObject *args, PyObject *keywords) {
PyObject *f;
PyObject *arguments;
if (!PyArg_ParseTuple(args, "O|O", &f, &arguments))
{
PyErr_SetString(PyExc_ValueError, "invalid args");
return NULL;
}
printf("Calling...\n");
return PyObject_Call(f, arguments, keywords);
}
Here's how I'm trying to execute it:
def printer(value="test"):
print(f"printed: {value}")
def call_c_wrapper(callable, *args, **kwargs):
call_c(callable, args, **kwargs)
call_c(print, "some print value")
# Calling...
# Segmentation fault
call_c(printer, value="test")
# Traceback (most recent call last):
# File "main.py", line 35, in <module>
# call_c(printer, value="test")
# TypeError: call_c() takes no keyword arguments
call_c_wrapper(print, "some print value")
# > this is the only one that works:
# Calling...
# some print value
call_c_wrapper(printer, value="my value")
# > same as `call_c` with kwargs:
# Traceback (most recent call last):
# File "main.py", line 37, in <module>
# call_c_wrapper(printer, value="my value")
# File "main.py", line 32, in call_c_wrapper
# call_c(callable, args, **kwargs)
# TypeError: call_c() takes no keyword arguments
What I'm doing wrong with the code? The *args
parsing seems somewhat working, I'm OK with using a wrapper function if that's necessary, but why **kwargs
doesn't work at all?
Edit
I've changed the PyMethodDef
from METH_VARARGS
to METH_VARARGS | METH_KEYWORDS
, so call_c_wrapper()
now works fine.
The call_c
with args
still gives me SegmentationFault
, and the one with kwargs
raises MemoryError
.