paleozogt
paleozogt

Reputation: 6563

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:

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?

Upvotes: 1

Views: 1707

Answers (2)

Mark Tolonen
Mark Tolonen

Reputation: 177481

Try this typemap (I'm using SWIG 2.0.0):

%include <cstring.i>
%cstring_output_allocate(char **str, free(*$1));

Documentation: cstring.i

Upvotes: 1

salezica
salezica

Reputation: 76899

Why not use it normally, passing a parameter, and wrap it inside python to return a tuple?

Upvotes: 0

Related Questions