Commit 679551b3 authored by gabrieldemarmiesse's avatar gabrieldemarmiesse

Added an example of using python strings with const memoryviews.

parent 084a25f5
cdef bint is_y_in(const unsigned char[:] string_view):
cdef int i
for i in range(string_view.shape[0]):
if string_view[i] == b'y':
return True
return False
print(is_y_in(b'hello world')) # False
print(is_y_in(b'hello Cython')) # True
...@@ -186,6 +186,11 @@ support read-only buffers as input:: ...@@ -186,6 +186,11 @@ support read-only buffers as input::
a.setflags(write=False) a.setflags(write=False)
myslice = a myslice = a
Using a non-const memoryview with a binary Python string produces a runtime error.
You can solve this issue with a ``const`` memoryview:
.. literalinclude:: ../../examples/userguide/memoryviews/view_string.pyx
Note that this does not *require* the input buffer to be read-only:: Note that this does not *require* the input buffer to be read-only::
a = np.linspace(0, 10, num=50) a = np.linspace(0, 10, num=50)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment