I have a python memoryview
pointing to a bytes
object on which I would like to perform some processing in cython.
My problem is:
- because the
bytes
object is not writable, cython does not allow constructing a typed (cython) memoryview from it - I cannot use pointers either because I cannot get a pointer to the memoryview start
Example:
In python:
array = memoryview(b'abcdef')[3:]
In cython:
cdef char * my_ptr = &array[0]
fails to compile with the message:Cannot take address of Python variable
cdef char[:] my_view = array
fails at runtime with the message:BufferError: memoryview: underlying buffer is not writable
How does one solve this?
array
argument in you Cython function? – Hanfurddef myfunc(arr): pass
– AntiscorbuticPy_buffer
struct instead this might help. docs.python.org/3.5/c-api/buffer.html – HanfurdPy_buffer
struct hint! I came to the same solution. See the my answer to my own question below... – Antiscorbutic