1

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?

2
  • may I suggest you try ctypes? Its much faster to port C functions this way Commented Dec 8, 2010 at 23:49
  • Unfortunately, ctypes is python-only. I'm using swig because I need to wrap for many different languages (Java, etc). Commented Dec 8, 2010 at 23:56

2 Answers 2

1

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

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

Documentation: cstring.i

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.