From 89d4bcabab5ad51455ea43780ed04d106dbf5b0f Mon Sep 17 00:00:00 2001 From: Stefan Wang <1fannnw@gmail.com> Date: Tue, 8 Sep 2026 12:36:10 -0700 Subject: [PATCH 1/2] GH-51229: [Python] Check MemoryMappedFile before resize Reject resize calls on directly constructed MemoryMappedFile objects before dereferencing the native handle. Generated-by: GitHub Copilot CLI (Claude Opus 5) Signed-off-by: Stefan Wang <1fannnw@gmail.com> --- python/pyarrow/io.pxi | 1 + python/pyarrow/tests/test_io.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/python/pyarrow/io.pxi b/python/pyarrow/io.pxi index b648fbf66980..bc3ee1841990 100644 --- a/python/pyarrow/io.pxi +++ b/python/pyarrow/io.pxi @@ -1101,6 +1101,7 @@ cdef class MemoryMappedFile(NativeFile): ---------- new_size : new size in bytes """ + self._assert_open() check_status(self.handle.get().Resize(new_size)) def fileno(self): diff --git a/python/pyarrow/tests/test_io.py b/python/pyarrow/tests/test_io.py index 8494a0ee66b4..5e96a3a36772 100644 --- a/python/pyarrow/tests/test_io.py +++ b/python/pyarrow/tests/test_io.py @@ -27,6 +27,7 @@ import pathlib import pytest import random +import subprocess import sys import tempfile import weakref @@ -1216,6 +1217,24 @@ def test_memory_map_resize(tmpdir): assert f.read() == bytes(arr[:SIZE]) +def test_memory_map_resize_uninitialized(): + code = """if 1: + import pyarrow as pa + + try: + pa.MemoryMappedFile().resize(0) + except ValueError as exc: + assert str(exc) == "I/O operation on closed file" + else: + raise AssertionError("expected ValueError") + """ + res = subprocess.run([sys.executable, "-c", code], + universal_newlines=True, stderr=subprocess.PIPE) + if res.returncode != 0: + print(res.stderr, file=sys.stderr) + res.check_returncode() + + def test_memory_zero_length(tmpdir): path = os.path.join(str(tmpdir), guid()) f = open(path, 'wb') From 1a2052d26950354bccaab926fcef05184e9f2ddc Mon Sep 17 00:00:00 2001 From: 1fanwang <1fannnw@gmail.com> Date: 2026年9月10日 04:54:52 -0700 Subject: [PATCH 2/2] GH-51229: Assert the value error with pytest.raises The subprocess wrapper guarded against the crash this change removes, so the in-process form reads better now. Signed-off-by: 1fanwang <1fannnw@gmail.com> --- python/pyarrow/tests/test_io.py | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/python/pyarrow/tests/test_io.py b/python/pyarrow/tests/test_io.py index 5e96a3a36772..79f14eef7dff 100644 --- a/python/pyarrow/tests/test_io.py +++ b/python/pyarrow/tests/test_io.py @@ -27,7 +27,6 @@ import pathlib import pytest import random -import subprocess import sys import tempfile import weakref @@ -1218,21 +1217,8 @@ def test_memory_map_resize(tmpdir): def test_memory_map_resize_uninitialized(): - code = """if 1: - import pyarrow as pa - - try: + with pytest.raises(ValueError, match="I/O operation on closed file"): pa.MemoryMappedFile().resize(0) - except ValueError as exc: - assert str(exc) == "I/O operation on closed file" - else: - raise AssertionError("expected ValueError") - """ - res = subprocess.run([sys.executable, "-c", code], - universal_newlines=True, stderr=subprocess.PIPE) - if res.returncode != 0: - print(res.stderr, file=sys.stderr) - res.check_returncode() def test_memory_zero_length(tmpdir):

AltStyle によって変換されたページ (->オリジナル) /