From 9365d0d089423cd6285b325d82d249e5c1772ba1 Mon Sep 17 00:00:00 2001 From: JonasIsensee Date: Tue, 19 Dec 2023 12:20:44 +0100 Subject: [PATCH] correct canbeuninitialized (#517) * correct canbeuninitialized * add test * non-transitive custom serialization --------- Co-authored-by: Jonas Isensee --- src/data/custom_serialization.jl | 2 +- test/loadsave.jl | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/data/custom_serialization.jl b/src/data/custom_serialization.jl index fcb4de1d..77acfe2e 100644 --- a/src/data/custom_serialization.jl +++ b/src/data/custom_serialization.jl @@ -38,7 +38,7 @@ h5convert_uninitialized!(out::Pointers, odr::Type{CustomSerialization{T,ODR}}) w h5convert_uninitialized!(out, ODR) jlconvert_canbeuninitialized(::ReadRepresentation{T,CustomSerialization{S,ODR}}) where {T,S,ODR} = - jlconvert_canbeuninitialized(ODR) + jlconvert_canbeuninitialized(ReadRepresentation{S,ODR}()) jlconvert_isinitialized(::ReadRepresentation{T,CustomSerialization{S,ODR}}, ptr::Ptr) where {T,S,ODR} = jlconvert_isinitialized(ReadRepresentation{S,ODR}(), ptr) diff --git a/test/loadsave.jl b/test/loadsave.jl index 6135b7b6..725c43c3 100644 --- a/test/loadsave.jl +++ b/test/loadsave.jl @@ -556,10 +556,16 @@ Base.@kwdef mutable struct T3 <: AT t2::T2 end -DSA=Dict{Symbol, Any} +# Custom Serialization is not transitive, so using a custom serialization via a type +# that is custom serialized itself will not work +mutable struct PseudoDict{K,V} + kv::Vector{Pair{K,V}} +end + +DSA=PseudoDict{Symbol, Any} JLD2.writeas(::Type{T}) where {T <: AT} = DSA -JLD2.wconvert(::Type{DSA}, t::AT) = DSA(f => getproperty(t, f) for f in fieldnames(typeof(t))) -JLD2.rconvert(::Type{T}, dsa::DSA) where {T <: AT} = T(; dsa...) +JLD2.wconvert(::Type{DSA}, t::AT) = DSA([f => getproperty(t, f) for f in fieldnames(typeof(t))]) +JLD2.rconvert(::Type{T}, dsa::DSA) where {T <: AT} = T(; dsa.kv...) @testset "Issue #431 Identity-Preservation" begin cd(mktempdir()) do @@ -687,3 +693,13 @@ end @test T == load("test.jld2", "T") end end + + +@testset "Issue #510 uninitialized Dicts" begin + cd(mktempdir()) do + save_object("test.jld2", Vector{Dict{String,Float64}}(undef, 10)) + o = load_object("test.jld2") + @test !any(isassigned.(Ref(o), eachindex(o))) + end + +end