Skip to content

Commit

Permalink
Consistency of read() and write() functions. JuliaLang#14608
Browse files Browse the repository at this point in the history
replace readall() with readstring()
replace readbytes() with read()

readbytes! returns nb whereas read! returns array, so they can't
trivially be merged.

Fix test/replutil.jl test that matches output of method error message
that has changed due to introduction of readbytes(::AbstractString).
  • Loading branch information
samoconnor committed Jan 12, 2016
1 parent 53ecbaa commit 066c27e
Show file tree
Hide file tree
Showing 47 changed files with 160 additions and 144 deletions.
4 changes: 2 additions & 2 deletions base/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ function write_response_buffer(s::PromptState, data)
offset = s.input_buffer.ptr
ptr = data.response_buffer.ptr
seek(data.response_buffer, 0)
write(s.input_buffer, readall(data.response_buffer))
write(s.input_buffer, readstring(data.response_buffer))
s.input_buffer.ptr = offset + ptr - 2
data.response_buffer.ptr = ptr
refresh_line(s)
Expand Down Expand Up @@ -1129,7 +1129,7 @@ function refresh_multi_line(termbuf::TerminalBuffer, s::SearchState)
offset = buf.ptr
ptr = s.response_buffer.ptr
seek(s.response_buffer, 0)
write(buf, readall(s.response_buffer))
write(buf, readstring(s.response_buffer))
buf.ptr = offset + ptr - 1
s.response_buffer.ptr = ptr
s.ias = refresh_multi_line(termbuf, s.terminal, buf, s.ias, s.backward ? "(reverse-i-search)`" : "(forward-i-search)`")
Expand Down
2 changes: 1 addition & 1 deletion base/base64.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ close(b::Base64DecodePipe) = nothing
function base64decode(s)
b = IOBuffer(s)
try
return readbytes(Base64DecodePipe(b))
return read(Base64DecodePipe(b))
finally
close(b)
end
Expand Down
4 changes: 2 additions & 2 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ end

function load_machine_file(path::AbstractString)
machines = []
for line in split(readall(path),'\n'; keep=false)
for line in split(readstring(path),'\n'; keep=false)
s = map!(strip, split(line,'*'; keep=false))
if length(s) > 1
cnt = isnumber(s[1]) ? parse(Int,s[1]) : symbol(s[1])
Expand Down Expand Up @@ -331,7 +331,7 @@ function _start()
# note: currently IOStream is used for file STDIN
if isa(STDIN,File) || isa(STDIN,IOStream)
# reading from a file, behave like include
eval(Main,parse_input_line(readall(STDIN)))
eval(Main,parse_input_line(readstring(STDIN)))
else
# otherwise behave repl-like
while !eof(STDIN)
Expand Down
4 changes: 2 additions & 2 deletions base/datafmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ function readdlm_auto(input, dlm::Char, T::Type, eol::Char, auto::Bool; opts...)
if use_mmap && fsz > 0 && fsz < typemax(Int)
input = as_mmap(input, fsz)
else
input = readall(input)
input = readstring(input)
end
end
sinp = isa(input, Vector{UInt8}) ? bytestring(input) :
isa(input, IO) ? readall(input) :
isa(input, IO) ? readstring(input) :
input
readdlm_string(sinp, dlm, T, eol, auto, optsd)
end
Expand Down
5 changes: 5 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -952,3 +952,8 @@ end
#14555
@deprecate_binding Coff_t Int64
@deprecate_binding FileOffset Int64


#https://github.com/JuliaLang/julia/issues/14608
@deprecate readall readstring
@deprecate readbytes read
22 changes: 11 additions & 11 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2054,7 +2054,7 @@ open(file_name, mode="r")
Apply the function `f` to the result of `open(args...)` and close the resulting file
descriptor upon completion.
**Example**: `open(readall, "file.txt")`
**Example**: `open(readstring, "file.txt")`
"""
open(f::Function, args...)

Expand Down Expand Up @@ -2438,19 +2438,19 @@ the process.
triu!(M, k)

"""
readall(stream::IO)
readstring(stream::IO)
Read the entire contents of an I/O stream as a string.
"""
readall(stream::IO)
readstring(stream::IO)

"""
readall(filename::AbstractString)
readstring(filename::AbstractString)
Open `filename`, read the entire contents as a string, then close the file. Equivalent to
`open(readall, filename)`.
`open(readstring, filename)`.
"""
readall(filename::AbstractString)
readstring(filename::AbstractString)

"""
poll_file(path, interval_s::Real, timeout_s::Real) -> (previous::StatStruct, current::StatStruct)
Expand Down Expand Up @@ -6398,7 +6398,7 @@ besselk
"""
readchomp(x)
Read the entirety of `x` as a string but remove trailing newlines. Equivalent to `chomp(readall(x))`.
Read the entirety of `x` as a string but remove trailing newlines. Equivalent to `chomp(readstring(x))`.
"""
readchomp

Expand Down Expand Up @@ -6443,9 +6443,9 @@ asecd
Read at most `nb` bytes from the stream into `b`, returning the number of bytes read
(increasing the size of `b` as needed).
See `readbytes` for a description of the `all` option.
See `read` for a description of the `all` option.
"""
readbytes!
read!

"""
basename(path::AbstractString) -> AbstractString
Expand Down Expand Up @@ -10260,7 +10260,7 @@ a series of integer arguments.
cell

"""
readbytes(stream, nb=typemax(Int); all=true)
read(stream, nb=typemax(Int); all=true)
Read at most `nb` bytes from the stream, returning a `Vector{UInt8}` of the bytes read.
Expand All @@ -10269,7 +10269,7 @@ requested bytes, until an error or end-of-file occurs. If `all` is `false`, at m
`read` call is performed, and the amount of data returned is device-dependent. Note that not
all stream types support the `all` option.
"""
readbytes
read

"""
eig(A,[irange,][vl,][vu,][permute=true,][scale=true]) -> D, V
Expand Down
3 changes: 1 addition & 2 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1165,10 +1165,9 @@ export
RawFD,
read,
read!,
readall,
readstring,
readavailable,
readbytes!,
readbytes,
readchomp,
readcsv,
readdir,
Expand Down
12 changes: 3 additions & 9 deletions base/filesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export File,
S_IROTH, S_IWOTH, S_IXOTH, S_IRWXO

import Base: uvtype, uvhandle, eventloop, fd, position, stat, close,
write, read, read!, readbytes, isopen, show,
write, read, read!, isopen, show,
check_open, _sizeof_uv_fs, uv_error, UVError

include("path.jl")
Expand Down Expand Up @@ -172,14 +172,8 @@ function readbytes!(f::File, b::Array{UInt8}, nb=length(b))
read!(f, b, nr)
return nr
end
readbytes(io::File) = read!(io, Array(UInt8, nb_available(io)))
readbytes(io::File, nb) = read!(io, Array(UInt8, min(nb, nb_available(io))))

function readbytes(f::File)
a = Array(UInt8, nb_available(f))
read!(f,a)
return a
end
read(io::File) = read!(io, Array(UInt8, nb_available(io)))
read(io::File, nb::Integer) = read!(io, Array(UInt8, min(nb, nb_available(io))))

const SEEK_SET = Int32(0)
const SEEK_CUR = Int32(1)
Expand Down
8 changes: 4 additions & 4 deletions base/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ less(file, line::Integer) = error("could not find source file for function")
print(io, x)
end
end
clipboard() = readall(`pbpaste`)
clipboard() = readstring(`pbpaste`)
end

@linux_only begin
Expand All @@ -120,7 +120,7 @@ end
cmd = c == :xsel ? `xsel --nodetach --output --clipboard` :
c == :xclip ? `xclip -quiet -out -selection clipboard` :
error("unexpected clipboard command: $c")
readall(pipeline(cmd, stderr=STDERR))
readstring(pipeline(cmd, stderr=STDERR))
end
end

Expand Down Expand Up @@ -181,7 +181,7 @@ function versioninfo(io::IO=STDOUT, verbose::Bool=false)
if verbose
lsb = ""
@linux_only try lsb = readchomp(pipeline(`lsb_release -ds`, stderr=DevNull)) end
@windows_only try lsb = strip(readall(`$(ENV["COMSPEC"]) /c ver`)) end
@windows_only try lsb = strip(readstring(`$(ENV["COMSPEC"]) /c ver`)) end
if lsb != ""
println(io, " ", lsb)
end
Expand Down Expand Up @@ -416,7 +416,7 @@ function runtests(tests = ["all"], numcores = ceil(Int,CPU_CORES/2))
buf = PipeBuffer()
versioninfo(buf)
error("A test has failed. Please submit a bug report (https://github.com/JuliaLang/julia/issues)\n" *
"including error messages above and the output of versioninfo():\n$(readall(buf))")
"including error messages above and the output of versioninfo():\n$(readstring(buf))")
end
end

Expand Down
30 changes: 24 additions & 6 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -60,6 +60,18 @@ eof(io::AbstractPipe) = eof(pipe_reader(io))
reseteof(io::AbstractPipe) = reseteof(pipe_reader(io))


# Exception-safe wrappes. (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]
Expand Down Expand Up @@ -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


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})
Expand Down Expand Up @@ -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))
Expand All @@ -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 ##

Expand Down
4 changes: 2 additions & 2 deletions base/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))))

function search(buf::IOBuffer, delim::UInt8)
p = pointer(buf.data, buf.ptr)
Expand Down
4 changes: 2 additions & 2 deletions base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function readbytes!(s::IOStream, b::Array{UInt8}, nb=length(b); all::Bool=true)
return all ? readbytes_all!(s, b, nb) : readbytes_some!(s, b, nb)
end

function readbytes(s::IOStream)
function read(s::IOStream)
sz = 0
try # filesize is just a hint, so ignore if it fails
sz = filesize(s)
Expand All @@ -255,7 +255,7 @@ function readbytes(s::IOStream)
resize!(b, nr)
end

function readbytes(s::IOStream, nb::Integer; all::Bool=true)
function read(s::IOStream, nb::Integer; all::Bool=true)
b = Array(UInt8, nb)
nr = readbytes!(s, b, nb, all=all)
resize!(b, nr)
Expand Down
12 changes: 6 additions & 6 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ function _require_from_serialized(node::Int, mod::Symbol, path_to_try::ByteStrin
recompile_stale(mod, path_to_try)
# broadcast top-level import/using from node 1 (only)
if node == myid()
content = open(readbytes, path_to_try)
content = open(read, path_to_try)
else
content = remotecall_fetch(open, node, readbytes, path_to_try)
content = remotecall_fetch(open, node, read, path_to_try)
end
restored = _include_from_serialized(content)
if restored !== nothing
Expand All @@ -164,7 +164,7 @@ function _require_from_serialized(node::Int, mod::Symbol, path_to_try::ByteStrin
myid() == 1 && recompile_stale(mod, path_to_try)
restored = ccall(:jl_restore_incremental, Any, (Ptr{UInt8},), path_to_try)
else
content = remotecall_fetch(open, node, readbytes, path_to_try)
content = remotecall_fetch(open, node, read, path_to_try)
restored = _include_from_serialized(content)
end
# otherwise, continue search
Expand Down Expand Up @@ -401,7 +401,7 @@ function include_from_node1(_path::AbstractString)
result = Core.include(path)
nprocs()>1 && sleep(0.005)
else
result = include_string(remotecall_fetch(readall, 1, path), path)
result = include_string(remotecall_fetch(readstring, 1, path), path)
end
finally
if prev === nothing
Expand Down Expand Up @@ -493,14 +493,14 @@ function cache_dependencies(f::IO)
n = ntoh(read(f, Int32))
n == 0 && break
push!(modules,
(symbol(readbytes(f, n)), # module symbol
(symbol(read(f, n)), # module symbol
ntoh(read(f, UInt64)))) # module UUID (timestamp)
end
read(f, Int64) # total bytes for file dependencies
while true
n = ntoh(read(f, Int32))
n == 0 && break
push!(files, (bytestring(readbytes(f, n)), ntoh(read(f, Float64))))
push!(files, (bytestring(read(f, n)), ntoh(read(f, Float64))))
end
return modules, files
end
Expand Down
2 changes: 1 addition & 1 deletion base/markdown/Julia/interp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

function Base.parse(stream::IO; greedy::Bool = true, raise::Bool = true)
pos = position(stream)
ex, Δ = Base.parse(readall(stream), 1, greedy = greedy, raise = raise)
ex, Δ = Base.parse(readstring(stream), 1, greedy = greedy, raise = raise)
seek(stream, pos + Δ - 1)
return ex
end
Expand Down
2 changes: 1 addition & 1 deletion base/markdown/Markdown.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ include("render/terminal/render.jl")
export readme, license, @md_str, @doc_str

parse(markdown::AbstractString; flavor = julia) = parse(IOBuffer(markdown), flavor = flavor)
parse_file(file::AbstractString; flavor = julia) = parse(readall(file), flavor = flavor)
parse_file(file::AbstractString; flavor = julia) = parse(readstring(file), flavor = flavor)

readme(pkg::AbstractString; flavor = github) = parse_file(Pkg.dir(pkg, "README.md"), flavor = flavor)
readme(pkg::Module; flavor = github) = readme(string(pkg), flavor = flavor)
Expand Down
2 changes: 1 addition & 1 deletion base/markdown/parse/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ end

function showrest(io::IO)
start = position(io)
show(readall(io))
show(readstring(io))
println()
seek(io, start)
end
Loading

0 comments on commit 066c27e

Please sign in to comment.