#1210 %shared_ptr(std::vector) broken

python (259)

This concerns python bindings produced by SWIG 2.0.4. When std::vector<T> is instantiated after having been marked as %shared_ptr, the implementation of some methods (e.g. __setitem__) is incorrect. Here's a small test module that shows the problem:
%module fail

%include "boost_shared_ptr.i"
%include "std_vector.i"

#include <vector>
#include "boost/shared_ptr.hpp"

%template(IntVec) std::vector<int>;

When exercising the generated code, I get:

>>> import fail
>>> v = fail.IntVec()
>>> v.append(0)
>>> v.append(1)
>>> v.append(2)
>>> v[0] = v[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "fail.py", line 117, in __setitem__
def __setitem__(self, *args): return _fail.IntVec___setitem__(self, *args)
NotImplementedError: Wrong number or type of arguments for overloaded function 'IntVec___setitem__'.
Possible C/C++ prototypes are:
std::vector< int >::__setitem__(PySliceObject *,std::vector< int,std::allocator< int > > const &)
std::vector< int >::__setitem__(PySliceObject *)
std::vector< int >::__setitem__(std::vector< int >::difference_type,std::vector< int >::value_type const &)

I don't understand the SWIG standard library well enough to fix this, but my guess is that the problem comes from std/std_vector.i containing the line:

%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<Type_, Alloc_ >);

prior to the declaration of std::vector methods. Presumably that overwrites some of the typemaps set up by %shared_ptr and causes wonky argument type-checking code to get generated. In case it helps understand the problem, my current work-around is below. The strategy is to introduce a partial specialization of std::vector, with the %typemap_traits_ptr invocation moved to the end of the class.

%module example

%include "boost_shared_ptr.i"
%include "std_vector.i"

%define %shared_vec(NAME, TYPE...)
namespace std {
template <class _Alloc >
class vector<TYPE, _Alloc > {
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef TYPE value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef TYPE& reference;
typedef const TYPE& const_reference;
typedef _Alloc allocator_type;


%fragment(SWIG_Traits_frag(std::vector<TYPE, _Alloc >), "header",
fragment="StdVectorTraits") {
namespace swig {
template <> struct traits<std::vector<TYPE, _Alloc > > {
typedef pointer_category category;
static const char* type_name() {
return "std::vector<" #TYPE " >";

%swig_vector_methods(std::vector<TYPE, _Alloc >);

%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<TYPE, _Alloc >);

%shared_ptr(std::vector<TYPE >);
%template(NAME) std::vector<TYPE >;


%shared_vec(IntVec, int);


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks