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
9 /////////////// PyThreadStateGet.proto ///////////////
10 //@substitute: naming
11
12 #if CYTHON_FAST_THREAD_STATE
13 #define __Pyx_PyThreadState_declare PyThreadState *$local_tstate_cname;
14 #define __Pyx_PyThreadState_assign $local_tstate_cname = __Pyx_PyThreadState_Current;
15 #define __Pyx_PyErr_Occurred() $local_tstate_cname->curexc_type
16 #else
17 #define __Pyx_PyThreadState_declare
18 #define __Pyx_PyThreadState_assign
19 #define __Pyx_PyErr_Occurred() PyErr_Occurred()
20 #endif
21
22
23 /////////////// PyErrExceptionMatches.proto ///////////////
24 //@substitute: naming
25
26 #if CYTHON_FAST_THREAD_STATE
27 #define __Pyx_PyErr_ExceptionMatches(err) __Pyx_PyErr_ExceptionMatchesInState($local_tstate_cname, err)
28 static CYTHON_INLINE int __Pyx_PyErr_ExceptionMatchesInState(PyThreadState* tstate, PyObject* err);
29 #else
30 #define __Pyx_PyErr_ExceptionMatches(err) PyErr_ExceptionMatches(err)
31 #endif
32
33 /////////////// PyErrExceptionMatches ///////////////
34
35 #if CYTHON_FAST_THREAD_STATE
__Pyx_PyErr_ExceptionMatchesTuple(PyObject * exc_type,PyObject * tuple)36 static int __Pyx_PyErr_ExceptionMatchesTuple(PyObject *exc_type, PyObject *tuple) {
37 Py_ssize_t i, n;
38 n = PyTuple_GET_SIZE(tuple);
39 #if PY_MAJOR_VERSION >= 3
40 // the tighter subtype checking in Py3 allows faster out-of-order comparison
41 for (i=0; i<n; i++) {
42 if (exc_type == PyTuple_GET_ITEM(tuple, i)) return 1;
43 }
44 #endif
45 for (i=0; i<n; i++) {
46 if (__Pyx_PyErr_GivenExceptionMatches(exc_type, PyTuple_GET_ITEM(tuple, i))) return 1;
47 }
48 return 0;
49 }
50
__Pyx_PyErr_ExceptionMatchesInState(PyThreadState * tstate,PyObject * err)51 static CYTHON_INLINE int __Pyx_PyErr_ExceptionMatchesInState(PyThreadState* tstate, PyObject* err) {
52 PyObject *exc_type = tstate->curexc_type;
53 if (exc_type == err) return 1;
54 if (unlikely(!exc_type)) return 0;
55 if (unlikely(PyTuple_Check(err)))
56 return __Pyx_PyErr_ExceptionMatchesTuple(exc_type, err);
57 return __Pyx_PyErr_GivenExceptionMatches(exc_type, err);
58 }
59 #endif
60
61 /////////////// PyErrFetchRestore.proto ///////////////
62 //@substitute: naming
63 //@requires: PyThreadStateGet
64
65 #if CYTHON_FAST_THREAD_STATE
66 #define __Pyx_PyErr_Clear() __Pyx_ErrRestore(NULL, NULL, NULL)
67 #define __Pyx_ErrRestoreWithState(type, value, tb) __Pyx_ErrRestoreInState(PyThreadState_GET(), type, value, tb)
68 #define __Pyx_ErrFetchWithState(type, value, tb) __Pyx_ErrFetchInState(PyThreadState_GET(), type, value, tb)
69 #define __Pyx_ErrRestore(type, value, tb) __Pyx_ErrRestoreInState($local_tstate_cname, type, value, tb)
70 #define __Pyx_ErrFetch(type, value, tb) __Pyx_ErrFetchInState($local_tstate_cname, type, value, tb)
71 static CYTHON_INLINE void __Pyx_ErrRestoreInState(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb); /*proto*/
72 static CYTHON_INLINE void __Pyx_ErrFetchInState(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb); /*proto*/
73
74 #if CYTHON_COMPILING_IN_CPYTHON
75 #define __Pyx_PyErr_SetNone(exc) (Py_INCREF(exc), __Pyx_ErrRestore((exc), NULL, NULL))
76 #else
77 #define __Pyx_PyErr_SetNone(exc) PyErr_SetNone(exc)
78 #endif
79
80 #else
81 #define __Pyx_PyErr_Clear() PyErr_Clear()
82 #define __Pyx_PyErr_SetNone(exc) PyErr_SetNone(exc)
83 #define __Pyx_ErrRestoreWithState(type, value, tb) PyErr_Restore(type, value, tb)
84 #define __Pyx_ErrFetchWithState(type, value, tb) PyErr_Fetch(type, value, tb)
85 #define __Pyx_ErrRestoreInState(tstate, type, value, tb) PyErr_Restore(type, value, tb)
86 #define __Pyx_ErrFetchInState(tstate, type, value, tb) PyErr_Fetch(type, value, tb)
87 #define __Pyx_ErrRestore(type, value, tb) PyErr_Restore(type, value, tb)
88 #define __Pyx_ErrFetch(type, value, tb) PyErr_Fetch(type, value, tb)
89 #endif
90
91 /////////////// PyErrFetchRestore ///////////////
92
93 #if CYTHON_FAST_THREAD_STATE
__Pyx_ErrRestoreInState(PyThreadState * tstate,PyObject * type,PyObject * value,PyObject * tb)94 static CYTHON_INLINE void __Pyx_ErrRestoreInState(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb) {
95 PyObject *tmp_type, *tmp_value, *tmp_tb;
96 tmp_type = tstate->curexc_type;
97 tmp_value = tstate->curexc_value;
98 tmp_tb = tstate->curexc_traceback;
99 tstate->curexc_type = type;
100 tstate->curexc_value = value;
101 tstate->curexc_traceback = tb;
102 Py_XDECREF(tmp_type);
103 Py_XDECREF(tmp_value);
104 Py_XDECREF(tmp_tb);
105 }
106
__Pyx_ErrFetchInState(PyThreadState * tstate,PyObject ** type,PyObject ** value,PyObject ** tb)107 static CYTHON_INLINE void __Pyx_ErrFetchInState(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb) {
108 *type = tstate->curexc_type;
109 *value = tstate->curexc_value;
110 *tb = tstate->curexc_traceback;
111 tstate->curexc_type = 0;
112 tstate->curexc_value = 0;
113 tstate->curexc_traceback = 0;
114 }
115 #endif
116
117 /////////////// RaiseException.proto ///////////////
118
119 static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause); /*proto*/
120
121 /////////////// RaiseException ///////////////
122 //@requires: PyErrFetchRestore
123 //@requires: PyThreadStateGet
124
125 // The following function is based on do_raise() from ceval.c. There
126 // are separate versions for Python2 and Python3 as exception handling
127 // has changed quite a lot between the two versions.
128
129 #if PY_MAJOR_VERSION < 3
__Pyx_Raise(PyObject * type,PyObject * value,PyObject * tb,CYTHON_UNUSED PyObject * cause)130 static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb,
131 CYTHON_UNUSED PyObject *cause) {
132 __Pyx_PyThreadState_declare
133 /* 'cause' is only used in Py3 */
134 Py_XINCREF(type);
135 if (!value || value == Py_None)
136 value = NULL;
137 else
138 Py_INCREF(value);
139
140 if (!tb || tb == Py_None)
141 tb = NULL;
142 else {
143 Py_INCREF(tb);
144 if (!PyTraceBack_Check(tb)) {
145 PyErr_SetString(PyExc_TypeError,
146 "raise: arg 3 must be a traceback or None");
147 goto raise_error;
148 }
149 }
150
151 if (PyType_Check(type)) {
152 /* instantiate the type now (we don't know when and how it will be caught) */
153 #if CYTHON_COMPILING_IN_PYPY
154 /* PyPy can't handle value == NULL */
155 if (!value) {
156 Py_INCREF(Py_None);
157 value = Py_None;
158 }
159 #endif
160 PyErr_NormalizeException(&type, &value, &tb);
161
162 } else {
163 /* Raising an instance. The value should be a dummy. */
164 if (value) {
165 PyErr_SetString(PyExc_TypeError,
166 "instance exception may not have a separate value");
167 goto raise_error;
168 }
169 /* Normalize to raise <class>, <instance> */
170 value = type;
171 type = (PyObject*) Py_TYPE(type);
172 Py_INCREF(type);
173 if (!PyType_IsSubtype((PyTypeObject *)type, (PyTypeObject *)PyExc_BaseException)) {
174 PyErr_SetString(PyExc_TypeError,
175 "raise: exception class must be a subclass of BaseException");
176 goto raise_error;
177 }
178 }
179
180 __Pyx_PyThreadState_assign
181 __Pyx_ErrRestore(type, value, tb);
182 return;
183 raise_error:
184 Py_XDECREF(value);
185 Py_XDECREF(type);
186 Py_XDECREF(tb);
187 return;
188 }
189
190 #else /* Python 3+ */
191
__Pyx_Raise(PyObject * type,PyObject * value,PyObject * tb,PyObject * cause)192 static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause) {
193 PyObject* owned_instance = NULL;
194 if (tb == Py_None) {
195 tb = 0;
196 } else if (tb && !PyTraceBack_Check(tb)) {
197 PyErr_SetString(PyExc_TypeError,
198 "raise: arg 3 must be a traceback or None");
199 goto bad;
200 }
201 if (value == Py_None)
202 value = 0;
203
204 if (PyExceptionInstance_Check(type)) {
205 if (value) {
206 PyErr_SetString(PyExc_TypeError,
207 "instance exception may not have a separate value");
208 goto bad;
209 }
210 value = type;
211 type = (PyObject*) Py_TYPE(value);
212 } else if (PyExceptionClass_Check(type)) {
213 // make sure value is an exception instance of type
214 PyObject *instance_class = NULL;
215 if (value && PyExceptionInstance_Check(value)) {
216 instance_class = (PyObject*) Py_TYPE(value);
217 if (instance_class != type) {
218 int is_subclass = PyObject_IsSubclass(instance_class, type);
219 if (!is_subclass) {
220 instance_class = NULL;
221 } else if (unlikely(is_subclass == -1)) {
222 // error on subclass test
223 goto bad;
224 } else {
225 // believe the instance
226 type = instance_class;
227 }
228 }
229 }
230 if (!instance_class) {
231 // instantiate the type now (we don't know when and how it will be caught)
232 // assuming that 'value' is an argument to the type's constructor
233 // not using PyErr_NormalizeException() to avoid ref-counting problems
234 PyObject *args;
235 if (!value)
236 args = PyTuple_New(0);
237 else if (PyTuple_Check(value)) {
238 Py_INCREF(value);
239 args = value;
240 } else
241 args = PyTuple_Pack(1, value);
242 if (!args)
243 goto bad;
244 owned_instance = PyObject_Call(type, args, NULL);
245 Py_DECREF(args);
246 if (!owned_instance)
247 goto bad;
248 value = owned_instance;
249 if (!PyExceptionInstance_Check(value)) {
250 PyErr_Format(PyExc_TypeError,
251 "calling %R should have returned an instance of "
252 "BaseException, not %R",
253 type, Py_TYPE(value));
254 goto bad;
255 }
256 }
257 } else {
258 PyErr_SetString(PyExc_TypeError,
259 "raise: exception class must be a subclass of BaseException");
260 goto bad;
261 }
262
263 if (cause) {
264 PyObject *fixed_cause;
265 if (cause == Py_None) {
266 // raise ... from None
267 fixed_cause = NULL;
268 } else if (PyExceptionClass_Check(cause)) {
269 fixed_cause = PyObject_CallObject(cause, NULL);
270 if (fixed_cause == NULL)
271 goto bad;
272 } else if (PyExceptionInstance_Check(cause)) {
273 fixed_cause = cause;
274 Py_INCREF(fixed_cause);
275 } else {
276 PyErr_SetString(PyExc_TypeError,
277 "exception causes must derive from "
278 "BaseException");
279 goto bad;
280 }
281 PyException_SetCause(value, fixed_cause);
282 }
283
284 PyErr_SetObject(type, value);
285
286 if (tb) {
287 #if CYTHON_COMPILING_IN_PYPY
288 PyObject *tmp_type, *tmp_value, *tmp_tb;
289 PyErr_Fetch(&tmp_type, &tmp_value, &tmp_tb);
290 Py_INCREF(tb);
291 PyErr_Restore(tmp_type, tmp_value, tb);
292 Py_XDECREF(tmp_tb);
293 #else
294 PyThreadState *tstate = __Pyx_PyThreadState_Current;
295 PyObject* tmp_tb = tstate->curexc_traceback;
296 if (tb != tmp_tb) {
297 Py_INCREF(tb);
298 tstate->curexc_traceback = tb;
299 Py_XDECREF(tmp_tb);
300 }
301 #endif
302 }
303
304 bad:
305 Py_XDECREF(owned_instance);
306 return;
307 }
308 #endif
309
310
311 /////////////// GetTopmostException.proto ///////////////
312
313 #if CYTHON_USE_EXC_INFO_STACK
314 static _PyErr_StackItem * __Pyx_PyErr_GetTopmostException(PyThreadState *tstate);
315 #endif
316
317 /////////////// GetTopmostException ///////////////
318
319 #if CYTHON_USE_EXC_INFO_STACK
320 // Copied from errors.c in CPython.
321 static _PyErr_StackItem *
__Pyx_PyErr_GetTopmostException(PyThreadState * tstate)322 __Pyx_PyErr_GetTopmostException(PyThreadState *tstate)
323 {
324 _PyErr_StackItem *exc_info = tstate->exc_info;
325 while ((exc_info->exc_type == NULL || exc_info->exc_type == Py_None) &&
326 exc_info->previous_item != NULL)
327 {
328 exc_info = exc_info->previous_item;
329 }
330 return exc_info;
331 }
332 #endif
333
334
335 /////////////// GetException.proto ///////////////
336 //@substitute: naming
337 //@requires: PyThreadStateGet
338
339 #if CYTHON_FAST_THREAD_STATE
340 #define __Pyx_GetException(type, value, tb) __Pyx__GetException($local_tstate_cname, type, value, tb)
341 static int __Pyx__GetException(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb); /*proto*/
342 #else
343 static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
344 #endif
345
346 /////////////// GetException ///////////////
347
348 #if CYTHON_FAST_THREAD_STATE
__Pyx__GetException(PyThreadState * tstate,PyObject ** type,PyObject ** value,PyObject ** tb)349 static int __Pyx__GetException(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb)
350 #else
351 static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb)
352 #endif
353 {
354 PyObject *local_type, *local_value, *local_tb;
355 #if CYTHON_FAST_THREAD_STATE
356 PyObject *tmp_type, *tmp_value, *tmp_tb;
357 local_type = tstate->curexc_type;
358 local_value = tstate->curexc_value;
359 local_tb = tstate->curexc_traceback;
360 tstate->curexc_type = 0;
361 tstate->curexc_value = 0;
362 tstate->curexc_traceback = 0;
363 #else
364 PyErr_Fetch(&local_type, &local_value, &local_tb);
365 #endif
366 PyErr_NormalizeException(&local_type, &local_value, &local_tb);
367 #if CYTHON_FAST_THREAD_STATE
368 if (unlikely(tstate->curexc_type))
369 #else
370 if (unlikely(PyErr_Occurred()))
371 #endif
372 goto bad;
373 #if PY_MAJOR_VERSION >= 3
374 if (local_tb) {
375 if (unlikely(PyException_SetTraceback(local_value, local_tb) < 0))
376 goto bad;
377 }
378 #endif
379 // traceback may be NULL for freshly raised exceptions
380 Py_XINCREF(local_tb);
381 // exception state may be temporarily empty in parallel loops (race condition)
382 Py_XINCREF(local_type);
383 Py_XINCREF(local_value);
384 *type = local_type;
385 *value = local_value;
386 *tb = local_tb;
387 #if CYTHON_FAST_THREAD_STATE
388 #if CYTHON_USE_EXC_INFO_STACK
389 {
390 _PyErr_StackItem *exc_info = tstate->exc_info;
391 tmp_type = exc_info->exc_type;
392 tmp_value = exc_info->exc_value;
393 tmp_tb = exc_info->exc_traceback;
394 exc_info->exc_type = local_type;
395 exc_info->exc_value = local_value;
396 exc_info->exc_traceback = local_tb;
397 }
398 #else
399 tmp_type = tstate->exc_type;
400 tmp_value = tstate->exc_value;
401 tmp_tb = tstate->exc_traceback;
402 tstate->exc_type = local_type;
403 tstate->exc_value = local_value;
404 tstate->exc_traceback = local_tb;
405 #endif
406 // Make sure tstate is in a consistent state when we XDECREF
407 // these objects (DECREF may run arbitrary code).
408 Py_XDECREF(tmp_type);
409 Py_XDECREF(tmp_value);
410 Py_XDECREF(tmp_tb);
411 #else
412 PyErr_SetExcInfo(local_type, local_value, local_tb);
413 #endif
414 return 0;
415 bad:
416 *type = 0;
417 *value = 0;
418 *tb = 0;
419 Py_XDECREF(local_type);
420 Py_XDECREF(local_value);
421 Py_XDECREF(local_tb);
422 return -1;
423 }
424
425 /////////////// ReRaiseException.proto ///////////////
426
427 static CYTHON_INLINE void __Pyx_ReraiseException(void); /*proto*/
428
429 /////////////// ReRaiseException ///////////////
430 //@requires: GetTopmostException
431
__Pyx_ReraiseException(void)432 static CYTHON_INLINE void __Pyx_ReraiseException(void) {
433 PyObject *type = NULL, *value = NULL, *tb = NULL;
434 #if CYTHON_FAST_THREAD_STATE
435 PyThreadState *tstate = PyThreadState_GET();
436 #if CYTHON_USE_EXC_INFO_STACK
437 _PyErr_StackItem *exc_info = __Pyx_PyErr_GetTopmostException(tstate);
438 type = exc_info->exc_type;
439 value = exc_info->exc_value;
440 tb = exc_info->exc_traceback;
441 #else
442 type = tstate->exc_type;
443 value = tstate->exc_value;
444 tb = tstate->exc_traceback;
445 #endif
446 #else
447 PyErr_GetExcInfo(&type, &value, &tb);
448 #endif
449 if (!type || type == Py_None) {
450 #if !CYTHON_FAST_THREAD_STATE
451 Py_XDECREF(type);
452 Py_XDECREF(value);
453 Py_XDECREF(tb);
454 #endif
455 // message copied from Py3
456 PyErr_SetString(PyExc_RuntimeError,
457 "No active exception to reraise");
458 } else {
459 #if CYTHON_FAST_THREAD_STATE
460 Py_INCREF(type);
461 Py_XINCREF(value);
462 Py_XINCREF(tb);
463
464 #endif
465 PyErr_Restore(type, value, tb);
466 }
467 }
468
469 /////////////// SaveResetException.proto ///////////////
470 //@substitute: naming
471 //@requires: PyThreadStateGet
472
473 #if CYTHON_FAST_THREAD_STATE
474 #define __Pyx_ExceptionSave(type, value, tb) __Pyx__ExceptionSave($local_tstate_cname, type, value, tb)
475 static CYTHON_INLINE void __Pyx__ExceptionSave(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb); /*proto*/
476 #define __Pyx_ExceptionReset(type, value, tb) __Pyx__ExceptionReset($local_tstate_cname, type, value, tb)
477 static CYTHON_INLINE void __Pyx__ExceptionReset(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb); /*proto*/
478
479 #else
480
481 #define __Pyx_ExceptionSave(type, value, tb) PyErr_GetExcInfo(type, value, tb)
482 #define __Pyx_ExceptionReset(type, value, tb) PyErr_SetExcInfo(type, value, tb)
483 #endif
484
485 /////////////// SaveResetException ///////////////
486 //@requires: GetTopmostException
487
488 #if CYTHON_FAST_THREAD_STATE
__Pyx__ExceptionSave(PyThreadState * tstate,PyObject ** type,PyObject ** value,PyObject ** tb)489 static CYTHON_INLINE void __Pyx__ExceptionSave(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb) {
490 #if CYTHON_USE_EXC_INFO_STACK
491 _PyErr_StackItem *exc_info = __Pyx_PyErr_GetTopmostException(tstate);
492 *type = exc_info->exc_type;
493 *value = exc_info->exc_value;
494 *tb = exc_info->exc_traceback;
495 #else
496 *type = tstate->exc_type;
497 *value = tstate->exc_value;
498 *tb = tstate->exc_traceback;
499 #endif
500 Py_XINCREF(*type);
501 Py_XINCREF(*value);
502 Py_XINCREF(*tb);
503 }
504
__Pyx__ExceptionReset(PyThreadState * tstate,PyObject * type,PyObject * value,PyObject * tb)505 static CYTHON_INLINE void __Pyx__ExceptionReset(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb) {
506 PyObject *tmp_type, *tmp_value, *tmp_tb;
507
508 #if CYTHON_USE_EXC_INFO_STACK
509 _PyErr_StackItem *exc_info = tstate->exc_info;
510 tmp_type = exc_info->exc_type;
511 tmp_value = exc_info->exc_value;
512 tmp_tb = exc_info->exc_traceback;
513 exc_info->exc_type = type;
514 exc_info->exc_value = value;
515 exc_info->exc_traceback = tb;
516 #else
517 tmp_type = tstate->exc_type;
518 tmp_value = tstate->exc_value;
519 tmp_tb = tstate->exc_traceback;
520 tstate->exc_type = type;
521 tstate->exc_value = value;
522 tstate->exc_traceback = tb;
523 #endif
524 Py_XDECREF(tmp_type);
525 Py_XDECREF(tmp_value);
526 Py_XDECREF(tmp_tb);
527 }
528 #endif
529
530 /////////////// SwapException.proto ///////////////
531 //@substitute: naming
532 //@requires: PyThreadStateGet
533
534 #if CYTHON_FAST_THREAD_STATE
535 #define __Pyx_ExceptionSwap(type, value, tb) __Pyx__ExceptionSwap($local_tstate_cname, type, value, tb)
536 static CYTHON_INLINE void __Pyx__ExceptionSwap(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb); /*proto*/
537 #else
538 static CYTHON_INLINE void __Pyx_ExceptionSwap(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
539 #endif
540
541 /////////////// SwapException ///////////////
542
543 #if CYTHON_FAST_THREAD_STATE
__Pyx__ExceptionSwap(PyThreadState * tstate,PyObject ** type,PyObject ** value,PyObject ** tb)544 static CYTHON_INLINE void __Pyx__ExceptionSwap(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb) {
545 PyObject *tmp_type, *tmp_value, *tmp_tb;
546
547 #if CYTHON_USE_EXC_INFO_STACK
548 _PyErr_StackItem *exc_info = tstate->exc_info;
549 tmp_type = exc_info->exc_type;
550 tmp_value = exc_info->exc_value;
551 tmp_tb = exc_info->exc_traceback;
552
553 exc_info->exc_type = *type;
554 exc_info->exc_value = *value;
555 exc_info->exc_traceback = *tb;
556 #else
557 tmp_type = tstate->exc_type;
558 tmp_value = tstate->exc_value;
559 tmp_tb = tstate->exc_traceback;
560
561 tstate->exc_type = *type;
562 tstate->exc_value = *value;
563 tstate->exc_traceback = *tb;
564 #endif
565
566 *type = tmp_type;
567 *value = tmp_value;
568 *tb = tmp_tb;
569 }
570
571 #else
572
__Pyx_ExceptionSwap(PyObject ** type,PyObject ** value,PyObject ** tb)573 static CYTHON_INLINE void __Pyx_ExceptionSwap(PyObject **type, PyObject **value, PyObject **tb) {
574 PyObject *tmp_type, *tmp_value, *tmp_tb;
575 PyErr_GetExcInfo(&tmp_type, &tmp_value, &tmp_tb);
576 PyErr_SetExcInfo(*type, *value, *tb);
577 *type = tmp_type;
578 *value = tmp_value;
579 *tb = tmp_tb;
580 }
581 #endif
582
583 /////////////// WriteUnraisableException.proto ///////////////
584
585 static void __Pyx_WriteUnraisable(const char *name, int clineno,
586 int lineno, const char *filename,
587 int full_traceback, int nogil); /*proto*/
588
589 /////////////// WriteUnraisableException ///////////////
590 //@requires: PyErrFetchRestore
591 //@requires: PyThreadStateGet
592
__Pyx_WriteUnraisable(const char * name,CYTHON_UNUSED int clineno,CYTHON_UNUSED int lineno,CYTHON_UNUSED const char * filename,int full_traceback,CYTHON_UNUSED int nogil)593 static void __Pyx_WriteUnraisable(const char *name, CYTHON_UNUSED int clineno,
594 CYTHON_UNUSED int lineno, CYTHON_UNUSED const char *filename,
595 int full_traceback, CYTHON_UNUSED int nogil) {
596 PyObject *old_exc, *old_val, *old_tb;
597 PyObject *ctx;
598 __Pyx_PyThreadState_declare
599 #ifdef WITH_THREAD
600 PyGILState_STATE state;
601 if (nogil)
602 state = PyGILState_Ensure();
603 #ifdef _MSC_VER
604 /* arbitrary, to suppress warning */
605 else state = (PyGILState_STATE)-1;
606 #endif
607 #endif
608 __Pyx_PyThreadState_assign
609 __Pyx_ErrFetch(&old_exc, &old_val, &old_tb);
610 if (full_traceback) {
611 Py_XINCREF(old_exc);
612 Py_XINCREF(old_val);
613 Py_XINCREF(old_tb);
614 __Pyx_ErrRestore(old_exc, old_val, old_tb);
615 PyErr_PrintEx(1);
616 }
617 #if PY_MAJOR_VERSION < 3
618 ctx = PyString_FromString(name);
619 #else
620 ctx = PyUnicode_FromString(name);
621 #endif
622 __Pyx_ErrRestore(old_exc, old_val, old_tb);
623 if (!ctx) {
624 PyErr_WriteUnraisable(Py_None);
625 } else {
626 PyErr_WriteUnraisable(ctx);
627 Py_DECREF(ctx);
628 }
629 #ifdef WITH_THREAD
630 if (nogil)
631 PyGILState_Release(state);
632 #endif
633 }
634
635 /////////////// CLineInTraceback.proto ///////////////
636
637 #ifdef CYTHON_CLINE_IN_TRACEBACK /* 0 or 1 to disable/enable C line display in tracebacks at C compile time */
638 #define __Pyx_CLineForTraceback(tstate, c_line) (((CYTHON_CLINE_IN_TRACEBACK)) ? c_line : 0)
639 #else
640 static int __Pyx_CLineForTraceback(PyThreadState *tstate, int c_line);/*proto*/
641 #endif
642
643 /////////////// CLineInTraceback ///////////////
644 //@requires: ObjectHandling.c::PyObjectGetAttrStr
645 //@requires: ObjectHandling.c::PyDictVersioning
646 //@requires: PyErrFetchRestore
647 //@substitute: naming
648
649 #ifndef CYTHON_CLINE_IN_TRACEBACK
__Pyx_CLineForTraceback(CYTHON_NCP_UNUSED PyThreadState * tstate,int c_line)650 static int __Pyx_CLineForTraceback(CYTHON_NCP_UNUSED PyThreadState *tstate, int c_line) {
651 PyObject *use_cline;
652 PyObject *ptype, *pvalue, *ptraceback;
653 #if CYTHON_COMPILING_IN_CPYTHON
654 PyObject **cython_runtime_dict;
655 #endif
656
657 if (unlikely(!${cython_runtime_cname})) {
658 // Very early error where the runtime module is not set up yet.
659 return c_line;
660 }
661
662 __Pyx_ErrFetchInState(tstate, &ptype, &pvalue, &ptraceback);
663
664 #if CYTHON_COMPILING_IN_CPYTHON
665 cython_runtime_dict = _PyObject_GetDictPtr(${cython_runtime_cname});
666 if (likely(cython_runtime_dict)) {
667 __PYX_PY_DICT_LOOKUP_IF_MODIFIED(
668 use_cline, *cython_runtime_dict,
669 __Pyx_PyDict_GetItemStr(*cython_runtime_dict, PYIDENT("cline_in_traceback")))
670 } else
671 #endif
672 {
673 PyObject *use_cline_obj = __Pyx_PyObject_GetAttrStr(${cython_runtime_cname}, PYIDENT("cline_in_traceback"));
674 if (use_cline_obj) {
675 use_cline = PyObject_Not(use_cline_obj) ? Py_False : Py_True;
676 Py_DECREF(use_cline_obj);
677 } else {
678 PyErr_Clear();
679 use_cline = NULL;
680 }
681 }
682 if (!use_cline) {
683 c_line = 0;
684 PyObject_SetAttr(${cython_runtime_cname}, PYIDENT("cline_in_traceback"), Py_False);
685 }
686 else if (use_cline == Py_False || (use_cline != Py_True && PyObject_Not(use_cline) != 0)) {
687 c_line = 0;
688 }
689 __Pyx_ErrRestoreInState(tstate, ptype, pvalue, ptraceback);
690 return c_line;
691 }
692 #endif
693
694 /////////////// AddTraceback.proto ///////////////
695
696 static void __Pyx_AddTraceback(const char *funcname, int c_line,
697 int py_line, const char *filename); /*proto*/
698
699 /////////////// AddTraceback ///////////////
700 //@requires: ModuleSetupCode.c::CodeObjectCache
701 //@requires: CLineInTraceback
702 //@substitute: naming
703
704 #include "compile.h"
705 #include "frameobject.h"
706 #include "traceback.h"
707
__Pyx_CreateCodeObjectForTraceback(const char * funcname,int c_line,int py_line,const char * filename)708 static PyCodeObject* __Pyx_CreateCodeObjectForTraceback(
709 const char *funcname, int c_line,
710 int py_line, const char *filename) {
711 PyCodeObject *py_code = 0;
712 PyObject *py_srcfile = 0;
713 PyObject *py_funcname = 0;
714
715 #if PY_MAJOR_VERSION < 3
716 py_srcfile = PyString_FromString(filename);
717 #else
718 py_srcfile = PyUnicode_FromString(filename);
719 #endif
720 if (!py_srcfile) goto bad;
721 if (c_line) {
722 #if PY_MAJOR_VERSION < 3
723 py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, $cfilenm_cname, c_line);
724 #else
725 py_funcname = PyUnicode_FromFormat( "%s (%s:%d)", funcname, $cfilenm_cname, c_line);
726 #endif
727 }
728 else {
729 #if PY_MAJOR_VERSION < 3
730 py_funcname = PyString_FromString(funcname);
731 #else
732 py_funcname = PyUnicode_FromString(funcname);
733 #endif
734 }
735 if (!py_funcname) goto bad;
736 py_code = __Pyx_PyCode_New(
737 0, /*int argcount,*/
738 0, /*int kwonlyargcount,*/
739 0, /*int nlocals,*/
740 0, /*int stacksize,*/
741 0, /*int flags,*/
742 $empty_bytes, /*PyObject *code,*/
743 $empty_tuple, /*PyObject *consts,*/
744 $empty_tuple, /*PyObject *names,*/
745 $empty_tuple, /*PyObject *varnames,*/
746 $empty_tuple, /*PyObject *freevars,*/
747 $empty_tuple, /*PyObject *cellvars,*/
748 py_srcfile, /*PyObject *filename,*/
749 py_funcname, /*PyObject *name,*/
750 py_line, /*int firstlineno,*/
751 $empty_bytes /*PyObject *lnotab*/
752 );
753 Py_DECREF(py_srcfile);
754 Py_DECREF(py_funcname);
755 return py_code;
756 bad:
757 Py_XDECREF(py_srcfile);
758 Py_XDECREF(py_funcname);
759 return NULL;
760 }
761
__Pyx_AddTraceback(const char * funcname,int c_line,int py_line,const char * filename)762 static void __Pyx_AddTraceback(const char *funcname, int c_line,
763 int py_line, const char *filename) {
764 PyCodeObject *py_code = 0;
765 PyFrameObject *py_frame = 0;
766 PyThreadState *tstate = __Pyx_PyThreadState_Current;
767
768 if (c_line) {
769 c_line = __Pyx_CLineForTraceback(tstate, c_line);
770 }
771
772 // Negate to avoid collisions between py and c lines.
773 py_code = $global_code_object_cache_find(c_line ? -c_line : py_line);
774 if (!py_code) {
775 py_code = __Pyx_CreateCodeObjectForTraceback(
776 funcname, c_line, py_line, filename);
777 if (!py_code) goto bad;
778 $global_code_object_cache_insert(c_line ? -c_line : py_line, py_code);
779 }
780 py_frame = PyFrame_New(
781 tstate, /*PyThreadState *tstate,*/
782 py_code, /*PyCodeObject *code,*/
783 $moddict_cname, /*PyObject *globals,*/
784 0 /*PyObject *locals*/
785 );
786 if (!py_frame) goto bad;
787 __Pyx_PyFrame_SetLineNumber(py_frame, py_line);
788 PyTraceBack_Here(py_frame);
789 bad:
790 Py_XDECREF(py_code);
791 Py_XDECREF(py_frame);
792 }
793