Skip to content

Commit

Permalink
Fix inconsistency of module numbers (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seelengrab committed Jun 23, 2022
1 parent 95be1a4 commit 2aa6fb5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
18 changes: 5 additions & 13 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,6 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()
end
end

modulecolordict = STACKTRACE_FIXEDCOLORS
modulecolorcycler = Iterators.Stateful(Iterators.cycle(STACKTRACE_MODULECOLORS))

for (func, arg_types_param) in funcs
for method in methods(func)
buf = IOBuffer()
Expand Down Expand Up @@ -546,8 +543,9 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()
println(iob)

m = parentmodule_before_main(method.module)
color = get!(() -> popfirst!(modulecolorcycler), modulecolordict, m)
color = get!(() -> popfirst!(STACKTRACE_MODULECOLORS), STACKTRACE_FIXEDCOLORS, m)
print_module_path_file(iob, m, string(file), line, color, 1)

# TODO: indicate if it's in the wrong world
push!(lines, (buf, right_matches))
end
Expand Down Expand Up @@ -584,20 +582,17 @@ end
# replace `sf` as needed.
const update_stackframes_callback = Ref{Function}(identity)

const STACKTRACE_MODULECOLORS = [:magenta, :cyan, :green, :yellow]
const STACKTRACE_MODULECOLORS = Iterators.Stateful(Iterators.cycle([:magenta, :cyan, :green, :yellow]))
const STACKTRACE_FIXEDCOLORS = IdDict(Base => :light_black, Core => :light_black)

function show_full_backtrace(io::IO, trace::Vector; print_linebreaks::Bool)
num_frames = length(trace)
ndigits_max = ndigits(num_frames)

modulecolordict = STACKTRACE_FIXEDCOLORS
modulecolorcycler = Iterators.Stateful(Iterators.cycle(STACKTRACE_MODULECOLORS))

println(io, "\nStacktrace:")

for (i, (frame, n)) in enumerate(trace)
print_stackframe(io, i, frame, n, ndigits_max, modulecolordict, modulecolorcycler)
print_stackframe(io, i, frame, n, ndigits_max, STACKTRACE_FIXEDCOLORS, STACKTRACE_MODULECOLORS)
if i < num_frames
println(io)
print_linebreaks && println(io)
Expand Down Expand Up @@ -657,15 +652,12 @@ function show_reduced_backtrace(io::IO, t::Vector)

ndigits_max = ndigits(length(t))

modulecolordict = Dict{Module, Symbol}()
modulecolorcycler = Iterators.Stateful(Iterators.cycle(STACKTRACE_MODULECOLORS))

push!(repeated_cycle, (0,0,0)) # repeated_cycle is never empty
frame_counter = 1
for i in 1:length(displayed_stackframes)
(frame, n) = displayed_stackframes[i]

print_stackframe(io, frame_counter, frame, n, ndigits_max, modulecolordict, modulecolorcycler)
print_stackframe(io, frame_counter, frame, n, ndigits_max, STACKTRACE_FIXEDCOLORS, STACKTRACE_MODULECOLORS)

if i < length(displayed_stackframes)
println(io)
Expand Down
7 changes: 2 additions & 5 deletions base/methodshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,7 @@ function show_method_list_header(io::IO, ms::MethodList, namefmt::Function)
"generic function")
print(io, " for ", what, " ", namedisplay, " from ")

modulecolorcycler = Iterators.Stateful(Iterators.cycle(STACKTRACE_MODULECOLORS))
col = get!(() -> popfirst!(modulecolorcycler), STACKTRACE_FIXEDCOLORS, parentmodule_before_main(ms.mt.module))
col = get!(() -> popfirst!(STACKTRACE_MODULECOLORS), STACKTRACE_FIXEDCOLORS, parentmodule_before_main(ms.mt.module))

printstyled(io, ms.mt.module, color=col)
elseif '#' in sname
Expand Down Expand Up @@ -299,8 +298,6 @@ function show_method_table(io::IO, ms::MethodList, max::Int=-1, header::Bool=tru
mt.module
end

modulecolordict = STACKTRACE_FIXEDCOLORS
modulecolorcycler = Iterators.Stateful(Iterators.cycle(STACKTRACE_MODULECOLORS))
digit_align_width = length(string(max > 0 ? max : length(ms)))

for meth in ms
Expand All @@ -314,7 +311,7 @@ function show_method_table(io::IO, ms::MethodList, max::Int=-1, header::Bool=tru
nothing
else
m = parentmodule_before_main(meth.module)
get!(() -> popfirst!(modulecolorcycler), modulecolordict, m)
get!(() -> popfirst!(STACKTRACE_MODULECOLORS), STACKTRACE_FIXEDCOLORS, m)
end
show(io, meth; modulecolor)

Expand Down
3 changes: 2 additions & 1 deletion test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ Base.show_method_candidates(buf, Base.MethodError(method_c1,(1, 1, "")))
@test length(methods(method_c1)) <= 3 # because of '...' in candidate printing
Base.show_method_candidates(IOContext(buf, :color => true), Base.MethodError(method_c1,(1, 1, "")))

@test occursin("\n\n\e[0mClosest candidates are:\n\e[0m method_c1(\e[91m::Float64\e[39m, \e[91m::AbstractString...\e[39m)\n\e[0m\e[90m @\e[39m \e[35m$modul\e[39m \e[90m$dname$sep\e[39m\e[90m\e[4m$fname:$c1line\e[24m\e[39m\n", String(take!(buf)))
mod_col = Base.text_colors[Base.STACKTRACE_FIXEDCOLORS[modul]]
@test occursin("\n\n\e[0mClosest candidates are:\n\e[0m method_c1(\e[91m::Float64\e[39m, \e[91m::AbstractString...\e[39m)\n\e[0m\e[90m @\e[39m $mod_col$modul\e[39m \e[90m$dname$sep\e[39m\e[90m\e[4m$fname:$c1line\e[24m\e[39m\n", String(take!(buf)))
Base.show_method_candidates(buf, Base.MethodError(method_c1,(1, "", "")))
@test occursin("\n\nClosest candidates are:\n method_c1(!Matched::Float64, ::AbstractString...)$cmod$cfile$c1line\n", String(take!(buf)))

Expand Down

0 comments on commit 2aa6fb5

Please sign in to comment.