Unable to create proper numpy array from cffi buffer
Created originally on Bitbucket by yuyichao (Yichao Yu)
I am writing something that need to convert a c buffer from cffi to a numpy array with arbitrary shape and strides. Besides the fact that
as_strided is not really working, another problem is that the c buffer is maintained by the underlaying c/c++ library (OpenCL in this case). Therefore it is necessary to let the numpy ndarray hold a reference of the object which manage the memory.
In CPython, this is done by setting the base on the ndarray. However, there doesn't seem to be a way to do the same in (num)pypy.
In pypy, although I can pass the buffer to
np.frombuffer and call
reshape to get an array roughly correct, the array is only holding a reference to the buffer returned by cffi but not the (c++) object (or the python delegate of it) which manages the memory. Furthermore, the base property of the result array cannot be used to access the underlying object (which is currently a PyOpenCL API that I would love not to break.)
(The problem can probably be solved along with the
as_strided support bug by allowing creating arrays from object with a proper array interface)