4

I am trying to document pycurl which is largely a C extension module.

In this extension module there are some classes, written in C. They have methods. The methods have docstrings defined on them:

>>> help(pycurl.Curl().close)
Help on built-in function close:

close(...)
    close() -> None.  Close handle and end curl session.

But, these methods are not defined on classes themselves:

>>> 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'

As a result, when I try to use automethod to document them, sphinx says these methods do not exist:

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

How can I get sphinx to take docstrings from methods defined in a C extension module?

3
  • 1
    I've answered what seems to be the same questions here: stackoverflow.com/a/30110104/582436 May 7, 2015 at 19:36
  • @RonenBotzer it seems your answer has been moved or deleted. Jan 30, 2020 at 0:21
  • Isn’t that great. Sorry answered that several years ago, so I’m not able to reproduce it. Thanks for the heads up. Jan 30, 2020 at 3:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.