Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Depth-limited printing of types in stacktraces take 2! #49795

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ Standard library changes

#### REPL

* When stack traces are printed, the printed depth of types in function signatures will be limited
to avoid overly verbose output ([#49795]).

#### SuiteSparse

Expand Down
16 changes: 11 additions & 5 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,9 @@ 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 @@ -781,12 +784,15 @@ function show_backtrace(io::IO, t::Vector)
if length(filtered) > BIG_STACKTRACE_SIZE
show_reduced_backtrace(IOContext(io, :backtrace => true), filtered)
return
else
try invokelatest(update_stackframes_callback[], filtered) catch end
# process_backtrace returns a Vector{Tuple{Frame, Int}}
show_full_backtrace(io, filtered; print_linebreaks = stacktrace_linebreaks())
end

try invokelatest(update_stackframes_callback[], filtered) catch end
# process_backtrace returns a Vector{Tuple{Frame, Int}}
show_full_backtrace(io, filtered; print_linebreaks = stacktrace_linebreaks())
return
if limitflag[]
print(io, "\nSome type information was truncated. Use `show(err)` to see complete types.")
end
nothing
end


Expand Down
111 changes: 109 additions & 2 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2470,15 +2470,16 @@ function print_within_stacktrace(io, s...; color=:normal, bold=false)
end
end

function show_tuple_as_call(io::IO, name::Symbol, sig::Type;
function show_tuple_as_call(out::IO, name::Symbol, sig::Type;
demangle=false, kwargs=nothing, argnames=nothing,
qualified=false, hasfirst=true)
# print a method signature tuple for a lambda definition
if sig === Tuple
print(io, demangle ? demangle_function_name(name) : name, "(...)")
print(out, demangle ? demangle_function_name(name) : name, "(...)")
return
end
tv = Any[]
io = IOContext(IOBuffer(), out)
env_io = io
while isa(sig, UnionAll)
push!(tv, sig.var)
Expand Down Expand Up @@ -2516,9 +2517,115 @@ function show_tuple_as_call(io::IO, name::Symbol, sig::Type;
end
print_within_stacktrace(io, ")", bold=true)
show_method_params(io, tv)
str = String(take!(unwrapcontext(io)[1]))
if get(out, :limit, false)::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
end
str = str_lim
end
print(out, str)
nothing
end

# limit nesting depth of `{ }` until string textwidth is less than `n`
function type_depth_limit(str::String, n::Int; maxdepth = nothing)
depth = 0
width_at = Int[] # total textwidth at each nesting depth
depths = zeros(Int16, lastindex(str)) # depth at each character index
levelcount = Int[] # number of nodes at each level
strwid = 0
st_0, st_backslash, st_squote, st_dquote = 0,1,2,4
state::Int = st_0
stateis(s) = (state & s) != 0
quoted() = stateis(st_squote) || stateis(st_dquote)
enter(s) = (state |= s)
leave(s) = (state &= ~s)
for (i, c) in ANSIIterator(str)
if c isa ANSIDelimiter
depths[i] = depth
continue
end

if c == '\\' && quoted()
enter(st_backslash)
elseif c == '\''
if stateis(st_backslash) || stateis(st_dquote)
elseif stateis(st_squote)
leave(st_squote)
else
enter(st_squote)
end
elseif c == '"'
if stateis(st_backslash) || stateis(st_squote)
elseif stateis(st_dquote)
leave(st_dquote)
else
enter(st_dquote)
end
end
if c == '}' && !quoted()
depth -= 1
end

wid = textwidth(c)
strwid += wid
if depth > 0
width_at[depth] += wid
end
depths[i] = depth

if c == '{' && !quoted()
depth += 1
if depth > length(width_at)
push!(width_at, 0)
push!(levelcount, 0)
end
levelcount[depth] += 1
end
if c != '\\' && stateis(st_backslash)
leave(st_backslash)
end
end
if maxdepth === nothing
limit_at = length(width_at) + 1
while strwid > n
limit_at -= 1
limit_at <= 1 && break
# add levelcount[] to include space taken by `…`
strwid = strwid - width_at[limit_at] + levelcount[limit_at]
if limit_at < length(width_at)
# take away the `…` from the previous considered level
strwid -= levelcount[limit_at+1]
end
end
else
limit_at = maxdepth
end
output = IOBuffer()
prev = 0
for (i, c) in ANSIIterator(str)
di = depths[i]
if di < limit_at
if c isa ANSIDelimiter
write(output, c.del)
else
write(output, c)
end
end
if di > prev && di == limit_at
write(output, "…")
end
prev = di
end
return String(take!(output))
end

function print_type_bicolor(io, type; kwargs...)
str = sprint(show, type, context=io)
print_type_bicolor(io, str; kwargs...)
Expand Down
5 changes: 5 additions & 0 deletions base/stacktraces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ function show_spec_linfo(io::IO, frame::StackFrame)
linfo, linfo.sig
end
if def isa Method
if get(io, :limit, :false)::Bool
if !haskey(io, :displaysize)
io = IOContext(io, :displaysize => displaysize(io))
end
end
argnames = Base.method_argnames(def)
argnames = replace(argnames, :var"#unused#" => :var"")
if def.nkw > 0
Expand Down
54 changes: 54 additions & 0 deletions test/stacktraces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,57 @@ let bt
end
@test any(s->startswith(string(s), "f33065(x::Float32, y::Float32; b::Float64, a::String, c::"), bt)
end

struct F49231{a,b,c,d,e,f,g} end
(::F49231)(a,b,c) = error("oops")

@testset "type_depth_limit" begin
tdl = Base.type_depth_limit

str = repr(typeof(view([1, 2, 3], 1:2)))
@test tdl(str, 0, maxdepth = 1) == "SubArray{…}"
@test tdl(str, 0, maxdepth = 2) == "SubArray{$Int, 1, Vector{…}, Tuple{…}, true}"
@test tdl(str, 0, maxdepth = 3) == "SubArray{$Int, 1, Vector{$Int}, Tuple{UnitRange{…}}, true}"
@test tdl(str, 0, maxdepth = 4) == "SubArray{$Int, 1, Vector{$Int}, Tuple{UnitRange{$Int}}, true}"
@test tdl(str, 3) == "SubArray{…}"
@test tdl(str, 44) == "SubArray{…}"
@test tdl(str, 45) == "SubArray{$Int, 1, Vector{…}, Tuple{…}, true}"
@test tdl(str, 59) == "SubArray{$Int, 1, Vector{…}, Tuple{…}, true}"
@test tdl(str, 60) == "SubArray{$Int, 1, Vector{$Int}, Tuple{UnitRange{…}}, true}"
@test tdl(str, 100) == "SubArray{$Int, 1, Vector{$Int}, Tuple{UnitRange{$Int}}, true}"

str = repr(Vector{V} where V<:AbstractVector{T} where T<:Real)
@test tdl(str, 0, maxdepth = 1) == "Vector{…} where {…}"
@test tdl(str, 0, maxdepth = 2) == "Vector{V} where {T<:Real, V<:AbstractVector{…}}"
@test tdl(str, 0, maxdepth = 3) == "Vector{V} where {T<:Real, V<:AbstractVector{T}}"
@test tdl(str, 20) == "Vector{…} where {…}"
@test tdl(str, 46) == "Vector{…} where {…}"
@test tdl(str, 47) == "Vector{V} where {T<:Real, V<:AbstractVector{T}}"

str = "F49231{Vector,Val{('}','}')},Vector{Vector{Vector{Vector}}},Tuple{Int,Int,Int,Int,Int,Int,Int},Int,Int,Int}"
@test tdl(str, 105) == "F49231{Vector,Val{('}','}')},Vector{Vector{Vector{…}}},Tuple{Int,Int,Int,Int,Int,Int,Int},Int,Int,Int}"
@test tdl(str, 85) == "F49231{Vector,Val{…},Vector{…},Tuple{…},Int,Int,Int}"

# Stacktrace
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]
st = try
sqrteach(d)
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.")
@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)))
@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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you need a test:

julia> Val{:var"\\\""}
Val{Symbol("\\\"")}