Skip to content

Commit cfc4b94

Browse files
author
Christopher Doris
committed
pyarray now serializable
1 parent 898cb77 commit cfc4b94

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

docs/src/releasenotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44
* New unexported functions: `python_executable_path`, `python_library_path`, `python_library_handle` and `python_version`.
55
* `Py` is now treated as a scalar when broadcasting.
6+
* `PyArray` is now serializable.
67
* Bug fixes.
78

89
## 0.9.15 (2023-10-25)

src/Compat/Compat.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Compat
88
using ..Core
99
using ..Core: C, Utils, pynew, incref, getptr, pycopy!, pymodulehooks, pyisnull, pybytes_asvector, pysysmodule, pyosmodule, pystr_fromUTF8
1010
using ..Convert: pyconvert, @pyconvert
11-
using ..Wrap: PyPandasDataFrame
11+
using ..Wrap: PyArray, PyPandasDataFrame
1212
using Serialization: Serialization, AbstractSerializer, serialize, deserialize
1313
using Tables: Tables
1414
using Requires: @require

src/Compat/serialization.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,19 @@ function Serialization.serialize(s::AbstractSerializer, x::PyException)
4242
end
4343

4444
Serialization.deserialize(s::AbstractSerializer, ::Type{PyException}) = PyException(deserialize_py(s))
45+
46+
### PyArray
47+
#
48+
# This type holds a pointer and a handle (usually a python memoryview or capsule) which are
49+
# not serializable by default, and even if they were would not be consistent after
50+
# serializing each field independently. So we just serialize the wrapped Python object.
51+
52+
function Serialization.serialize(s::AbstractSerializer, x::PyArray)
53+
Serialization.serialize_type(s, typeof(x), false)
54+
serialize_py(s, x.py)
55+
end
56+
57+
function Serialization.deserialize(s::AbstractSerializer, ::Type{T}) where {T<:PyArray}
58+
# TODO: set buffer and array args too?
59+
T(deserialize_py(s); copy=false)
60+
end

test/Wrap.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@
6969
@test y == [1, 2, 3]
7070
@test z == [1, 2, 3]
7171
end
72+
@testset "serialize" begin
73+
using Serialization: serialize, deserialize
74+
io = IOBuffer()
75+
serialize(io, y)
76+
seekstart(io)
77+
y2 = deserialize(io)
78+
@test typeof(y) == typeof(y2)
79+
@test eltype(y) == eltype(y2)
80+
@test ndims(y) == ndims(y2)
81+
@test size(y) == size(y2)
82+
@test strides(y) == strides(y2)
83+
@test y == y2
84+
end
7285
end
7386

7487
@testitem "PyDict" begin

0 commit comments

Comments
 (0)