question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typed memoryview doesn't work with read-only buffers

See original GitHub issue

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)
0.9341213061235054

>>> 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')
      4 
      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: https://mail.python.org/pipermail/cython-devel/2015-February/004316.html Some discussion about a possible use for a const keyword: https://groups.google.com/forum/#!topic/cython-users/32CMgaLrNhc

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
scodercommented, Sep 6, 2017

I have an initial implementation up at https://github.com/scoder/cython/tree/readonly_buffers that automatically determines the need for a writable buffer, and otherwise sticks to a read-only one. It has some bugs and fails to detect some “read-only” vs. “needs writable” cases at compile time, but it mostly works.

It also implements explicit const memory views, e.g. cdef const int[:] a, which should always request a read-only view.

Feedback welcome.

3reactions
rainwoodmancommented, Jul 16, 2017

It appears this bug is keeping people away from the memoryview to use the older ndarray interface. for example, see this workaround in pandas https://github.com/pandas-dev/pandas/pull/12013

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typed memoryview doesn't support READ-ONLY buffers?
We could use memoryview of Python on readonly objects, but it seems we cannot apply ... I searched StackOverflow, there is someone having...
Read more >
Developers - Typed memoryview doesn't work with read-only buffers -
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...
Read more >
Typed Memoryviews — Cython 3.0.0a11 documentation
Writable buffers are still accepted by const views, but read-only buffers are not accepted for non-const, writable views: cdef double[:] myslice # a...
Read more >
How to use Cython typed memoryviews to accept strings from ...
Despite the documentation suggesting otherwise, Cython (at least up to version 0.22) does not support coercing read-only buffer objects into ...
Read more >
[Cython] [RFC PATCH] add read-only buffer support to typed ...
The problem: The following example function raises an exception when ... is raised because the code generated for typed memoryviews always ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found