New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot assign MemoryView values from read-only object #2644

Open
aparamon opened this Issue Oct 5, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@aparamon

aparamon commented Oct 5, 2018

Cython MemoryView values can only be assigned from writable bytes-like objects:

import array
from libc.stdlib cimport malloc

cdef void *buf = malloc(5)

cdef unsigned char[:] mview
mview = <unsigned char[:5]>(buf)

def test(b):
    b[:] = bytes(b'hello')  # fail
    b[:] = bytearray(b'hello')  # success
    b[:] = array.array('B', b'hello')  # success
    b[:] = memoryview(b'hello')  # fail
   
test(mview)
  File "test.pyx", line 10, in test.test
    b[:] = bytes(b'hello')
  File "stringsource", line 418, in View.MemoryView.memoryview.__setitem__
  File "stringsource", line 429, in View.MemoryView.memoryview.is_slice
  File "stringsource", line 348, in View.MemoryView.memoryview.__cinit__
BufferError: Object is not writable.

numpy/numpy#12075 (comment)

@aparamon aparamon changed the title from Cannot assign MemoryView values from read/only object to Cannot assign MemoryView values from read-only object Oct 9, 2018

@aparamon

This comment has been minimized.

Show comment
Hide comment
@aparamon

aparamon Oct 9, 2018

It is proposed to modify

obj = memoryview(obj, self.flags|PyBUF_ANY_CONTIGUOUS,

so memoryview is created with flags (self.flags|PyBUF_ANY_CONTIGUOUS) & (~PyBUF_WRITABLE).
For PR, where is the best place to put tests?

aparamon commented Oct 9, 2018

It is proposed to modify

obj = memoryview(obj, self.flags|PyBUF_ANY_CONTIGUOUS,

so memoryview is created with flags (self.flags|PyBUF_ANY_CONTIGUOUS) & (~PyBUF_WRITABLE).
For PR, where is the best place to put tests?

@scoder

This comment has been minimized.

Show comment
Hide comment
@scoder

scoder Oct 14, 2018

Contributor

That change looks good. Tests should go into tests/memoryview/memoryview.pyx.

Contributor

scoder commented Oct 14, 2018

That change looks good. Tests should go into tests/memoryview/memoryview.pyx.

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