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

Prettyerrors #844

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/exception.jl
Expand Up @@ -28,11 +28,35 @@ struct PyError <: Exception
# indicator.
end

function pystr_nofail(o::PyObject)
if ispynull(o)
return "NULL"
else
s = ccall((@pysym :PyObject_Str), PyPtr, (PyPtr,), o)
if (s == C_NULL)
pyerr_clear()
return string(PyPtr(o))
end
return convert(AbstractString, PyObject(s))
end
end

function Base.showerror(io::IO, e::PyError)
println(io, "PyError:\n", pystr_nofail(e.val))
Copy link
Contributor Author

@jw3126 jw3126 Oct 2, 2020

Choose a reason for hiding this comment

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

This line is crucial. e.val is the python exception object. And the question is how should we print it. Master does it calling repr. From looking at some tutorials, it seems that calling str is preferred. But I am by no means sure if it is the best analog of showerror in python. Or if it even depends on the python version, what's the preferred thing.

if !ispynull(e.traceback)
o = pycall(format_traceback, PyObject, e.traceback)
if !ispynull(o)
for s in PyVector{AbstractString}(o)
print(io, s)
end
end
end
end

function show(io::IO, e::PyError)
print(io, "PyError",
isempty(e.msg) ? e.msg : string(" (",e.msg,")"),
" ")

if ispynull(e.T)
println(io, "None")
else
Expand Down