-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Consistency of read() and write() functions (per #14608) #14660
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1165,10 +1165,9 @@ export | |
RawFD, | ||
read, | ||
read!, | ||
readall, | ||
readstring, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this list is sorted |
||
readavailable, | ||
readbytes!, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in-place version should also be deprecated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See explanation in commit message: samoconnor@066c27e There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. then don't touch its docstring in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in 9d46584 |
||
readbytes, | ||
readchomp, | ||
readcsv, | ||
readdir, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ read!(io::AbstractPipe, bytes::Vector{UInt8}) = read!(pipe_reader(io), bytes) | |
read{T<:AbstractPipe}(io::T, args...) = read(pipe_reader(io), args...) | ||
read!{T<:AbstractPipe}(io::T, args...) = read!(pipe_reader(io), args...) | ||
readuntil{T<:AbstractPipe}(io::T, args...) = readuntil(pipe_reader(io), args...) | ||
readbytes(io::AbstractPipe) = readbytes(pipe_reader(io)) | ||
read(io::AbstractPipe) = read(pipe_reader(io)) | ||
readavailable(io::AbstractPipe) = readavailable(pipe_reader(io)) | ||
|
||
isreadable(io::AbstractPipe) = isreadable(pipe_reader(io)) | ||
|
@@ -60,6 +60,18 @@ eof(io::AbstractPipe) = eof(pipe_reader(io)) | |
reseteof(io::AbstractPipe) = reseteof(pipe_reader(io)) | ||
|
||
|
||
# Exception-safe wrappers (io = open(); try f(io) finally close(io)) | ||
|
||
write(filename::AbstractString, args...) = open(io->write(io, args...), filename, "w") | ||
|
||
read(filename::AbstractString, args...) = open(io->read(io, args...), filename) | ||
read!(filename::AbstractString, a) = open(io->read!(io, a), filename) | ||
readstring(filename::AbstractString) = open(readstring, filename) | ||
readuntil(filename::AbstractString, args...) = open(io->readuntil(io, args...), filename) | ||
readline(filename::AbstractString) = open(readline, filename) | ||
readlines(filename::AbstractString) = open(readlines, filename) | ||
|
||
|
||
## byte-order mark, ntoh & hton ## | ||
|
||
const ENDIAN_BOM = reinterpret(UInt32,UInt8[1:4;])[1] | ||
|
@@ -160,6 +172,13 @@ function write(io::IO, s::Symbol) | |
return write(io, pname, Int(ccall(:strlen, Csize_t, (Cstring,), pname))) | ||
end | ||
|
||
function write(to::IO, from::IO) | ||
while !eof(from) | ||
write(to, readavailable(from)) | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs-test label is for all the new methods here and above edit: and docs |
||
|
||
|
||
read(s::IO, ::Type{Int8}) = reinterpret(Int8, read(s,UInt8)) | ||
|
||
function read{T <: Union{Int16,UInt16,Int32,UInt32,Int64,UInt64,Int128,UInt128}}(s::IO, ::Type{T}) | ||
|
@@ -285,7 +304,7 @@ end | |
|
||
readline() = readline(STDIN) | ||
readline(s::IO) = readuntil(s, '\n') | ||
readchomp(x) = chomp!(readall(x)) | ||
readchomp(x) = chomp!(readstring(x)) | ||
|
||
# read up to nb bytes into nb, returning # bytes read | ||
function readbytes!(s::IO, b::AbstractArray{UInt8}, nb=length(b)) | ||
|
@@ -307,19 +326,18 @@ function readbytes!(s::IO, b::AbstractArray{UInt8}, nb=length(b)) | |
end | ||
|
||
# read up to nb bytes from s, returning a Vector{UInt8} of bytes read. | ||
function readbytes(s::IO, nb=typemax(Int)) | ||
function read(s::IO, nb=typemax(Int)) | ||
# Let readbytes! grow the array progressively by default | ||
# instead of taking of risk of over-allocating | ||
b = Array(UInt8, nb == typemax(Int) ? 1024 : nb) | ||
nr = readbytes!(s, b, nb) | ||
resize!(b, nr) | ||
end | ||
|
||
function readall(s::IO) | ||
b = readbytes(s) | ||
function readstring(s::IO) | ||
b = read(s) | ||
return isvalid(ASCIIString, b) ? ASCIIString(b) : UTF8String(b) | ||
end | ||
readall(filename::AbstractString) = open(readall, filename) | ||
|
||
## high-level iterator interfaces ## | ||
|
||
|
@@ -330,6 +348,7 @@ type EachLine | |
EachLine(stream, ondone) = new(stream, ondone) | ||
end | ||
eachline(stream::IO) = EachLine(stream) | ||
eachline(filename::AbstractString) = EachLine(open(filename), close) | ||
|
||
start(itr::EachLine) = nothing | ||
function done(itr::EachLine, nada) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -336,8 +336,8 @@ function readbytes!(io::AbstractIOBuffer, b::Array{UInt8}, nb=length(b)) | |
read_sub(io, b, 1, nr) | ||
return nr | ||
end | ||
readbytes(io::AbstractIOBuffer) = read!(io, Array(UInt8, nb_available(io))) | ||
readbytes(io::AbstractIOBuffer, nb) = read!(io, Array(UInt8, min(nb, nb_available(io)))) | ||
read(io::AbstractIOBuffer) = read!(io, Array(UInt8, nb_available(io))) | ||
read(io::AbstractIOBuffer, nb::Integer) = read!(io, Array(UInt8, min(nb, nb_available(io)))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a good idea to hide small changes in behavior like that in a haystack of renames. Better have a separate commit. |
||
|
||
function search(buf::IOBuffer, delim::UInt8) | ||
p = pointer(buf.data, buf.ptr) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
closes files when done, but not
::IO
inputsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No.
A filename has no concept of open or close.
The doc says that it reads the entire contents of a file. That is what it does, open/close is hidden implementation detail. A stream may have a concept of open/close but if it does that is beyond the scope of this function, all this function does is read as a string.