Describe the bug
ValueError raised when passing a Cython 1D byte array to io.BufferedWriter.write(ar)
To Reproduce
"test_module.pyx":
# cython: language_level=3
from libc.string cimport strlen
cdef const char* data = b'simulates something from a C-based source'
def get_data_array():
cdef size_t data_len = strlen(data)
result = <const char[:data_len]> data
return result
"tester.py":
#!/usr/bin/env python3
import sys
import test_module
ar = test_module.get_data_array()
sys.stdout.buffer.write(ar)
sys.stdout.buffer.write(b'\n')
Expected behavior
Prints the following to the console: simulates something from a C-based source\n
Actual behavior
Traceback (most recent call last):
File "./test_it.py", line 5, in <module>
sys.stdout.buffer.write(ar)
File "stringsource", line 185, in View.MemoryView.array.__getbuffer__
ValueError: Can only create a buffer that is contiguous in memory.
Environment (please complete the following information):
- OS: Linux (Debian)
- Version 3.0a6
Additional context
- Works okay if the output IO object is an
io.BytesIO.
- As a workaround, you can wrap the Cython array in a memoryview before sending to the
write() method; e.g. sys.stdout.buffer.write(memoryview(ar)). This does add a small cost (temporary object), but is otherwise about as cheap as it gets.
Describe the bug
ValueError raised when passing a Cython 1D byte array to
io.BufferedWriter.write(ar)To Reproduce
"test_module.pyx":
"tester.py":
Expected behavior
Prints the following to the console:
simulates something from a C-based source\nActual behavior
Environment (please complete the following information):
Additional context
io.BytesIO.write()method; e.g.sys.stdout.buffer.write(memoryview(ar)). This does add a small cost (temporary object), but is otherwise about as cheap as it gets.