How to use Cython typed memoryviews to accept strings from Python?

后端 未结 2 1341
伪装坚强ぢ
伪装坚强ぢ 2021-02-07 12:32

How can I write a Cython function that takes a byte string object (a normal string, a bytearray, or another object that follows the buffer protocol) as a typed memoryview?

2条回答
  •  野性不改
    2021-02-07 12:55

    Despite the documentation suggesting otherwise, Cython (at least up to version 0.22) does not support coercing read-only buffer objects into typed memoryview objects. Cython always passes the PyBUF_WRITABLE flag to PyObject_GetBuffer(), even when it doesn't need write access. This causes read-only buffer objects to raise an exception.

    I raised this issue on the Cython developer mailing list, and even included a (very rough) patch. I never got a reply, so I assume the Cython developers are not interested in fixing this bug.

提交回复
热议问题