diff --git a/NEWS.md b/NEWS.md index f2ffb103abb8b..d277817a98277 100644 --- a/NEWS.md +++ b/NEWS.md @@ -182,6 +182,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 ========================== diff --git a/base/deprecated.jl b/base/deprecated.jl index c42d669a8fcde..153d758d5b959 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1589,6 +1589,8 @@ end @deprecate_binding AbstractIOBuffer GenericIOBuffer false +@deprecate String(io::GenericIOBuffer) String(take!(copy(io))) + # END 0.7 deprecations # BEGIN 1.0 deprecations diff --git a/base/iobuffer.jl b/base/iobuffer.jl index 23b1de0281fb6..a379adfff0e25 100644 --- a/base/iobuffer.jl +++ b/base/iobuffer.jl @@ -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) diff --git a/base/repl/LineEdit.jl b/base/repl/LineEdit.jl index 941a184ce0743..05802bb6cc747 100644 --- a/base/repl/LineEdit.jl +++ b/base/repl/LineEdit.jl @@ -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) @@ -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 diff --git a/base/repl/REPL.jl b/base/repl/REPL.jl index 76a8bca75189d..f6208db887883 100644 --- a/base/repl/REPL.jl +++ b/base/repl/REPL.jl @@ -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) && @@ -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 @@ -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 @@ -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 diff --git a/test/dict.jl b/test/dict.jl index d8001e27fc62e..c27af86751a5b 100644 --- a/test/dict.jl +++ b/test/dict.jl @@ -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)") @@ -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 diff --git a/test/iobuffer.jl b/test/iobuffer.jl index d8b3a19d80c8f..875504432879d 100644 --- a/test/iobuffer.jl +++ b/test/iobuffer.jl @@ -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) @@ -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 @@ -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) == "dω" - @test String(io) == "αhelloworldω" + @test bufcontents(io) == "αhelloworldω" @test_throws ArgumentError write(io,"!") @test take!(io) == b"αhelloworldω" seek(io, 2) @@ -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 diff --git a/test/repl.jl b/test/repl.jl index 1e33a5ec0221d..5fb4d2655dd9e 100644 --- a/test/repl.jl +++ b/test/repl.jl @@ -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 diff --git a/test/replutil.jl b/test/replutil.jl index 72f43108c00b3..d0597d4d919f5 100644 --- a/test/replutil.jl +++ b/test/replutil.jl @@ -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)) @@ -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