Typed memoryview doesn't work with read-only buffers #1605

kernc opened this Issue Feb 14, 2017 · 0 comments


None yet

1 participant

kernc commented Feb 14, 2017

Typed memoryviews don't work with read-only buffers. A banal example:

cpdef getmax(double[:] x):
    cdef double max = -float('inf')
    for val in x:
        if val > max:
            max = val
    return max

Example session:

>>> import numpy as np
>>> values = np.random.random(8)
>>> values 
array([ 0.4574,  0.8468,  0.744 ,  0.3764,  0.7581,  0.8123,  0.8783,  0.9341])

>>> import getmax
>>> getmax.getmax(values)

>>> values.setflags(write=False)
>>> assert values.flags.writeable == False
>>> getmax.getmax(values)
ValueError                                Traceback (most recent call last)
<ipython-input-8-669165de7389> in <module>()
----> 1 getmax.getmax(values)

/tmp/getmax.pyx in getmax.getmax (getmax.c:1617)()
----> 1 cpdef getmax(double[:] x):
      2     cdef:
      3         double max = -float('inf')
      5     for val in x:

/tmp/getmax.so in View.MemoryView.memoryview_cwrapper (getmax.c:7403)()
/tmp/getmax.so in View.MemoryView.memoryview.__cinit__ (getmax.c:3678)()

ValueError: buffer source array is read-only

There is no evident reason why above simple function wouldn't work with a read-only buffer. About two years ago, this issue was raised on the mailing list along with a RFC patch:
Some discussion about a possible use for a const keyword:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment