Swig, python and output strings
I am using Swig to wrap a C interface that looks like this:
int dosomething(char **str);
where str is an output string. For example, from C its called like this:
开发者_StackOverflow社区char *str= NULL;
int val= dosomething(&str);
...
free(str);
In Python, I'd like to be able to call it like this:
val,str = dosomething()
However, python keeps reporting
TypeError: dosomething() takes exactly 1 arguments (0 given)
I've tried using OUTPUT in a typemap, but to no avail. Any ideas?
Try this typemap (I'm using SWIG 2.0.0):
%include <cstring.i>
%cstring_output_allocate(char **str, free(*$1));
Documentation: cstring.i
Why not use it normally, passing a parameter, and wrap it inside python to return a tuple?
精彩评论