diff --git a/python/pyarrow/io.pxi b/python/pyarrow/io.pxi index b648fbf6698..32f7bc26b1f 100644 --- a/python/pyarrow/io.pxi +++ b/python/pyarrow/io.pxi @@ -548,6 +548,8 @@ cdef class NativeFile(_Weakrefable): handle = self.get_input_stream() py_buf = py_buffer(b) + if not py_buf.buffer.get().is_mutable(): + raise TypeError("readinto() argument must be a writable buffer") buf_len = py_buf.size buf = py_buf.buffer.get().mutable_data() diff --git a/python/pyarrow/tests/test_io.py b/python/pyarrow/tests/test_io.py index 8494a0ee66b..0dfe58258e2 100644 --- a/python/pyarrow/tests/test_io.py +++ b/python/pyarrow/tests/test_io.py @@ -217,6 +217,13 @@ def test_python_file_readinto(): assert len(dst_buf) == length +@pytest.mark.parametrize("dst_buf", [b"a", memoryview(b"a")]) +def test_native_file_readinto_rejects_readonly_buffer(dst_buf): + with pa.BufferReader(b"x") as f: + with pytest.raises(TypeError, match="writable buffer"): + f.readinto(dst_buf) + + def test_python_file_read_buffer(): length = 10 data = b'0123456798'