djh
djh

Reputation: 410

SWIG (C & Python) cstring.i problems

I have read http://www.swig.org/Doc1.3/Library.html#Library_nn12 and am having trouble in Python using functions of the following form:

/* foo.h */
//fills *filename with useful stuff in the usual way
int foo(char *filename);

I have an interface file that roughly looks like this:

%module foo
{% #include "foo.h" %}
%include "foo.h"

%cstring_bounded_output(char *filename, 1024);
extern int foo(char *filename);

The swig doc leads me to believe I can call foo() from python with no arguments and the return value will be the python string filename. However:

In [4]: foo.foo()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-db4ddc3a33ec> in <module>()
----> 1 foo.foo()

TypeError: foo() takes exactly 1 argument (0 given)

foo() is still expecting a char *. Is my interface file correct? Is this the preferred method to get a value in python from a C function of this type?

http://web.mit.edu/svn/src/swig-1.3.25/Lib/python/cstring.i is quite hard to parse. If anyone has info on what this macro is doing, it would be nice to have some light shed on it.

Note: I am stuck in a production environment with Swig 1.3.z, if that's relevant. Further, I must use Swig here.

Upvotes: 0

Views: 472

Answers (1)

Robᵩ
Robᵩ

Reputation: 168626

The %cstring_bounded_output must precede the declaration of foo, even the declaration that is in the header file. This version works for me:

%module foo

%include cstring.i

%{
#include "foo.h"
%}

%cstring_bounded_output(char* filename, 1024);
%include "foo.h"

extern int foo(char *filename);

Upvotes: 1

Related Questions