From c36cde6c4da12df76c8e5254902a0718387c5e26 Mon Sep 17 00:00:00 2001 From: Morten Piibeleht Date: Sun, 27 Sep 2020 18:09:38 +1300 Subject: [PATCH] Use IOCapture instead of withoutput --- Project.toml | 2 ++ src/Documenter.jl | 42 ------------------------------------------ src/Literate.jl | 16 +++++----------- 3 files changed, 7 insertions(+), 53 deletions(-) delete mode 100644 src/Documenter.jl diff --git a/Project.toml b/Project.toml index f72df05..f71b284 100644 --- a/Project.toml +++ b/Project.toml @@ -4,10 +4,12 @@ version = "2.7.0" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" +IOCapture = "b5f81e59-6552-4d32-b1f0-c071b021bf89" JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" [compat] +IOCapture = "0.1" JSON = "0.18, 0.19, 0.20, 0.21, 1" julia = "1" diff --git a/src/Documenter.jl b/src/Documenter.jl deleted file mode 100644 index 115d3e7..0000000 --- a/src/Documenter.jl +++ /dev/null @@ -1,42 +0,0 @@ -# this file contains some utilities copied from the Documenter.jl package -# (https://github.com/JuliaDocs/Documenter.jl), see LICENSE.md for license -module Documenter - -function withoutput(f) - # Save the default output streams. - default_stdout = stdout - default_stderr = stderr - - # Redirect both the `stdout` and `stderr` streams to a single `Pipe` object. - pipe = Pipe() - Base.link_pipe!(pipe; reader_supports_async = true, writer_supports_async = true) - redirect_stdout(pipe.in) - redirect_stderr(pipe.in) - # Also redirect logging stream to the same pipe - logger = Base.CoreLogging.SimpleLogger(pipe.in) - - # Bytes written to the `pipe` are captured in `output` and converted to a `String`. - output = UInt8[] - - # Run the function `f`, capturing all output that it might have generated. - # Success signals whether the function `f` did or did not throw an exception. - result, success, backtrace = Base.CoreLogging.with_logger(logger) do - try - f(), true, Vector{Ptr{Cvoid}}() - catch err - err, false, catch_backtrace() - finally - # Force at least a single write to `pipe`, otherwise `readavailable` blocks. - println() - # Restore the original output streams. - redirect_stdout(default_stdout) - redirect_stderr(default_stderr) - # NOTE: `close` must always be called *after* `readavailable`. - append!(output, readavailable(pipe)) - close(pipe) - end - end - return result, success, backtrace, chomp(String(output)) -end - -end diff --git a/src/Literate.jl b/src/Literate.jl index 92cf041..22063bd 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -6,12 +6,10 @@ https://fredrikekre.github.io/Literate.jl/ for documentation. """ module Literate -import JSON, REPL +import JSON, REPL, IOCapture include("IJulia.jl") import .IJulia -include("Documenter.jl") -import .Documenter # # Some simple rules: # @@ -670,16 +668,12 @@ end # Execute a code-block in a module and capture stdout/stderr and the result function execute_block(sb::Module, block::String) - # r is the result - # status = (true|false) - # _: backtrace - # str combined stdout, stderr output - r, status, _, str = Documenter.withoutput() do + c = IOCapture.iocapture(throwerrors=false) do include_string(sb, block) end - if !status + if c.error error(""" - $(sprint(showerror, r)) + $(sprint(showerror, c.value)) when executing the following code block ```julia @@ -687,7 +681,7 @@ function execute_block(sb::Module, block::String) ``` """) end - return r, str + return c.value, c.output end end # module