Fast construction of Python list from large C array of int's - Extension Module
Alex Martelli
aleax at aleax.it
Mon Nov 18 18:43:47 EST 2002
quadric at primenet.com wrote:
> Hi,
>
> I'm a bit of a Python newbie and I've written a C extension module. One
> of the functions must return a Python list whose source is a large (5000
> -
> 6000) C array of integers. I need a fast way
> of constructing the Python list. I was planning on something like:
>
> PyObject * CreateListFromArray( int * array , int array_length)
> {
> PyObject * list = PyList_New( array_length);
>
> for int i = 0 ; i < array_length ; i++ )
> {
> PyList_SetItem( list , i , Py_LongFromLong( (long) array[i] ) );
> }
>
> return list;
> }
>
>
> Is this the fastest way to do this?
I can't think of a faster one, at least not offhand. Returning other types,
such as an instance of array.array, might be faster, but if you need a list
specifically, that's not an option.
> Any inherent dangers here?
Yes: not only pointer 'list', as you realize later, but also EACH pointer
resulting from PyLong_FromLong COULD be null, with dangerous
results if you don't check. Do check, and if you get any null, decref
the ones you had previously added and then raise out-of-memory.
This may slow you down by a tiny amount, but risking a nasty crash
is not a viable alternative, IMHO. You probably won't be able to
_measure_ the proportional slowdown, I suspect.
Alex
More information about the Python-list
mailing list