Skip to content

Commit

Permalink
fix #288
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasIsensee committed May 16, 2021
1 parent a582aef commit a14ae67
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 52 deletions.
14 changes: 8 additions & 6 deletions src/data/reconstructing_datatypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,22 @@ function jltype(f::JLDFile, cdt::CommittedDatatype)
mtname = gensym()
stn.name = Symbol(string(mtname) * string(gensym()))

datatype = new_typename(TypeName, stn).wrapper

f.type_map[writtenname] = datatype
newtype = new_typename(TypeName, stn).wrapper
f.type_map[writtenname] = newtype

if !isempty(datatype.parameters)
newtype = newtype{datatype.parameters...}
end
# Need fully created type before adding methods
rr, canonical = constructrr(f, datatype, dt, attrs)::Tuple{ReadRepresentation,Bool}
canonical && (f.jlh5type[datatype] = cdt)
rr, canonical = constructrr(f, newtype, dt, attrs)::Tuple{ReadRepresentation,Bool}
canonical && (f.jlh5type[newtype] = cdt)
f.datatypes[cdt.index] = dt
f.h5jltype[cdt] = rr

# Finally add methods
smt = read_attr_data(f, method_table_attr)
smt.name = mtname
add_method_table(datatype.name, smt)
add_method_table(newtype.name, smt)

return rr
end
Expand Down
41 changes: 0 additions & 41 deletions test/customserialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,44 +241,3 @@ end
read_tests(file, "k", k)
close(file)
end


# Function Reconstruction (does not strictly belong to custom serialization but was
# broken by it)
struct S1
a
f
end

struct S2{F}
a
f::F
end

struct S3{F}
f::F
end

λ() = 42

function round_trip(x)
mktempdir() do dir
fn = joinpath(dir, "test.jld2")
@save fn grp=x
@load fn grp
return grp
end
end

@testset "round trip Function values" begin
@test 42 == round_trip(λ)()
@test 42 == first(round_trip((λ,)))()
@test 42 == first(round_trip((λ, λ)))()
@test 42 == first(round_trip((λ, 42)))()
@test 42 == first(round_trip([λ]))()
@test 42 == first(round_trip([λ, λ]))()
@test 42 == first(round_trip([λ, 42]))()
@test 42 == round_trip(S1(42, λ)).f()
@test 42 == round_trip(S2(42, λ)).f()
@test 42 == round_trip(S3(λ)).f()
end
41 changes: 37 additions & 4 deletions test/recon_funcs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ end
@test 42 == round_trip(S3(λ)).f()
end

# Same as in "modules-nested.jl". Only here to make script run as standalone.
function better_success(cmd)
fn1, _ = mktemp()
fn2, _ = mktemp()
try
run(pipeline(cmd, stdout=fn1, stderr=fn2))
catch
println(String(read(fn1)))
println(String(read(fn2)))
return false
end
return true
end


@testset "Anonymous Functions" begin
fn = joinpath(mktempdir(), "test.jld2")

Expand Down Expand Up @@ -97,6 +112,17 @@ end
# Global Ref
f3 = () -> global_variable
# With struct
struct SomeStructWithFunctionInside
x::Int
func::Function
function SomeStructWithFunctionInside(x)
return new(x, (y)-> y * exp(x))
end
end
module AnonFunctionModule
# "Pure" function
f4 = (x) -> sqrt(x)
Expand All @@ -111,7 +137,7 @@ end
f6 = () -> module_variable
end
using .AnonFunctionModule: f4, f5, f6
jldsave("$(fn)"; f1, f2, f3, f4, f5, f6)
jldsave("$(fn)"; f1, f2, f3, f4, f5, f6, a=SomeStructWithFunctionInside(1))
"""
open(saving_filename, "w") do io
println(io, saving_contents)
Expand All @@ -132,14 +158,21 @@ end
f6 = load(fn, "f6")
@test_throws UndefVarError f6()
global module_variable = 3
@test f6() == module_variable

module AnonFunctionModule
@test f6() == module_variable
@eval module AnonFunctionModule
module_variable = 4
end
f6 = load(fn, "f6")
f6()

# With struct
load(fn, "a")
struct SomeStructWithFunctionInside
x::Int
func::Function
SomeStructWithFunctionInside(x) = new(x, (y)-> y * exp(x))
end
@test_nowarn load(fn, "a")
end

end
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ include("isreconstructed.jl")
include("backwards_compatibility.jl")
include("inlineunion.jl")
include("customserialization.jl")
include("compression.jl")
include("compression.jl")
include("recon_funcs.jl")

0 comments on commit a14ae67

Please sign in to comment.