From ecdeb432b6e3f16040f77531aef8a75d169abcc9 Mon Sep 17 00:00:00 2001 From: nhz2 Date: Tue, 19 May 2026 14:08:29 -0400 Subject: [PATCH 1/2] Implement try_strides --- src/Wrap/PyArray.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Wrap/PyArray.jl b/src/Wrap/PyArray.jl index 7b1dc8d3..37d9aca4 100644 --- a/src/Wrap/PyArray.jl +++ b/src/Wrap/PyArray.jl @@ -636,6 +636,17 @@ Base.strides(x::PyArray{T,N,M,L,R}) where {T,N,M,L,R} = error("strides are not a multiple of element size") end +@static if isdefined(Base, :try_strides) + Base.try_strides(x::PyArray{T,N,M,L,T}) where {T,N,M,L} = + if all(mod.(x.strides, sizeof(T)) .== 0) + div.(x.strides, sizeof(T)) + else + nothing + end + Base.is_ptr_loadable(x::PyArray{T,N,M,L,T}) where {T,N,M,L} = true + Base.is_ptr_storable(x::PyArray{T,N,M,L,T}) where {T,N,M,L} = Utils.ismutablearray(x) +end + function Base.showarg(io::IO, x::PyArray{T,N}, toplevel::Bool) where {T,N} toplevel || print(io, "::") print(io, "PyArray{") From f5b2524055af89f93fb6ce13fb4300ee0b37405b Mon Sep 17 00:00:00 2001 From: nhz2 Date: Wed, 8 Jul 2026 16:25:44 -0400 Subject: [PATCH 2/2] traits --- src/Wrap/PyArray.jl | 24 ++++++++---------------- test/Wrap.jl | 12 ++++++------ 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/src/Wrap/PyArray.jl b/src/Wrap/PyArray.jl index 37d9aca4..a7710e18 100644 --- a/src/Wrap/PyArray.jl +++ b/src/Wrap/PyArray.jl @@ -627,24 +627,16 @@ Base.IndexStyle(::Type{PyArray{T,N,M,L,R}}) where {T,N,M,L,R} = Base.unsafe_convert(::Type{Ptr{T}}, x::PyArray{T,N,M,L,T}) where {T,N,M,L} = x.ptr -Base.elsize(::Type{PyArray{T,N,M,L,T}}) where {T,N,M,L} = sizeof(T) +# Setting this to 1 means strides will be interpreted as bytes not elements +Base.elsize(::Type{PyArray{T,N,M,L,T}}) where {T,N,M,L} = 1 -Base.strides(x::PyArray{T,N,M,L,R}) where {T,N,M,L,R} = - if all(mod.(x.strides, sizeof(R)) .== 0) - div.(x.strides, sizeof(R)) - else - error("strides are not a multiple of element size") - end +Base.strides(x::PyArray{T,N,M,L,R}) where {T,N,M,L,R} = x.strides -@static if isdefined(Base, :try_strides) - Base.try_strides(x::PyArray{T,N,M,L,T}) where {T,N,M,L} = - if all(mod.(x.strides, sizeof(T)) .== 0) - div.(x.strides, sizeof(T)) - else - nothing - end - Base.is_ptr_loadable(x::PyArray{T,N,M,L,T}) where {T,N,M,L} = true - Base.is_ptr_storable(x::PyArray{T,N,M,L,T}) where {T,N,M,L} = Utils.ismutablearray(x) +@static if isdefined(Base, :is_strided) + Base.is_strided(::Type{<:PyArray{T,N,M,L,T}}) where {T,N,M,L} = true + Base.is_vec_strided(::Type{<:PyArray{T,N,M,L,T}}) where {T,N,M,L} = L + Base.is_ptr_loadable(::Type{<:PyArray{T,N,M,L,T}}) where {T,N,M,L} = true + Base.is_ptr_storable(::Type{<:PyArray{T,N,M,L,T}}) where {T,N,M,L} = M end function Base.showarg(io::IO, x::PyArray{T,N}, toplevel::Bool) where {T,N} diff --git a/test/Wrap.jl b/test/Wrap.jl index c42d45ab..88c5f813 100644 --- a/test/Wrap.jl +++ b/test/Wrap.jl @@ -23,14 +23,14 @@ @test Base.IndexStyle(z) === Base.IndexCartesian() end @testset "strides" begin - @test strides(y) === (1,) - @test strides(z) === (1,) + @test strides(y) === (4,) + @test strides(z) === (4,) end @testset "elsize" begin - @test Base.elsize(y) === sizeof(Cint) - @test Base.elsize(z) === sizeof(Cint) - @test Base.elsize(PyArray{Cint,1,true,true,Cint}) === sizeof(Cint) - @test Base.elsize(PyArray{Cint,1,false,false,Cint}) === sizeof(Cint) + @test Base.elsize(y) === 1 + @test Base.elsize(z) === 1 + @test Base.elsize(PyArray{Cint,1,true,true,Cint}) === 1 + @test Base.elsize(PyArray{Cint,1,false,false,Cint}) === 1 @test_throws Exception elsize(PyArray{Cint,1,true,false,Cchar}) end @testset "getindex" begin