Extending Python - variable sized arrays?

Tim Egbert tegbert at abl.med.utah.edu
Tue Oct 16 17:47:55 EDT 2001


I'm developing a native method module for Python from some existing code.
One function that I call returns an integer array that can be any size up
to 128.

Returning the array as a tuple using Py_BuildValue() is a problem because,
although I can create the format string dynamically, I can't create a
dymanically sized argument list.  Maybe I'm just being dense, but I'd like
to do something like this (illustrated here with a currently nonexistent
function call, Py_BuildArrayTuple()):

   PyObject *ret;
   int *arr;
   ...
   (arr gets allocated and loaded with 6 values)
   ...
   ret = Py_BuildArrayTuple("6i",arr);
   return ret;
      
which would be the equivalent of:

   ret = Py_BuildValue("iiiiii",
      arr[0],arr[1],arr[2],arr[3],arr[4],arr[5]);

I can't go the Py_BuildValue() route because the problems is, the next
time around, there may be 17 or 127 elements in the array, the exact
number only to be known during runtime.

Any thoughts on how to do this?

Tim Egbert




More information about the Python-list mailing list