Skip to content

Commit

Permalink
Add do-block support for redirect_std[out,err,in]
Browse files Browse the repository at this point in the history
Fixes JuliaLang#7022. In particular, these simplify testing for warnings.
  • Loading branch information
timholy committed Aug 22, 2016
1 parent 03e7c79 commit d0b901b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 34 deletions.
33 changes: 0 additions & 33 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3936,14 +3936,6 @@ Convert `x` from degrees to radians.
"""
deg2rad

"""
redirect_stdin([stream])
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.
"""
redirect_stdin

"""
mktemp([parent=tempdir()])
Expand Down Expand Up @@ -5196,13 +5188,6 @@ The result of an expression is too large for the specified type and will cause a
"""
OverflowError

"""
redirect_stderr([stream])
Like `redirect_stdout`, but for `STDERR`.
"""
redirect_stderr

"""
ctranspose!(dest,src)
Expand Down Expand Up @@ -6759,24 +6744,6 @@ General unescaping of traditional C and Unicode escape sequences. Reverse of
"""
unescape_string(s)

"""
redirect_stdout()
Create a pipe to which all C and Julia level `STDOUT` output will be redirected. Returns a
tuple `(rd,wr)` representing the pipe ends. Data written to `STDOUT` may now be read from
the rd end of the pipe. The wr end is given for convenience in case the old `STDOUT` object
was cached by the user and needs to be replaced elsewhere.
"""
redirect_stdout

"""
redirect_stdout(stream)
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`.
"""
redirect_stdout(stream)

"""
print_with_color(color::Symbol, [io], strings...)
Expand Down
68 changes: 68 additions & 0 deletions base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,74 @@ for (x, writable, unix_fd, c_symbol) in
end
end

"""
redirect_stdout()
Create a pipe to which all C and Julia level `STDOUT` output will be redirected. Returns a
tuple `(rd,wr)` representing the pipe ends. Data written to `STDOUT` may now be read from
the rd end of the pipe. The wr end is given for convenience in case the old `STDOUT` object
was cached by the user and needs to be replaced elsewhere.
"""
redirect_stdout

"""
redirect_stdout(stream)
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`.
"""
redirect_stdout(stream)

"""
redirect_stderr([stream])
Like `redirect_stdout`, but for `STDERR`.
"""
redirect_stderr

"""
redirect_stdin([stream])
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.
"""
redirect_stdin

for (F,S) in ((:redirect_stdin, :STDIN), (: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)

"""
redirect_stdin(f::Function, stream)
Run the function `f` while redirecting `STDIN` to `stream`. Upon completion, `STDIN` is restored to its prior setting.
"""
redirect_stdin(f::Function, stream)

mark(x::LibuvStream) = mark(x.buffer)
unmark(x::LibuvStream) = unmark(x.buffer)
reset(x::LibuvStream) = reset(x.buffer)
Expand Down
18 changes: 18 additions & 0 deletions doc/stdlib/io-network.rst
Original file line number Diff line number Diff line change
Expand Up @@ -350,18 +350,36 @@ 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
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.

.. function:: redirect_stdin(f::Function, stream)

.. Docstring generated from Julia source
Run the function ``f`` while redirecting ``STDIN`` to ``stream``\ . Upon completion, ``STDIN`` is restored to its prior setting.

.. function:: readchomp(x)

.. Docstring generated from Julia source
Expand Down
28 changes: 27 additions & 1 deletion test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,32 @@ 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")
ret = open(filename) do f
redirect_stdin(f) do
readline()
end
end
@test contains(ret, "WARNING: hello")
rm(filename)
end

# issue #12960
type T12960 end
let
Expand Down Expand Up @@ -576,4 +602,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 d0b901b

Please sign in to comment.