From 1c2f69f93e5563cec7ce1b9fb2245e1fc400f490 Mon Sep 17 00:00:00 2001 From: JamesWrigley Date: Tue, 20 Oct 2026 11:39:08 +0200 Subject: [PATCH] Fix support for 0d arrays This was accidentally removed in the PyArray reparametrization. --- src/Wrap/PyArray.jl | 3 ++- test/Wrap.jl | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Wrap/PyArray.jl b/src/Wrap/PyArray.jl index 64bb3176..4b260ea3 100644 --- a/src/Wrap/PyArray.jl +++ b/src/Wrap/PyArray.jl @@ -639,7 +639,8 @@ function pyarray_offset(x::PyArray{T,N,F}, i::Int) where {T,N,F} end function pyarray_offset(x::PyArray{T,N,F}, i::Vararg{Int,N}) where {T,N,F} - sum((i .- 1) .* x.strides) + # `init` covers 0-d arrays indexed as `x[]`, where `i` is empty + sum((i .- 1) .* x.strides; init=0) end function pyarray_load(::Type{T}, p::Ptr{R}) where {T,R} diff --git a/test/Wrap.jl b/test/Wrap.jl index 4720cdd4..a8d2cfbe 100644 --- a/test/Wrap.jl +++ b/test/Wrap.jl @@ -100,6 +100,16 @@ ) @test_throws Exception PyArray(nd; array = false, buffer = true) end + @testset "0-d" begin + tb = pyimport("_testbuffer") + nd = tb.ndarray(5, shape = pylist(), flags = tb.ND_WRITABLE) + a = PyArray(nd; array = false, buffer = true) + @test ndims(a) == 0 + @test a[] == 5 + a[] = 6 + @test a[] == 6 + @test pyconvert(Int, nd.tolist()) == 6 + end end @testitem "PyDict" begin