diff --git a/python/pyarrow/io.pxi b/python/pyarrow/io.pxi index b648fbf6698..b709c1391de 100644 --- a/python/pyarrow/io.pxi +++ b/python/pyarrow/io.pxi @@ -1336,6 +1336,8 @@ cdef class FixedSizeBufferWriter(NativeFile): """ def __cinit__(self, Buffer buffer): + if not buffer.is_mutable: + raise ValueError("pa.FixedSizeBufferWriter() requires a mutable buffer") self.output_stream.reset(new CFixedSizeBufferWriter(buffer.buffer)) self.is_writable = True diff --git a/python/pyarrow/tests/test_io.py b/python/pyarrow/tests/test_io.py index 3d4ba997b3e..84726d4bc01 100644 --- a/python/pyarrow/tests/test_io.py +++ b/python/pyarrow/tests/test_io.py @@ -2225,6 +2225,19 @@ def test_output_stream_errors(tmpdir): with pytest.raises(ValueError): pa.output_stream(buf, compression="foo") + for arg in [memoryview(b"x"), pa.py_buffer(b"x"), + memoryview(b""), pa.py_buffer(b"")]: + with pytest.raises(ValueError, match="requires a mutable buffer"): + pa.output_stream(arg) + + for arg in [pa.py_buffer(b"x"), pa.py_buffer(b"")]: + with pytest.raises(ValueError, match="requires a mutable buffer"): + pa.FixedSizeBufferWriter(arg) + + for arg in [memoryview(bytearray(1)), pa.py_buffer(bytearray(1))]: + with pa.output_stream(arg) as stream: + stream.write(b"x") + for arg in [bytearray(), StringIO()]: with pytest.raises(TypeError): pa.output_stream(arg)