JuliaIO / JLD.jl

Saving and loading julia variables while preserving native types
MIT License
277 stars 55 forks source link

Error when reconstructing JLD type with filednames of non-ascii characters #235

Closed shipengcheng1230 closed 3 years ago

shipengcheng1230 commented 5 years ago

Hello, consider the following example:

julia> using JLD

julia> struct MyStruct
       σ
       end

julia> a = MyStruct(5.5);

julia> save("tmp.jld", "a", a);

Then I restart the julia terminal,

julia> using JLD

julia> load("tmp.jld", "a")
┌ Warning: type MyStruct not present in workspace; reconstructing
└ @ JLD ~/.julia/packages/JLD/1BoSz/src/jld_types.jl:703
ERROR: StringIndexError("σ_", 2)
Stacktrace:
 [1] string_index_err(::String, ::Int64) at ./strings/string.jl:12
 [2] getindex(::String, ::UnitRange{Int64}) at ./strings/string.jl:246
 [3] reconstruct_type(::JLD.JldFile, ::HDF5.HDF5Datatype, ::String) at /Users/spc/.julia/packages/JLD/1BoSz/src/jld_types.jl:762
 [4] macro expansion at ./logging.jl:305 [inlined]
 [5] jldatatype(::JLD.JldFile, ::HDF5.HDF5Datatype) at /Users/spc/.julia/packages/JLD/1BoSz/src/jld_types.jl:703
 [6] read(::JLD.JldDataset) at /Users/spc/.julia/packages/JLD/1BoSz/src/JLD.jl:370
 [7] read(::JLD.JldFile, ::String) at /Users/spc/.julia/packages/JLD/1BoSz/src/JLD.jl:346
 [8] #42 at /Users/spc/.julia/packages/JLD/1BoSz/src/JLD.jl:1240 [inlined]
 [9] #jldopen#14(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::getfield(JLD, Symbol("##42#43")){String}, ::String, ::Vararg{String,N} where N) at /Users/spc/.julia/packages/JLD/1BoSz/src/JLD.jl:246
 [10] jldopen at /Users/spc/.julia/packages/JLD/1BoSz/src/JLD.jl:244 [inlined]
 [11] load at /Users/spc/.julia/packages/JLD/1BoSz/src/JLD.jl:1239 [inlined]
 [12] #load#13(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::String, ::String) at /Users/spc/.julia/packages/FileIO/lt9I0/src/loadsave.jl:118
 [13] load(::String, ::String) at /Users/spc/.julia/packages/FileIO/lt9I0/src/loadsave.jl:118
 [14] top-level scope at none:0

I am running:

julia> versioninfo()
Julia Version 1.0.2

julia> JLD.version_current
v"0.1.2"

It would be fine if the fieldname is something like "a", "b", etc. I think that may be related to this. Could be nice if this is improved. Thanks!

AmebaBrain commented 5 years ago

same here inside reconstruct_type method it finds index and then takes substring

idx = first(something(findlast("_", membername), 0:-1))
fieldname = fieldnames[i] = Symbol(membername[1:idx-1])

in calculation idx = 3 and we end up with

ulia> "μ_"[1:2]
ERROR: StringIndexError("μ_", 2)

julia> "ab"[1:2]
"ab"

julia> "μ_"[1]
'μ': Unicode U+03bc (category Ll: Letter, lowercase)

julia> "μ_"[2]
ERROR: StringIndexError("μ_", 2)