SWIG:为生成的.py文件添加注释

时间:2011-11-03 20:00:26

标签: c++ python comments swig

使用SWIG为C ++应用程序生成Python接口,有没有办法让它在生成的.py文件中注释函数?我实际上将整个.h文件放入.i文件中,但是只是一个小例子:

%module example

bool do_something_interesting(int number, string text);

swig -python example.i生成:

...
def do_something_interesting(*args):
  return _example.do_something_interesting(*args)
do_something_interesting = _example.do_something_interesting

理想情况下,我希望自动添加带有原始方法签名的评论

#bool do_something_interesting(int number, string text)
def do_something_interesting(*args):
  return _example.do_something_interesting(*args)
do_something_interesting = _example.do_something_interesting

但我完全可以在某处写自己的评论(特别是如果评论可能以某种方式在.h文件中)。我认为%pythonprepend可能是一种可能的解决方案,但它会在函数定义中而不是在函数定义之前插入代码。

编辑:以下是我在.h文件中提到的内容。不是最漂亮的东西,但它会做:

#ifdef SWIG
   %pythonprepend do_something_interesting(int, string) %{
    """Do some interesting thing

    number:Int -- a number
    text:String -- some text

    """
  %}
#endif
bool do_something_interesting(int number, string text);

1 个答案:

答案 0 :(得分:6)

查看SWIG for Python中的docstring功能。 可能将以下行放在.i文件中

%feature("autodoc", "1")

会做你想做的事情(有关更多选项,请参阅http://www.swig.org/Doc2.0/Python.html#Python_nn65

相关问题