Extending Python - variable sized arrays?

Ignacio Vazquez-Abrams ignacio at openservices.net
Wed Oct 17 01:10:20 CEST 2001


On Tue, 16 Oct 2001, Tim Egbert wrote:

> 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?

Use PyTuple_New() and then do PyTuple_SetItem() in a loop.

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>

   "As far as I can tell / It doesn't matter who you are /
    If you can believe there's something worth fighting for."
       - "Parade", Garbage





More information about the Python-list mailing list