Cython: memory view over 'bytes', incorrect doc?

Created on 23 Apr 2017  路  5Comments  路  Source: cython/cython

Hi! I was trying to write a function accepting string/bytes and returning string/bytes again. I found an example of accepting strings in the documentation: https://cython.readthedocs.io/en/latest/src/tutorial/strings.html#accepting-strings-from-python-code

but it doesn't work:

def process_byte_data(unsigned char[:] data):
    length = data.shape[0]
    first_byte = data[0]
    slice_view = data[1:-1]

b = b'asd'
process_byte_data(b)

results in

BufferError: Object is not writable.

(Relevant topic in mailing list: https://groups.google.com/d/topic/cython-users/32CMgaLrNhc/discussion)

Documentation

Most helpful comment

You need to specify it as const, since bytes is readonly. That should probably be highlighted more in the tutorial.

All 5 comments

Likely a duplicate of #1605.

So will the documentation be updated or not? Because the docs still claim unsigned char[:] works for bytes.

Or does this actually work in a newer version of Cython now?

You need to specify it as const, since bytes is readonly. That should probably be highlighted more in the tutorial.

The tutorial currently doesn't mention this at all, so yeah, it really should! It suggests unsigned char[:] right after saying things along the lines of "well you want to support bytearray, bytes, string, all of that!" without any note that this suggestion doesn't actually support all of those, so it is very misleading right now. (At least was for me, it is a bit subjective after all)

You need to specify it as const, since bytes is readonly. That
should probably be highlighted more in the tutorial.

Absolutely. Docs PR welcome.

Was this page helpful?
0 / 5 - 0 ratings