From 1a194cc982198334af9794fc8d80a5a9992ff9ab Mon Sep 17 00:00:00 2001 From: Juergen Fuhrmann Date: Mon, 14 Sep 2020 11:42:17 +0200 Subject: [PATCH] Add try/finally to @capture --- src/Capture.jl | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Capture.jl b/src/Capture.jl index fbee4df2..a4202b79 100644 --- a/src/Capture.jl +++ b/src/Capture.jl @@ -27,13 +27,16 @@ macro capture(expr) original_stdout = stdout out_rd, out_wr = redirect_stdout() reader = @async read(out_rd) - # Redirect both logging output and print(stderr,...) - # to stdout - with_logger(SimpleLogger(stdout)) do - redirect_stderr(()->$(esc(expr)),stdout) - end - redirect_stdout(original_stdout) - close(out_wr) + try + # Redirect both logging output and print(stderr,...) + # to stdout + with_logger(SimpleLogger(stdout)) do + redirect_stderr(()->$(esc(expr)),stdout) + end + finally + redirect_stdout(original_stdout) + close(out_wr) + end String(fetch(reader)) end end