18

I'm trying to expose this function to Python using SWIG:

std::vector<int> get_match_stats();

And I want SWIG to generate wrapping code for Python so I can see it as a list of integers.

Adding this to the .i file:

%include "typemaps.i"
%include "std_vector.i"

namespace std
{
  %template(IntVector) vector<int>;
}

I'm running SWIG Version 1.3.36 and calling swig with -Wall and I get no warnings.

I'm able to get access to a list but I get a bunch of warnings when compiling with -Wall (with g++ (GCC) 4.2.4 ) the generated C++ code that say:

  warning: dereferencing type-punned pointer will break strict-aliasing rules

Am I exposing the function correctly? If so, what does the warning mean?


These are the lines before the offending line in the same function:

SWIGINTERN PyObject *_wrap_IntVector_erase__SWIG_0(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
  PyObject *resultobj = 0;
  std::vector *arg1 = (std::vector *) 0 ;
  std::vector::iterator arg2 ;
  std::vector::iterator result;
  void *argp1 = 0 ;
  int res1 = 0 ;
  swig::PySwigIterator *iter2 = 0 ;
  int res2 ;
  PyObject * obj0 = 0 ;
  PyObject * obj1 = 0 ;

  if (!PyArg_ParseTuple(args,(char *)"OO:IntVector_erase",&obj0,&obj1)) SWIG_fail;
  res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_std__vectorT_int_std__allocatorT_int_t_t, 0 |  0 );
  if (!SWIG_IsOK(res1)) {
    SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IntVector_erase" "', argument " "1"" of type '" "std::vector *""'"); 
  }
  arg1 = reinterpret_cast * >(argp1);

And this is the offending line:

  res2 = SWIG_ConvertPtr(obj1, SWIG_as_voidptrptr(&iter2), swig::PySwigIterator::descriptor(), 0);

More code follows that.

The warning generated when compiling with g++ 4.2.4 is:

swig_iss_wrap.cxx: In function ‘PyObject* _wrap_IntVector_erase__SWIG_0(PyObject*, PyObject*)’:
swig_iss_wrap.cxx:5885: warning: dereferencing type-punned pointer will break strict-aliasing rules
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Marcos Lara
  • 1,883
  • 5
  • 18
  • 20
  • More recent swig version still generate code that cause warnings like this. Use -fno-strict-aliasing to get g++ to accept the swig wrapper without issues. – J Quinn Nov 04 '21 at 00:13

2 Answers2

14
%template(IntVector) vector<int>;
Mr Fooz
  • 109,094
  • 6
  • 73
  • 101
  • That's what I tried and I get a bunch of warnings when compiling with g++. Any ideas? – Marcos Lara Nov 10 '08 at 19:54
  • 1
    What version of g++ are you using? I'm using 4.1.2 and I don't get any warnings, even with -Wall. I don't remember any warnings back when I was using 3.3 or so either. Note the extra "" in this post that's missing from the original post. – Mr Fooz Nov 11 '08 at 17:41
  • I'm using g++ 4.2.4 and trying to build with `-Wall -Werror -ansi -pedantic`. I just tried building it with just -Wall and still get the warnings. – Marcos Lara Nov 12 '08 at 09:27
  • This might be a new 4.2-ism. There have been some big changes under the hood with gcc 4.2. Could you post the generated line of code that's creating this warning? – Mr Fooz Nov 12 '08 at 14:02
  • Here is the code: http://stackoverflow.com/questions/276769/how-to-expose-stdvectorint-as-a-python-list-using-swig#287580 – Marcos Lara Nov 13 '08 at 17:22
  • See if you can hunt down the definition for SWIG_ConvertPtr (or run the cxx file through the gcc preprocessor to see what that expands to). I'm guessing that it's a macro that needs to be rewritten. Also, take a look at http://mail.opensolaris.org/pipermail/tools-gcc/2005-August/000047.html – Mr Fooz Nov 14 '08 at 06:28
  • 1
    Some of the older versions of SWIG generate sloppy code. GCC 4.2 doesn't like a lot of it. – Seth Johnson Oct 21 '09 at 20:31
0

I don't have much experience with Swig, but are you #including your C++ header file in your .i file? Try one (or both) of

%include "myvector.h"


%{
#   include "myvector.h"
%}
Fergal
  • 494
  • 3
  • 12