Skip to content

Commit 37deeb3

Browse files
author
Christopher Doris
committed
PyArray is a wrapper type
1 parent 8d536ba commit 37deeb3

File tree

2 files changed

+85
-12
lines changed

2 files changed

+85
-12
lines changed

src/pywrap/PyArray.jl

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,28 @@ struct PyArray{T,N,M,L,R} <: AbstractArray{T,N}
3636
end
3737
export PyArray
3838

39+
ispy(::PyArray) = true
40+
Py(x::PyArray) = x.py
41+
3942
for N in (missing, 1, 2)
4043
for M in (missing, true, false)
4144
for L in (missing, true, false)
4245
for R in (true, false)
43-
name = Symbol(
44-
"Py",
45-
M === missing ? "" : M ? "Mutable" : "Immutable",
46-
L === missing ? "" : L ? "Linear" : "Cartesian",
47-
R ? "Raw" : "",
48-
N === missing ? "Array" : N == 1 ? "Vector" : "Matrix",
49-
)
50-
name == :PyArray && continue
51-
vars = Any[:T, N===missing ? :N : N, M===missing ? :M : M, L===missing ? :L : L, R ? :T : :R]
52-
@eval const $name{$(unique([v for v in vars if v isa Symbol])...)} = PyArray{$(vars...)}
53-
@eval export $name
46+
name = Symbol(
47+
"Py",
48+
M === missing ? "" : M ? "Mutable" : "Immutable",
49+
L === missing ? "" : L ? "Linear" : "Cartesian",
50+
R ? "Raw" : "",
51+
N === missing ? "Array" : N == 1 ? "Vector" : "Matrix",
52+
)
53+
name == :PyArray && continue
54+
vars = Any[:T, N===missing ? :N : N, M===missing ? :M : M, L===missing ? :L : L, R ? :T : :R]
55+
@eval const $name{$(unique([v for v in vars if v isa Symbol])...)} = PyArray{$(vars...)}
56+
@eval export $name
57+
end
5458
end
5559
end
5660
end
57-
end
5861

5962
(::Type{A})(x; array::Bool=true, buffer::Bool=true, copy::Bool=true) where {A<:PyArray} = @autopy x begin
6063
r = pyarray_make(A, x_, array=array, buffer=buffer, copy=copy)

test/pywrap.jl

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,74 @@
11
@testitem "PyArray" begin
2+
x = pyimport("array").array("i", [1,2,3])
3+
y = PyArray(x)
4+
z = PyArray{Cint,1,false,false,Cint}(x)
5+
@testset "construct" begin
6+
@test y isa PyArray{Cint,1,true,true,Cint}
7+
@test z isa PyArray{Cint,1,false,false,Cint}
8+
@test PythonCall.ispy(y)
9+
@test PythonCall.ispy(z)
10+
@test Py(y) === x
11+
@test Py(z) === x
12+
end
13+
@testset "length" begin
14+
@test length(y) === 3
15+
@test length(z) === 3
16+
end
17+
@testset "size" begin
18+
@test size(y) === (3,)
19+
@test size(z) === (3,)
20+
end
21+
@testset "IndexStyle" begin
22+
@test Base.IndexStyle(y) === Base.IndexLinear()
23+
@test Base.IndexStyle(z) === Base.IndexCartesian()
24+
end
25+
@testset "strides" begin
26+
@test strides(y) === (1,)
27+
@test strides(z) === (1,)
28+
end
29+
@testset "getindex" begin
30+
@test_throws BoundsError y[0]
31+
@test y[1] == 1
32+
@test y[2] == 2
33+
@test y[3] == 3
34+
@test_throws BoundsError y[4]
35+
@test_throws BoundsError z[0]
36+
@test z[1] == 1
37+
@test z[2] == 2
38+
@test z[3] == 3
39+
@test_throws BoundsError z[4]
40+
end
41+
@testset "copy" begin
42+
@test copy(y) == [1, 2, 3]
43+
@test copy(z) == [1, 2, 3]
44+
end
45+
@testset "setindex!" begin
46+
@test_throws BoundsError y[0] = 0
47+
y[2] = 22
48+
@test y[2] == 22
49+
y[2] = 2
50+
@test y[2] == 2
51+
@test_throws BoundsError y[4] = 0
52+
@test_throws Exception z[0] = 0
53+
@test_throws Exception z[1] = 0
54+
@test_throws Exception z[2] = 0
55+
@test_throws Exception z[3] = 0
56+
@test_throws Exception z[4] = 0
57+
end
58+
@testset "mutate" begin
59+
y[2] = 22
60+
@test pyconvert(Int, x[1]) == 22
61+
@test y[2] == 22
62+
@test z[2] == 22
63+
y[2] = 2
64+
x[2] = 33
65+
@test pyconvert(Int, x[2]) == 33
66+
@test y[3] == 33
67+
@test z[3] == 33
68+
x[2] = 3
69+
@test y == [1, 2, 3]
70+
@test z == [1, 2, 3]
71+
end
272
end
373

474
@testitem "PyDict" begin

0 commit comments

Comments
 (0)