Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Wrap/PyArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,13 @@ 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)
# Strides from the size make the unit first stride a compile-time constant, so loops vectorise.
# Also matters for N == 1, where this method is picked over `pyarray_offset(x, i::Int)`.
if (:contiguous in F)
sum((i .- 1) .* Utils.size_to_fstrides(sizeof(pyarray_get_R(T)), x.size))
else
sum((i .- 1) .* x.strides)
end
end

function pyarray_load(::Type{T}, p::Ptr{R}) where {T,R}
Expand Down
12 changes: 12 additions & 0 deletions test/Wrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@
)
@test_throws Exception PyArray(nd; array = false, buffer = true)
end
@testset "contiguous 2D" begin
tb = pyimport("_testbuffer")
nd = tb.ndarray(
pylist(1:6),
shape = pylist([2, 3]),
format = "i",
flags = tb.ND_FORTRAN,
)
a = PyArray(nd; array = false)
@test a isa PyArray{Cint,2,(:linear,:contiguous)}
@test [a[i, j] for i = 1:2, j = 1:3] == reshape(1:6, 2, 3)
end
end

@testitem "PyDict" begin
Expand Down
Loading