|
| 1 | +mutable struct PyException <: Exception |
| 2 | + tptr :: CPyPtr |
| 3 | + vptr :: CPyPtr |
| 4 | + bptr :: CPyPtr |
| 5 | + function PyException(::Val{:new}, t::Ptr=C_NULL, v::Ptr=C_NULL, b::Ptr=C_NULL, borrowed::Bool=false) |
| 6 | + o = new(CPyPtr(t), CPyPtr(v), CPyPtr(b)) |
| 7 | + if borrowed |
| 8 | + C.Py_IncRef(t) |
| 9 | + C.Py_IncRef(v) |
| 10 | + C.Py_IncRef(b) |
| 11 | + end |
| 12 | + finalizer(o) do o |
| 13 | + if CONFIG.isinitialized |
| 14 | + tptr = getfield(o, :tptr) |
| 15 | + vptr = getfield(o, :vptr) |
| 16 | + bptr = getfield(o, :bptr) |
| 17 | + if !isnull(tptr) || !isnull(vptr) || !isnull(bptr) |
| 18 | + with_gil(false) do |
| 19 | + C.Py_DecRef(tptr) |
| 20 | + C.Py_DecRef(vptr) |
| 21 | + C.Py_DecRef(bptr) |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | + end |
| 26 | + return o |
| 27 | + end |
| 28 | +end |
| 29 | +export PyException |
| 30 | + |
| 31 | +pythrow() = throw(PyException(Val(:new), C.PyErr_FetchTuple()...)) |
| 32 | + |
| 33 | +function Base.showerror(io::IO, e::PyException) |
| 34 | + if isnull(e.tptr) |
| 35 | + print(io, "Python: mysterious error (no error was actually set)") |
| 36 | + return |
| 37 | + end |
| 38 | + |
| 39 | + if CONFIG.sysautolasttraceback |
| 40 | + err = C.Py_DecRef(C.PyImport_ImportModule("sys"), PYERR()) do sys |
| 41 | + err = C.PyObject_SetAttrString(sys, "last_type", isnull(e.tptr) ? C.Py_None() : e.tptr) |
| 42 | + ism1(err) && return PYERR() |
| 43 | + err = C.PyObject_SetAttrString(sys, "last_value", isnull(e.vptr) ? C.Py_None() : e.vptr) |
| 44 | + ism1(err) && return PYERR() |
| 45 | + err = C.PyObject_SetAttrString(sys, "last_traceback", isnull(e.bptr) ? C.Py_None() : e.bptr) |
| 46 | + ism1(err) && return PYERR() |
| 47 | + nothing |
| 48 | + end |
| 49 | + if err == PYERR() |
| 50 | + C.PyErr_Clear() |
| 51 | + print(io, "<error while setting 'sys.last_traceback'>") |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + # # if this is a Julia exception then recursively print it and its stacktrace |
| 56 | + # if pyerrmatches(e.t, pyjlexception) |
| 57 | + # try |
| 58 | + # jp = pyjlgetvalue(e.v.args[0]) |
| 59 | + # if jp !== nothing |
| 60 | + # je, jb = jp |
| 61 | + # print(io, "Python: Julia: ") |
| 62 | + # showerror(io, je) |
| 63 | + # if jb === nothing |
| 64 | + # println(io) |
| 65 | + # print(io, "Stacktrace: none") |
| 66 | + # else |
| 67 | + # io2 = IOBuffer() |
| 68 | + # Base.show_backtrace(IOContext(io2, :color=>true, :displaysize=>displaysize(io)), jb) |
| 69 | + # printstyled(io, String(take!(io2))) |
| 70 | + # end |
| 71 | + # end |
| 72 | + # if pyisnone(e.b) |
| 73 | + # println(io) |
| 74 | + # printstyled(io, "Python stacktrace: none") |
| 75 | + # return |
| 76 | + # else |
| 77 | + # @goto pystacktrace |
| 78 | + # end |
| 79 | + # catch err |
| 80 | + # println(io, "<error while printing Julia excpetion inside Python exception: $(err)>") |
| 81 | + # end |
| 82 | + # end |
| 83 | + |
| 84 | + # otherwise, print the Python exception ****** TODO ****** |
| 85 | + print(io, "Python: ") |
| 86 | + |
| 87 | + # print the type name |
| 88 | + tname = C.Py_DecRef(C.PyObject_GetAttrString(e.tptr, "__name__")) do tnameo |
| 89 | + C.PyUnicode_As(tnameo, String) |
| 90 | + end |
| 91 | + if tname === PYERR() |
| 92 | + C.PyErr_Clear() |
| 93 | + print(io, "<error while printing type>") |
| 94 | + else |
| 95 | + print(io, tname) |
| 96 | + end |
| 97 | + |
| 98 | + # print the error message |
| 99 | + if !isnull(e.vptr) |
| 100 | + print(io, ": ") |
| 101 | + vstr = C.PyObject_StrAs(e.vptr, String) |
| 102 | + if vstr === PYERR() |
| 103 | + C.PyErr_Clear() |
| 104 | + print(io, "<error while printing value>") |
| 105 | + else |
| 106 | + print(io, vstr) |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + # print the stacktrace |
| 111 | + if !isnull(e.bptr) |
| 112 | + @label pystacktrace |
| 113 | + println(io) |
| 114 | + printstyled(io, "Python stacktrace:") |
| 115 | + err = C.Py_DecRef(C.PyImport_ImportModule("traceback")) do tb |
| 116 | + C.Py_DecRef(C.PyObject_GetAttrString(tb, "extract_tb")) do extr |
| 117 | + C.Py_DecRef(C.PyObject_CallNice(extr, C.PyObjectRef(e.bptr))) do fs |
| 118 | + nfs = C.PySequence_Length(fs) |
| 119 | + ism1(nfs) && return PYERR() |
| 120 | + for i in 1:nfs |
| 121 | + println(io) |
| 122 | + printstyled(io, " [", i, "] ") |
| 123 | + # name |
| 124 | + err = C.Py_DecRef(C.PySequence_GetItem(fs, i-1)) do f |
| 125 | + name = C.Py_DecRef(C.PyObject_GetAttrString(f, "name")) do nameo |
| 126 | + C.PyObject_StrAs(nameo, String) |
| 127 | + end |
| 128 | + name === PYERR() && return PYERR() |
| 129 | + printstyled(io, name, bold=true) |
| 130 | + printstyled(io, " at ") |
| 131 | + fname = C.Py_DecRef(C.PyObject_GetAttrString(f, "filename")) do fnameo |
| 132 | + C.PyObject_StrAs(fnameo, String) |
| 133 | + end |
| 134 | + fname === PYERR() && return PYERR() |
| 135 | + printstyled(io, fname, ":", bold=true) |
| 136 | + lineno = C.Py_DecRef(C.PyObject_GetAttrString(f, "lineno")) do linenoo |
| 137 | + C.PyObject_StrAs(linenoo, String) |
| 138 | + end |
| 139 | + lineno === PYERR() && return PYERR() |
| 140 | + printstyled(io, lineno, bold=true) |
| 141 | + nothing |
| 142 | + end |
| 143 | + err === PYERR() && return PYERR() |
| 144 | + end |
| 145 | + end |
| 146 | + end |
| 147 | + end |
| 148 | + if err === PYERR() |
| 149 | + C.PyErr_Clear() |
| 150 | + print(io, "<error while printing stacktrace>") |
| 151 | + end |
| 152 | + end |
| 153 | +end |
0 commit comments