在Python的C-扩展狮身人面像车博士功能 [英] Sphinx autodoc functionality on Python-C-Extension

查看:123
本文介绍了在Python的C-扩展狮身人面像车博士功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有困难的时候得到狮身人面像的车博士功能与我的Python C扩展工作。这甚至可能与狮身人面像?我应该使用另一种工具?

我一直没能找到这样做的任何信息。就在这个(<一个href=\"http://stackoverflow.com/questions/21691772/sphinx-documentation-via-autodoc-for-c-python-modules\">Sphinx车博士通过对C Python模块),但这个问题的文件无人接听。

我可以让狮身人面像车博士做美丽的东西,我与Python code,但我无法弄清楚如何复制我的C文件的成功。


解决方案

在为了做到这一点,你的C扩展类型的对象应该有一个描述他们(PyTypeObject.tp_doc)和方法(PyMethodDef)一个文档字符串应该包括一个文档字符串为好。

  {go_do_stuff
    (PyCFunction)Foo_Go_Do_Stuff,METH_VARARGS | METH_KEYWORDS,
    的方式得到的东西做的。},

一个Python的C扩展通常被打包成与动态加载它的包装文件一个鸡蛋。首先要确保 PIP安装它。然后,你需要包含装载机的目录添加到您的狮身人面像sys.path中。修改 conf.py

<$p$p><$c$c>sys.path.append(os.path.abspath('/usr/local/lib/python2.7/site-packages/foo-0.9.99-py2.7-macosx-10.9-x86_64.egg/'))

这将是包括装载机路径 foo.py

 高清__bootstrap __():
  全球__bootstrap__,__loader__,__FILE__
  进口SYS,通过pkg_resources,IMP
  __file__ = pkg_resources.resource_filename(__ name__,foo.so')
  __loader__ =无;德尔__bootstrap__,__loader__
  imp.load_dynamic(__ __名,__ FILE__)
__bootstrap __()

现在,您可以创建一个 foo.rst 通过如下声明:

  ************ *************
:MOD:`foo` ---富客户端
************************************************** *.. automodule :: foo的
   :简介:富客户端。
   :成员:

当您运行狮身人面像文档字符串将得到记录。

I'm having a difficult time getting the Sphinx autodoc functionality to work with my Python C Extension. Is this even possible with Sphinx? Should I be using another tool?

I haven't been able to find any information on doing this. Just this (Sphinx documentation via autodoc for C Python modules), but that question went unanswered.

I can make Sphinx autodoc do beautiful things for me with Python code, but I can't figure out how to replicate the success with my C files.

解决方案

In order to do that, your C extension type objects should have a doc string that describes them (PyTypeObject.tp_doc) and the methods (PyMethodDef) should include a doc string as well.

    {"go_do_stuff",
    (PyCFunction) Foo_Go_Do_Stuff, METH_VARARGS | METH_KEYWORDS,
    "The way to get stuff done."},

A Python C extension usually comes packaged as an egg with a wrapper file that dynamically loads it. Make sure to first pip install it. Then, you need to add the directory containing that loader to your Sphinx sys.path. Edit your conf.py:

sys.path.append(os.path.abspath('/usr/local/lib/python2.7/site-packages/foo-0.9.99-py2.7-macosx-10.9-x86_64.egg/'))

This would be the path that includes the loader foo.py:

def __bootstrap__():
  global __bootstrap__, __loader__, __file__
  import sys, pkg_resources, imp
  __file__ = pkg_resources.resource_filename(__name__, 'foo.so')
  __loader__ = None; del __bootstrap__, __loader__
  imp.load_dynamic(__name__,__file__)
__bootstrap__()

Now you can create a foo.rst with the following declaration:

***************************************************
:mod:`foo` --- Foo Client
***************************************************

.. automodule:: foo
   :synopsis: Foo client.
   :members:

When you run Sphinx the doc strings will get documented.

这篇关于在Python的C-扩展狮身人面像车博士功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆