The goal of this example is to show how an existing C codebase for numerical computing (here c_code.c) can be wrapped in Cython to be exposed in Python.
The meat of the example is that the data is allocated in C, but exposed in Python without a copy using the PyArray_SimpleNewFromData numpy function in the Cython file cython_wrapper.pyx.
The purpose of the ArrayWrapper object, is to be garbage-collected by Python when the ndarray Python object disappear. The memory is then freed. Note that there is no control of when Python will deallocate the memory. If the memory is still being used by the C code, please refer to the following blog post by Travis Oliphant:
http://blog.enthought.com/python/numpy-arrays-with-pre-allocated-memory
You will need Cython, numpy, and a C compiler.
To build the C extension in-place run:
$ python setup.py build_ext -i
To test the C-Python bindings, run the test.py file.
Files | |
---|---|
c_code.c | The C code to bind. Knows nothing about Python |
cython_wrapper.c | The Cython code implementing the binding |
setup.py | The configure/make/install script |
test.py | Python code using the C extension |
- Author
Gael Varoquaux
- License
BSD 3 clause
Thanks for this useful complete example. When I copied over portions into my code, I ran into the issue that the assignment
didn't work. It's because I forgot to declare the
ndarray
as a cython variable, which is done properly in the gist.Independent of that code-copying issue, I think it is easiest to just set
PyArray_ENABLEFLAGS(arr, np.NPY_OWNDATA)
tofree
the memory as @syrte asked but this example demonstrates how to implement a custom delete function. I need to callfftw_free
instead offree
and here is the only way I found that achieves this. 💯They suggestion by @Qwlouse avoids code duplication when the
ArrayWrapper
is needed multiple times and works just as fine. In my application, the array is 2D and I have