diff --git a/base/client.jl b/base/client.jl index 7cf6dc334b240a..2a6e63bd926668 100644 --- a/base/client.jl +++ b/base/client.jl @@ -132,14 +132,14 @@ function eval_user_input(errio, @nospecialize(ast), show_value::Bool) end if lasterr !== nothing lasterr = scrub_repl_backtrace(lasterr) - istrivialerror(lasterr) || setglobal!(Main, :err, lasterr) + istrivialerror(lasterr) || setglobal!(Base.MainInclude, :err, lasterr) invokelatest(display_error, errio, lasterr) errcount = 0 lasterr = nothing else ast = Meta.lower(Main, ast) value = Core.eval(Main, ast) - setglobal!(Main, :ans, value) + setglobal!(Base.MainInclude, :ans, value) if !(value === nothing) && show_value if have_color print(answer_color()) @@ -159,7 +159,7 @@ function eval_user_input(errio, @nospecialize(ast), show_value::Bool) end errcount += 1 lasterr = scrub_repl_backtrace(current_exceptions()) - setglobal!(Main, :err, lasterr) + setglobal!(Base.MainInclude, :err, lasterr) if errcount > 2 @error "It is likely that something important is broken, and Julia will not be able to continue normally" errcount break @@ -478,6 +478,10 @@ function include(fname::AbstractString) Base._include(identity, Main, fname) end eval(x) = Core.eval(Main, x) + +# weakly exposes ans and err and Out variables to Main +global ans, err, Out +export ans, err, Out end """ diff --git a/base/sysimg.jl b/base/sysimg.jl index ef7bad929b7437..fb2f875f2c0f23 100644 --- a/base/sysimg.jl +++ b/base/sysimg.jl @@ -5,6 +5,7 @@ Core.include(Main, "Base.jl") using .Base # Set up Main module +using Base.MainInclude # ans, err, Out import Base.MainInclude: eval, include # Ensure this file is also tracked diff --git a/stdlib/REPL/src/REPL.jl b/stdlib/REPL/src/REPL.jl index 9c8712e0d41fc1..1f134ab8c92ba7 100644 --- a/stdlib/REPL/src/REPL.jl +++ b/stdlib/REPL/src/REPL.jl @@ -151,7 +151,7 @@ function eval_user_input(@nospecialize(ast), backend::REPLBackend, mod::Module) end value = Core.eval(mod, ast) backend.in_eval = false - setglobal!(mod, :ans, value) + setglobal!(Base.MainInclude, :ans, value) put!(backend.response_channel, Pair{Any, Bool}(value, false)) end break @@ -293,7 +293,7 @@ function print_response(errio::IO, response, show_value::Bool, have_color::Bool, Base.sigatomic_end() if iserr val = Base.scrub_repl_backtrace(val) - Base.istrivialerror(val) || setglobal!(Main, :err, val) + Base.istrivialerror(val) || setglobal!(Base.MainInclude, :err, val) Base.invokelatest(Base.display_error, errio, val) else if val !== nothing && show_value @@ -316,7 +316,7 @@ function print_response(errio::IO, response, show_value::Bool, have_color::Bool, println(errio, "SYSTEM (REPL): showing an error caused an error") try excs = Base.scrub_repl_backtrace(current_exceptions()) - setglobal!(Main, :err, excs) + setglobal!(Base.MainInclude, :err, excs) Base.invokelatest(Base.display_error, errio, excs) catch e # at this point, only print the name of the type as a Symbol to @@ -1413,7 +1413,7 @@ end function capture_result(n::Ref{Int}, @nospecialize(x)) n = n[] - mod = REPL.active_module() + mod = Base.MainInclude if !isdefined(mod, :Out) setglobal!(mod, :Out, Dict{Int, Any}()) end