如何强制 swig 在模块的帮助页面中生成适当的参数列表?

How to force swig to generate the appropriate list of arguments in the help page of a module?

SWIG 生成的模块的 help 页面不是很有帮助。事实上,它甚至没有列出每个函数的参数。

Help on module example:

NAME
    example

FILE
    /home/anon/example.py

DESCRIPTION
    # This file was automatically generated by SWIG (http://www.swig.org).
    # Version 3.0.12
    #
    # Do not make changes to this file unless you know what you are doing--modify
    # the SWIG interface file instead.

FUNCTIONS
    fact(...)

    get_time(...)

    my_mod(...)

DATA
    cvar = <Swig global variables>

(END)

问题: 有没有办法告诉 swig - 有一些选择 - 至少 包括每个函数的命名参数的确切列表?

我希望至少得到如下信息:

...
fact(n)
...
my_mod(x, y)
...

通常也欢迎更高质量的文档。

我知道如果我将一个原始函数foo重命名为_foo然后我手动定义一个新的foo(),我可以获得这个结果。但是是否有其他一些系统的和内置的方法可以达到同样的目的?


这是我执行过的命令列表,取自 tutorial:

 ~$ swig -python example.i
 ~$ gcc -fPIC -c example.c example_wrap.c \
        -I/usr/include/python2.7
 ~$ ld -shared example.o example_wrap.o -o _example.so 

文件example.c:

 /* File : example.c */

 #include <time.h>
 double My_variable = 3.0;

 int fact(int n) {
     if (n <= 1) return 1;
     else return n*fact(n-1);
 }

 int my_mod(int x, int y) {
     return (x%y);
 }

 char *get_time()
 {
     time_t ltime;
     time(&ltime);
     return ctime(&ltime);
 }

文件example.i:

 /* example.i */
 %module example
 %{
 /* Put header files here or function declarations like below */
 extern double My_variable;
 extern int fact(int n);
 extern int my_mod(int x, int y);
 extern char *get_time();
 %}

 extern double My_variable;
 extern int fact(int n);
 extern int my_mod(int x, int y);
 extern char *get_time();

请参阅 SWIG 文档中的 36.10 Docstring Features

特别是,autodoc 功能非常适合您的示例。只需使用:

swig -python -features autodoc example.i

示例输出:

>>> import example
>>> help(example)
Help on module example:

NAME
    example

DESCRIPTION
    # This file was automatically generated by SWIG (http://www.swig.org).
    # Version 3.0.12
    #
    # Do not make changes to this file unless you know what you are doing--modify
    # the SWIG interface file instead.

FUNCTIONS
    fact(n)
        fact(int n) -> int

    get_time()
        get_time() -> char *

    my_mod(x, y)
        my_mod(int x, int y) -> int

DATA
    cvar = <Swig global variables>

FILE
    c:\example\example.py

另一种选择是使用 doxy2swig.py,参见例如http://github.com/m7thon/doxy2swig

Header 使用 doxygen example.h

#pragma once

extern double My_variable; ///< My variable for something

/**
 * Factorial function
 *
 * @param n
 *
 * @return n!
 */
extern int fact(int n);

/**
 * Module function
 *
 * @param x
 * @param y
 *
 * @return
 */
extern int my_mod(int x, int y);

/**
 * Get the current time
 *
 *
 * @return string representation of time
 */
extern char *get_time();

接口文件example.i

 %module example
 %{
 /* Put header files here or function declarations like below */
 #include "example.h"
 %}

 %include "documentation.i"

 %include "example.h"

要 SWIG 和编译,请执行以下操作。如果愿意,这当然可以使用 automake 或 CMake 很好地设置。

doxygen -g
sed -i 's/GENERATE_XML           = NO/GENERATE_XML = YES/g' Doxyfile
python doxy2swig.py -c -a ./xml/index.xml documentation.i
swig -python example.i
gcc -fPIC -c example.c example_wrap.c -I/usr/include/python2.7
ld -shared example.o example_wrap.o -o _example.so

在Python中,文档显示为

In [1]: import example
In [2]: help(example.get_time)

Help on function get_time in module example:

get_time()
    Get the current time

    Returns
    -------
    string representation of time

        get_time() -> char *

它概括为 类 的文档并且非常灵活。