From 84603866ab669b1f58cc1f5e315e233fa83cd6b6 Mon Sep 17 00:00:00 2001 From: nhz2 Date: Tue, 19 May 2026 13:53:23 -0400 Subject: [PATCH] Reject PyArray buffer with suboffsets --- src/Wrap/PyArray.jl | 2 ++ test/Wrap.jl | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/Wrap/PyArray.jl b/src/Wrap/PyArray.jl index 7b1dc8d3..dc11c085 100644 --- a/src/Wrap/PyArray.jl +++ b/src/Wrap/PyArray.jl @@ -531,6 +531,8 @@ end function PyArraySource_Buffer(x::Py) memview = pybuiltins.memoryview(x) buf = C.UnsafePtr(C.PyMemoryView_GET_BUFFER(memview)) + buf.suboffsets[] == C_NULL || + error("PyArray does not support buffers with non-trivial suboffsets (PIL-style indirect layout)") PyArraySource_Buffer(x, memview, buf) end diff --git a/test/Wrap.jl b/test/Wrap.jl index c42d45ab..5665003e 100644 --- a/test/Wrap.jl +++ b/test/Wrap.jl @@ -91,6 +91,17 @@ end end +@testitem "PyArray buffer with suboffsets is rejected" begin + tb = pyimport("_testbuffer") + nd = tb.ndarray( + pylist([1, 2, 3, 4, 5, 6]), + shape = pylist([2, 3]), + format = "i", + flags = tb.ND_PIL | tb.ND_WRITABLE, + ) + @test_throws Exception PyArray(nd; array = false, buffer = true) +end + @testitem "PyDict" begin x = pydict(["foo" => 12]) y = PyDict(x)