[pypy-dev] Issue #1777: Unable to create proper numpy array from cffi buffer (pypy/pypy)

Yichao Yu issues-reply at bitbucket.org
Mon May 26 01:42:15 CEST 2014


New issue 1777: Unable to create proper numpy array from cffi buffer
https://bitbucket.org/pypy/pypy/issue/1777/unable-to-create-proper-numpy-array-from

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`](https://bitbucket.org/pypy/numpy/issue/9/numpylibstride_tricksas_strided-is-not) 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](https://github.com/pyopencl/pyopencl/blob/master/src/wrapper/wrap_cl.hpp#L2718). 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](https://bitbucket.org/pypy/numpy/issue/9/numpylibstride_tricksas_strided-is-not) by allowing creating arrays from object with a proper array interface)





More information about the pypy-dev mailing list