1// Exception raising code 2// 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() 7 8/////////////// PyErrFetchRestore.proto /////////////// 9 10static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb); /*proto*/ 11static CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb); /*proto*/ 12 13/////////////// PyErrFetchRestore /////////////// 14 15static 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(); 19 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; 26 Py_XDECREF(tmp_type); 27 Py_XDECREF(tmp_value); 28 Py_XDECREF(tmp_tb); 29#else 30 PyErr_Restore(type, value, tb); 31#endif 32} 33 34static 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; 40 41 tstate->curexc_type = 0; 42 tstate->curexc_value = 0; 43 tstate->curexc_traceback = 0; 44#else 45 PyErr_Fetch(type, value, tb); 46#endif 47} 48 49/////////////// RaiseException.proto /////////////// 50 51static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause); /*proto*/ 52 53/////////////// RaiseException /////////////// 54//@requires: PyErrFetchRestore 55 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. 59 60#if PY_MAJOR_VERSION < 3 61static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, 62 CYTHON_UNUSED PyObject *cause) { 63 /* 'cause' is only used in Py3 */ 64 Py_XINCREF(type); 65 if (!value || value == Py_None) 66 value = NULL; 67 else 68 Py_INCREF(value); 69 70 if (!tb || tb == Py_None) 71 tb = NULL; 72 else { 73 Py_INCREF(tb); 74 if (!PyTraceBack_Check(tb)) { 75 PyErr_SetString(PyExc_TypeError, 76 "raise: arg 3 must be a traceback or None"); 77 goto raise_error; 78 } 79 } 80 81 #if PY_VERSION_HEX < 0x02050000 82 if (PyClass_Check(type)) { 83 #else 84 if (PyType_Check(type)) { 85 #endif 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 */ 89 if (!value) { 90 Py_INCREF(Py_None); 91 value = Py_None; 92 } 93#endif 94 PyErr_NormalizeException(&type, &value, &tb); 95 96 } else { 97 /* Raising an instance. The value should be a dummy. */ 98 if (value) { 99 PyErr_SetString(PyExc_TypeError, 100 "instance exception may not have a separate value"); 101 goto raise_error; 102 } 103 /* Normalize to raise <class>, <instance> */ 104 value = type; 105 #if PY_VERSION_HEX < 0x02050000 106 if (PyInstance_Check(type)) { 107 type = (PyObject*) ((PyInstanceObject*)type)->in_class; 108 Py_INCREF(type); 109 } else { 110 type = 0; 111 PyErr_SetString(PyExc_TypeError, 112 "raise: exception must be an old-style class or instance"); 113 goto raise_error; 114 } 115 #else 116 type = (PyObject*) Py_TYPE(type); 117 Py_INCREF(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"); 121 goto raise_error; 122 } 123 #endif 124 } 125 126 __Pyx_ErrRestore(type, value, tb); 127 return; 128raise_error: 129 Py_XDECREF(value); 130 Py_XDECREF(type); 131 Py_XDECREF(tb); 132 return; 133} 134 135#else /* Python 3+ */ 136 137static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause) { 138 PyObject* owned_instance = NULL; 139 if (tb == Py_None) { 140 tb = 0; 141 } else if (tb && !PyTraceBack_Check(tb)) { 142 PyErr_SetString(PyExc_TypeError, 143 "raise: arg 3 must be a traceback or None"); 144 goto bad; 145 } 146 if (value == Py_None) 147 value = 0; 148 149 if (PyExceptionInstance_Check(type)) { 150 if (value) { 151 PyErr_SetString(PyExc_TypeError, 152 "instance exception may not have a separate value"); 153 goto bad; 154 } 155 value = type; 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; 166 } else { 167 instance_class = NULL; 168 } 169 } 170 } 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 175 PyObject *args; 176 if (!value) 177 args = PyTuple_New(0); 178 else if (PyTuple_Check(value)) { 179 Py_INCREF(value); 180 args = value; 181 } else 182 args = PyTuple_Pack(1, value); 183 if (!args) 184 goto bad; 185 owned_instance = PyObject_Call(type, args, NULL); 186 Py_DECREF(args); 187 if (!owned_instance) 188 goto bad; 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)); 195 goto bad; 196 } 197 } 198 } else { 199 PyErr_SetString(PyExc_TypeError, 200 "raise: exception class must be a subclass of BaseException"); 201 goto bad; 202 } 203 204#if PY_VERSION_HEX >= 0x03030000 205 if (cause) { 206#else 207 if (cause && cause != Py_None) { 208#endif 209 PyObject *fixed_cause; 210 if (cause == Py_None) { 211 // raise ... from None 212 fixed_cause = NULL; 213 } else if (PyExceptionClass_Check(cause)) { 214 fixed_cause = PyObject_CallObject(cause, NULL); 215 if (fixed_cause == NULL) 216 goto bad; 217 } else if (PyExceptionInstance_Check(cause)) { 218 fixed_cause = cause; 219 Py_INCREF(fixed_cause); 220 } else { 221 PyErr_SetString(PyExc_TypeError, 222 "exception causes must derive from " 223 "BaseException"); 224 goto bad; 225 } 226 PyException_SetCause(value, fixed_cause); 227 } 228 229 PyErr_SetObject(type, value); 230 231 if (tb) { 232 PyThreadState *tstate = PyThreadState_GET(); 233 PyObject* tmp_tb = tstate->curexc_traceback; 234 if (tb != tmp_tb) { 235 Py_INCREF(tb); 236 tstate->curexc_traceback = tb; 237 Py_XDECREF(tmp_tb); 238 } 239 } 240 241bad: 242 Py_XDECREF(owned_instance); 243 return; 244} 245#endif 246 247/////////////// GetException.proto /////////////// 248 249static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb); /*proto*/ 250 251/////////////// GetException /////////////// 252 253static 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; 264#else 265 PyErr_Fetch(&local_type, &local_value, &local_tb); 266#endif 267 PyErr_NormalizeException(&local_type, &local_value, &local_tb); 268#if CYTHON_COMPILING_IN_CPYTHON 269 if (unlikely(tstate->curexc_type)) 270#else 271 if (unlikely(PyErr_Occurred())) 272#endif 273 goto bad; 274 #if PY_MAJOR_VERSION >= 3 275 if (local_tb) { 276 if (unlikely(PyException_SetTraceback(local_value, local_tb) < 0)) 277 goto bad; 278 } 279 #endif 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); 285 *type = local_type; 286 *value = local_value; 287 *tb = local_tb; 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); 299 Py_XDECREF(tmp_tb); 300#else 301 PyErr_SetExcInfo(local_type, local_value, local_tb); 302#endif 303 return 0; 304bad: 305 *type = 0; 306 *value = 0; 307 *tb = 0; 308 Py_XDECREF(local_type); 309 Py_XDECREF(local_value); 310 Py_XDECREF(local_tb); 311 return -1; 312} 313 314/////////////// ReRaiseException.proto /////////////// 315 316static CYTHON_INLINE void __Pyx_ReraiseException(void); /*proto*/ 317 318/////////////// ReRaiseException.proto /////////////// 319 320static 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; 327#else 328 PyErr_GetExcInfo(&type, &value, &tb); 329#endif 330 if (!type || type == Py_None) { 331#if !CYTHON_COMPILING_IN_CPYTHON 332 Py_XDECREF(type); 333 Py_XDECREF(value); 334 Py_XDECREF(tb); 335#endif 336 // message copied from Py3 337 PyErr_SetString(PyExc_RuntimeError, 338 "No active exception to reraise"); 339 } else { 340#if CYTHON_COMPILING_IN_CPYTHON 341 Py_INCREF(type); 342 Py_XINCREF(value); 343 Py_XINCREF(tb); 344 345#endif 346 PyErr_Restore(type, value, tb); 347 } 348} 349 350/////////////// SaveResetException.proto /////////////// 351 352static CYTHON_INLINE void __Pyx_ExceptionSave(PyObject **type, PyObject **value, PyObject **tb); /*proto*/ 353static void __Pyx_ExceptionReset(PyObject *type, PyObject *value, PyObject *tb); /*proto*/ 354 355/////////////// SaveResetException /////////////// 356 357static 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; 363 Py_XINCREF(*type); 364 Py_XINCREF(*value); 365 Py_XINCREF(*tb); 366#else 367 PyErr_GetExcInfo(type, value, tb); 368#endif 369} 370 371static 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); 383 Py_XDECREF(tmp_tb); 384#else 385 PyErr_SetExcInfo(type, value, tb); 386#endif 387} 388 389/////////////// SwapException.proto /////////////// 390 391static CYTHON_INLINE void __Pyx_ExceptionSwap(PyObject **type, PyObject **value, PyObject **tb); /*proto*/ 392 393/////////////// SwapException /////////////// 394 395static 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(); 399 400 tmp_type = tstate->exc_type; 401 tmp_value = tstate->exc_value; 402 tmp_tb = tstate->exc_traceback; 403 404 tstate->exc_type = *type; 405 tstate->exc_value = *value; 406 tstate->exc_traceback = *tb; 407#else 408 PyErr_GetExcInfo(&tmp_type, &tmp_value, &tmp_tb); 409 PyErr_SetExcInfo(*type, *value, *tb); 410#endif 411 412 *type = tmp_type; 413 *value = tmp_value; 414 *tb = tmp_tb; 415} 416 417/////////////// WriteUnraisableException.proto /////////////// 418 419static void __Pyx_WriteUnraisable(const char *name, int clineno, 420 int lineno, const char *filename, 421 int full_traceback); /*proto*/ 422 423/////////////// WriteUnraisableException /////////////// 424//@requires: PyErrFetchRestore 425 426static 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; 430 PyObject *ctx; 431 __Pyx_ErrFetch(&old_exc, &old_val, &old_tb); 432 if (full_traceback) { 433 Py_XINCREF(old_exc); 434 Py_XINCREF(old_val); 435 Py_XINCREF(old_tb); 436 __Pyx_ErrRestore(old_exc, old_val, old_tb); 437 PyErr_PrintEx(1); 438 } 439 #if PY_MAJOR_VERSION < 3 440 ctx = PyString_FromString(name); 441 #else 442 ctx = PyUnicode_FromString(name); 443 #endif 444 __Pyx_ErrRestore(old_exc, old_val, old_tb); 445 if (!ctx) { 446 PyErr_WriteUnraisable(Py_None); 447 } else { 448 PyErr_WriteUnraisable(ctx); 449 Py_DECREF(ctx); 450 } 451} 452 453/////////////// AddTraceback.proto /////////////// 454 455static void __Pyx_AddTraceback(const char *funcname, int c_line, 456 int py_line, const char *filename); /*proto*/ 457 458/////////////// AddTraceback /////////////// 459//@requires: ModuleSetupCode.c::CodeObjectCache 460//@substitute: naming 461 462#include "compile.h" 463#include "frameobject.h" 464#include "traceback.h" 465 466static 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; 472 473 #if PY_MAJOR_VERSION < 3 474 py_srcfile = PyString_FromString(filename); 475 #else 476 py_srcfile = PyUnicode_FromString(filename); 477 #endif 478 if (!py_srcfile) goto bad; 479 if (c_line) { 480 #if PY_MAJOR_VERSION < 3 481 py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, $cfilenm_cname, c_line); 482 #else 483 py_funcname = PyUnicode_FromFormat( "%s (%s:%d)", funcname, $cfilenm_cname, c_line); 484 #endif 485 } 486 else { 487 #if PY_MAJOR_VERSION < 3 488 py_funcname = PyString_FromString(funcname); 489 #else 490 py_funcname = PyUnicode_FromString(funcname); 491 #endif 492 } 493 if (!py_funcname) goto bad; 494 py_code = __Pyx_PyCode_New( 495 0, /*int argcount,*/ 496 0, /*int kwonlyargcount,*/ 497 0, /*int nlocals,*/ 498 0, /*int stacksize,*/ 499 0, /*int flags,*/ 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*/ 510 ); 511 Py_DECREF(py_srcfile); 512 Py_DECREF(py_funcname); 513 return py_code; 514bad: 515 Py_XDECREF(py_srcfile); 516 Py_XDECREF(py_funcname); 517 return NULL; 518} 519 520static 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; 525 526 py_code = $global_code_object_cache_find(c_line ? c_line : py_line); 527 if (!py_code) { 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); 532 } 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*/ 540 ); 541 if (!py_frame) goto bad; 542 py_frame->f_lineno = py_line; 543 PyTraceBack_Here(py_frame); 544bad: 545 Py_XDECREF(py_code); 546 Py_XDECREF(py_frame); 547} 548