I'm new to C extensions for NumPy and I'm wondering if the following workflow is possible.
- Pre-allocate an array in NumPy
- Pass this array to a C extension
- Modify array data in-place in C
- Use the updated array in Python with standard NumPy functions
In particular, I'd like to do this while ensuring I'm making zero new copies of the data at any step.
I'm familiar with boilerplate on the C side such as PyModuleDef
, PyMethodDef
, and the PyObject*
arguments but a lot of examples I've seen involve coercion to C arrays which to my understanding involves copying and/or casting. I'm also aware of Cython though I don't know if it does similar coercions or copies under the hood. I'm specifically interested in simple indexed get- and set- operations on ndarray
with numeric (eg. int32
) values.
Could someone provide a minimal working example of creating a NumPy array, modifying it in-place in a C extension, and using the results in Python subsequently?