Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWIG: Add comments to generated .py file

Using SWIG to generate a Python interface to a C++ application, is there any way to have it comment the functions in the generated .py file? I'm actually pulling in an entire .h file into the .i file, but for a small example:

%module example

bool do_something_interesting(int number, string text);

swig -python example.i Generates:

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

Ideally, I would like it to automatically add a comment with the original method signature

#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

But I would be perfectly amenable to writing my own comment somewhere (particularly if the comment could somehow be in the .h file). I thought %pythonprepend might be a possible solution, but it inserts code inside the function definition rather than before it.

EDIT: Here's what I came up with in the .h file. Not the prettiest thing ever but it'll do:

#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);
like image 835
kylewm Avatar asked Nov 03 '11 20:11

kylewm


People also ask

How does SWIG work Python?

In a nutshell, SWIG is a compiler that takes C/C++ declarations and creates a wrapper needed to access those declarations from other languages like Python, Tcl, Ruby etc. It normally required no changes in existing code and create an interface within a minute. Building interpreted interface for existing C programs.

Does Numpy use SWIG?

We use the SWIG %apply directive to apply the typemap for one-dimensional input arrays of type double to the actual prototype used by rms . Using numpy. i effectively, therefore, requires knowing what typemaps are available and what they do.

How do I create a swig interface file?

The most common format of a SWIG interface is as follows: %module mymodule %{ #include "myheader. h" %} // Now list ANSI C/C++ declarations int foo; int bar(int x); ... The module name is supplied using the special %module directive.

What is SWIG object Python?

The Simplified Wrapper and Interface Generator (SWIG) is an open-source software tool used to connect computer programs or libraries written in C or C++ with scripting languages such as Lua, Perl, PHP, Python, R, Ruby, Tcl, and other languages like C#, Java, JavaScript, Go, D, OCaml, Octave, Scilab and Scheme.


1 Answers

Look at the docstring feature in SWIG for Python. Probably putting the following line in your .i file

%feature("autodoc", "1")

would do what you want (see http://www.swig.org/Doc2.0/Python.html#Python_nn65 for more options)

like image 128
benpmorgan Avatar answered Oct 21 '22 05:10

benpmorgan