Skip to content

Commit 47c3fd7

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

File tree

7 files changed

+14
-14
lines changed

7 files changed

+14
-14
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

test/dict.jl

+1-1
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)")

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::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)