Skip to content

Commit

Permalink
Add elsize method for PyArray (#593)
Browse files Browse the repository at this point in the history
Co-authored-by: Christopher Doris <github.com/cjdoris>
  • Loading branch information
cjdoris authored Feb 21, 2025
1 parent 2522a06 commit d4baa18
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
* Added `PYTHON_JULIACALL_HEAP_SIZE_HINT` option to configure initial Julia heap size.
* `Base.elsize` now defined for `PyArray`.

## 0.9.24 (2025-01-22)
* Bug fixes.
Expand Down
2 changes: 2 additions & 0 deletions src/Wrap/PyArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,8 @@ 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)

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))
Expand Down
7 changes: 7 additions & 0 deletions test/Wrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
@test strides(y) === (1,)
@test strides(z) === (1,)
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_throws Exception elsize(PyArray{Cint,1,true,false,Cchar})
end
@testset "getindex" begin
@test_throws BoundsError y[0]
@test y[1] == 1
Expand Down

0 comments on commit d4baa18

Please sign in to comment.