newbie SWIG typemap question

S

Syrinx

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);
PyTuple_SET_ITEM(o, i, o2);
}
$result = o;
}
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top