Skip to content

Commit

Permalink
change read(::IO, ::Ref) to read!`, fix fix #21592
Browse files Browse the repository at this point in the history
deprecate `read(io, type, dims)`, fix #21450
  • Loading branch information
JeffBezanson committed Jun 30, 2017
1 parent c509321 commit 5e80d9c
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 21 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ Deprecated or removed
Ternaries must now include some amount of whitespace, e.g. `x ? a : b` rather than
`x? a : b` ([#22523]).

* `read(io, type, dims)` is deprecated to `read!(io, Array{type}(dims))` ([#21450]).

* `read(::IO, ::Ref)` is now a method of `read!`, since it mutates its `Ref` argument ([#21592]).


Julia v0.6.0 Release Notes
==========================
Expand Down
6 changes: 6 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,12 @@ function bkfact!(A::StridedMatrix, uplo::Symbol, symmetric::Bool = issymmetric(A
return bkfact!(symmetric ? Symmetric(A, uplo) : Hermitian(A, uplo), rook)
end

@deprecate read(s::IO, x::Ref) read!(s, x)

@deprecate read(s::IO, t::Type, d1::Int, dims::Int...) read!(s, Array{t}(tuple(d1,dims...)))
@deprecate read(s::IO, t::Type, d1::Integer, dims::Integer...) read!(s, Array{t}(convert(Tuple{Vararg{Int}},tuple(d1,dims...))))
@deprecate read(s::IO, t::Type, dims::Dims) read!(s, Array{t}(dims))

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
2 changes: 1 addition & 1 deletion base/distributed/messages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function serialize_hdr_raw(io, hdr)
end

function deserialize_hdr_raw(io)
data = read(io, Ref{NTuple{4,Int}}())[]
data = read!(io, Ref{NTuple{4,Int}}())[]
return MsgHeader(RRID(data[1], data[2]), RRID(data[3], data[4]))
end

Expand Down
19 changes: 3 additions & 16 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -368,29 +368,16 @@ end

@noinline unsafe_read(s::IO, p::Ref{T}, n::Integer) where {T} = unsafe_read(s, unsafe_convert(Ref{T}, p)::Ptr, n) # mark noinline to ensure ref is gc-rooted somewhere (by the caller)
unsafe_read(s::IO, p::Ptr, n::Integer) = unsafe_read(s, convert(Ptr{UInt8}, p), convert(UInt, n))
read(s::IO, x::Ref{T}) where {T} = (unsafe_read(s, x, Core.sizeof(T)); x)
read!(s::IO, x::Ref{T}) where {T} = (unsafe_read(s, x, Core.sizeof(T)); x)

read(s::IO, ::Type{Int8}) = reinterpret(Int8, read(s, UInt8))
function read(s::IO, T::Union{Type{Int16},Type{UInt16},Type{Int32},Type{UInt32},Type{Int64},Type{UInt64},Type{Int128},Type{UInt128},Type{Float16},Type{Float32},Type{Float64}})
return read(s, Ref{T}(0))[]::T
return read!(s, Ref{T}(0))[]::T
end

read(s::IO, ::Type{Bool}) = (read(s, UInt8) != 0)
read(s::IO, ::Type{Ptr{T}}) where {T} = convert(Ptr{T}, read(s, UInt))

read(s::IO, t::Type{T}, d1::Int, dims::Int...) where {T} = read(s, t, tuple(d1,dims...))
read(s::IO, t::Type{T}, d1::Integer, dims::Integer...) where {T} =
read(s, t, convert(Tuple{Vararg{Int}},tuple(d1,dims...)))

"""
read(stream::IO, T, dims)
Read a series of values of type `T` from `stream`, in canonical binary representation.
`dims` is either a tuple or a series of integer arguments specifying the size of the `Array{T}`
to return.
"""
read(s::IO, ::Type{T}, dims::Dims) where {T} = read!(s, Array{T}(dims))

@noinline function read!(s::IO, a::Array{UInt8}) # mark noinline to ensure the array is gc-rooted somewhere (by the caller)
unsafe_read(s, pointer(a), sizeof(a))
return a
Expand Down Expand Up @@ -530,7 +517,7 @@ end
Read at most `nb` bytes from `s`, returning a `Vector{UInt8}` of the bytes read.
"""
function read(s::IO, nb=typemax(Int))
function read(s::IO, nb::Integer = typemax(Int))
# Let readbytes! grow the array progressively by default
# instead of taking of risk of over-allocating
b = Vector{UInt8}(nb == typemax(Int) ? 1024 : nb)
Expand Down
2 changes: 1 addition & 1 deletion base/serialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ function deserialize_array(s::AbstractSerializer)
end
end
else
A = read(s.io, elty, dims)
A = read!(s.io, Array{elty}(dims))
end
s.table[slot] = A
return A
Expand Down
6 changes: 3 additions & 3 deletions test/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ for (name, f) in l
@test read(io(), Int) == read(filename,Int)
s1 = io()
s2 = IOBuffer(text)
@test read(s1, UInt32, 2) == read(s2, UInt32, 2)
@test read!(s1, Array{UInt32}(2)) == read!(s2, Array{UInt32}(2))
@test !eof(s1)
@test read(s1, UInt8, 5) == read(s2, UInt8, 5)
@test read!(s1, Array{UInt8}(5)) == read!(s2, Array{UInt8}(5))
@test !eof(s1)
@test read(s1, UInt8, 1) == read(s2, UInt8, 1)
@test read!(s1, Array{UInt8}(1)) == read!(s2, Array{UInt8}(1))
@test eof(s1)
@test_throws EOFError read(s1, UInt8)
@test eof(s1)
Expand Down

0 comments on commit 5e80d9c

Please sign in to comment.