Skip to content

Commit

Permalink
Add do-block support for redirect_std[out,err]
Browse files Browse the repository at this point in the history
In particular, these simplify testing for warnings
  • Loading branch information
timholy committed Aug 21, 2016
1 parent 03e7c79 commit aaec64b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
28 changes: 28 additions & 0 deletions base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions doc/stdlib/io-network.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"
@test repr(Tuple{Float32, Float32, Float32}) == "Tuple{Float32,Float32,Float32}"

0 comments on commit aaec64b

Please sign in to comment.