@@ -49,6 +49,19 @@ function show(io::IO, e::PyError)
4949 end
5050end
5151
52+ # like pyerror(msg) but type-stable: always returns PyError
53+ function PyError (msg:: AbstractString )
54+ ptype, pvalue, ptraceback = Ref {PyPtr} (), Ref {PyPtr} (), Ref {PyPtr} ()
55+ # equivalent of passing C pointers &exc[1], &exc[2], &exc[3]:
56+ ccall ((@pysym :PyErr_Fetch ), Cvoid, (Ref{PyPtr},Ref{PyPtr},Ref{PyPtr}), ptype, pvalue, ptraceback)
57+ ccall ((@pysym :PyErr_NormalizeException ), Cvoid, (Ref{PyPtr},Ref{PyPtr},Ref{PyPtr}), ptype, pvalue, ptraceback)
58+ return PyError (msg, PyObject (ptype[]), PyObject (pvalue[]), PyObject (ptraceback[]))
59+ end
60+
61+ # replace the message from another error
62+ PyError (msg:: AbstractString , e:: PyError ) =
63+ PyError (msg, e. T, e. val, e. traceback)
64+
5265# ########################################################################
5366# Conversion of Python exceptions into Julia exceptions
5467
@@ -106,17 +119,12 @@ macro pycheckz(ex)
106119 :(@pycheckv $ (esc (ex)) - 1 )
107120end
108121
109- function pyerror (msg:: AbstractString )
110- ptype, pvalue, ptraceback = Ref {PyPtr} (), Ref {PyPtr} (), Ref {PyPtr} ()
111- # equivalent of passing C pointers &exc[1], &exc[2], &exc[3]:
112- ccall ((@pysym :PyErr_Fetch ), Cvoid, (Ref{PyPtr},Ref{PyPtr},Ref{PyPtr}), ptype, pvalue, ptraceback)
113- ccall ((@pysym :PyErr_NormalizeException ), Cvoid, (Ref{PyPtr},Ref{PyPtr},Ref{PyPtr}), ptype, pvalue, ptraceback)
114- pyerror (msg, PyObject (ptype[]), PyObject (pvalue[]), PyObject (ptraceback[]))
115- end
122+ # like PyError(...) but type-unstable: may unwrap a PyJlError
123+ # if one was thrown by a nested pyjlwrap function.
116124
117- function pyerror (msg:: AbstractString , e:: PyError )
125+ pyerror (msg:: AbstractString ) = pyerror (msg, PyError (msg))
126+ pyerror (msg:: AbstractString , e:: PyError ) =
118127 pyerror (msg, e. T, e. val, e. traceback)
119- end
120128
121129function pyerror (msg:: AbstractString , ptype:: PyObject , pvalue:: PyObject , ptraceback:: PyObject )
122130 pargs = _getproperty (pvalue, " args" )
0 commit comments