1 // Exception raising code
3 // Exceptions are raised by __Pyx_Raise() and stored as plain
4 // type/value/tb in PyThreadState->curexc_*. When being caught by an
5 // 'except' statement, curexc_* is moved over to exc_* by
6 // __Pyx_GetException()
8 /////////////// PyErrFetchRestore.proto ///////////////
10 static CYTHON_INLINE
void __Pyx_ErrRestore(PyObject
*type
, PyObject
*value
, PyObject
*tb
); /*proto*/
11 static CYTHON_INLINE
void __Pyx_ErrFetch(PyObject
**type
, PyObject
**value
, PyObject
**tb
); /*proto*/
13 /////////////// PyErrFetchRestore ///////////////
15 static CYTHON_INLINE
void __Pyx_ErrRestore(PyObject
*type
, PyObject
*value
, PyObject
*tb
) {
16 #if CYTHON_COMPILING_IN_CPYTHON
17 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
18 PyThreadState
*tstate
= PyThreadState_GET();
20 tmp_type
= tstate
->curexc_type
;
21 tmp_value
= tstate
->curexc_value
;
22 tmp_tb
= tstate
->curexc_traceback
;
23 tstate
->curexc_type
= type
;
24 tstate
->curexc_value
= value
;
25 tstate
->curexc_traceback
= tb
;
27 Py_XDECREF(tmp_value
);
30 PyErr_Restore(type
, value
, tb
);
34 static CYTHON_INLINE
void __Pyx_ErrFetch(PyObject
**type
, PyObject
**value
, PyObject
**tb
) {
35 #if CYTHON_COMPILING_IN_CPYTHON
36 PyThreadState
*tstate
= PyThreadState_GET();
37 *type
= tstate
->curexc_type
;
38 *value
= tstate
->curexc_value
;
39 *tb
= tstate
->curexc_traceback
;
41 tstate
->curexc_type
= 0;
42 tstate
->curexc_value
= 0;
43 tstate
->curexc_traceback
= 0;
45 PyErr_Fetch(type
, value
, tb
);
49 /////////////// RaiseException.proto ///////////////
51 static void __Pyx_Raise(PyObject
*type
, PyObject
*value
, PyObject
*tb
, PyObject
*cause
); /*proto*/
53 /////////////// RaiseException ///////////////
54 //@requires: PyErrFetchRestore
56 // The following function is based on do_raise() from ceval.c. There
57 // are separate versions for Python2 and Python3 as exception handling
58 // has changed quite a lot between the two versions.
60 #if PY_MAJOR_VERSION < 3
61 static void __Pyx_Raise(PyObject
*type
, PyObject
*value
, PyObject
*tb
,
62 CYTHON_UNUSED PyObject
*cause
) {
63 /* 'cause' is only used in Py3 */
65 if (!value
|| value
== Py_None
)
70 if (!tb
|| tb
== Py_None
)
74 if (!PyTraceBack_Check(tb
)) {
75 PyErr_SetString(PyExc_TypeError
,
76 "raise: arg 3 must be a traceback or None");
81 #if PY_VERSION_HEX < 0x02050000
82 if (PyClass_Check(type
)) {
84 if (PyType_Check(type
)) {
86 /* instantiate the type now (we don't know when and how it will be caught) */
87 #if CYTHON_COMPILING_IN_PYPY
88 /* PyPy can't handle value == NULL */
94 PyErr_NormalizeException(&type
, &value
, &tb
);
97 /* Raising an instance. The value should be a dummy. */
99 PyErr_SetString(PyExc_TypeError
,
100 "instance exception may not have a separate value");
103 /* Normalize to raise <class>, <instance> */
105 #if PY_VERSION_HEX < 0x02050000
106 if (PyInstance_Check(type
)) {
107 type
= (PyObject
*) ((PyInstanceObject
*)type
)->in_class
;
111 PyErr_SetString(PyExc_TypeError
,
112 "raise: exception must be an old-style class or instance");
116 type
= (PyObject
*) Py_TYPE(type
);
118 if (!PyType_IsSubtype((PyTypeObject
*)type
, (PyTypeObject
*)PyExc_BaseException
)) {
119 PyErr_SetString(PyExc_TypeError
,
120 "raise: exception class must be a subclass of BaseException");
126 __Pyx_ErrRestore(type
, value
, tb
);
135 #else /* Python 3+ */
137 static void __Pyx_Raise(PyObject
*type
, PyObject
*value
, PyObject
*tb
, PyObject
*cause
) {
138 PyObject
* owned_instance
= NULL
;
141 } else if (tb
&& !PyTraceBack_Check(tb
)) {
142 PyErr_SetString(PyExc_TypeError
,
143 "raise: arg 3 must be a traceback or None");
146 if (value
== Py_None
)
149 if (PyExceptionInstance_Check(type
)) {
151 PyErr_SetString(PyExc_TypeError
,
152 "instance exception may not have a separate value");
156 type
= (PyObject
*) Py_TYPE(value
);
157 } else if (PyExceptionClass_Check(type
)) {
158 // make sure value is an exception instance of type
159 PyObject
*instance_class
= NULL
;
160 if (value
&& PyExceptionInstance_Check(value
)) {
161 instance_class
= (PyObject
*) Py_TYPE(value
);
162 if (instance_class
!= type
) {
163 if (PyObject_IsSubclass(instance_class
, type
)) {
164 // believe the instance
165 type
= instance_class
;
167 instance_class
= NULL
;
171 if (!instance_class
) {
172 // instantiate the type now (we don't know when and how it will be caught)
173 // assuming that 'value' is an argument to the type's constructor
174 // not using PyErr_NormalizeException() to avoid ref-counting problems
177 args
= PyTuple_New(0);
178 else if (PyTuple_Check(value
)) {
182 args
= PyTuple_Pack(1, value
);
185 owned_instance
= PyObject_Call(type
, args
, NULL
);
189 value
= owned_instance
;
190 if (!PyExceptionInstance_Check(value
)) {
191 PyErr_Format(PyExc_TypeError
,
192 "calling %R should have returned an instance of "
193 "BaseException, not %R",
194 type
, Py_TYPE(value
));
199 PyErr_SetString(PyExc_TypeError
,
200 "raise: exception class must be a subclass of BaseException");
204 #if PY_VERSION_HEX >= 0x03030000
207 if (cause
&& cause
!= Py_None
) {
209 PyObject
*fixed_cause
;
210 if (cause
== Py_None
) {
211 // raise ... from None
213 } else if (PyExceptionClass_Check(cause
)) {
214 fixed_cause
= PyObject_CallObject(cause
, NULL
);
215 if (fixed_cause
== NULL
)
217 } else if (PyExceptionInstance_Check(cause
)) {
219 Py_INCREF(fixed_cause
);
221 PyErr_SetString(PyExc_TypeError
,
222 "exception causes must derive from "
226 PyException_SetCause(value
, fixed_cause
);
229 PyErr_SetObject(type
, value
);
232 PyThreadState
*tstate
= PyThreadState_GET();
233 PyObject
* tmp_tb
= tstate
->curexc_traceback
;
236 tstate
->curexc_traceback
= tb
;
242 Py_XDECREF(owned_instance
);
247 /////////////// GetException.proto ///////////////
249 static int __Pyx_GetException(PyObject
**type
, PyObject
**value
, PyObject
**tb
); /*proto*/
251 /////////////// GetException ///////////////
253 static int __Pyx_GetException(PyObject
**type
, PyObject
**value
, PyObject
**tb
) {
254 PyObject
*local_type
, *local_value
, *local_tb
;
255 #if CYTHON_COMPILING_IN_CPYTHON
256 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
257 PyThreadState
*tstate
= PyThreadState_GET();
258 local_type
= tstate
->curexc_type
;
259 local_value
= tstate
->curexc_value
;
260 local_tb
= tstate
->curexc_traceback
;
261 tstate
->curexc_type
= 0;
262 tstate
->curexc_value
= 0;
263 tstate
->curexc_traceback
= 0;
265 PyErr_Fetch(&local_type
, &local_value
, &local_tb
);
267 PyErr_NormalizeException(&local_type
, &local_value
, &local_tb
);
268 #if CYTHON_COMPILING_IN_CPYTHON
269 if (unlikely(tstate
->curexc_type
))
271 if (unlikely(PyErr_Occurred()))
274 #if PY_MAJOR_VERSION >= 3
276 if (unlikely(PyException_SetTraceback(local_value
, local_tb
) < 0))
280 // traceback may be NULL for freshly raised exceptions
281 Py_XINCREF(local_tb
);
282 // exception state may be temporarily empty in parallel loops (race condition)
283 Py_XINCREF(local_type
);
284 Py_XINCREF(local_value
);
286 *value
= local_value
;
288 #if CYTHON_COMPILING_IN_CPYTHON
289 tmp_type
= tstate
->exc_type
;
290 tmp_value
= tstate
->exc_value
;
291 tmp_tb
= tstate
->exc_traceback
;
292 tstate
->exc_type
= local_type
;
293 tstate
->exc_value
= local_value
;
294 tstate
->exc_traceback
= local_tb
;
295 // Make sure tstate is in a consistent state when we XDECREF
296 // these objects (DECREF may run arbitrary code).
297 Py_XDECREF(tmp_type
);
298 Py_XDECREF(tmp_value
);
301 PyErr_SetExcInfo(local_type
, local_value
, local_tb
);
308 Py_XDECREF(local_type
);
309 Py_XDECREF(local_value
);
310 Py_XDECREF(local_tb
);
314 /////////////// ReRaiseException.proto ///////////////
316 static CYTHON_INLINE
void __Pyx_ReraiseException(void); /*proto*/
318 /////////////// ReRaiseException.proto ///////////////
320 static CYTHON_INLINE
void __Pyx_ReraiseException(void) {
321 PyObject
*type
= NULL
, *value
= NULL
, *tb
= NULL
;
322 #if CYTHON_COMPILING_IN_CPYTHON
323 PyThreadState
*tstate
= PyThreadState_GET();
324 type
= tstate
->exc_type
;
325 value
= tstate
->exc_value
;
326 tb
= tstate
->exc_traceback
;
328 PyErr_GetExcInfo(&type
, &value
, &tb
);
330 if (!type
|| type
== Py_None
) {
331 #if !CYTHON_COMPILING_IN_CPYTHON
336 // message copied from Py3
337 PyErr_SetString(PyExc_RuntimeError
,
338 "No active exception to reraise");
340 #if CYTHON_COMPILING_IN_CPYTHON
346 PyErr_Restore(type
, value
, tb
);
350 /////////////// SaveResetException.proto ///////////////
352 static CYTHON_INLINE
void __Pyx_ExceptionSave(PyObject
**type
, PyObject
**value
, PyObject
**tb
); /*proto*/
353 static void __Pyx_ExceptionReset(PyObject
*type
, PyObject
*value
, PyObject
*tb
); /*proto*/
355 /////////////// SaveResetException ///////////////
357 static CYTHON_INLINE
void __Pyx_ExceptionSave(PyObject
**type
, PyObject
**value
, PyObject
**tb
) {
358 #if CYTHON_COMPILING_IN_CPYTHON
359 PyThreadState
*tstate
= PyThreadState_GET();
360 *type
= tstate
->exc_type
;
361 *value
= tstate
->exc_value
;
362 *tb
= tstate
->exc_traceback
;
367 PyErr_GetExcInfo(type
, value
, tb
);
371 static void __Pyx_ExceptionReset(PyObject
*type
, PyObject
*value
, PyObject
*tb
) {
372 #if CYTHON_COMPILING_IN_CPYTHON
373 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
374 PyThreadState
*tstate
= PyThreadState_GET();
375 tmp_type
= tstate
->exc_type
;
376 tmp_value
= tstate
->exc_value
;
377 tmp_tb
= tstate
->exc_traceback
;
378 tstate
->exc_type
= type
;
379 tstate
->exc_value
= value
;
380 tstate
->exc_traceback
= tb
;
381 Py_XDECREF(tmp_type
);
382 Py_XDECREF(tmp_value
);
385 PyErr_SetExcInfo(type
, value
, tb
);
389 /////////////// SwapException.proto ///////////////
391 static CYTHON_INLINE
void __Pyx_ExceptionSwap(PyObject
**type
, PyObject
**value
, PyObject
**tb
); /*proto*/
393 /////////////// SwapException ///////////////
395 static CYTHON_INLINE
void __Pyx_ExceptionSwap(PyObject
**type
, PyObject
**value
, PyObject
**tb
) {
396 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
397 #if CYTHON_COMPILING_IN_CPYTHON
398 PyThreadState
*tstate
= PyThreadState_GET();
400 tmp_type
= tstate
->exc_type
;
401 tmp_value
= tstate
->exc_value
;
402 tmp_tb
= tstate
->exc_traceback
;
404 tstate
->exc_type
= *type
;
405 tstate
->exc_value
= *value
;
406 tstate
->exc_traceback
= *tb
;
408 PyErr_GetExcInfo(&tmp_type
, &tmp_value
, &tmp_tb
);
409 PyErr_SetExcInfo(*type
, *value
, *tb
);
417 /////////////// WriteUnraisableException.proto ///////////////
419 static void __Pyx_WriteUnraisable(const char *name
, int clineno
,
420 int lineno
, const char *filename
,
421 int full_traceback
); /*proto*/
423 /////////////// WriteUnraisableException ///////////////
424 //@requires: PyErrFetchRestore
426 static void __Pyx_WriteUnraisable(const char *name
, CYTHON_UNUSED
int clineno
,
427 CYTHON_UNUSED
int lineno
, CYTHON_UNUSED
const char *filename
,
428 int full_traceback
) {
429 PyObject
*old_exc
, *old_val
, *old_tb
;
431 __Pyx_ErrFetch(&old_exc
, &old_val
, &old_tb
);
432 if (full_traceback
) {
436 __Pyx_ErrRestore(old_exc
, old_val
, old_tb
);
439 #if PY_MAJOR_VERSION < 3
440 ctx
= PyString_FromString(name
);
442 ctx
= PyUnicode_FromString(name
);
444 __Pyx_ErrRestore(old_exc
, old_val
, old_tb
);
446 PyErr_WriteUnraisable(Py_None
);
448 PyErr_WriteUnraisable(ctx
);
453 /////////////// AddTraceback.proto ///////////////
455 static void __Pyx_AddTraceback(const char *funcname
, int c_line
,
456 int py_line
, const char *filename
); /*proto*/
458 /////////////// AddTraceback ///////////////
459 //@requires: ModuleSetupCode.c::CodeObjectCache
460 //@substitute: naming
463 #include "frameobject.h"
464 #include "traceback.h"
466 static PyCodeObject
* __Pyx_CreateCodeObjectForTraceback(
467 const char *funcname
, int c_line
,
468 int py_line
, const char *filename
) {
469 PyCodeObject
*py_code
= 0;
470 PyObject
*py_srcfile
= 0;
471 PyObject
*py_funcname
= 0;
473 #if PY_MAJOR_VERSION < 3
474 py_srcfile
= PyString_FromString(filename
);
476 py_srcfile
= PyUnicode_FromString(filename
);
478 if (!py_srcfile
) goto bad
;
480 #if PY_MAJOR_VERSION < 3
481 py_funcname
= PyString_FromFormat( "%s (%s:%d)", funcname
, $cfilenm_cname
, c_line
);
483 py_funcname
= PyUnicode_FromFormat( "%s (%s:%d)", funcname
, $cfilenm_cname
, c_line
);
487 #if PY_MAJOR_VERSION < 3
488 py_funcname
= PyString_FromString(funcname
);
490 py_funcname
= PyUnicode_FromString(funcname
);
493 if (!py_funcname
) goto bad
;
494 py_code
= __Pyx_PyCode_New(
496 0, /*int kwonlyargcount,*/
498 0, /*int stacksize,*/
500 $empty_bytes
, /*PyObject *code,*/
501 $empty_tuple
, /*PyObject *consts,*/
502 $empty_tuple
, /*PyObject *names,*/
503 $empty_tuple
, /*PyObject *varnames,*/
504 $empty_tuple
, /*PyObject *freevars,*/
505 $empty_tuple
, /*PyObject *cellvars,*/
506 py_srcfile
, /*PyObject *filename,*/
507 py_funcname
, /*PyObject *name,*/
508 py_line
, /*int firstlineno,*/
509 $empty_bytes
/*PyObject *lnotab*/
511 Py_DECREF(py_srcfile
);
512 Py_DECREF(py_funcname
);
515 Py_XDECREF(py_srcfile
);
516 Py_XDECREF(py_funcname
);
520 static void __Pyx_AddTraceback(const char *funcname
, int c_line
,
521 int py_line
, const char *filename
) {
522 PyCodeObject
*py_code
= 0;
523 PyObject
*py_globals
= 0;
524 PyFrameObject
*py_frame
= 0;
526 py_code
= $
global_code_object_cache_find(c_line
? c_line
: py_line
);
528 py_code
= __Pyx_CreateCodeObjectForTraceback(
529 funcname
, c_line
, py_line
, filename
);
530 if (!py_code
) goto bad
;
531 $
global_code_object_cache_insert(c_line
? c_line
: py_line
, py_code
);
533 py_globals
= PyModule_GetDict($module_cname
);
534 if (!py_globals
) goto bad
;
535 py_frame
= PyFrame_New(
536 PyThreadState_GET(), /*PyThreadState *tstate,*/
537 py_code
, /*PyCodeObject *code,*/
538 py_globals
, /*PyObject *globals,*/
539 0 /*PyObject *locals*/
541 if (!py_frame
) goto bad
;
542 py_frame
->f_lineno
= py_line
;
543 PyTraceBack_Here(py_frame
);
546 Py_XDECREF(py_frame
);