From d61d5ac4ee31efb4b091c277f434b19c8a5f22d3 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Tue, 20 Jun 2017 19:32:55 -0700 Subject: [PATCH] change `read(::IO, ::Ref) to `read!`, fix fix #21592 deprecate `read(io, type, dims)`, fix #21450 --- NEWS.md | 4 ++++ base/deprecated.jl | 6 ++++++ base/distributed/messages.jl | 2 +- base/io.jl | 19 +++---------------- base/serialize.jl | 2 +- test/read.jl | 6 +++--- 6 files changed, 18 insertions(+), 21 deletions(-) diff --git a/NEWS.md b/NEWS.md index 11ed91cdeae6b..6fba86f08749f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -139,6 +139,10 @@ Deprecated or removed * The method `replace(s::AbstractString, pat, r, count)` with `count <= 0` is deprecated in favor of `replace(s::AbstractString, pat, r, typemax(Int))` ([#22325]). + * `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 ========================== diff --git a/base/deprecated.jl b/base/deprecated.jl index 978892ad82732..3ecc00bfe4d26 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1543,6 +1543,12 @@ end @deprecate cat_t{N,T}(::Type{Val{N}}, ::Type{T}, A, B) cat_t(Val(N), T, A, B) false @deprecate reshape{N}(A::AbstractArray, ::Type{Val{N}}) reshape(A, Val(N)) +@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 diff --git a/base/distributed/messages.jl b/base/distributed/messages.jl index 17a447a154d1f..222c38c9ba1ba 100644 --- a/base/distributed/messages.jl +++ b/base/distributed/messages.jl @@ -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 diff --git a/base/io.jl b/base/io.jl index 128e18f4bd843..7bd901fe8d627 100644 --- a/base/io.jl +++ b/base/io.jl @@ -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 @@ -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) diff --git a/base/serialize.jl b/base/serialize.jl index 70f1203d59c20..8d7a85fc96bcd 100644 --- a/base/serialize.jl +++ b/base/serialize.jl @@ -865,7 +865,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 diff --git a/test/read.jl b/test/read.jl index 6f794a84f59bd..8c87c9f345a26 100644 --- a/test/read.jl +++ b/test/read.jl @@ -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)