Gaël Varoquaux

Wed 28 September 2011

←Home

Cython example of exposing C-computed arrays in Python without data copies

Some advice on passing arrays from C to Python avoiding copies. I use Cython as I have found the code to be more maintainable than hand-written Python C-API code.

I found out that there was no self-contained example of creating numpy arrays from existing data in Cython. Thus I created my own. The full code with readme build and demo scripts is available on a gist. Here I only give an executive summary.

The core functionality is implemented by the PyArray_SimpleNewFromData function of the C API of numpy that can create an ndarray from a pointer to the data, a simple data type, and the shape of the data. The Cython file just builds around that function:

Go Top