From d4a779f06301a82589d3722e41e851ec17eb5ff0 Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Fri, 2 Oct 2020 16:39:34 +0200 Subject: [PATCH 1/2] improve error printing --- src/exception.jl | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/exception.jl b/src/exception.jl index 7df89269..a2fad462 100644 --- a/src/exception.jl +++ b/src/exception.jl @@ -28,15 +28,27 @@ 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 show(io::IO, e::PyError) print(io, "PyError", isempty(e.msg) ? e.msg : string(" (",e.msg,")"), " ") - if ispynull(e.T) println(io, "None") else - println(io, pystring(e.T), "\n", pystring(e.val)) + println(io, pystring(e.T), "\n", pystr_nofail(e.val)) end if !ispynull(e.traceback) From 19520619eb20a0795c35abc090d3c08bea5766de Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Fri, 2 Oct 2020 16:44:50 +0200 Subject: [PATCH 2/2] less verbose error --- src/exception.jl | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/exception.jl b/src/exception.jl index a2fad462..3b8cef08 100644 --- a/src/exception.jl +++ b/src/exception.jl @@ -41,6 +41,18 @@ function pystr_nofail(o::PyObject) end end +function Base.showerror(io::IO, e::PyError) + println(io, "PyError:\n", pystr_nofail(e.val)) + 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,")"), @@ -48,7 +60,7 @@ function show(io::IO, e::PyError) if ispynull(e.T) println(io, "None") else - println(io, pystring(e.T), "\n", pystr_nofail(e.val)) + println(io, pystring(e.T), "\n", pystring(e.val)) end if !ispynull(e.traceback)