Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWIG wrapping C++ for Python: translating a list of strings to an STL vector of STL strings

I would like to wrap a C++ function with SWIG which accepts a vector of STL strings as an input argument:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

void print_function(vector<string> strs) {
  for (unsigned int i=0; i < strs.size(); i++)
  cout << strs[i] << endl;
}

I want to wrap this into a Python function available in a module called `mymod':

/*mymod.i*/
%module mymod
%include "typemaps.i"
%include "std_string.i"
%include "std_vector.i"

%{
 #include "mymod.hpp"
%}

%include "mymod.hpp"

When I build this extension with

from distutils.core import setup, Extension

setup(name='mymod',
  version='0.1.0',
  description='test module',
  author='Craig',
  author_email='balh.org',
  packages=['mymod'],
  ext_modules=[Extension('mymod._mymod',
                         ['mymod/mymod.i'],
                         language='c++',
                         swig_opts=['-c++']),
                         ],
  )

and then import it and try to run it, I get this error:

Python 2.7.2 (default, Sep 19 2011, 11:18:13) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mymod
>>> mymod.print_function("hello is seymour butts available".split())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: in method 'print_function', argument 1 of type 'std::vector<  std::string,std::allocator< std::string > >'
>>> 

I'm guessing this is saying that SWIG doesn't provide a default typemap for translating between a Python list of Python strings and a C++ STL vector of STL strings. I feel like this is something they might provide somewhere by default, but perhaps I don't know the right file to include. So how can I get this to work?

Thanks in advance!

like image 323
involucelate Avatar asked Dec 12 '11 01:12

involucelate


2 Answers

You need to tell SWIG that you want a vector string typemap. It does not magically guess all the different vector types that can exist.

This is at the link provided by Schollii:

//To wrap with SWIG, you might write the following:

%module example
%{
#include "example.h"
%}

%include "std_vector.i"
%include "std_string.i"

// Instantiate templates used by example
namespace std {
   %template(IntVector) vector<int>;
   %template(DoubleVector) vector<double>;
   %template(StringVector) vector<string>;
   %template(ConstCharVector) vector<const char*>;
}

// Include the header file with above prototypes
%include "example.h"
like image 97
Demolishun Avatar answered Nov 18 '22 08:11

Demolishun


SWIG does support passing a list to a function that takes a vector as value, or a const vector reference. The example at http://www.swig.org/Doc2.0/Library.html#Library_std_vector shows this, I can't see anything wrong with what you posted. Something else is wrong; DLL found by python was't the latest, the using namespace std in the header is confusing the SWIG wrapper code that does the type checking (note that "using namespace" statements in .hpp is a no-no in general as it pulls everything from std into global namespace), etc.

like image 42
Oliver Avatar answered Nov 18 '22 09:11

Oliver