[Cython] MemoryViews require writeable arrays?

Dave Hirschfeld dave.hirschfeld at gmail.com
Wed Feb 27 20:05:08 CET 2013


%%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





More information about the cython-devel mailing list