Skip to content

Commit 7199111

Browse files
authored
Merge pull request #18165 from JuliaLang/teh/redirect
Add do-block support for redirect_std[out,err,in]
2 parents d474ca3 + d0b901b commit 7199111

File tree

4 files changed

+113
-34
lines changed

4 files changed

+113
-34
lines changed

base/docs/helpdb/Base.jl

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3936,14 +3936,6 @@ Convert `x` from degrees to radians.
39363936
"""
39373937
deg2rad
39383938

3939-
"""
3940-
redirect_stdin([stream])
3941-
3942-
Like redirect_stdout, but for STDIN. Note that the order of the return tuple is still
3943-
(rd,wr), i.e. data to be read from STDIN, may be written to wr.
3944-
"""
3945-
redirect_stdin
3946-
39473939
"""
39483940
mktemp([parent=tempdir()])
39493941
@@ -5196,13 +5188,6 @@ The result of an expression is too large for the specified type and will cause a
51965188
"""
51975189
OverflowError
51985190

5199-
"""
5200-
redirect_stderr([stream])
5201-
5202-
Like `redirect_stdout`, but for `STDERR`.
5203-
"""
5204-
redirect_stderr
5205-
52065191
"""
52075192
ctranspose!(dest,src)
52085193
@@ -6759,24 +6744,6 @@ General unescaping of traditional C and Unicode escape sequences. Reverse of
67596744
"""
67606745
unescape_string(s)
67616746

6762-
"""
6763-
redirect_stdout()
6764-
6765-
Create a pipe to which all C and Julia level `STDOUT` output will be redirected. Returns a
6766-
tuple `(rd,wr)` representing the pipe ends. Data written to `STDOUT` may now be read from
6767-
the rd end of the pipe. The wr end is given for convenience in case the old `STDOUT` object
6768-
was cached by the user and needs to be replaced elsewhere.
6769-
"""
6770-
redirect_stdout
6771-
6772-
"""
6773-
redirect_stdout(stream)
6774-
6775-
Replace `STDOUT` by stream for all C and Julia level output to `STDOUT`. Note that `stream`
6776-
must be a TTY, a `Pipe` or a `TCPSocket`.
6777-
"""
6778-
redirect_stdout(stream)
6779-
67806747
"""
67816748
print_with_color(color::Symbol, [io], strings...)
67826749

base/stream.jl

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,74 @@ for (x, writable, unix_fd, c_symbol) in
955955
end
956956
end
957957

958+
"""
959+
redirect_stdout()
960+
961+
Create a pipe to which all C and Julia level `STDOUT` output will be redirected. Returns a
962+
tuple `(rd,wr)` representing the pipe ends. Data written to `STDOUT` may now be read from
963+
the rd end of the pipe. The wr end is given for convenience in case the old `STDOUT` object
964+
was cached by the user and needs to be replaced elsewhere.
965+
"""
966+
redirect_stdout
967+
968+
"""
969+
redirect_stdout(stream)
970+
971+
Replace `STDOUT` by stream for all C and Julia level output to `STDOUT`. Note that `stream`
972+
must be a TTY, a `Pipe` or a `TCPSocket`.
973+
"""
974+
redirect_stdout(stream)
975+
976+
"""
977+
redirect_stderr([stream])
978+
979+
Like `redirect_stdout`, but for `STDERR`.
980+
"""
981+
redirect_stderr
982+
983+
"""
984+
redirect_stdin([stream])
985+
986+
Like redirect_stdout, but for STDIN. Note that the order of the return tuple is still
987+
(rd,wr), i.e. data to be read from STDIN, may be written to wr.
988+
"""
989+
redirect_stdin
990+
991+
for (F,S) in ((:redirect_stdin, :STDIN), (:redirect_stdout, :STDOUT), (:redirect_stderr, :STDERR))
992+
@eval function $F(f::Function, stream)
993+
STDOLD = $S
994+
local ret
995+
$F(stream)
996+
try
997+
ret = f()
998+
finally
999+
$F(STDOLD)
1000+
end
1001+
ret
1002+
end
1003+
end
1004+
1005+
"""
1006+
redirect_stdout(f::Function, stream)
1007+
1008+
Run the function `f` while redirecting `STDOUT` to `stream`. Upon completion, `STDOUT` is restored to its prior setting.
1009+
"""
1010+
redirect_stdout(f::Function, stream)
1011+
1012+
"""
1013+
redirect_stderr(f::Function, stream)
1014+
1015+
Run the function `f` while redirecting `STDERR` to `stream`. Upon completion, `STDERR` is restored to its prior setting.
1016+
"""
1017+
redirect_stderr(f::Function, stream)
1018+
1019+
"""
1020+
redirect_stdin(f::Function, stream)
1021+
1022+
Run the function `f` while redirecting `STDIN` to `stream`. Upon completion, `STDIN` is restored to its prior setting.
1023+
"""
1024+
redirect_stdin(f::Function, stream)
1025+
9581026
mark(x::LibuvStream) = mark(x.buffer)
9591027
unmark(x::LibuvStream) = unmark(x.buffer)
9601028
reset(x::LibuvStream) = reset(x.buffer)

doc/stdlib/io-network.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,18 +350,36 @@ General I/O
350350
351351
Replace ``STDOUT`` by stream for all C and Julia level output to ``STDOUT``\ . Note that ``stream`` must be a TTY, a ``Pipe`` or a ``TCPSocket``\ .
352352

353+
.. function:: redirect_stdout(f::Function, stream)
354+
355+
.. Docstring generated from Julia source
356+
357+
Run the function ``f`` while redirecting ``STDOUT`` to ``stream``\ . Upon completion, ``STDOUT`` is restored to its prior setting.
358+
353359
.. function:: redirect_stderr([stream])
354360

355361
.. Docstring generated from Julia source
356362
357363
Like ``redirect_stdout``\ , but for ``STDERR``\ .
358364

365+
.. function:: redirect_stderr(f::Function, stream)
366+
367+
.. Docstring generated from Julia source
368+
369+
Run the function ``f`` while redirecting ``STDERR`` to ``stream``\ . Upon completion, ``STDERR`` is restored to its prior setting.
370+
359371
.. function:: redirect_stdin([stream])
360372

361373
.. Docstring generated from Julia source
362374
363375
Like redirect_stdout, but for STDIN. Note that the order of the return tuple is still (rd,wr), i.e. data to be read from STDIN, may be written to wr.
364376

377+
.. function:: redirect_stdin(f::Function, stream)
378+
379+
.. Docstring generated from Julia source
380+
381+
Run the function ``f`` while redirecting ``STDIN`` to ``stream``\ . Upon completion, ``STDIN`` is restored to its prior setting.
382+
365383
.. function:: readchomp(x)
366384

367385
.. Docstring generated from Julia source

test/show.jl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,32 @@ let oldout = STDOUT, olderr = STDERR
318318
end
319319
end
320320

321+
let filename = tempname()
322+
ret = open(filename, "w") do f
323+
redirect_stdout(f) do
324+
println("hello")
325+
[1,3]
326+
end
327+
end
328+
@test ret == [1,3]
329+
@test chomp(readstring(filename)) == "hello"
330+
ret = open(filename, "w") do f
331+
redirect_stderr(f) do
332+
warn("hello")
333+
[2]
334+
end
335+
end
336+
@test ret == [2]
337+
@test contains(readstring(filename), "WARNING: hello")
338+
ret = open(filename) do f
339+
redirect_stdin(f) do
340+
readline()
341+
end
342+
end
343+
@test contains(ret, "WARNING: hello")
344+
rm(filename)
345+
end
346+
321347
# issue #12960
322348
type T12960 end
323349
let
@@ -576,4 +602,4 @@ end
576602
# Test compact printing of homogeneous tuples
577603
@test repr(NTuple{7,Int64}) == "NTuple{7,Int64}"
578604
@test repr(Tuple{Float64, Float64, Float64, Float64}) == "NTuple{4,Float64}"
579-
@test repr(Tuple{Float32, Float32, Float32}) == "Tuple{Float32,Float32,Float32}"
605+
@test repr(Tuple{Float32, Float32, Float32}) == "Tuple{Float32,Float32,Float32}"

0 commit comments

Comments
 (0)