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)
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, sincebytesis readonly. That
should probably be highlighted more in the tutorial.
Absolutely. Docs PR welcome.
Most helpful comment
You need to specify it as
const, sincebytesis readonly. That should probably be highlighted more in the tutorial.