Skip to content

Commit

Permalink
fix(REPL): using/import statements should on top-level, #49041 (#49098)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangl-cc authored Mar 30, 2023
1 parent 329f92c commit e5c2c51
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
14 changes: 13 additions & 1 deletion stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1407,12 +1407,24 @@ function repl_eval_counter(hp)
end

function out_transform(@nospecialize(x), n::Ref{Int})
return quote
return Expr(:toplevel, get_usings!([], x)..., quote
let __temp_val_a72df459 = $x
$capture_result($n, __temp_val_a72df459)
__temp_val_a72df459
end
end)
end

function get_usings!(usings, ex)
# get all `using` and `import` statements which are at the top level
for (i, arg) in enumerate(ex.args)
if Base.isexpr(arg, :toplevel)
get_usings!(usings, arg)
elseif Base.isexpr(arg, [:using, :import])
push!(usings, popat!(ex.args, i))
end
end
return usings
end

function capture_result(n::Ref{Int}, @nospecialize(x))
Expand Down
5 changes: 5 additions & 0 deletions stdlib/REPL/test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,11 @@ fake_repl() do stdin_write, stdout_read, repl
s = sendrepl2("x_47878 = range(-1; stop = 1)\n", "-1:1")
@test contains(s, "Out[11]: -1:1")

# Test for https://github.com/JuliaLang/julia/issues/49041
s = sendrepl2("using Test; @test true", "In [14]")
@test !contains(s, "ERROR")
@test contains(s, "Test Passed")

write(stdin_write, '\x04')
Base.wait(repltask)
end

0 comments on commit e5c2c51

Please sign in to comment.