我试图记录pycurl,它主要是一个C扩展模块 .
在这个扩展模块中有一些用C语言编写的类 . 它们有方法 . 这些方法在其上定义了docstrings:
>>> help(pycurl.Curl().close)
Help on built-in function close:
close(...)
close() -> None. Close handle and end curl session.
但是,这些方法没有在类本身上定义:
>>> dir(pycurl.Curl)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> pycurl.Curl.close
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'builtin_function_or_method' object has no attribute 'close'
因此,当我尝试使用automethod来记录它们时,sphinx说这些方法不存在:
.. automethod:: pycurl.Curl.close
curlobject.rst:13: WARNING: autodoc: failed to import method u'Curl.close' from module u'pycurl'; the following exception was raised:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/Sphinx-1.2-py2.7.egg/sphinx/ext/autodoc.py", line 342, in import_object
obj = self.get_attr(obj, part)
File "/usr/local/lib/python2.7/site-packages/Sphinx-1.2-py2.7.egg/sphinx/ext/autodoc.py", line 241, in get_attr
return safe_getattr(obj, name, *defargs)
File "/usr/local/lib/python2.7/site-packages/Sphinx-1.2-py2.7.egg/sphinx/util/inspect.py", line 114, in safe_getattr
raise AttributeError(name)
AttributeError: close
如何让sphinx从C扩展模块中定义的方法中获取文档字符串?