Skip to content

Commit

Permalink
only limit types in stack traces in the REPL (#50598)
Browse files Browse the repository at this point in the history
fixes #50575

Co-authored-by: Shuhei Kadowaki <[email protected]>
  • Loading branch information
JeffBezanson and aviatesk authored Aug 1, 2023
1 parent 7b587ac commit 9822257
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
6 changes: 0 additions & 6 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -772,9 +772,6 @@ function show_backtrace(io::IO, t::Vector)
if haskey(io, :last_shown_line_infos)
empty!(io[:last_shown_line_infos])
end
# this will be set to true if types in the stacktrace are truncated
limitflag = Ref(false)
io = IOContext(io, :stacktrace_types_limited => limitflag)

# t is a pre-processed backtrace (ref #12856)
if t isa Vector{Any}
Expand All @@ -800,9 +797,6 @@ function show_backtrace(io::IO, t::Vector)
# process_backtrace returns a Vector{Tuple{Frame, Int}}
show_full_backtrace(io, filtered; print_linebreaks = stacktrace_linebreaks())
end
if limitflag[]
print(io, "\nSome type information was truncated. Use `show(err)` to see complete types.")
end
nothing
end

Expand Down
8 changes: 3 additions & 5 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2559,14 +2559,12 @@ function show_tuple_as_call(out::IO, name::Symbol, sig::Type;
print_within_stacktrace(io, ")", bold=true)
show_method_params(io, tv)
str = String(take!(unwrapcontext(io)[1]))
if get(out, :limit, false)::Bool
typelimitflag = get(out, :stacktrace_types_limited, nothing)
if typelimitflag isa RefValue{Bool}
sz = get(out, :displaysize, (typemax(Int), typemax(Int)))::Tuple{Int, Int}
str_lim = type_depth_limit(str, max(sz[2], 120))
if sizeof(str_lim) < sizeof(str)
typelimitflag = get(out, :stacktrace_types_limited, nothing)
if typelimitflag !== nothing
typelimitflag[] = true
end
typelimitflag[] = true
end
str = str_lim
end
Expand Down
17 changes: 15 additions & 2 deletions stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,19 @@ function print_response(repl::AbstractREPL, response, show_value::Bool, have_col
end
return nothing
end

function repl_display_error(errio::IO, @nospecialize errval)
# this will be set to true if types in the stacktrace are truncated
limitflag = Ref(false)
errio = IOContext(errio, :stacktrace_types_limited => limitflag)
Base.invokelatest(Base.display_error, errio, errval)
if limitflag[]
print(errio, "Some type information was truncated. Use `show(err)` to see complete types.")
println(errio)
end
return nothing
end

function print_response(errio::IO, response, show_value::Bool, have_color::Bool, specialdisplay::Union{AbstractDisplay,Nothing}=nothing)
Base.sigatomic_begin()
val, iserr = response
Expand All @@ -292,7 +305,7 @@ function print_response(errio::IO, response, show_value::Bool, have_color::Bool,
if iserr
val = Base.scrub_repl_backtrace(val)
Base.istrivialerror(val) || setglobal!(Base.MainInclude, :err, val)
Base.invokelatest(Base.display_error, errio, val)
repl_display_error(errio, val)
else
if val !== nothing && show_value
try
Expand All @@ -315,7 +328,7 @@ function print_response(errio::IO, response, show_value::Bool, have_color::Bool,
try
excs = Base.scrub_repl_backtrace(current_exceptions())
setglobal!(Base.MainInclude, :err, excs)
Base.invokelatest(Base.display_error, errio, excs)
repl_display_error(errio, excs)
catch e
# at this point, only print the name of the type as a Symbol to
# minimize the possibility of further errors.
Expand Down
11 changes: 11 additions & 0 deletions stdlib/REPL/test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1659,3 +1659,14 @@ fake_repl() do stdin_write, stdout_read, repl
write(stdin_write, '\x04')
Base.wait(repltask)
end

fake_repl() do stdin_write, stdout_read, repl
backend = REPL.REPLBackend()
repltask = @async REPL.run_repl(repl; backend)
write(stdin_write,
"a = UInt8(81):UInt8(160); b = view(a, 1:64); c = reshape(b, (8, 8)); d = reinterpret(reshape, Float64, c); sqrteach(a) = [sqrt(x) for x in a]; sqrteach(d)\n\"ZZZZZ\"\n")
txt = readuntil(stdout_read, "ZZZZZ")
write(stdin_write, '\x04')
wait(repltask)
@test contains(txt, "Some type information was truncated. Use `show(err)` to see complete types.")
end
5 changes: 2 additions & 3 deletions test/stacktraces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,14 @@ struct F49231{a,b,c,d,e,f,g} end
catch e
stacktrace(catch_backtrace())
end
str = sprint(Base.show_backtrace, st, context = (:limit=>true, :color=>true, :displaysize=>(50,105)))
@test endswith(str, "to see complete types.")
str = sprint(Base.show_backtrace, st, context = (:limit=>true, :stacktrace_types_limited => Ref(false), :color=>true, :displaysize=>(50,105)))
@test contains(str, "[5] \e[0m\e[1mcollect_to!\e[22m\e[0m\e[1m(\e[22m\e[90mdest\e[39m::\e[0mVector\e[90m{…}\e[39m, \e[90mitr\e[39m::\e[0mBase.Generator\e[90m{…}\e[39m, \e[90moffs\e[39m::\e[0m$Int, \e[90mst\e[39m::\e[0mTuple\e[90m{…}\e[39m\e[0m\e[1m)\e[22m\n\e[90m")

st = try
F49231{Vector,Val{'}'},Vector{Vector{Vector{Vector}}},Tuple{Int,Int,Int,Int,Int,Int,Int},Int,Int,Int}()(1,2,3)
catch e
stacktrace(catch_backtrace())
end
str = sprint(Base.show_backtrace, st, context = (:limit=>true, :color=>true, :displaysize=>(50,132)))
str = sprint(Base.show_backtrace, st, context = (:limit=>true, :stacktrace_types_limited => Ref(false), :color=>true, :displaysize=>(50,132)))
@test contains(str, "[2] \e[0m\e[1m(::$F49231{Vector, Val{…}, Vector{…}, NTuple{…}, $Int, $Int, $Int})\e[22m\e[0m\e[1m(\e[22m\e[90ma\e[39m::\e[0m$Int, \e[90mb\e[39m::\e[0m$Int, \e[90mc\e[39m::\e[0m$Int\e[0m\e[1m)\e[22m\n\e[90m")
end

0 comments on commit 9822257

Please sign in to comment.