1

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();
Patrick Trentin
  • 7,126
  • 3
  • 23
  • 40
  • Another option is to use doxygen to generate `.xml` output and use `doxy2swig.pl` to generate a comprehensive documentation, which can be included in your `.i` file. See e.g. https://github.com/m7thon/doxy2swig – Jens Munk Mar 20 '19 at 18:15
  • @JensMunk please post it as an answer, possibly with a mcve, so that I can upvote your suggestion and it can be more visible. – Patrick Trentin Mar 20 '19 at 18:29
  • I will do that, it is pretty nice – Jens Munk Mar 20 '19 at 20:22

2 Answers2

2

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
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
1

Another alternative is to use doxy2swig.py, see e.g. http://github.com/m7thon/doxy2swig

Header using 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();

Interface file example.i

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

 %include "documentation.i"

 %include "example.h"

To SWIG and compile, do the following. This can of course be setup nicely using automake or CMake if one prefers.

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

In Python, the documentation appears like

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 *

It generalizes to documentation for classes and it is quite flexible.

Jens Munk
  • 4,627
  • 1
  • 25
  • 40
  • this looks more promising. But the github page says its deprecated with -doxygen and %feature("autodoc","1") though it doesn't give the desired result like the above answer. Do you happen to know the alternate without doxy2swig? – Maverickgugu Feb 27 '23 at 20:04
  • 1
    No. I try to avoid `autodoc`. It is an attempt to auto-generate documentation and it will override the doxygen comments that you provide. The `doxy2swig.py` is neat in the sense that it simply inserts comments into your `.i` file. You can customize it in any way you like – Jens Munk Mar 01 '23 at 19:40
  • Thank you @Jens. I was experimenting with autodoc and realized the override. Also, How do you generally handle interfaces modified with typemaps? I just started a separate question on the same: https://stackoverflow.com/q/75598695/420157 – Maverickgugu Mar 01 '23 at 20:46