Commit e4260c0a authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2433 from gabrieldemarmiesse/mention_memoryviews_with_strings

Added an example of using python strings with const memoryviews.
parents 3198026c 679551b3
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
...@@ -169,6 +169,11 @@ support read-only buffers as input:: ...@@ -169,6 +169,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