How to find the number of parameters to a Python function from C?

后端 未结 3 431
没有蜡笔的小新
没有蜡笔的小新 2021-01-20 05:13

I\'m using the Python C API to call Python functions from my application. I\'d like to present a list of functions that could be called and would like to be able to limit th

相关标签:
3条回答
  • 2021-01-20 05:19

    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 :)

    0 讨论(0)
  • 2021-01-20 05:35

    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.

    0 讨论(0)
  • 2021-01-20 05:40

    Maybe this is helpful? (not tested, there could be relevant pieces of information along this thread)...

    0 讨论(0)
提交回复
热议问题