Browse Source

copy the withoutput function from Documenter.jl

pull/1/head
Fredrik Ekre 8 years ago
parent
commit
4f8df48b04
  1. 1
      REQUIRE
  2. 55
      src/Documenter.jl
  3. 6
      src/Examples.jl
  4. 1
      src/IJulia.jl

1
REQUIRE

@ -1,3 +1,2 @@
julia 0.6 julia 0.6
JSON JSON
Documenter

55
src/Documenter.jl

@ -0,0 +1,55 @@
# copied from Documenter.jl
module Documenter
@static if VERSION < v"0.7.0-DEV.3951"
link_pipe!(pipe; reader_supports_async = true, writer_supports_async = true) =
Base.link_pipe(pipe, julia_only_read = reader_supports_async, julia_only_write = writer_supports_async)
else
import Base: link_pipe!
end
@static if isdefined(Base, :with_logger)
using Logging
else # make things a no-op since warnings/info already print to stdout
struct ConsoleLogger end
ConsoleLogger(io) = ConsoleLogger()
with_logger(f, logger) = f()
end
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()
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 = ConsoleLogger(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 = 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

6
src/Examples.jl

@ -6,6 +6,8 @@ import JSON
include("IJulia.jl") include("IJulia.jl")
import .IJulia import .IJulia
include("Documenter.jl")
import .Documenter
# # Some simple rules: # # Some simple rules:
# #
@ -326,8 +328,6 @@ function notebook(inputfile, outputdir; preprocess = identity, postprocess = ide
return outputfile return outputfile
end end
import Documenter # just copy paste Documenter.Utilities.withoutput instead
function execute_notebook(nb) function execute_notebook(nb)
# sandbox module for the notebook (TODO: Do this in Main?) # sandbox module for the notebook (TODO: Do this in Main?)
m = Module(gensym()) m = Module(gensym())
@ -343,7 +343,7 @@ function execute_notebook(nb)
# status = (true|false) # status = (true|false)
# _: backtrace # _: backtrace
# str combined stdout, stderr output # str combined stdout, stderr output
r, status, _, str = Documenter.Utilities.withoutput() do r, status, _, str = Documenter.withoutput() do
include_string(m, block) include_string(m, block)
end end
status || error("something went wrong when evaluating code") status || error("something went wrong when evaluating code")

1
src/IJulia.jl

@ -1,4 +1,3 @@
# minimal (modified) subset of IJulia.jl to evaluate notebooks # minimal (modified) subset of IJulia.jl to evaluate notebooks
module IJulia module IJulia

Loading…
Cancel
Save