newbie SWIG typemap question

Syrinx at dbtech.net Syrinx at dbtech.net
Mon Feb 13 02:50:43 EST 2006


Hi everyone.  I'm new to SWIG, and I'm trying to wrap a C library for
Python.

I have a function that is declared similarly to this:

double *foo(int x);

The function returns a pointer to an array of 7 double values.  I want
to return a 7-tuple to Python.  I've been trying variations of the
following typemap, but I get a segmentation fault when I try it.
(It's been awhile since I've done any C, so it may be an obvious C
error, I'm not sure.)

Can anyone tell me what's wrong, or just show me how to accomplish
what I'm after?  I'd appreciate it a lot.  Thank you.

%typemap(python, out) double* {
    int i;
    PyObject *o = PyTuple_New(7);
    for(i = 0; i < 7; i++) {
        PyObject *o2 = PyFloat_FromDouble((double)$1[i]);
        PyTuple_SET_ITEM(o, i, o2);
    }
    $result = o;
} 




More information about the Python-list mailing list