Fast construction of Python list from large C array of int's - Extension Module

quadric at primenet.com quadric at primenet.com
Mon Nov 18 14:14:42 EST 2002


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?

Any inherent dangers here?

Any issues with memory shortage etc... in Python interpreter. In other 
words should I do thusly:

	PyObject * list = PyList_New( array_length);

	if (list != NULL)
	{
    	    for int i = 0 ; i < array_length ; i++ )
  	    {
	        PyList_SetItem( list , i , Py_LongFromLong( (long) array[i] )  );
	    }
	}


Thanks for your help.


quadric at primenet.com








More information about the Python-list mailing list