开发者

Swig, python and output strings

开发者 https://www.devze.com 2023-01-29 08:20 出处:网络
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:

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号