From 293db9c05a27d5398b8e054f8b7f764003ef8306 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Wed, 15 Jul 2020 21:49:22 -0400 Subject: [PATCH] allow redirecting standard streams to `IOContext` --- NEWS.md | 1 + base/stream.jl | 6 ++++++ test/spawn.jl | 5 +++++ 3 files changed, 12 insertions(+) diff --git a/NEWS.md b/NEWS.md index d9548b0166d71..33709f5b8c03c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -53,6 +53,7 @@ New library functions New library features -------------------- +* The `redirect_*` functions can now be called on `IOContext` objects. Standard library changes ------------------------ diff --git a/base/stream.jl b/base/stream.jl index 93614b21a2438..b0864409d7ea2 100644 --- a/base/stream.jl +++ b/base/stream.jl @@ -1166,6 +1166,12 @@ for (x, writable, unix_fd, c_symbol) in $x = devnull return devnull end + function ($f)(io::IOContext) + io2, _dict = unwrapcontext(io) + ($f)(io2) + global $x = io + return io + end end end diff --git a/test/spawn.jl b/test/spawn.jl index 8a73394d8ae47..d7b685eaf32c1 100644 --- a/test/spawn.jl +++ b/test/spawn.jl @@ -252,6 +252,11 @@ end @test "Hello World\n" == read(fname, String) @test OLD_STDOUT === stdout rm(fname) + + col = get(stdout, :color, false) + redirect_stdout(IOContext(stdout, :color=>!col)) + @test get(stdout, :color, col) == !col + redirect_stdout(OLD_STDOUT) end end