Skip to content

Commit 2c711d3

Browse files
Fix support for 0d arrays (#844)
This was accidentally removed in the PyArray reparametrization. Co-authored-by: Christopher Rowley <cjdrwly@gmail.com>
1 parent c50fd4a commit 2c711d3

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎src/Wrap/PyArray.jl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,8 @@ function pyarray_offset(x::PyArray{T,N,F}, i::Vararg{Int,N}) where {T,N,F}
644644
if N >= 1 && (:contiguous in F)
645645
sum((i .- 1) .* (sizeof(pyarray_get_R(T)), x.strides[2:end]...))
646646
else
647-
sum((i .- 1) .* x.strides)
647+
# `init` covers 0-d arrays indexed as `x[]`, where `i` is empty
648+
sum((i .- 1) .* x.strides; init=0)
648649
end
649650
end
650651

‎test/Wrap.jl‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@
100100
)
101101
@test_throws Exception PyArray(nd; array = false, buffer = true)
102102
end
103+
@testset "0-d" begin
104+
tb = pyimport("_testbuffer")
105+
nd = tb.ndarray(5, shape = pylist(), flags = tb.ND_WRITABLE)
106+
a = PyArray(nd; array = false, buffer = true)
107+
@test ndims(a) == 0
108+
@test a[] == 5
109+
a[] = 6
110+
@test a[] == 6
111+
@test pyconvert(Int, nd.tolist()) == 6
112+
end
103113
@testset "contiguous 2D" begin
104114
tb = pyimport("_testbuffer")
105115
nd = tb.ndarray(

0 commit comments

Comments
 (0)