Commit 8c4e86c6 by GaelVaroquaux

Fix: the code was actually doing a copy

parent bd8a6922
......@@ -14,6 +14,7 @@ cdef extern from "c_code.c":
float *compute(int size)
from libc.stdlib cimport free
from cpython cimport PyObject, Py_INCREF
# Import the Python-level symbols of numpy
import numpy as np
......@@ -70,10 +71,18 @@ def py_compute(int size):
not copy the data allocated in C.
"""
cdef float *array
cdef np.ndarray ndarray
# Call the C function
array = compute(size)
array_wrapper = ArrayWrapper()
array_wrapper.set_data(size, <void*> array)
ndarray = np.array(array_wrapper, copy=False)
# Assign our object to the 'base' of the ndarray object
ndarray.base = <PyObject*> array_wrapper
# Increment the reference count, as the above assignement was done in
# C, and Python does not know that there is this additional reference
Py_INCREF(array_wrapper)
return np.array(array_wrapper)
return ndarray
......@@ -2,7 +2,11 @@
"""
# Author: Gael Varoquaux
# License: BSD
import numpy as np
import cython_wrapper
a = cython_wrapper.py_compute(10)
print a
print 'The array created is %s' % a
print 'It carries a reference to our deallocator: %s ' % a.base
np.testing.assert_allclose(a, np.arange(10))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment