Skip to content

Commit 72d6b73

Browse files
author
Christopher Doris
committed
julia 1.6 style exception printing
1 parent 5007d54 commit 72d6b73

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

src/PyException.jl

+31-9
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ checknullconvert(::Type{T}, x::Ptr, ambig::Bool = false) where {T} = begin
7575
C.takeresult(T)
7676
end
7777

78+
file_to_pymodule(fname::String) = begin
79+
isfile(fname) || return nothing
80+
modules = PyDict{String}(pyimport("sys").modules)
81+
for (n,m) in modules
82+
if pyhasattr(m, :__file__)
83+
fname2 = pystr(String, m.__file__)
84+
if isfile(fname2) && realpath(fname) == realpath(fname2)
85+
return n
86+
end
87+
end
88+
end
89+
end
90+
7891
function Base.showerror(io::IO, e::PyException)
7992
print(io, "Python: ")
8093

@@ -169,15 +182,24 @@ function Base.showerror(io::IO, e::PyException)
169182
$(fs :: Vector{Tuple{String, String, Int}}) = [(x.name, x.filename, x.lineno) for x in traceback.extract_tb($(e.bref))]
170183
```
171184
for (i, (name, fname, lineno)) in enumerate(reverse(fs))
172-
println(io)
173-
printstyled(io, " [", i, "] ")
174-
printstyled(io, name, bold = true)
175-
printstyled(io, " at ")
176-
# if (m=match(r"^(.*):([0-9]+)$", fname)) !== nothing
177-
# fname = m.captures[1]
178-
# lineno += parse(Int, m.captures[2]) - 1
179-
# end
180-
printstyled(io, fname, ":", lineno, bold = true)
185+
if VERSION < v"1.6.0-rc1"
186+
println(io)
187+
printstyled(io, " [", i, "] ")
188+
printstyled(io, name, bold = true)
189+
printstyled(io, " at ")
190+
printstyled(io, fname, ":", lineno, bold = true)
191+
else
192+
println(io)
193+
printstyled(io, " [", i, "] ")
194+
printstyled(io, name, bold = true)
195+
println(io)
196+
printstyled(io, " @ ", color = :light_black)
197+
mod = file_to_pymodule(fname)
198+
if mod !== nothing
199+
printstyled(io, mod, " ", color = :magenta)
200+
end
201+
printstyled(io, fname, ":", lineno, color = :light_black)
202+
end
181203
end
182204
catch err
183205
print(io, "<error while printing stacktrace: $err>")

0 commit comments

Comments
 (0)