Python C extension: method signatures for documentation?

前端 未结 2 1554
不知归路
不知归路 2020-12-10 12:14

I am writing C extensions, and I\'d like to make the signature of my methods visible for introspection.

static PyObject* foo(PyObject *self, PyObject *args)          


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-10 12:45

    My usual approach to finding out about things like this is: "use the source".

    Basically, I would presume that the standard modules of python would use such a feature when available. Looking at the source (for example here) should help, but in fact even the standard modules add the prototype after the automatic output. Like this:

    torsten@pulsar:~$ python2.6
    >>> import fcntl
    >>> help(fcntl.flock)
    flock(...)
        flock(fd, operation)
    
        Perform the lock operation op on file descriptor fd.  See the Unix [...]
    

    So as upstream is not using such a feature, I would assume it is not there. :-)

    Okay, I just checked current python3k sources and this is still the case. That signature is generated in pydoc.py in the python sources here: pydoc.py. Relevant excerpt starting in line 1260:

            if inspect.isfunction(object):
                args, varargs, varkw, defaults = inspect.getargspec(object)
                ...
            else:
                argspec = '(...)'
    

    inspect.isfunction checks if the object the documentation is requested for is a Python function. But C implemented functions are considered builtins, therefore you will always get name(...) as the output.

提交回复
热议问题