Skip to content

Commit ad4b1ec

Browse files
authored
allow redirecting standard streams to IOContext (#36688)
1 parent 81d34c5 commit ad4b1ec

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ New library functions
5757
New library features
5858
--------------------
5959

60+
* The `redirect_*` functions can now be called on `IOContext` objects.
6061

6162
Standard library changes
6263
------------------------

base/stream.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,12 @@ for (x, writable, unix_fd, c_symbol) in
11661166
$x = devnull
11671167
return devnull
11681168
end
1169+
function ($f)(io::IOContext)
1170+
io2, _dict = unwrapcontext(io)
1171+
($f)(io2)
1172+
global $x = io
1173+
return io
1174+
end
11691175
end
11701176
end
11711177

test/spawn.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ end
252252
@test "Hello World\n" == read(fname, String)
253253
@test OLD_STDOUT === stdout
254254
rm(fname)
255+
256+
col = get(stdout, :color, false)
257+
redirect_stdout(IOContext(stdout, :color=>!col))
258+
@test get(stdout, :color, col) == !col
259+
redirect_stdout(OLD_STDOUT)
255260
end
256261
end
257262

0 commit comments

Comments
 (0)