7

I am trying to output an array of values from a C function wrapped using SWIG for Python. The way I am trying to do is using the following typemap.

Pseudo code:

int oldmain() {
float *output = {0,1};
return output;
}

Typemap:

%typemap(out) float* { 
   int i; 
  $result = PyList_New($1_dim0); 
   for (i = 0; i < $1_dim0; i++) { 
 PyObject *o = PyFloat_FromDouble((double) $1[i]); 
 PyList_SetItem($result,i,o); 
 } 
} 

My code compiles well, but it hangs when I run access this function (with no more ways to debug it).

Any suggestions on where I am going wrong?

Thanks.

2
  • Can you share your Makefile or similar? You might find Cython easier than SWIG, unless you might do more languages than just Python. Jul 26, 2012 at 23:07
  • Thanks, Oleksiy's answer solved my issue, but as you said, Cython seems to be easier and I am exploring that. Thanks again.
    – SEU
    Jul 27, 2012 at 6:46

2 Answers 2

7

The easiest way to allow the length to vary is to add another output parameter that tells you the size of the array too:

%module test

%include <stdint.i>

%typemap(in,numinputs=0,noblock=1) size_t *len  {
  size_t templen;
  $1 = &templen;
}

%typemap(out) float* oldmain {
  int i;
  $result = PyList_New(templen);
  for (i = 0; i < templen; i++) {
    PyObject *o = PyFloat_FromDouble((double)$1[i]);
    PyList_SetItem($result,i,o);
  }
}

%inline %{
float *oldmain(size_t *len) {
  static float output[] = {0.f, 1.f, 2, 3, 4};
  *len = sizeof output/sizeof *output;
  return output;
}
%}

This is modified from this answer to add size_t *len which can be used to return the length of the array at run time. The typemap completely hides that output from the Python wrapper though and instead uses it in the %typemap(out) instead of a fixed size to control the length of the returned list.

4
  • Thanks for the detailed reply. I'll try this. Another question: If I have a variable or a #define constant in my C code, which is my output array length, is there a way I could use this instead of "templen" in your example?
    – SEU
    Jul 27, 2012 at 8:15
  • @seu - the easiest way is just to stick it inside the out typemap for the call to PyList_New and the for loop.
    – Flexo
    Jul 27, 2012 at 8:17
  • It doesn't seem to work: I tried define a constant inside my c file and I get the following errors example_wrap.c:2980: error: ‘ARRLENGTH’ undeclared (first use in this function) . My intent is to not declare this again but somehow be able to access this variable/constant.
    – SEU
    Jul 27, 2012 at 8:39
  • @SEU - you'll need to do %{ #include "whatever.h" %} before the typemap to make the definition of ARRLENGTH visible in the generated wrapper.
    – Flexo
    Jul 27, 2012 at 8:42
6

This should get you going:

/* example.c */

float * oldmain() {
    static float output[] = {0.,1.};
    return output;
}

You are returning a pointer here, and swig has no idea about the size of it. Plain $1_dim0 would not work, so you would have to hard code or do some other magic. Something like this:

/* example.i */
%module example
%{
 /* Put header files here or function declarations like below */
  extern float * oldmain();
%}

%typemap(out) float* oldmain {
  int i;
  //$1, $1_dim0, $1_dim1
  $result = PyList_New(2);
  for (i = 0; i < 2; i++) {
    PyObject *o = PyFloat_FromDouble((double) $1[i]);
    PyList_SetItem($result,i,o);
  }
}

%include "example.c"

Then in python you should get:

>> import example
>> example.oldmain()
[0.0, 1.0]

When adding typemaps you may find -debug-tmsearch very handy, i.e.

swig -python -debug-tmsearch example.i

Should clearly indicate that your typemap is used when looking for a suitable 'out' typemap for float *oldmain. Also if you just like to access c global variable array you can do the same trick using typemap for varout instead of just out.

4
  • Hi, Thanks a lot. This worked well. I am trying to generalize this for any length of array, so I am using $1_dim0 instead of "2" and I encounter this issue (also found in the documentation swig.org/Doc1.3/Typemaps.html#Typemaps_nn39) . example_wrap.c:2979: error: ‘result_dim0’ undeclared (first use in this function)
    – SEU
    Jul 27, 2012 at 6:38
  • @seu - you can't do that because the size of the array isn't known. The return type is float*, not float[2] which is needed for the $1_dim0 thing to work.
    – Flexo
    Jul 27, 2012 at 7:09
  • @Flexo. Thanks. I tried to use "%typemap(out) float [ANY] {" as in the documentation and it compiles, but in Python the return value I get is <Swig Object of type 'float *' at 0x9175470> . Essentially I am trying to return an array with variable length. Is there a way to do this, or do I always have to know the length?
    – SEU
    Jul 27, 2012 at 7:21
  • +1 for the -debug-tmsearch; I had a similar problem and the debugging output helped me to figure out that I actually want to write an 'argout' typemap and not 'out' typemap
    – user7610
    Jul 30, 2013 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.