|
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 | + np = pyimport("numpy") |
| 318 | + |
| 319 | + numeric = pyjl(Float64[1, 2, 3]) |
| 320 | + numeric_array = numeric.__array__() |
| 321 | + @test pyisinstance(numeric_array, np.ndarray) |
| 322 | + @test pyconvert(Vector{Float64}, numeric_array) == [1.0, 2.0, 3.0] |
| 323 | + |
| 324 | + numeric_no_copy = numeric.__array__(copy=false) |
| 325 | + numeric_data = pyjlvalue(numeric) |
| 326 | + numeric_data[1] = 42.0 |
| 327 | + @test pyconvert(Vector{Float64}, numeric_no_copy) == [42.0, 2.0, 3.0] |
| 328 | + |
| 329 | + string_array = pyjl(["a", "b"]) |
| 330 | + string_result = string_array.__array__() |
| 331 | + @test pyisinstance(string_result, np.ndarray) |
| 332 | + @test pyconvert(Vector{String}, pybuiltins.list(string_result)) == ["a", "b"] |
| 333 | + |
| 334 | + err = try |
| 335 | + string_array.__array__(copy=false) |
| 336 | + nothing |
| 337 | + catch err |
| 338 | + err |
| 339 | + end |
| 340 | + @test err !== nothing |
| 341 | + @test err isa PythonCall.PyException |
| 342 | + @test pyis(err._t, pybuiltins.ValueError) |
| 343 | + @test occursin( |
| 344 | + "copy=False is not supported when collecting ArrayValue data", |
| 345 | + sprint(showerror, err), |
| 346 | + ) |
| 347 | + end |
316 | 348 | @testset "array_interface" begin |
317 | 349 | x = pyjl(Float32[1 2 3; 4 5 6]).__array_interface__ |
318 | 350 | @test pyisinstance(x, pybuiltins.dict) |
|
0 commit comments