I have a SWIG generated function as follows:
SWIGINTERN PyObject *_wrap_StrVector___getitem____SWIG_0(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
std::vector< std::string > *arg1 = (std::vector< std::string > *) 0 ;
PySliceObject *arg2 = (PySliceObject *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
PyObject * obj0 = 0 ;
PyObject * obj1 = 0 ;
std::vector< std::string,std::allocator< std::string > > *result = 0 ;
if (!PyArg_ParseTuple(args,(char *)"OO:StrVector___getitem__",&obj0,&obj1)) SWIG_fail;
res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_std__vectorT_std__string_std__allocatorT_std__string_t_t, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "StrVector___getitem__" "', argument " "1"" of type '" "std::vector< std::string > *""'");
}
arg1 = reinterpret_cast< std::vector< std::string > * >(argp1);
{
if (!PySlice_Check(obj1)) {
SWIG_exception_fail(SWIG_ArgError(SWIG_TypeError), "in method '" "StrVector___getitem__" "', argument " "2"" of type '" "PySliceObject *""'");
}
arg2 = (PySliceObject *) obj1;
}
try {
result = (std::vector< std::string,std::allocator< std::string > > *)std_vector_Sl_std_string_Sg____getitem____SWIG_0(arg1,arg2);
}
catch(std::out_of_range &_e) {
SWIG_exception_fail(SWIG_IndexError, (&_e)->what());
}
*****1*****
***//I want to modify or print variable result here like printf("%s", result->c_str());***
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_std__vectorT_std__string_std__allocatorT_std__string_t_t, 0 | 0 );
return resultobj;
fail:
return NULL;
}
I need to print the variable result at position 1(as mentioned in the code). Typemap(argout) doesn’t seem to be f much help here.
I’m assuming here that the function you want to modify is a
std::vector<std::string>‘s__getitem__routine in Python.The easiest and safest way to modify or intercept the result of the returned value is actually to do it on the Python side, using
%feature("pythonappend"), e.g.:The reason this is the easiest way of modifying the result is that the next easiest way of modifying the result requires altering the typemap for the return type – in this case
std::string. To do that you’d end up altering the existingstd::stringtypemap, which gets somewhat messy.Alternatively you may wish to use the
%exceptiondirective to put some C++ code after$action, but unless this is for the purposes of validating the returned result it feels quite hackish.