MemoryViews require writeable arrays?
%%cython cimport cython import numpy as np cimport numpy as np @cython.boundscheck(False) @cython.wraparound(False) @cython.cdivision(True) cpdef double[:] return_one(double[:] x): return np.array([1.0]) In [43]: x = randn(3) ...: return_one(x) Out[43]: <MemoryView of 'ndarray' at 0x8ae14e0> In [44]: x.flags['WRITEABLE'] = False ...: return_one(x) Traceback (most recent call last): File "<ipython-input-44-4fbbd1035d56>", line 2, in <module> return_one(x) File "_cython_magic_7761e77f78c4e321261152684b47c674.pyx", line 11, in _cython_magic_7761e77f78c4e321261152684b47c674.return_one (C:\Users\dhirschfeld\.ipython\cython\_cython_magic_7761e77f78c4e321261152684b47 c674.c:1727) File "stringsource", line 619, in View.MemoryView.memoryview_cwrapper (C:\Users\dhirschfeld\.ipython\cython\_cython_magic_7761e77f78c4e321261152684b47 c674.c:8819) File "stringsource", line 327, in View.MemoryView.memoryview.__cinit__ (C:\Users\dhirschfeld\.ipython\cython\_cython_magic_7761e77f78c4e321261152684b47 c674.c:5594) ValueError: buffer source array is read-only Is this a required restriction? Is there any workaround? The context is calling cython routines using IPython.parallel. IIUC any input arrays sent over zmq are necessarily read-only. As can be seen with the example, even if we don't modify (or use) the input array at all we still get the error. Any help, esp. in regards to a workaround would be greatly appreciated! Thanks, Dave
Dave Hirschfeld <dave.hirschfeld@...> writes:
cpdef double[:] return_one(double[:] x): return np.array([1.0])
In [43]: x = randn(3) ...: return_one(x) Out[43]: <MemoryView of 'ndarray' at 0x8ae14e0>
In [44]: x.flags['WRITEABLE'] = False ...: return_one(x) ValueError: buffer source array is read-only
Any help, esp. in regards to a workaround would be greatly appreciated!
Thanks, Dave
It seems using the numpy buffer interface works but I guess it would still be good if this worked for memviews too: %%cython cimport cython import numpy as np cimport numpy as np ctypedef np.float64_t float64_t @cython.boundscheck(False) @cython.wraparound(False) @cython.cdivision(True) cpdef double[:] return_one_np(np.ndarray[float64_t, ndim=1] x): return np.array([1.0]) In [203]: return_one_np(x) Out[203]: <MemoryView of 'ndarray' at 0x7d2d558> Cheers, Dave
On 27.02.2013 20:05, Dave Hirschfeld wrote:
Is this a required restriction? Is there any workaround?
http://www.python.org/dev/peps/pep-3118/ What you should consider is the "readonly" field in "struct bufferinfo" or the access flag "PyBUF_WRITEABLE". In short: A PEP3118 buffer can be readonly, and then you shouldn't write to it! When you set the readonly flag, Cython cannot retrieve the buffer with PyBUF_WRITEABLE. Thus, Cython helps you not to shoot yourself in the foot. I don't think you can declare a read-only memoryview in Cython. (Well, not by any means I know of.) Sturla
Sturla Molden <sturla@...> writes:
On 27.02.2013 20:05, Dave Hirschfeld wrote:
Is this a required restriction? Is there any workaround?
http://www.python.org/dev/peps/pep-3118/
What you should consider is the "readonly" field in "struct bufferinfo" or the access flag "PyBUF_WRITEABLE".
In short:
A PEP3118 buffer can be readonly, and then you shouldn't write to it! When you set the readonly flag, Cython cannot retrieve the buffer with PyBUF_WRITEABLE. Thus, Cython helps you not to shoot yourself in the foot. I don't think you can declare a read-only memoryview in Cython. (Well, not by any means I know of.)
Sturla
So the issue is that at present memoryviews can't be readonly? Presumably because this works for numpy arrays it would be possible to also make readonly memoryviews? I think that would certainly be nice to have, but maybe it's a niche use case. Certainly, for IPython.parallel use it's easy enough to write a shim which sets the array to writeable with the understanding that changes don't get propagated back. Thanks, Dave
On 28.02.2013 15:55, Dave Hirschfeld wrote:
So the issue is that at present memoryviews can't be readonly?
https://github.com/cython/cython/blob/master/Cython/Compiler/MemoryView.py#L... Typed memoryviews are thus acquired with the PyBUF_WRITEABLE flag. If the the assigned buffer is readonly, the request to acquire the PEP3118 buffer will fail. If you remove the PyBUF_WRITEABLE flag from lines 33 to 36, you can acquire a readonly buffer with typed memoryviews. But this is not recommended. In this case you would have to check for the readonly flag yourself and make sure you don't write to readonly buffer. Sturla
participants (2)
-
Dave Hirschfeld -
Sturla Molden