Swig and Python

J

Java and Swing

I am trying to wrap some C code I have. Currently I have something
like...

defs.h
-----------
typedef unsigned long MY_DIGIT;

myapp.c
-------------
void MakeDigits(MY_DIGIT digits[]) {
....
}

char* GetString(char *inMessage, MY_DIGIT *digit) {
char *results;
...
...
return results;
}


....ok so my interface for swig looks like..

%module MyApp
%{
#include "math.h"
%}

extern void MakeDigits(MY_DIGIT digits[]);
extern char* GetString(char *inMessage, MY_DIGIT *digit);

%include "cpointer.i"
%pointer_functions(MY_DIGIT, digit_array);

%include "cmalloc.i"
%malloc(int);
%free(int);

....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);

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);

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

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

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.

thanks in the mean time.
 
S

Steve Juranich

...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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top