Skip to content

Commit 9822257

Browse files
only limit types in stack traces in the REPL (#50598)
fixes #50575 Co-authored-by: Shuhei Kadowaki <[email protected]>
1 parent 7b587ac commit 9822257

File tree

5 files changed

+31
-16
lines changed

5 files changed

+31
-16
lines changed

base/errorshow.jl

-6
Original file line numberDiff line numberDiff line change
@@ -772,9 +772,6 @@ function show_backtrace(io::IO, t::Vector)
772772
if haskey(io, :last_shown_line_infos)
773773
empty!(io[:last_shown_line_infos])
774774
end
775-
# this will be set to true if types in the stacktrace are truncated
776-
limitflag = Ref(false)
777-
io = IOContext(io, :stacktrace_types_limited => limitflag)
778775

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

base/show.jl

+3-5
Original file line numberDiff line numberDiff line change
@@ -2559,14 +2559,12 @@ function show_tuple_as_call(out::IO, name::Symbol, sig::Type;
25592559
print_within_stacktrace(io, ")", bold=true)
25602560
show_method_params(io, tv)
25612561
str = String(take!(unwrapcontext(io)[1]))
2562-
if get(out, :limit, false)::Bool
2562+
typelimitflag = get(out, :stacktrace_types_limited, nothing)
2563+
if typelimitflag isa RefValue{Bool}
25632564
sz = get(out, :displaysize, (typemax(Int), typemax(Int)))::Tuple{Int, Int}
25642565
str_lim = type_depth_limit(str, max(sz[2], 120))
25652566
if sizeof(str_lim) < sizeof(str)
2566-
typelimitflag = get(out, :stacktrace_types_limited, nothing)
2567-
if typelimitflag !== nothing
2568-
typelimitflag[] = true
2569-
end
2567+
typelimitflag[] = true
25702568
end
25712569
str = str_lim
25722570
end

stdlib/REPL/src/REPL.jl

+15-2
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,19 @@ function print_response(repl::AbstractREPL, response, show_value::Bool, have_col
283283
end
284284
return nothing
285285
end
286+
287+
function repl_display_error(errio::IO, @nospecialize errval)
288+
# this will be set to true if types in the stacktrace are truncated
289+
limitflag = Ref(false)
290+
errio = IOContext(errio, :stacktrace_types_limited => limitflag)
291+
Base.invokelatest(Base.display_error, errio, errval)
292+
if limitflag[]
293+
print(errio, "Some type information was truncated. Use `show(err)` to see complete types.")
294+
println(errio)
295+
end
296+
return nothing
297+
end
298+
286299
function print_response(errio::IO, response, show_value::Bool, have_color::Bool, specialdisplay::Union{AbstractDisplay,Nothing}=nothing)
287300
Base.sigatomic_begin()
288301
val, iserr = response
@@ -292,7 +305,7 @@ function print_response(errio::IO, response, show_value::Bool, have_color::Bool,
292305
if iserr
293306
val = Base.scrub_repl_backtrace(val)
294307
Base.istrivialerror(val) || setglobal!(Base.MainInclude, :err, val)
295-
Base.invokelatest(Base.display_error, errio, val)
308+
repl_display_error(errio, val)
296309
else
297310
if val !== nothing && show_value
298311
try
@@ -315,7 +328,7 @@ function print_response(errio::IO, response, show_value::Bool, have_color::Bool,
315328
try
316329
excs = Base.scrub_repl_backtrace(current_exceptions())
317330
setglobal!(Base.MainInclude, :err, excs)
318-
Base.invokelatest(Base.display_error, errio, excs)
331+
repl_display_error(errio, excs)
319332
catch e
320333
# at this point, only print the name of the type as a Symbol to
321334
# minimize the possibility of further errors.

stdlib/REPL/test/repl.jl

+11
Original file line numberDiff line numberDiff line change
@@ -1659,3 +1659,14 @@ fake_repl() do stdin_write, stdout_read, repl
16591659
write(stdin_write, '\x04')
16601660
Base.wait(repltask)
16611661
end
1662+
1663+
fake_repl() do stdin_write, stdout_read, repl
1664+
backend = REPL.REPLBackend()
1665+
repltask = @async REPL.run_repl(repl; backend)
1666+
write(stdin_write,
1667+
"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")
1668+
txt = readuntil(stdout_read, "ZZZZZ")
1669+
write(stdin_write, '\x04')
1670+
wait(repltask)
1671+
@test contains(txt, "Some type information was truncated. Use `show(err)` to see complete types.")
1672+
end

test/stacktraces.jl

+2-3
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,14 @@ struct F49231{a,b,c,d,e,f,g} end
241241
catch e
242242
stacktrace(catch_backtrace())
243243
end
244-
str = sprint(Base.show_backtrace, st, context = (:limit=>true, :color=>true, :displaysize=>(50,105)))
245-
@test endswith(str, "to see complete types.")
244+
str = sprint(Base.show_backtrace, st, context = (:limit=>true, :stacktrace_types_limited => Ref(false), :color=>true, :displaysize=>(50,105)))
246245
@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")
247246

248247
st = try
249248
F49231{Vector,Val{'}'},Vector{Vector{Vector{Vector}}},Tuple{Int,Int,Int,Int,Int,Int,Int},Int,Int,Int}()(1,2,3)
250249
catch e
251250
stacktrace(catch_backtrace())
252251
end
253-
str = sprint(Base.show_backtrace, st, context = (:limit=>true, :color=>true, :displaysize=>(50,132)))
252+
str = sprint(Base.show_backtrace, st, context = (:limit=>true, :stacktrace_types_limited => Ref(false), :color=>true, :displaysize=>(50,132)))
254253
@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")
255254
end

0 commit comments

Comments
 (0)