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

Compat.IOBuffer bugfixes #504

Merged
merged 1 commit into from
Feb 28, 2018
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
48 changes: 34 additions & 14 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1567,29 +1567,49 @@ end

# https://github.com/JuliaLang/julia/pull/25872
if VERSION < v"0.7.0-DEV.3734"
if isdefined(Base, :open_flags)
import Base.open_flags
else
# copied from Base:
function open_flags(; read=nothing, write=nothing, create=nothing, truncate=nothing, append=nothing)
if write === true && read !== true && append !== true
create === nothing && (create = true)
truncate === nothing && (truncate = true)
end
if truncate === true || append === true
write === nothing && (write = true)
create === nothing && (create = true)
end
write === nothing && (write = false)
read === nothing && (read = !write)
create === nothing && (create = false)
truncate === nothing && (truncate = false)
append === nothing && (append = false)
return (read, write, create, truncate, append)
end
end
function IOBuffer(
data::Union{AbstractVector{UInt8},Nothing}=nothing;
read::Union{Bool,Nothing}=nothing,
write::Union{Bool,Nothing}=nothing,
truncate::Union{Bool,Nothing}=nothing,
read::Union{Bool,Nothing}=data===nothing?true:nothing,
Copy link
Member

Choose a reason for hiding this comment

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

0.7 deprecated the omission of spaces around ? and :, so this causes deprecation warnings. Fixed in #508.

write::Union{Bool,Nothing}=data===nothing?true:nothing,
truncate::Union{Bool,Nothing}=data===nothing?true:nothing,
maxsize::Integer=typemax(Int),
sizehint::Union{Integer,Nothing}=nothing)
write === nothing && (write = false)
read === nothing && (read = !write)
truncate === nothing && (truncate = false)
flags = open_flags(read=read, write=write, append=nothing, truncate=truncate)
if maxsize < 0
throw(ArgumentError("negative maxsize: $(maxsize)"))
end
if sizehint !== nothing
sizehint!(data, sizehint)
end
buf = if data !== nothing
Base.IOBuffer(data, read, write, Int(maxsize))
if data !== nothing
if sizehint !== nothing
sizehint!(data, sizehint)
end
buf = Base.IOBuffer(data, flags[1], flags[2], Int(maxsize))
else
size = maxsize == typemax(Int) ? 32 : Int(maxsize)
Base.IOBuffer(StringVector(size), read, write, Int(maxsize))
size = sizehint !== nothing ? Int(sizehint) : maxsize != typemax(Int) ? Int(maxsize) : 32
buf = Base.IOBuffer(StringVector(size), flags[1], flags[2], Int(maxsize))
buf.data[:] = 0
end
if truncate
if flags[4] # flags.truncate
buf.size = 0
end
return buf
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1438,5 +1438,13 @@ let buf = Compat.IOBuffer(read=true, write=false, maxsize=25)
@test !buf.writable
@test buf.maxsize == 25
end
let buf = Compat.IOBuffer(zeros(UInt8, 4), write=true) # issue #502
write(buf, 'a')
@test take!(buf) == [0x61]
end
let buf = Compat.IOBuffer(sizehint=20)
println(buf, "Hello world.")
@test String(take!(buf)) == "Hello world.\n"
end

nothing