Skip to content

Commit

Permalink
Merge pull request #22795 from JuliaLang/jb/Stringiobuf
Browse files Browse the repository at this point in the history
deprecate `String(io::IOBuffer)`. fixes #21438
  • Loading branch information
JeffBezanson authored Jul 14, 2017
2 parents 6b9d2c6 + dc017e9 commit 02efe03
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 22 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ Deprecated or removed

* The unexported type `AbstractIOBuffer` has been renamed to `GenericIOBuffer` ([#17360] [#22796]).

* The method `String(io::IOBuffer)` is deprecated to `String(take!(copy(io)))` ([#21438]).


Julia v0.6.0 Release Notes
==========================
Expand Down
2 changes: 2 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,8 @@ end

@deprecate_binding AbstractIOBuffer GenericIOBuffer false

@deprecate String(io::GenericIOBuffer) String(take!(copy(io)))

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
6 changes: 0 additions & 6 deletions base/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,6 @@ end

isopen(io::GenericIOBuffer) = io.readable || io.writable || io.seekable || nb_available(io) > 0

function String(io::GenericIOBuffer)
io.readable || throw(ArgumentError("IOBuffer is not readable"))
io.seekable || throw(ArgumentError("IOBuffer is not seekable"))
return unsafe_string(pointer(io.data), io.size)
end

"""
take!(b::IOBuffer)
Expand Down
4 changes: 2 additions & 2 deletions base/repl/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ mutable struct PromptState <: ModeState
indent::Int
end

input_string(s::PromptState) = String(s.input_buffer)
input_string(s::PromptState) = String(take!(copy(s.input_buffer)))

input_string_newlines(s::PromptState) = count(c->(c == '\n'), input_string(s))
function input_string_newlines_aftercursor(s::PromptState)
Expand Down Expand Up @@ -1045,7 +1045,7 @@ refresh_multi_line(termbuf::TerminalBuffer, terminal::UnixTerminal,
s::Union{PromptState,PrefixSearchState}) = s.ias =
refresh_multi_line(termbuf, terminal, buffer(s), s.ias, s, indent = s.indent)

input_string(s::PrefixSearchState) = String(s.response_buffer)
input_string(s::PrefixSearchState) = String(take!(copy(s.response_buffer)))

# a meta-prompt that presents itself as parent_prompt, but which has an independent keymap
# for prefix searching
Expand Down
8 changes: 4 additions & 4 deletions base/repl/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ function mode_idx(hist::REPLHistoryProvider, mode)
end

function add_history(hist::REPLHistoryProvider, s)
str = rstrip(String(s.input_buffer))
str = rstrip(String(take!(copy(s.input_buffer))))
isempty(strip(str)) && return
mode = mode_idx(hist, LineEdit.mode(s))
!isempty(hist.history) &&
Expand Down Expand Up @@ -506,7 +506,7 @@ function history_move_prefix(s::LineEdit.PrefixSearchState,
prefix::AbstractString,
backwards::Bool,
cur_idx = hist.cur_idx)
cur_response = String(LineEdit.buffer(s))
cur_response = String(take!(copy(LineEdit.buffer(s))))
# when searching forward, start at last_idx
if !backwards && hist.last_idx > 0
cur_idx = hist.last_idx
Expand Down Expand Up @@ -547,7 +547,7 @@ function history_search(hist::REPLHistoryProvider, query_buffer::IOBuffer, respo
qpos = position(query_buffer)
qpos > 0 || return true
searchdata = beforecursor(query_buffer)
response_str = String(response_buffer)
response_str = String(take!(copy(response_buffer)))

# Alright, first try to see if the current match still works
a = position(response_buffer) + 1 # position is zero-indexed
Expand Down Expand Up @@ -596,7 +596,7 @@ LineEdit.reset_state(hist::REPLHistoryProvider) = history_reset_state(hist)

function return_callback(s)
ast = Base.syntax_deprecation_warnings(false) do
Base.parse_input_line(String(LineEdit.buffer(s)))
Base.parse_input_line(String(take!(copy(LineEdit.buffer(s)))))
end
if !isa(ast, Expr) || (ast.head != :continue && ast.head != :incomplete)
return true
Expand Down
7 changes: 4 additions & 3 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ let d = Dict((1=>2) => (3=>45), (3=>10) => (10=>11))

# Check explicitly for the expected strings, since the CPU bitness effects
# dictionary ordering.
result = String(buf)
result = String(take!(buf))
@test contains(result, "Dict")
@test contains(result, "(1=>2)=>(3=>45)")
@test contains(result, "(3=>10)=>(10=>11)")
Expand All @@ -314,8 +314,9 @@ let sbuff = IOBuffer(),
io = Base.IOContext(Base.IOContext(sbuff, :limit => true), :displaysize => (10, 20))

Base.show(io, MIME("text/plain"), Dict(Alpha()=>1))
@test !contains(String(sbuff), "")
@test endswith(String(sbuff), "α => 1")
local str = String(take!(sbuff))
@test !contains(str, "")
@test endswith(str, "α => 1")
end

# issue #2540
Expand Down
8 changes: 5 additions & 3 deletions test/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

ioslength(io::IOBuffer) = (io.seekable ? io.size : nb_available(io))

bufcontents(io::Base.GenericIOBuffer) = unsafe_string(pointer(io.data), io.size)

let io = IOBuffer()
@test eof(io)
@test_throws EOFError read(io,UInt8)
Expand All @@ -17,7 +19,7 @@ seek(io, 0)
a = Array{UInt8}(2)
@test read!(io, a) == a
@test a == UInt8['b','c']
@test String(io) == "abc"
@test bufcontents(io) == "abc"
seek(io, 1)
truncate(io, 2)
@test position(io) == 1
Expand Down Expand Up @@ -178,7 +180,7 @@ let io=IOBuffer(SubString("***αhelloworldω***",4,16)), io2 = IOBuffer(b"goodni
@test_throws EOFError read(io,UInt8)
skip(io, -3)
@test readstring(io) == ""
@test String(io) == "αhelloworldω"
@test bufcontents(io) == "αhelloworldω"
@test_throws ArgumentError write(io,"!")
@test take!(io) == b"αhelloworldω"
seek(io, 2)
Expand All @@ -193,7 +195,7 @@ let io=IOBuffer(SubString("***αhelloworldω***",4,16)), io2 = IOBuffer(b"goodni
seek(io2, 0)
write(io2, io2)
@test readstring(io2) == ""
@test String(io2) == "goodnightmoonhelloworld"
@test bufcontents(io2) == "goodnightmoonhelloworld"
end

# issue #11917
Expand Down
2 changes: 1 addition & 1 deletion test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ function history_move_prefix(s::LineEdit.MIState,
buf = LineEdit.buffer(s)
pos = position(buf)
prefix = REPL.beforecursor(buf)
allbuf = String(buf)
allbuf = String(take!(copy(buf)))
cur_idx = hist.cur_idx
# when searching forward, start at last_idx
if !backwards && hist.last_idx > 0
Expand Down
6 changes: 3 additions & 3 deletions test/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ let d = Dict(1 => 2, 3 => 45)
buf = IOBuffer()
td = TextDisplay(buf)
display(td, d)
result = String(td.io)
result = String(take!(td.io))

@test contains(result, summary(d))

Expand All @@ -466,13 +466,13 @@ let err, buf = IOBuffer()
try Array() catch err end
Base.show_method_candidates(buf,err)
@test isa(err, MethodError)
@test contains(String(buf), "Closest candidates are:")
@test contains(String(take!(buf)), "Closest candidates are:")
end

# Issue 20111
let K20111(x) = y -> x, buf = IOBuffer()
show(buf, methods(K20111(1)))
@test contains(String(buf), " 1 method for generic function")
@test contains(String(take!(buf)), " 1 method for generic function")
end

# @macroexpand tests
Expand Down

0 comments on commit 02efe03

Please sign in to comment.