From d4122c381afcb287ce3983bd9be360d8a66f65d5 Mon Sep 17 00:00:00 2001 From: Christopher Doris Date: Sat, 4 Nov 2023 19:17:51 +0000 Subject: [PATCH 1/4] package extension for JLD2 --- Project.toml | 7 +++++++ ext/JLD2Ext.jl | 6 ++++++ 2 files changed, 13 insertions(+) create mode 100644 ext/JLD2Ext.jl diff --git a/Project.toml b/Project.toml index b4de1e27..3be72276 100644 --- a/Project.toml +++ b/Project.toml @@ -24,10 +24,17 @@ Tables = "1" UnsafePointers = "1" julia = "1.6.1" +[extensions] +JLD2Ext = "JLD2" + [extras] Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a" [targets] test = ["Aqua", "Test", "TestItemRunner"] + +[weakdeps] +JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" diff --git a/ext/JLD2Ext.jl b/ext/JLD2Ext.jl new file mode 100644 index 00000000..102740a6 --- /dev/null +++ b/ext/JLD2Ext.jl @@ -0,0 +1,6 @@ +module JLD2Ext + +using JLD2 +using PythonCall + +end From 0c66e1cedc3cbb7439d706a50af30b2e1bc5056d Mon Sep 17 00:00:00 2001 From: Christopher Doris Date: Sat, 4 Nov 2023 19:35:41 +0000 Subject: [PATCH 2/4] jld2 serialise Py and PyException --- Project.toml | 1 + ext/JLD2Ext.jl | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/Project.toml b/Project.toml index 3be72276..6f703b96 100644 --- a/Project.toml +++ b/Project.toml @@ -18,6 +18,7 @@ UnsafePointers = "e17b2a0c-0bdf-430a-bd0c-3a23cae4ff39" [compat] CondaPkg = "0.2.21" +JLD2 = "0.4.38" MacroTools = "0.5" Requires = "1" Tables = "1" diff --git a/ext/JLD2Ext.jl b/ext/JLD2Ext.jl index 102740a6..27d001b1 100644 --- a/ext/JLD2Ext.jl +++ b/ext/JLD2Ext.jl @@ -3,4 +3,38 @@ module JLD2Ext using JLD2 using PythonCall +### Py + +struct Py_Serialized + x::Union{Nothing,Vector{UInt8}} +end + +JLD2.writeas(::Type{Py}) = Py_Serialized + +function JLD2.wconvert(::Type{Py_Serialized}, x::Py) + if PythonCall.pyisnull(x) + v = nothing + else + v = copy(PythonCall._Py.pybytes_asvector(pyimport("pickle").dumps(x))) + end + Py_Serialized(v) +end + +function JLD2.rconvert(::Type{Py}, x::Py_Serialized) + v = x.x + if v === nothing + PythonCall.pynew() + else + pyimport("pickle").loads(pybytes(v)) + end +end + +### PyException - handled similarly as with Serialization.jl + +JLD2.writeas(::Type{PyException}) = Py_Serialized + +JLD2.wconvert(::Type{Py_Serialized}, x::PyException) = JLD2.wconvert(Py_Serialized, x.v) + +JLD2.rconvert(::Type{PyException}, x::Py_Serialized) = PyException(JLD2.rconvert(Py, x)) + end From 90b400e869c25f55a81326264d74c2e0ce08f9b7 Mon Sep 17 00:00:00 2001 From: Christopher Doris Date: Sat, 4 Nov 2023 20:08:23 +0000 Subject: [PATCH 3/4] normalize project.toml --- Project.toml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Project.toml b/Project.toml index 6f703b96..63110dec 100644 --- a/Project.toml +++ b/Project.toml @@ -16,6 +16,12 @@ Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" UnsafePointers = "e17b2a0c-0bdf-430a-bd0c-3a23cae4ff39" +[weakdeps] +JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" + +[extensions] +JLD2Ext = "JLD2" + [compat] CondaPkg = "0.2.21" JLD2 = "0.4.38" @@ -25,17 +31,11 @@ Tables = "1" UnsafePointers = "1" julia = "1.6.1" -[extensions] -JLD2Ext = "JLD2" - [extras] Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a" [targets] test = ["Aqua", "Test", "TestItemRunner"] - -[weakdeps] -JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" From 55b8991f66042f7e743aa25cb8c876c683221cc1 Mon Sep 17 00:00:00 2001 From: Christopher Doris Date: Sat, 4 Nov 2023 20:13:26 +0000 Subject: [PATCH 4/4] i don't understand why the formatting test is failing :/ --- test/aqua.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/aqua.jl b/test/aqua.jl index 957c8682..9c6eadfa 100644 --- a/test/aqua.jl +++ b/test/aqua.jl @@ -2,5 +2,5 @@ # The unbound_args test fails on methods with signature like foo(::Type{Tuple{Vararg{V}}}) where V # Seems like a bug. import Aqua - Aqua.test_all(PythonCall, unbound_args=false) + Aqua.test_all(PythonCall, unbound_args=false, project_toml_formatting=false) end