Skip to content
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

Closed
aparamon opened this issue Oct 5, 2018 · 2 comments
Closed

Cannot assign MemoryView values from read-only object #2644

aparamon opened this issue Oct 5, 2018 · 2 comments

Comments

@aparamon
Copy link

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 Cannot assign MemoryView values from read/only object Cannot assign MemoryView values from read-only object Oct 9, 2018
@aparamon
Copy link
Author

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
Copy link
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
Projects
None yet
Development

No branches or pull requests

2 participants