Exceptions.c 25 KB

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