Skip to content

Commit 5420dd2

Browse files
authored
allow redirect_std* to devnull (#36136) (#36146)
1 parent 4d94a63 commit 5420dd2

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

base/stream.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,15 @@ for (x, writable, unix_fd, c_symbol) in
10981098
($f)($(writable ? :write : :read))
10991099
return (read, write)
11001100
end
1101+
function ($f)(::DevNull)
1102+
global $x
1103+
nulldev = @static Sys.iswindows() ? "NUL" : "/dev/null"
1104+
handle = open(nulldev, write=$writable)
1105+
$(_f)(handle)
1106+
close(handle) # handle has been dup'ed in $(_f)
1107+
$x = devnull
1108+
return devnull
1109+
end
11011110
end
11021111
end
11031112

@@ -1115,7 +1124,7 @@ elsewhere.
11151124
If called with the optional `stream` argument, then returns `stream` itself.
11161125
11171126
!!! note
1118-
`stream` must be a `TTY`, a `Pipe`, or a socket.
1127+
`stream` must be an `IOStream`, a `TTY`, a `Pipe`, a socket, or `devnull`.
11191128
"""
11201129
redirect_stdout
11211130

@@ -1125,7 +1134,7 @@ redirect_stdout
11251134
Like [`redirect_stdout`](@ref), but for [`stderr`](@ref).
11261135
11271136
!!! note
1128-
`stream` must be a `TTY`, a `Pipe`, or a socket.
1137+
`stream` must be an `IOStream`, a `TTY`, a `Pipe`, a socket, or `devnull`.
11291138
"""
11301139
redirect_stderr
11311140

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

test/spawn.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,19 @@ end
255255
end
256256
end
257257

258+
# issue #36136
259+
@testset "redirect to devnull" begin
260+
@test redirect_stdout(devnull) do; println("Hello") end === nothing
261+
@test redirect_stderr(devnull) do; println(stderr, "Hello") end === nothing
262+
# stdin is unavailable on the workers. Run test on master.
263+
ret = Core.eval(Main, quote
264+
remotecall_fetch(1) do
265+
redirect_stdin(devnull) do; read(stdin, String) end
266+
end
267+
end)
268+
@test ret == ""
269+
end
270+
258271
# Test that redirecting an IOStream does not crash the process
259272
let fname = tempname(), p
260273
cmd = """

0 commit comments

Comments
 (0)