returning a python array from a C extension

Jesper Olsen jolsen at mailme.dk
Tue May 28 05:24:59 EDT 2002


Maxwell Todd Sayles <sayles at cpsc.ucalgary.ca> wrote in message news:<3CF29A3D.5EF81E74 at cpsc.ucalgary.ca>...
> in python i can write:
> 
> from array import *
> a = array ('B', 'some test data')
> 
> and a refers to an array object.
> 
> now i have a C extension for python that returns a PyString, and within
> python i can convert it to an array
> 
> e.g.:
> 
> from array import *
> from MyCExtension import *
> a = array ('B', cfunction())
> 
> but this method requires an extra copy and construction from the string
> to the array.
> 
> is there a way i can have cfunction return a python array?  i tried
> looking through the include files with the Python/C API and couldn't
> find anything.  any help would be appreciated.
> 
> Thanks, Maxwell Sayles


You could also consider using the Numeric array type instead of the
build-in array type.

http://www.pfdubois.com/numpy/html2/numpy.html

If nothing else, the big advantage is that the Numeric documentation
actually describes how to do this.

Cheers
Jesper



More information about the Python-list mailing list