Skip to content

Commit

Permalink
move ans and err variables to MainInclude module
Browse files Browse the repository at this point in the history
This hides them from `names()` unfortunately, but means we will not
accidentally discard a variable from the user in Main, either because
the import will fail or the assignment will fail. If we later have
world-versioned bindings, this would also mean you could toggle (between
worlds) between being module-local and a special import value.

Fix JuliaLang#43172
Fix JuliaLang#48299
  • Loading branch information
vtjnash committed Jan 16, 2023
1 parent 687433b commit 27b8a5a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
10 changes: 7 additions & 3 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
Expand Down Expand Up @@ -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

"""
Expand Down
1 change: 1 addition & 0 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 27b8a5a

Please sign in to comment.