|
215 | 215 | end |
216 | 216 | end |
217 | 217 |
|
218 | | -@testitem "array" begin |
| 218 | +@testitem "array" setup=[Setup] begin |
219 | 219 | @testset "type" begin |
220 | 220 | @test pyis(pytype(pyjl(fill(nothing))), PythonCall.pyjlarraytype) |
221 | 221 | @test pyis(pytype(pyjl([1 2; 3 4])), PythonCall.pyjlarraytype) |
|
313 | 313 | @test pyjlvalue(x) == [0 2; 3 4] |
314 | 314 | @test pyjlvalue(y) == [1 2; 3 4] |
315 | 315 | end |
| 316 | + @testset "__array__" begin |
| 317 | + if Setup.devdeps |
| 318 | + np = pyimport("numpy") |
| 319 | + |
| 320 | + numeric = pyjl(Float64[1, 2, 3]) |
| 321 | + numeric_array = numeric.__array__() |
| 322 | + @test pyisinstance(numeric_array, np.ndarray) |
| 323 | + @test pyconvert(Vector{Float64}, numeric_array) == [1.0, 2.0, 3.0] |
| 324 | + |
| 325 | + numeric_no_copy = numeric.__array__(copy=false) |
| 326 | + numeric_data = pyjlvalue(numeric) |
| 327 | + numeric_data[1] = 42.0 |
| 328 | + @test pyconvert(Vector{Float64}, numeric_no_copy) == [42.0, 2.0, 3.0] |
| 329 | + |
| 330 | + string_array = pyjl(["a", "b"]) |
| 331 | + string_result = string_array.__array__() |
| 332 | + @test pyisinstance(string_result, np.ndarray) |
| 333 | + @test pyconvert(Vector{String}, pybuiltins.list(string_result)) == ["a", "b"] |
| 334 | + |
| 335 | + err = try |
| 336 | + string_array.__array__(copy=false) |
| 337 | + nothing |
| 338 | + catch err |
| 339 | + err |
| 340 | + end |
| 341 | + @test err !== nothing |
| 342 | + @test err isa PythonCall.PyException |
| 343 | + @test pyis(err._t, pybuiltins.ValueError) |
| 344 | + @test occursin( |
| 345 | + "copy=False is not supported when collecting ArrayValue data", |
| 346 | + sprint(showerror, err), |
| 347 | + ) |
| 348 | + else |
| 349 | + @test_skip Setup.devdeps |
| 350 | + end |
| 351 | + end |
316 | 352 | @testset "array_interface" begin |
317 | 353 | x = pyjl(Float32[1 2 3; 4 5 6]).__array_interface__ |
318 | 354 | @test pyisinstance(x, pybuiltins.dict) |
|
0 commit comments