Search code examples
pythonswigpydoc

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


The help page of a module generated with SWIG is not quite helpful. In fact, it doesn't even list the arguments of each function.

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)

Question: is there a way to tell swig --with some option-- to at least include the exact list of named arguments of each function?

I would like to get at least something as follows:

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

A higher-quality level of documentation in general would also be welcome.

I know that I can obtain this result if I rename an original function foo as _foo and then I manually define a new foo(). But is there some other, systematic and built-in, approach that achieves the same purpose?


This is the list of commands I have executed, taken from this 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 

File 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);
 }

File 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();

Solution

  • See the 36.10 Docstring Features in the SWIG documentation.

    In particular, the autodoc feature works well with your example. Just use:

    swig -python -features autodoc example.i
    

    Sample output:

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