Skip to content

Commit

Permalink
allow redirect_std* to devnull (JuliaLang#36136) (JuliaLang#36146)
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausC committed Jun 18, 2020
1 parent 4d94a63 commit 5420dd2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
15 changes: 12 additions & 3 deletions base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,15 @@ for (x, writable, unix_fd, c_symbol) in
($f)($(writable ? :write : :read))
return (read, write)
end
function ($f)(::DevNull)
global $x
nulldev = @static Sys.iswindows() ? "NUL" : "/dev/null"
handle = open(nulldev, write=$writable)
$(_f)(handle)
close(handle) # handle has been dup'ed in $(_f)
$x = devnull
return devnull
end
end
end

Expand All @@ -1115,7 +1124,7 @@ elsewhere.
If called with the optional `stream` argument, then returns `stream` itself.
!!! note
`stream` must be a `TTY`, a `Pipe`, or a socket.
`stream` must be an `IOStream`, a `TTY`, a `Pipe`, a socket, or `devnull`.
"""
redirect_stdout

Expand All @@ -1125,7 +1134,7 @@ redirect_stdout
Like [`redirect_stdout`](@ref), but for [`stderr`](@ref).
!!! note
`stream` must be a `TTY`, a `Pipe`, or a socket.
`stream` must be an `IOStream`, a `TTY`, a `Pipe`, a socket, or `devnull`.
"""
redirect_stderr

Expand All @@ -1137,7 +1146,7 @@ Note that the order of the return tuple is still `(rd, wr)`,
i.e. data to be read from [`stdin`](@ref) may be written to `wr`.
!!! note
`stream` must be a `TTY`, a `Pipe`, or a socket.
`stream` must be an `IOStream`, a `TTY`, a `Pipe`, a socket, or `devnull`.
"""
redirect_stdin

Expand Down
13 changes: 13 additions & 0 deletions test/spawn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,19 @@ end
end
end

# issue #36136
@testset "redirect to devnull" begin
@test redirect_stdout(devnull) do; println("Hello") end === nothing
@test redirect_stderr(devnull) do; println(stderr, "Hello") end === nothing
# stdin is unavailable on the workers. Run test on master.
ret = Core.eval(Main, quote
remotecall_fetch(1) do
redirect_stdin(devnull) do; read(stdin, String) end
end
end)
@test ret == ""
end

# Test that redirecting an IOStream does not crash the process
let fname = tempname(), p
cmd = """
Expand Down

0 comments on commit 5420dd2

Please sign in to comment.