Skip to content

Commit 31d4bf4

Browse files
committed
Refinements
1 parent a46df33 commit 31d4bf4

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

base/client.jl

+10-14
Original file line numberDiff line numberDiff line change
@@ -84,33 +84,26 @@ end
8484

8585
function scrub_repl_backtrace(bt)
8686
if bt !== nothing && !(bt isa Vector{Any}) # ignore our sentinel value types
87-
bt = stacktrace(bt)
87+
bt = bt isa Vector{StackFrame} ? copy(bt) : stacktrace(bt)
8888
# remove REPL-related frames from interactive printing
8989
eval_ind = findlast(frame -> !frame.from_c && frame.func === :eval, bt)
9090
eval_ind === nothing || deleteat!(bt, eval_ind:length(bt))
9191
end
9292
return bt
9393
end
94+
scrub_repl_backtrace(stack::ExceptionStack) =
95+
ExceptionStack([(exception = x[1], backtrace = scrub_repl_backtrace(x[2])) for x in stack])
96+
97+
istrivialerror(stack::ExceptionStack) =
98+
length(stack) == 1 && length(stack[1].backtrace) 1
99+
# frame 1 = top level; assumes already went through scrub_repl_backtrace
94100

95-
function display_error(io::IO, er, bt)
96-
printstyled(io, "ERROR: "; bold=true, color=Base.error_color())
97-
bt = scrub_repl_backtrace(bt)
98-
stack = ExceptionStack([(exception = er, backtrace = bt)])
99-
istrivial = length(bt) 1 # frame 1 = top level
100-
!istrivial && ccall(:jl_set_global, Cvoid, (Any, Any, Any), Main, :err, stack)
101-
showerror(IOContext(io, :limit => true), er, bt, backtrace = bt!==nothing)
102-
println(io)
103-
end
104101
function display_error(io::IO, stack::ExceptionStack)
105102
printstyled(io, "ERROR: "; bold=true, color=Base.error_color())
106-
stack = ExceptionStack([(exception = x[1], backtrace = scrub_repl_backtrace(x[2])) for x in stack ])
107-
istrivial = length(stack) == 1 && length(stack[1].backtrace) 1 # frame 1 = top level
108-
!istrivial && ccall(:jl_set_global, Cvoid, (Any, Any, Any), Main, :err, stack)
109103
show_exception_stack(IOContext(io, :limit => true), stack)
110104
println(io)
111105
end
112106
display_error(stack::ExceptionStack) = display_error(stderr, stack)
113-
display_error(er, bt=nothing) = display_error(stderr, er, bt)
114107

115108
function eval_user_input(errio, @nospecialize(ast), show_value::Bool)
116109
errcount = 0
@@ -122,6 +115,8 @@ function eval_user_input(errio, @nospecialize(ast), show_value::Bool)
122115
print(color_normal)
123116
end
124117
if lasterr !== nothing
118+
lasterr = scrub_repl_backtrace(lasterr)
119+
istrivialerror(lasterr) || ccall(:jl_set_global, Cvoid, (Any, Any, Any), Main, :err, lasterr)
125120
invokelatest(display_error, errio, lasterr)
126121
errcount = 0
127122
lasterr = nothing
@@ -149,6 +144,7 @@ function eval_user_input(errio, @nospecialize(ast), show_value::Bool)
149144
end
150145
errcount += 1
151146
lasterr = current_exceptions()
147+
ccall(:jl_set_global, Cvoid, (Any, Any, Any), Main, :err, lasterr)
152148
if errcount > 2
153149
@error "It is likely that something important is broken, and Julia will not be able to continue normally" errcount
154150
break

contrib/generate_precompile.jl

+1-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ precompile(Tuple{typeof(Base.recursive_prefs_merge), Base.Dict{String, Any}})
3232
precompile(Tuple{typeof(isassigned), Core.SimpleVector, Int})
3333
precompile(Tuple{typeof(getindex), Core.SimpleVector, Int})
3434
precompile(Tuple{typeof(Base.Experimental.register_error_hint), Any, Type})
35-
precompile(Tuple{typeof(Base.display_error), MethodError, Vector{Union{Ptr{Nothing}, Base.InterpreterIP}}})
36-
precompile(Tuple{typeof(Base.display_error), ErrorException})
37-
precompile(Tuple{typeof(Base.display_error), BoundsError})
35+
precompile(Tuple{typeof(Base.display_error), ExceptionStack})
3836
precompile(Tuple{Core.kwftype(typeof(Type)), NamedTuple{(:sizehint,), Tuple{Int}}, Type{IOBuffer}})
3937
precompile(Base.CoreLogging.current_logger_for_env, (Base.CoreLogging.LogLevel, String, Module))
4038
precompile(Base.CoreLogging.current_logger_for_env, (Base.CoreLogging.LogLevel, Symbol, Module))

stdlib/REPL/src/REPL.jl

+5-1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ function print_response(errio::IO, response, show_value::Bool, have_color::Bool,
284284
try
285285
Base.sigatomic_end()
286286
if iserr
287+
val = Base.scrub_repl_backtrace(val)
288+
Base.istrivialerror(val) || ccall(:jl_set_global, Cvoid, (Any, Any, Any), Main, :err, val)
287289
Base.invokelatest(Base.display_error, errio, val)
288290
else
289291
if val !== nothing && show_value
@@ -305,7 +307,9 @@ function print_response(errio::IO, response, show_value::Bool, have_color::Bool,
305307
println(errio) # an error during printing is likely to leave us mid-line
306308
println(errio, "SYSTEM (REPL): showing an error caused an error")
307309
try
308-
Base.invokelatest(Base.display_error, errio, current_exceptions())
310+
excs = current_exceptions()
311+
ccall(:jl_set_global, Cvoid, (Any, Any, Any), Main, :err, excs)
312+
Base.invokelatest(Base.display_error, errio, excs)
309313
catch e
310314
# at this point, only print the name of the type as a Symbol to
311315
# minimize the possibility of further errors.

stdlib/REPL/test/repl.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -885,13 +885,13 @@ end
885885

886886
# Test containers in error messages are limited #18726
887887
let io = IOBuffer()
888-
Base.display_error(io,
889-
try
888+
Base.display_error(io, Base.ExceptionStack([(exception =
889+
(try
890890
[][trues(6000)]
891891
@assert false
892892
catch e
893893
e
894-
end, [])
894+
end), backtrace = [])]))
895895
@test length(String(take!(io))) < 1500
896896
end
897897

0 commit comments

Comments
 (0)