Swig and Python

Steve Juranich sjuranic at gmail.com
Tue Oct 4 11:11:15 EDT 2005


On 4 Oct 2005 05:46:27 -0700, Java and Swing <codecraig at gmail.com> wrote:
> ...Ok, so I have a couple questions.
> 1)  How would I call MakeDigits from python?  In the C code I would
> normally have something like...
>         MY_DIGIT tmp[10];
>         MakeDigits(tmp);

How I usually do this is to write a typemap so that MY_DIGIT arrays
are automagically converted to/from regular python lists.  See the
SWIG docs on writing typemaps.  When that is done, you can call it
just like this:

MakeDigits(range(10))
MakeDigits([1, 3, 5, 7, 9])
# etc.

> 2)  How would I call GetString?  Again, in C I would have...
>         char *ptr;
>         char msg[100] = "Hello World";
>         MY_DIGIT x = 98;
>         ptr = GetString(msg, x);

This should happen for you fairly automatically:

GetString('Hello World', 98)

> 3) Did I define MY_DIGIT correctly in my SWIG interface file?

I can't really say.  I don't use typedefs all that much.  You can
always look at the generated C/C++ code to see if it looks right. 
Read the docs on the Python/C API if you haven't already.

> 4) If I try the following Python code...
>         x = new_digit_array()
>         print x
>    then Python.exe dies...any idea?

Smells like a segfault from here.  Might have to put some printfs in
the generated code to find out exactly what's happening.

> thanks for the help.  I am trying to find my answers int he SWIG
> docs...either i haven't found it or I didn't understand when I came
> across it.

The SWIG user community also has a very helpful list.  Check out
http://www.swig.org/mail.html for more info.  You might find that your
SWIG related issues get addressed more quickly when posting to the
SWIG list.  There are lots of Python hackers on the list, too.  So
don't be afraid to ask fairly Python-centric questions either.

--
Steve Juranich
Tucson, AZ
USA



More information about the Python-list mailing list