Swig and Python

Discussion in 'Python' started by Java and Swing, Oct 4, 2005.

  1. 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.
    Java and Swing, Oct 4, 2005
    #1
    1. Advertising

  2. On 4 Oct 2005 05:46:27 -0700, Java and Swing <> 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
    Steve Juranich, Oct 4, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Sheehan

    SWIG and Python in use?

    Alan Sheehan, Jul 7, 2003, in forum: Python
    Replies:
    1
    Views:
    316
    Wolfgang
    Jul 8, 2003
  2. Phil Schmidt

    SWIG, MinGW, and Python 2.3 problem

    Phil Schmidt, Sep 10, 2003, in forum: Python
    Replies:
    0
    Views:
    539
    Phil Schmidt
    Sep 10, 2003
  3. =?iso-8859-2?q?Bo=B9tjan_Jerko?=

    swig and list in python

    =?iso-8859-2?q?Bo=B9tjan_Jerko?=, Jun 2, 2004, in forum: Python
    Replies:
    1
    Views:
    286
    Miki Tebeka
    Jun 2, 2004
  4. Jeff
    Replies:
    0
    Views:
    714
  5. Keith Sabine
    Replies:
    0
    Views:
    575
    Keith Sabine
    Jun 6, 2008
Loading...

Share This Page