Skip to content

Commit 7f63664

Browse files
author
Christopher Doris
committed
better v1.6 style exception printing
1 parent 72d6b73 commit 7f63664

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/PyException.jl

+16-4
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,34 @@ function Base.showerror(io::IO, e::PyException)
181181
import traceback
182182
$(fs :: Vector{Tuple{String, String, Int}}) = [(x.name, x.filename, x.lineno) for x in traceback.extract_tb($(e.bref))]
183183
```
184-
for (i, (name, fname, lineno)) in enumerate(reverse(fs))
185-
if VERSION < v"1.6.0-rc1"
184+
if VERSION < v"1.6.0-rc1"
185+
for (i, (name, fname, lineno)) in enumerate(reverse(fs))
186186
println(io)
187187
printstyled(io, " [", i, "] ")
188188
printstyled(io, name, bold = true)
189189
printstyled(io, " at ")
190190
printstyled(io, fname, ":", lineno, bold = true)
191-
else
191+
end
192+
else
193+
mcdict = Dict{String, Symbol}()
194+
mccyclyer = Iterators.Stateful(Iterators.cycle(Base.STACKTRACE_MODULECOLORS))
195+
for (i, (name, fname, lineno)) in enumerate(reverse(fs))
192196
println(io)
193197
printstyled(io, " [", i, "] ")
194198
printstyled(io, name, bold = true)
195199
println(io)
196200
printstyled(io, " @ ", color = :light_black)
197201
mod = file_to_pymodule(fname)
198202
if mod !== nothing
199-
printstyled(io, mod, " ", color = :magenta)
203+
# print the module, with colour determined by the top level name
204+
tmod = first(split(mod, ".", limit=2))
205+
color = get!(mcdict, tmod) do
206+
popfirst!(mccyclyer)
207+
end
208+
printstyled(io, mod, " ", color = color)
209+
end
210+
if isfile(fname) && Base.stacktrace_contract_userdir()
211+
fname = Base.replaceuserpath(fname)
200212
end
201213
printstyled(io, fname, ":", lineno, color = :light_black)
202214
end

0 commit comments

Comments
 (0)