Skip to content

Commit

Permalink
Merge pull request #77 from simonster/aa/0.7
Browse files Browse the repository at this point in the history
Fixes and updates for Julia 0.7
  • Loading branch information
simonster authored Jun 14, 2018
2 parents c1b9521 + ba1aa0a commit e84b06e
Show file tree
Hide file tree
Showing 18 changed files with 169 additions and 142 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ os:
- osx
julia:
- 0.6
- 0.7
- nightly
matrix:
fast_finish: true
allow_failures:
- julia: nightly
notifications:
email: false
script:
Expand Down
7 changes: 7 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.7/julia-0.7-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.7/julia-0.7-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"

matrix:
allow_failures:
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"

branches:
only:
- master
Expand Down
18 changes: 13 additions & 5 deletions src/JLD2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ module JLD2
using DataStructures, CodecZlib, FileIO
import Base.sizeof
using Compat
if VERSION >= v"0.7.0-DEV.2009"
using Mmap
using Compat.Printf
using Compat.Mmap

if !isdefined(Base, :isbitstype)
const isbitstype = isbits # TODO: Add to Compat
end

export jldopen, @load, @save
Expand Down Expand Up @@ -54,7 +57,7 @@ const NULL_REFERENCE = RelOffset(0)
JLDWriteSession{T}
A JLDWriteSession keeps track of references to serialized objects. If `T` is a Dict,
`h5offset` maps an object ID (returned by calling `object_id`) to th `RelOffset` of the
`h5offset` maps an object ID (returned by calling `objectid`) to th `RelOffset` of the
written dataset. If it is `Union{}`, then references are not tracked, and objects
referenced multiple times are written multiple times.
"""
Expand Down Expand Up @@ -197,7 +200,12 @@ openfile(::Type{IOStream}, fname, wr, create, truncate) =
openfile(::Type{MmapIO}, fname, wr, create, truncate) =
MmapIO(fname, wr, create, truncate)

read_bytestring(io::IOStream) = chop(String(readuntil(io, 0x00)))
if VERSION >= v"0.7.0-DEV.3510"
# The delimiter is excluded by default
read_bytestring(io::IOStream) = String(readuntil(io, 0x00))
else
read_bytestring(io::IOStream) = chop(String(readuntil(io, 0x00)))
end

const OPEN_FILES = Dict{String,WeakRef}()
function jldopen(fname::AbstractString, wr::Bool, create::Bool, truncate::Bool, iotype::T=MmapIO;
Expand Down Expand Up @@ -240,7 +248,7 @@ function jldopen(fname::AbstractString, wr::Bool, create::Bool, truncate::Bool,
f.root_group = Group{typeof(f)}(f)
f.types_group = Group{typeof(f)}(f)
else
if String(read!(io, Vector{UInt8}(length(REQUIRED_FILE_HEADER)))) != REQUIRED_FILE_HEADER
if String(read!(io, Vector{UInt8}(undef, length(REQUIRED_FILE_HEADER)))) != REQUIRED_FILE_HEADER
throw(ArgumentError(string('"', fname, "\" is not a JLD file")))
end

Expand Down
10 changes: 5 additions & 5 deletions src/bufferedio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ end
function BufferedWriter(io::IOStream, buffer_size::Int)
pos = position(io)
skip(io, buffer_size)
BufferedWriter(io, Vector{UInt8}(buffer_size), pos, Ref{Int}(0))
BufferedWriter(io, Vector{UInt8}(undef, buffer_size), pos, Ref{Int}(0))
end
Base.show(io::IO, ::BufferedWriter) = print(io, "BufferedWriter")

Expand Down Expand Up @@ -53,7 +53,7 @@ function Base.unsafe_write(io::BufferedWriter, x::Ptr{UInt8}, n::UInt64)
buffer = io.buffer
position = io.position[]
n + position <= length(buffer) || throw(EOFError())
unsafe_copy!(pointer(buffer, position+1), x, n)
unsafe_copyto!(pointer(buffer, position+1), x, n)
io.position[] = position + n
# Base.show_backtrace(STDOUT, backtrace())
# gc()
Expand All @@ -70,7 +70,7 @@ struct BufferedReader <: IO
end

BufferedReader(io::IOStream) =
BufferedReader(io, Vector{UInt8}(0), position(io), Ref{Int}(0))
BufferedReader(io, Vector{UInt8}(), position(io), Ref{Int}(0))
Base.show(io::IO, ::BufferedReader) = print(io, "BufferedReader")

function readmore!(io::BufferedReader, n::Int)
Expand Down Expand Up @@ -103,8 +103,8 @@ function Base.read(io::BufferedReader, ::Type{T}, n::Int) where T
readmore!(io, sizeof(T))
end
io.position[] = position + n
arr = Vector{T}(n)
unsafe_copy!(pointer(arr), Ptr{T}(pointer(buffer, position+1)), n)
arr = Vector{T}(undef, n)
unsafe_copyto!(pointer(arr), Ptr{T}(pointer(buffer, position+1)), n)
arr
end
Base.read(io::BufferedReader, ::Type{T}, n::Integer) where {T} =
Expand Down
70 changes: 35 additions & 35 deletions src/data.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const Initialized = Union{Type{Val{true}}, Type{Val{false}}}

const Pointers = Union{Ptr{Void}, IndirectPointer}
const Pointers = Union{Ptr{Cvoid}, IndirectPointer}

struct OnDiskRepresentation{Offsets,JLTypes,H5Types} end
odr_sizeof(::Void) = 0
odr_sizeof(::Nothing) = 0
@Base.pure odr_sizeof(x::DataType) = Int(x.size)

struct UnknownType{T}
Expand Down Expand Up @@ -50,7 +50,7 @@ odr_sizeof(::ReadRepresentation{T,S}) where {T,S} = odr_sizeof(S)
# Determines whether a specific field type should be saved in the file
@noinline function hasfielddata(@nospecialize T)
T === Union{} && return false
!isconcrete(T) && return true
!isconcretetype(T) && return true
T = T::DataType
(T.mutable || T <: Type) && return true
hasdata(T)
Expand Down Expand Up @@ -85,19 +85,19 @@ function samelayout(T::DataType)
end
samelayout(::Type) = false

fieldnames(x::Type{T}) where {T<:Tuple} = [Symbol(x) for x = 1:length(x.types)]
fieldnames(@nospecialize x) = Base.fieldnames(x)
fieldnames(x::Type{T}) where {T<:Tuple} = [Symbol(i) for i = 1:length(x.types)]
fieldnames(@nospecialize x) = collect(Base.fieldnames(x))

# fieldodr gives the on-disk representation of a field of a given type,
# which is either always initialized (initialized=true) or potentially
# uninitialized (initialized=false)
@generated function fieldodr(::Type{T}, initialized::Bool) where T
if isconcrete(T)
if isconcretetype(T)
if !hasfielddata(T)
# A ghost type, so no need to store at all
return nothing
elseif isa(T, DataType)
if isbits(T)
if isbitstype(T)
return :(odr(T))
elseif !T.mutable
return :(initialized ? odr(T) : RelOffset)
Expand All @@ -111,10 +111,10 @@ end
# datatype reflecting the on-disk representation.
@generated function h5fieldtype(f::JLDFile, writeas::Type{T}, readas::Type,
initialized::Initialized) where T
if isconcrete(T)
if isconcretetype(T)
if !hasfielddata(T)
return nothing
elseif isbits(T) || (isa(initialized, Type{Type{Val{true}}}) && !T.mutable)
elseif isbitstype(T) || (isa(initialized, Type{Type{Val{true}}}) && !T.mutable)
return quote
@lookup_committed f T
$(if isempty(T.types)
Expand Down Expand Up @@ -283,7 +283,7 @@ function jltype(f::JLDFile, cdt::CommittedDatatype)
end
end

if isa(julia_type_attr, Void)
if isa(julia_type_attr, Nothing)
throw(InvalidDataException())
end
julia_type_attr = julia_type_attr::ReadAttribute
Expand Down Expand Up @@ -371,7 +371,7 @@ function constructrr(f::JLDFile, T::DataType, dt::CompoundDatatype,
field_datatypes = read_field_datatypes(f, attrs)

# If read type is not a leaf type, reconstruct
if !isconcrete(T)
if !isconcretetype(T)
warn("read type $T is not a leaf type in workspace; reconstructing")
return reconstruct_compound(f, string(T), dt, field_datatypes)
end
Expand All @@ -383,11 +383,11 @@ function constructrr(f::JLDFile, T::DataType, dt::CompoundDatatype,
end
mapped = falses(length(dt.names))

offsets = Vector{Int}(length(T.types))
types = Vector{Any}(length(T.types))
odrs = Vector{Any}(length(T.types))
offsets = Vector{Int}(undef, length(T.types))
types = Vector{Any}(undef, length(T.types))
odrs = Vector{Any}(undef, length(T.types))
fn = fieldnames(T)
samelayout = isbits(T) && T.size == dt.size
samelayout = isbitstype(T) && T.size == dt.size
dtindex = 0
for i = 1:length(T.types)
wstype = T.types[i]
Expand Down Expand Up @@ -446,7 +446,7 @@ function constructrr(f::JLDFile, T::DataType, dt::CompoundDatatype,
# This should theoretically be moved inside the if statement, but then it returns
# the wrong result due to a bug in type inference on 0.6
typeof_wodr = typeof(wodr)
offsets = (offsets...)
offsets = (offsets...,)
if wodr isa OnDiskRepresentation
odr_offsets = typeof_wodr.parameters[1]
odr_types = typeof_wodr.parameters[2].parameters
Expand Down Expand Up @@ -497,11 +497,11 @@ h5convert!(out::Pointers, ::Type{T}, ::JLDFile, x, ::JLDWriteSession) where {T}
args = ex.args
for i = 1:length(Offsets)
member = members[i]
isa(member, Void) && continue
isa(member, Nothing) && continue

offset = Offsets[i]
conv = :(h5convert!(out+$offset, $(member), file, convert($(types[i]), $getindex_fn(x, $i)), wsession))
if i > T.ninitialized && (!isconcrete(x.types[i]) || !isbits(x.types[i]))
if i > T.ninitialized && (!isconcretetype(x.types[i]) || !isbitstype(x.types[i]))
push!(args, quote
if !isdefined(x, $i)
h5convert_uninitialized!(out+$offset, $(member))
Expand Down Expand Up @@ -758,16 +758,16 @@ end

function h5convert!(out::Pointers, fls::FixedLengthString, f::JLDFile, x, ::JLDWriteSession)
fls.length == sizeof(x) || throw(InvalidDataException())
(unsafe_copy!(convert(Ptr{UInt8}, out), pointer(x), fls.length); nothing)
(unsafe_copyto!(convert(Ptr{UInt8}, out), pointer(x), fls.length); nothing)
end
h5convert!(out::Pointers, ::Type{Vlen{String}}, f::JLDFile, x, wsession::JLDWriteSession) =
store_vlen!(out, UInt8, f, Vector{UInt8}(x), wsession)

jlconvert(::ReadRepresentation{String,Vlen{String}}, f::JLDFile, ptr::Ptr, ::RelOffset) =
String(jlconvert(ReadRepresentation{UInt8,Vlen{UInt8}}(), f, ptr, NULL_REFERENCE))
function jlconvert(rr::FixedLengthString{String}, ::JLDFile, ptr::Ptr, ::RelOffset)
data = Vector{UInt8}(rr.length)
unsafe_copy!(pointer(data), convert(Ptr{UInt8}, ptr), rr.length)
data = Vector{UInt8}(undef, rr.length)
unsafe_copyto!(pointer(data), convert(Ptr{UInt8}, ptr), rr.length)
String(data)
end

Expand Down Expand Up @@ -848,9 +848,9 @@ odr(::Type{T}) where {T<:DataType} = DataTypeODR()
function typename(T::DataType)
tn = Symbol[]
m = T.name.module
while m != module_parent(m)
while m != parentmodule(m)
push!(tn, module_name(m))
m = module_parent(m)
m = parentmodule(m)
end
reverse!(tn)
push!(tn, T.name.name)
Expand Down Expand Up @@ -1025,15 +1025,15 @@ end
## SimpleVectors

writeas(::Type{Core.SimpleVector}) = Vector{Any}
wconvert(::Type{Vector{Any}}, x::Core.SimpleVector) = Any[x for x in x]
wconvert(::Type{Vector{Any}}, x::Core.SimpleVector) = collect(Any, x)
rconvert(::Type{Core.SimpleVector}, x::Vector{Any}) = Core.svec(x...)

## Dicts

writeas(::Type{Dict{K,V}}) where {K,V} = Vector{Pair{K,V}}
writeas(::Type{ObjectIdDict}) = Vector{Pair{Any,Any}}
wconvert(::Type{Vector{Pair{K,V}}}, x::Associative{K,V}) where {K,V} = collect(x)
function rconvert(::Type{T}, x::Vector{Pair{K,V}}) where {T<:Associative,K,V}
wconvert(::Type{Vector{Pair{K,V}}}, x::AbstractDict{K,V}) where {K,V} = collect(x)
function rconvert(::Type{T}, x::Vector{Pair{K,V}}) where {T<:AbstractDict,K,V}
d = T()
isa(d, Dict) && sizehint!(d::Dict, length(x))
for (k,v) in x
Expand Down Expand Up @@ -1156,8 +1156,8 @@ end
function reconstruct_odr(f::JLDFile, dt::CompoundDatatype,
field_datatypes::Vector{RelOffset})
# Get the type and ODR information for each field
types = Vector{Any}(length(dt.names))
h5types = Vector{Any}(length(dt.names))
types = Vector{Any}(undef, length(dt.names))
h5types = Vector{Any}(undef, length(dt.names))
for i = 1:length(dt.names)
if !isempty(field_datatypes) && (ref = field_datatypes[i]) != NULL_REFERENCE
dtrr = jltype(f, f.datatype_locations[ref])
Expand All @@ -1166,14 +1166,14 @@ function reconstruct_odr(f::JLDFile, dt::CompoundDatatype,
end
types[i], h5types[i] = typeof(dtrr).parameters
end
return OnDiskRepresentation{(dt.offsets...), Tuple{types...}, Tuple{h5types...}}()
return OnDiskRepresentation{(dt.offsets...,), Tuple{types...}, Tuple{h5types...}}()
end

# Reconstruct type that is a "lost cause": either we were not able to resolve
# the name, or the workspace type has additional fields, or cannot convert
# fields to workspace types
function reconstruct_compound(f::JLDFile, T::String, dt::H5Datatype,
field_datatypes::Union{Vector{RelOffset},Void})
field_datatypes::Union{Vector{RelOffset},Nothing})
rodr = reconstruct_odr(f, dt, field_datatypes)
types = typeof(rodr).parameters[2].parameters

Expand Down Expand Up @@ -1236,7 +1236,7 @@ jlconvert(::ReadRepresentation{Core.TypeofBottom,nothing}, f::JLDFile, ptr::Ptr,

blk = Expr(:block)
args = blk.args
if isbits(T)
if isbitstype(T)
# For bits types, we should always inline, because otherwise we'll just
# pass a lot of crap around in registers
push!(args, Expr(:meta, :inline))
Expand Down Expand Up @@ -1308,13 +1308,13 @@ end
# A pointer singleton or ghost. We need to write something, but we'll
# just write a single byte.
return nothing
elseif isbits(T) && samelayout(T)
elseif isbitstype(T) && samelayout(T)
# Has a specialized convert method or is an unpadded type
return T
end

offsets = zeros(Int, length(T.types))
odrs = Vector{Any}(length(T.types))
odrs = Vector{Any}(undef, length(T.types))
offset = 0
for i = 1:length(T.types)
ty = T.types[i]
Expand All @@ -1329,7 +1329,7 @@ end
offset += odr_sizeof(fodr)
end

OnDiskRepresentation{(offsets...), Tuple{T.types...}, Tuple{odrs...}}()
OnDiskRepresentation{(offsets...,), Tuple{T.types...}, Tuple{odrs...}}()
end

abstract type DataMode end
Expand All @@ -1340,7 +1340,7 @@ struct HasReferences <: DataMode end
@Base.pure datamode(::Union{Type{<:Vlen},Type{RelOffset}}) = HasReferences()
@Base.pure datamode(::DataType) = ReferenceFree()
@Base.pure datamode(::FixedLengthString) = ReferenceFree()
@Base.pure datamode(::Void) = ReferenceFree()
@Base.pure datamode(::Nothing) = ReferenceFree()
@generated function datamode(odr::OnDiskRepresentation{Offsets,JLTypes,H5Types} where {Offsets,JLTypes}) where H5Types
for ty in H5Types.parameters
datamode(ty) == HasReferences() && return HasReferences()
Expand Down
Loading

0 comments on commit e84b06e

Please sign in to comment.