Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some I/O deprecations #22456

Merged
merged 1 commit into from
Jul 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
==========================
Expand Down
6 changes: 6 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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))
Copy link
Member

Choose a reason for hiding this comment

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

Vector{elty} will probably be significantly faster

Copy link
Member Author

Choose a reason for hiding this comment

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

The number of dimensions isn't known.

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