From: Марк К. <soc...@gm...> - 2012-08-22 07:13:30
|
2012/8/22 Simon Busard <sim...@uc...> > Hello, > > I'm currently using SWIG to wrap a C library. I found a weird behaviour > and wanted to know if this is a bug or not, and how can I fix this. Here > is my problem: I have a function returning a pointer (I used in the > example below a pointer to an int for illustration, but in my case it's > a pointer to a structure); in addition, it uses an argument to return > another output (i.e. an argument is a pointer to an int that is filled > by the function). Here is my example: > > > int* test(int arg, int* error) { > int *res; > if(arg > 0) { > res = (int *) 1; > *error = 0; > } else { > res = NULL; > *error = 1; > } > return res; > } > > > I wrapped this with SWIG and applied a typemap on int* error to tell > that it is also an output. Here is my SWIG interface: > > > %include "typemaps.i" > > %apply int *OUTPUT {int* error}; > > %inline %{ > > int* test(int arg, int* error) { > int *res; > if(arg > 0) { > res = (int *) 1; > *error = 0; > } else { > res = NULL; > *error = 1; > } > return res; > } > > %} > > > So, when I use the module generated by SWIG with Python, I get this > behaviour: > > > >>> test(1) > [<Swig Object of type 'int *' at 0x1006bdde0>, 0] > >>> test(0) > 1 > > > It seems that when the function returns a NULL pointer, the output is > ignored and only the int* error output is used. > > Is it a bug or not? How can I fix this, such that when the result is > NULL I get "[None, 1]" in Python instead of just "1"? I can't modify the > initial function. > > Thank you very much for your help, > > Regards, > > Simon Busard > > > PS: I checked the manual but found nothing. Please forgive me if the > answer was there but I missed it. > You should write wrapper for that function. It's SWIG, baby. Yes, I have the same problem (Some library function return result in two output parameters (retval and via pointer in arg)), so, I have [<result>, <result>] in stead of just <result>. So I asked this mailing list for help, and people advice me to write a wrapper in .i file, and then export wrapper instead of original function. > > > ------------------------------------------------------------------------------ > Live Security Virtual Conference > Exclusive live event will cover all the ways today's security and > threat landscape has changed and how IT managers can respond. Discussions > will include endpoint security, mobile security and the latest in malware > threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ > _______________________________________________ > Swig-user mailing list > Swi...@li... > https://lists.sourceforge.net/lists/listinfo/swig-user > -- Segmentation fault |