Search code examples
pythonpydoc

Keyword-based documentation for CPython extensions


How CPython extensions should be written in order for pydoc to mention the arguments'name instead of (...)?

I've followed the official python tutorial about extending Python, and even for the keywdarg.parrot procedure I get:

$> pydoc kewdarg.parrot

parrot(...)
   Print a lovely skit to standard output.

whereas I would expect

parrot(voltage, state="a stiff", action="voom", type="Norwegian Blue")
   Print a lovely skit to standard output.

Solution

  • Looking at the sources of pydoc, if I'm not mistaken the stanza that produces the '...' is:

    if inspect.isfunction(object):
        args, varargs, varkw, defaults = inspect.getargspec(object)
        argspec = inspect.formatargspec(
            args, varargs, varkw, defaults, formatvalue=self.formatvalue)
        if realname == '<lambda>':
            title = '<strong>%s</strong> <em>lambda</em> ' % name
            argspec = argspec[1:-1] # remove parentheses
    else:
        argspec = '(...)'
    

    So, inspect.isfunction(object) is returning False in the case of CPython extensions. Since inspect.isfunction() checks that the object is a Python function, while C extension functions are considered builtins, then the above will return False and we get (...) in the output.