How To Find The Number Of Parameters To A Python Function From C?
Solution 1:
Okay, so in the end I've discovered how to do it. User-defined Python functions have a member called func_code
(in Python 3.0+ it's __code__
), which itself has a member co_argcount
, which is presumably what Boost::Python extracts in the example given by Christophe.
The code I'm using looks like this (it's heavily based on a documentation example of how to walk a Python dictionary):
PyObject *key, *value;
int pos = 0;
while(PyDict_Next(pyDictionary, &pos, &key, &value)) {
if(PyCallable_Check(value)) {
PyObject* fc = PyObject_GetAttrString(value, "func_code");
if(fc) {
PyObject* ac = PyObject_GetAttrString(fc, "co_argcount");
if(ac) {
const int count = PyInt_AsLong(ac);
// we now have the argument count, do something with this function
Py_DECREF(ac);
}
Py_DECREF(fc);
}
}
}
Thanks anyway - that thread did indeed lead me in the right direction :)
Solution 2:
Maybe this is helpful? (not tested, there could be relevant pieces of information along this thread)...
Solution 3:
Your C code can call inspect.getargspec just like any Python code would (e.g. via PyObject_CallMethod or other equivalent ways) and get all the scoop about the signature of each function or other callable that it may care about.
Post a Comment for "How To Find The Number Of Parameters To A Python Function From C?"