Skip to content

GH-51044: [Python] Reject read-only readinto destinations - #51126

Merged
raulcd merged 2 commits into
apache:mainfrom
carrerasdarren-cell:gh-51044-readinto-readonly
Sep 9, 2026
Merged

GH-51044: [Python] Reject read-only readinto destinations#51126
raulcd merged 2 commits into
apache:mainfrom
carrerasdarren-cell:gh-51044-readinto-readonly

Conversation

@carrerasdarren-cell

@carrerasdarren-cell carrerasdarren-cell commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Rationale for this change

BufferReader.readinto() currently segfaults when passed a read-only destination such as bytes or a read-only memoryview. py_buffer() creates an immutable Arrow buffer, mutable_data() returns a null pointer, and the C++ read path attempts to copy into that pointer. A Python API misuse should raise a Python exception rather than terminate the interpreter.

What changes are included in this PR?

  • Validate that a NativeFile.readinto() destination is mutable before obtaining its writable pointer.
  • Raise TypeError for immutable destinations.
  • Add regression coverage for bytes and read-only memoryview destinations.

Are these changes tested?

Yes. The focused readinto tests pass against a locally compiled patched pyarrow.lib. The stock 25.0.1 wheel exits with status 139 for the issue reproducer, while the patched build raises the expected TypeError; the writable bytearray control continues to read successfully.

Are there any user-facing changes?

Yes. Passing a read-only destination to NativeFile.readinto() now raises TypeError instead of terminating the interpreter. Writable-buffer behavior is unchanged.

Fixes #51044.

This contribution was developed with assistance from OpenAI Codex. I reviewed, tested, and take responsibility for the patch and this description.

Immutable destinations expose a null mutable pointer and can crash the native read path. Validate destination mutability and cover bytes and read-only memoryviews with regression tests.

Assisted-by: OpenAI Codex

@raulcd raulcd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reproduced this locally with 25.0.1, the fix seems reasonable to me:

$ python
Python 3.14.6 (main, Jun 10 2026, 18:54:31) [GCC 15.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyarrow as pa
>>> dst_buf=memoryview(b"a")
>>> with pa.BufferReader(b"x") as f:
...     f.readinto(dst_buf)
...     
Segmentation fault         python

@raulcd

raulcd commented Sep 9, 2026

Copy link
Copy Markdown
Member

@AlenkaF @rok are you ok with me merging this?

@github-actions github-actions Bot added awaiting merge Awaiting merge and removed awaiting review Awaiting review labels Sep 9, 2026

@AlenkaF AlenkaF left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor nit: can we move the test after test_nativefile_write_memoryview under the Buffers section in the same file?

Otherwise LGTM, thanks!

Move the existing decorated test immediately after
test_nativefile_write_memoryview, as requested in review.

AI-assisted mechanical relocation only; test logic and decorators
are unchanged. Checked Python syntax, statement and byte equivalence,
and whitespace. Runtime tests were not rerun for this ordering change.
@rok

rok commented Sep 9, 2026

Copy link
Copy Markdown
Member

Only did a quick look, but LGTM

@raulcd
raulcd merged commit b3a9c44 into apache:main Sep 9, 2026
37 checks passed
@raulcd raulcd removed the awaiting merge Awaiting merge label Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Python] BufferReader.readinto() segfaults on a read-only destination

4 participants