I have Python C-Extension function that takes a callable as parameter to act as a callback.
I have two types of callbacks, each one with different number of arguments.
I want to detect the callback type according to the count of its arguments.
Is there a direct way as I already have the PyObject*
here, instead of calling inspect
module ?
static PyObject *RegisterCallback(PyObject *self, PyObject *args, PyObject *kwdict)
{
PyObject* callback;
static char *kwlist[] = { "callback", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O", kwlist, &callback))
return NULL;
if(!PyCallable_Check(callback))
{
PyErr_SetString(PyExc_ValueError, "The callback should be callable function or callable object.");
return NULL;
}
int args_count = ????
bool registration_result = register_the_callback(callback);
if (registration_result )
{
Py_RETURN_TRUE;
}
else
Py_RETURN_FALSE;
}
Note:
Targeted Python: 2.7, 3.x.
N.B: I already know that python 2.x is dead and we keep it due to customer's request.
question from:
https://stackoverflow.com/questions/65894002/get-callable-arguments-count-in-cpython 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…