Skip to content

Commit 3a314bc

Browse files
committed
deprecate String(io::IOBuffer). fixes #21438
1 parent 9c1d1b8 commit 3a314bc

9 files changed

+23
-22
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ Deprecated or removed
175175
* The forms of `read`, `readstring`, and `eachline` that accepted both a `Cmd` object and an
176176
input stream are deprecated. Use e.g. `read(pipeline(stdin, cmd))` instead ([#22762]).
177177

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

179181
Julia v0.6.0 Release Notes
180182
==========================

base/deprecated.jl

+2
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,8 @@ end
15871587
@deprecate readstring(cmd::AbstractCmd, stdin::Redirectable) readstring(pipeline(stdin, cmd))
15881588
@deprecate eachline(cmd::AbstractCmd, stdin; chomp::Bool=true) eachline(pipeline(stdin, cmd), chomp=chomp)
15891589

1590+
@deprecate String(io::AbstractIOBuffer) String(take!(copy(io)))
1591+
15901592
# END 0.7 deprecations
15911593

15921594
# BEGIN 1.0 deprecations

base/iobuffer.jl

-6
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,6 @@ end
257257

258258
isopen(io::AbstractIOBuffer) = io.readable || io.writable || io.seekable || nb_available(io) > 0
259259

260-
function String(io::AbstractIOBuffer)
261-
io.readable || throw(ArgumentError("IOBuffer is not readable"))
262-
io.seekable || throw(ArgumentError("IOBuffer is not seekable"))
263-
return unsafe_string(pointer(io.data), io.size)
264-
end
265-
266260
"""
267261
take!(b::IOBuffer)
268262

base/repl/LineEdit.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ mutable struct PromptState <: ModeState
6464
indent::Int
6565
end
6666

67-
input_string(s::PromptState) = String(s.input_buffer)
67+
input_string(s::PromptState) = String(take!(copy(s.input_buffer)))
6868

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

1048-
input_string(s::PrefixSearchState) = String(s.response_buffer)
1048+
input_string(s::PrefixSearchState) = String(take!(copy(s.response_buffer)))
10491049

10501050
# a meta-prompt that presents itself as parent_prompt, but which has an independent keymap
10511051
# for prefix searching

base/repl/REPL.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ function mode_idx(hist::REPLHistoryProvider, mode)
392392
end
393393

394394
function add_history(hist::REPLHistoryProvider, s)
395-
str = rstrip(String(s.input_buffer))
395+
str = rstrip(String(take!(copy(s.input_buffer))))
396396
isempty(strip(str)) && return
397397
mode = mode_idx(hist, LineEdit.mode(s))
398398
!isempty(hist.history) &&
@@ -506,7 +506,7 @@ function history_move_prefix(s::LineEdit.PrefixSearchState,
506506
prefix::AbstractString,
507507
backwards::Bool,
508508
cur_idx = hist.cur_idx)
509-
cur_response = String(LineEdit.buffer(s))
509+
cur_response = String(take!(copy(LineEdit.buffer(s))))
510510
# when searching forward, start at last_idx
511511
if !backwards && hist.last_idx > 0
512512
cur_idx = hist.last_idx
@@ -547,7 +547,7 @@ function history_search(hist::REPLHistoryProvider, query_buffer::IOBuffer, respo
547547
qpos = position(query_buffer)
548548
qpos > 0 || return true
549549
searchdata = beforecursor(query_buffer)
550-
response_str = String(response_buffer)
550+
response_str = String(take!(copy(response_buffer)))
551551

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

597597
function return_callback(s)
598598
ast = Base.syntax_deprecation_warnings(false) do
599-
Base.parse_input_line(String(LineEdit.buffer(s)))
599+
Base.parse_input_line(String(take!(copy(LineEdit.buffer(s)))))
600600
end
601601
if !isa(ast, Expr) || (ast.head != :continue && ast.head != :incomplete)
602602
return true

test/dict.jl

+4-3
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ let d = Dict((1=>2) => (3=>45), (3=>10) => (10=>11))
301301

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

316316
Base.show(io, MIME("text/plain"), Dict(Alpha()=>1))
317-
@test !contains(String(sbuff), "")
318-
@test endswith(String(sbuff), "α => 1")
317+
local str = String(take!(sbuff))
318+
@test !contains(str, "")
319+
@test endswith(str, "α => 1")
319320
end
320321

321322
# issue #2540

test/iobuffer.jl

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

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

5+
bufcontents(io::Base.AbstractIOBuffer) = unsafe_string(pointer(io.data), io.size)
6+
57
let io = IOBuffer()
68
@test eof(io)
79
@test_throws EOFError read(io,UInt8)
@@ -17,7 +19,7 @@ seek(io, 0)
1719
a = Array{UInt8}(2)
1820
@test read!(io, a) == a
1921
@test a == UInt8['b','c']
20-
@test String(io) == "abc"
22+
@test bufcontents(io) == "abc"
2123
seek(io, 1)
2224
truncate(io, 2)
2325
@test position(io) == 1
@@ -178,7 +180,7 @@ let io=IOBuffer(SubString("***αhelloworldω***",4,16)), io2 = IOBuffer(b"goodni
178180
@test_throws EOFError read(io,UInt8)
179181
skip(io, -3)
180182
@test readstring(io) == ""
181-
@test String(io) == "αhelloworldω"
183+
@test bufcontents(io) == "αhelloworldω"
182184
@test_throws ArgumentError write(io,"!")
183185
@test take!(io) == b"αhelloworldω"
184186
seek(io, 2)
@@ -193,7 +195,7 @@ let io=IOBuffer(SubString("***αhelloworldω***",4,16)), io2 = IOBuffer(b"goodni
193195
seek(io2, 0)
194196
write(io2, io2)
195197
@test readstring(io2) == ""
196-
@test String(io2) == "goodnightmoonhelloworld"
198+
@test bufcontents(io2) == "goodnightmoonhelloworld"
197199
end
198200

199201
# issue #11917

test/repl.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ function history_move_prefix(s::LineEdit.MIState,
677677
buf = LineEdit.buffer(s)
678678
pos = position(buf)
679679
prefix = REPL.beforecursor(buf)
680-
allbuf = String(buf)
680+
allbuf = String(take!(copy(buf)))
681681
cur_idx = hist.cur_idx
682682
# when searching forward, start at last_idx
683683
if !backwards && hist.last_idx > 0

test/replutil.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ let d = Dict(1 => 2, 3 => 45)
450450
buf = IOBuffer()
451451
td = TextDisplay(buf)
452452
display(td, d)
453-
result = String(td.io)
453+
result = String(take!(td.io))
454454

455455
@test contains(result, summary(d))
456456

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

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

478478
# @macroexpand tests

0 commit comments

Comments
 (0)