Skip to content

Commit

Permalink
v0.5.2 (#598)
Browse files Browse the repository at this point in the history
* remove warning that is no longer relevant (#595)

* only warn for anon functions (#597)

* only warn for anon functions

* compat for v1.6

* can't test for warning if warning is removed

* improve test

* bump version
  • Loading branch information
JonasIsensee authored Sep 18, 2024
1 parent 3647927 commit b54623f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 44 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.2
- Remove left-over warning
- Restrict warning for storing `Function` objects to `anonymous` functions.

## 0.5.1
- Bugfix and added test for bug introduced in v0.5.0

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "JLD2"
uuid = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
version = "0.5.1"
version = "0.5.2"

[deps]
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Expand Down
3 changes: 1 addition & 2 deletions src/data/reconstructing_datatypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,8 @@ function reconstruct_odr(f::JLDFile, dt::CompoundDatatype,
i = findfirst(==(Symbol(k)), dt.names)
if typeref != NULL_REFERENCE
dtrr = jltype(f, f.datatype_locations[typeref])
odr_sizeof(dtrr) != 0 && @warn "Field $k has non-zero size in file, this should not happen"
elseif !isnothing(i)
offset = dt.offsets[i]
offset == dt.offsets[i] || throw(InternalError("Field offsets were incorrectly mapped."))
dtrr = jltype(f, dt.members[i])
else
throw(InternalError("Field $k not found in datatype"))
Expand Down
17 changes: 9 additions & 8 deletions src/data/writing_datatypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ end
if f.disable_commit
throw(ArgumentError("Attempted to commit DataType $readas but committing is disabled."))
end
io = f.io

# This needs to be written this way or type inference gets unhappy...
# Also needs to happen here so that we write the DataType type
Expand All @@ -229,7 +228,7 @@ end

offset = f.end_of_data

seek(io, offset)
seek(f.io, offset)
id = length(f.datatypes)+1
h5o = h5offset(f, offset)
cdt = CommittedDatatype(h5o, id)
Expand Down Expand Up @@ -383,15 +382,15 @@ const H5TYPE_DATATYPE = CompoundDatatype(
)

function h5fieldtype(f::JLDFile, ::Type{T}, readas::Type, ::Initialized) where T<:DataType
if f.disable_commit
throw(ArgumentError("Attempted to commit DataType $readas but committing is disabled."))
end
if !(readas <: DataType) || (T isa Type{Type{T}} where T)
@lookup_committed f readas
return commit(f, H5TYPE_DATATYPE, DataType, readas)
end

@lookup_committed f DataType
if f.disable_commit
throw(ArgumentError("Attempted to commit DataType $readas but committing is disabled."))
end
io = f.io
offset = f.end_of_data

Expand Down Expand Up @@ -446,6 +445,11 @@ end

function h5convert!(out::Pointers, ::DataTypeODR, f::JLDFile, T::DataType, wsession::JLDWriteSession)
t = typename(T)
if T <: Function && isgensym(nameof(T.instance))
@warn LazyString("Attempting to store ", T, ".\n",
"JLD2 only stores functions by name.\n",
" This may not be useful for anonymous functions.")
end
store_vlen!(out, UInt8, f, unsafe_wrap(Vector{UInt8}, t), f.datatype_wsession)
if isempty(T.parameters)
h5convert_uninitialized!(out+odr_sizeof(Vlen{UInt8}), Vlen{UInt8})
Expand Down Expand Up @@ -627,9 +631,6 @@ end
# fieldodr, but actually encoding the data for things that odr stores
# as references
@nospecializeinfer function odr(@nospecialize(T::Type))
if T <: Function
@warn LazyString("Attempting to store ", T, ".\n Function types cannot be propertly stored in JLD2 files.\n Loading may yield unexpected results.")
end
if !hasdata(T)
# A pointer singleton or ghost. We need to write something, but we'll
# just write a single byte.
Expand Down
6 changes: 6 additions & 0 deletions src/julia_compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ end
end
else
using Base: @nospecializeinfer
end

@static if VERSION < v"1.7.0"
isgensym(s::Symbol) = '#' in string(s)
else
using Base: isgensym
end
69 changes: 36 additions & 33 deletions test/loadsave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -745,44 +745,47 @@ end
end

@testset "Disable committing datatypes" begin
cd(mktempdir()) do
jldopen("test.jld2", "w") do f
f.disable_commit = true
fn = joinpath(mktempdir(), "disable_committing_datatypes.jld2")
jldopen(fn, "w") do f
f.disable_commit = true

@test_throws ArgumentError f["1"] = Dict(1=>2)
@test_throws ArgumentError f["2"] = Vector{Float64}
@test_throws ArgumentError f["3"] = (1,2,3)
# this could eventually be allowed
@test_throws ArgumentError f["4"] = (; a=1, b=2)
end
@test_throws ArgumentError f["1"] = Dict(1=>2)
@test_throws ArgumentError f["2"] = Vector{Float64}
@test_throws ArgumentError f["3"] = (1,2,3)
@test_throws ArgumentError f["4"] = :asymbol
# No throw
f["5"] = "a string"
# this could eventually be allowed
@test_throws ArgumentError f["4"] = (; a=1, b=2)
end
end


@testset "Missing Types in Tuples" begin
cd(mktempdir()) do
eval(:(module ModuleWithFunction
fun(x) = x+1
end))
if VERSION v"1.7"
@test_warn(
contains("Function types cannot"),
eval(:(save_object("test.jld2", (1, ModuleWithFunction.fun, 2))))
)
else
eval(:(save_object("test.jld2", (1, ModuleWithFunction.fun, 2))))
end
obj = load_object("test.jld2")
@test length(obj) == 3
@test obj[1] == 1
@test obj[2] == eval(:(ModuleWithFunction.fun))
@test obj[3] == 2

eval(:(module ModuleWithFunction end))
obj = load_object("test.jld2")
@test length(obj) == 3
@test obj[1] == 1
@test JLD2.isreconstructed(obj[2])
@test obj[3] == 2
fn = joinpath(mktempdir(), "missing_types_in_tuple.jld2")
eval(:(module ModuleWithFunction
fun(x) = x+1
end))
eval(:(save_object($fn, (1, ModuleWithFunction.fun, 2))))
obj = load_object(fn)
@test length(obj) == 3
@test obj[1] == 1
@test obj[2] == eval(:(ModuleWithFunction.fun))
@test obj[3] == 2

eval(:(module ModuleWithFunction end))
obj = load_object(fn)
@test length(obj) == 3
@test obj[1] == 1
@test JLD2.isreconstructed(obj[2])
@test obj[3] == 2

end

if VERSION v"1.7"
@testset "Storing an anonymous function" begin
fn = joinpath(mktempdir(), "storing_anon_function.jld2")
f = x -> x+1
@test_warn contains("Attempting to store") save_object(fn, f)
end
end

2 comments on commit b54623f

@JonasIsensee
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/115403

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.2 -m "<description of version>" b54623fd71aa7450ef1e1497a119a584f25c6a66
git push origin v0.5.2

Please sign in to comment.