Browse Source

Set `:SOURCE_PATH` when evaluating code in sandbox module (#252)

This patch sets `:SOURCE_PATH` in the task local storage to the output file
while executing code in the sandbox module so that recursive `include` work as
expected. Fixes #251.
pull/256/merge
Grant Bruer 1 year ago committed by GitHub
parent
commit
369ebb0db7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 15
      src/Literate.jl
  2. 9
      test/runtests.jl

15
src/Literate.jl

@ -851,8 +851,8 @@ function sandbox() @@ -851,8 +851,8 @@ function sandbox()
# eval(expr) is available in the REPL (i.e. Main) so we emulate that for the sandbox
Core.eval(m, :(eval(x) = Core.eval($m, x)))
# modules created with Module() does not have include defined
# abspath is needed since this will call `include_relative`
Core.eval(m, :(include(x) = Base.include($m, abspath(x))))
# the source path for recursive include is set while executing the block
Core.eval(m, :(include(x) = Base.include($m, x)))
return m
end
@ -894,10 +894,13 @@ function execute_block(sb::Module, block::String; inputfile::String, fake_source @@ -894,10 +894,13 @@ function execute_block(sb::Module, block::String; inputfile::String, fake_source
# `rethrow = Union{}` means that we try-catch all the exceptions thrown in the do-block
# and return them via the return value (they get handled below).
c = IOCapture.capture(rethrow = Union{}) do
if softscope
include_string(REPL.softscope, sb, block, fake_source)
else
include_string(sb, block, fake_source)
# TODO: Perhaps `include_string` should set :SOURCE_PATH?
task_local_storage(:SOURCE_PATH, fake_source) do
if softscope
include_string(REPL.softscope, sb, block, fake_source)
else
include_string(sb, block, fake_source)
end
end
end
popdisplay(disp) # IOCapture.capture has a try-catch so should always end up here

9
test/runtests.jl

@ -1310,6 +1310,15 @@ end end @@ -1310,6 +1310,15 @@ end end
notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@test occursin("\"data\": {\n \"text/plain\": \"31\"\n }", notebook)
# issue #251 (recursive include)
write(inputfile, "include(\"issue251/A.jl\")")
mkdir(joinpath(outdir, "issue251"))
write(joinpath(outdir, "issue251", "A.jl"), "include(\"B.jl\")")
write(joinpath(outdir, "issue251", "B.jl"), "230 + 21")
Literate.notebook(inputfile, outdir)
notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@test occursin("\"data\": {\n \"text/plain\": \"251\"\n }", notebook)
# test error when executing notebook
write(inputfile, "for i in 1:10\n println(i)")
r = @test_logs((:error, r"error when executing notebook based on input file: "), match_mode=:any,

Loading…
Cancel
Save