Browse Source

Use task_local_storage to set SOURCE_PATH

pull/252/head
Fredrik Ekre 1 year ago
parent
commit
f88ea650b7
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 23
      src/Literate.jl
  2. 4
      test/runtests.jl

23
src/Literate.jl

@ -582,7 +582,7 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg
preprocessor(inputfile, outputdir; user_config=config, user_kwargs=kwargs, type=:md) preprocessor(inputfile, outputdir; user_config=config, user_kwargs=kwargs, type=:md)
# create the markdown file # create the markdown file
sb = sandbox(config["literate_outputfile"]) sb = sandbox()
iomd = IOBuffer() iomd = IOBuffer()
for (chunknum, chunk) in enumerate(chunks) for (chunknum, chunk) in enumerate(chunks)
if isa(chunk, MDChunk) if isa(chunk, MDChunk)
@ -789,7 +789,7 @@ function jupyter_notebook(chunks, config)
end end
function execute_notebook(nb; inputfile::String, fake_source::String, softscope::Bool) function execute_notebook(nb; inputfile::String, fake_source::String, softscope::Bool)
sb = sandbox(fake_source) sb = sandbox()
execution_count = 0 execution_count = 0
for cell in nb["cells"] for cell in nb["cells"]
cell["cell_type"] == "code" || continue cell["cell_type"] == "code" || continue
@ -851,17 +851,11 @@ function sandbox()
# eval(expr) is available in the REPL (i.e. Main) so we emulate that for the 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))) Core.eval(m, :(eval(x) = Core.eval($m, x)))
# modules created with Module() does not have include defined # modules created with Module() does not have include defined
# the source path for recursive include is set while executing the block
Core.eval(m, :(include(x) = Base.include($m, x))) Core.eval(m, :(include(x) = Base.include($m, x)))
return m return m
end end
function sandbox(source_file)
m = sandbox()
# For `include` to work, the source file must be set.
Core.eval(m, :(current_task().storage[:SOURCE_PATH] = $source_file))
return m
end
# Capture display for notebooks # Capture display for notebooks
struct LiterateDisplay <: AbstractDisplay struct LiterateDisplay <: AbstractDisplay
data::Vector data::Vector
@ -900,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 # `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). # and return them via the return value (they get handled below).
c = IOCapture.capture(rethrow = Union{}) do c = IOCapture.capture(rethrow = Union{}) do
if softscope # TODO: Perhaps `include_string` should set :SOURCE_PATH?
include_string(REPL.softscope, sb, block, fake_source) task_local_storage(:SOURCE_PATH, fake_source) do
else if softscope
include_string(sb, block, fake_source) include_string(REPL.softscope, sb, block, fake_source)
else
include_string(sb, block, fake_source)
end
end end
end end
popdisplay(disp) # IOCapture.capture has a try-catch so should always end up here popdisplay(disp) # IOCapture.capture has a try-catch so should always end up here

4
test/runtests.jl

@ -1310,8 +1310,8 @@ end end
notebook = read(joinpath(outdir, "inputfile.ipynb"), String) notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@test occursin("\"data\": {\n \"text/plain\": \"31\"\n }", notebook) @test occursin("\"data\": {\n \"text/plain\": \"31\"\n }", notebook)
# issue #251 # issue #251 (recursive include)
write(inputfile, "include(joinpath(\"issue251\", \"A.jl\"))") write(inputfile, "include(\"issue251/A.jl\")")
mkdir(joinpath(outdir, "issue251")) mkdir(joinpath(outdir, "issue251"))
write(joinpath(outdir, "issue251", "A.jl"), "include(\"B.jl\")") write(joinpath(outdir, "issue251", "A.jl"), "include(\"B.jl\")")
write(joinpath(outdir, "issue251", "B.jl"), "230 + 21") write(joinpath(outdir, "issue251", "B.jl"), "230 + 21")

Loading…
Cancel
Save