From aaec64b6a641a6afb2bdb9060cdff62998029155 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Sun, 21 Aug 2016 06:08:51 -0500 Subject: [PATCH] Add do-block support for redirect_std[out,err] In particular, these simplify testing for warnings --- base/stream.jl | 28 ++++++++++++++++++++++++++++ doc/stdlib/io-network.rst | 12 ++++++++++++ test/show.jl | 22 +++++++++++++++++++++- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/base/stream.jl b/base/stream.jl index d57180fe7badb3..8a3e532875e7ad 100644 --- a/base/stream.jl +++ b/base/stream.jl @@ -955,6 +955,34 @@ for (x, writable, unix_fd, c_symbol) in end end +for (F,S) in ((:redirect_stdout, :STDOUT), (:redirect_stderr, :STDERR)) + @eval function $F(f::Function, stream) + STDOLD = $S + local ret + $F(stream) + try + ret = f() + finally + $F(STDOLD) + end + ret + end +end + +""" + redirect_stdout(f::Function, stream) + +Run the function `f` while redirecting `STDOUT` to `stream`. Upon completion, `STDOUT` is restored to its prior setting. +""" +redirect_stdout(f::Function, stream) + +""" + redirect_stderr(f::Function, stream) + +Run the function `f` while redirecting `STDERR` to `stream`. Upon completion, `STDERR` is restored to its prior setting. +""" +redirect_stderr(f::Function, stream) + mark(x::LibuvStream) = mark(x.buffer) unmark(x::LibuvStream) = unmark(x.buffer) reset(x::LibuvStream) = reset(x.buffer) diff --git a/doc/stdlib/io-network.rst b/doc/stdlib/io-network.rst index 67a1b123c57b6a..aeec513c0f45dd 100644 --- a/doc/stdlib/io-network.rst +++ b/doc/stdlib/io-network.rst @@ -350,12 +350,24 @@ General I/O 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``\ . +.. function:: redirect_stdout(f::Function, stream) + + .. Docstring generated from Julia source + + Run the function ``f`` while redirecting ``STDOUT`` to ``stream``\ . Upon completion, ``STDOUT`` is restored to its prior setting. + .. function:: redirect_stderr([stream]) .. Docstring generated from Julia source Like ``redirect_stdout``\ , but for ``STDERR``\ . +.. function:: redirect_stderr(f::Function, stream) + + .. Docstring generated from Julia source + + Run the function ``f`` while redirecting ``STDERR`` to ``stream``\ . Upon completion, ``STDERR`` is restored to its prior setting. + .. function:: redirect_stdin([stream]) .. Docstring generated from Julia source diff --git a/test/show.jl b/test/show.jl index 2d185d76fb3796..3292d31ff169df 100644 --- a/test/show.jl +++ b/test/show.jl @@ -318,6 +318,26 @@ let oldout = STDOUT, olderr = STDERR end end +let filename = tempname() + ret = open(filename, "w") do f + redirect_stdout(f) do + println("hello") + [1,3] + end + end + @test ret == [1,3] + @test chomp(readstring(filename)) == "hello" + ret = open(filename, "w") do f + redirect_stderr(f) do + warn("hello") + [2] + end + end + @test ret == [2] + @test contains(readstring(filename), "WARNING: hello") + rm(filename) +end + # issue #12960 type T12960 end let @@ -576,4 +596,4 @@ end # Test compact printing of homogeneous tuples @test repr(NTuple{7,Int64}) == "NTuple{7,Int64}" @test repr(Tuple{Float64, Float64, Float64, Float64}) == "NTuple{4,Float64}" -@test repr(Tuple{Float32, Float32, Float32}) == "Tuple{Float32,Float32,Float32}" \ No newline at end of file +@test repr(Tuple{Float32, Float32, Float32}) == "Tuple{Float32,Float32,Float32}"