2

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.
4

1 回答 1

1

查看pydoc 的来源,如果我没记错的话,产生 '...' 的节是:

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 = '(...)'

因此,inspect.isfunction(object)在 CPython 扩展的情况下返回 False。由于 inspect.isfunction() 检查对象是Python 函数,而 C 扩展函数被认为是内置函数,所以上面将返回 False 并且我们得到(...)输出。

于 2015-03-19T12:28:22.007 回答