Exceptions.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. /////////////// GetTopmostException.proto ///////////////
  278. #if CYTHON_USE_EXC_INFO_STACK
  279. static _PyErr_StackItem * __Pyx_PyErr_GetTopmostException(PyThreadState *tstate);
  280. #endif
  281. /////////////// GetTopmostException ///////////////
  282. #if CYTHON_USE_EXC_INFO_STACK
  283. // Copied from errors.c in CPython.
  284. static _PyErr_StackItem *
  285. __Pyx_PyErr_GetTopmostException(PyThreadState *tstate)
  286. {
  287. _PyErr_StackItem *exc_info = tstate->exc_info;
  288. while ((exc_info->exc_type == NULL || exc_info->exc_type == Py_None) &&
  289. exc_info->previous_item != NULL)
  290. {
  291. exc_info = exc_info->previous_item;
  292. }
  293. return exc_info;
  294. }
  295. #endif
  296. /////////////// GetException.proto ///////////////
  297. //@substitute: naming
  298. //@requires: PyThreadStateGet
  299. #if CYTHON_FAST_THREAD_STATE
  300. #define __Pyx_GetException(type, value, tb) __Pyx__GetException($local_tstate_cname, type, value, tb)
  301. static int __Pyx__GetException(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb); /*proto*/
  302. #else
  303. static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
  304. #endif
  305. /////////////// GetException ///////////////
  306. #if CYTHON_FAST_THREAD_STATE
  307. static int __Pyx__GetException(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb)
  308. #else
  309. static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb)
  310. #endif
  311. {
  312. PyObject *local_type, *local_value, *local_tb;
  313. #if CYTHON_FAST_THREAD_STATE
  314. PyObject *tmp_type, *tmp_value, *tmp_tb;
  315. local_type = tstate->curexc_type;
  316. local_value = tstate->curexc_value;
  317. local_tb = tstate->curexc_traceback;
  318. tstate->curexc_type = 0;
  319. tstate->curexc_value = 0;
  320. tstate->curexc_traceback = 0;
  321. #else
  322. PyErr_Fetch(&local_type, &local_value, &local_tb);
  323. #endif
  324. PyErr_NormalizeException(&local_type, &local_value, &local_tb);
  325. #if CYTHON_FAST_THREAD_STATE
  326. if (unlikely(tstate->curexc_type))
  327. #else
  328. if (unlikely(PyErr_Occurred()))
  329. #endif
  330. goto bad;
  331. #if PY_MAJOR_VERSION >= 3
  332. if (local_tb) {
  333. if (unlikely(PyException_SetTraceback(local_value, local_tb) < 0))
  334. goto bad;
  335. }
  336. #endif
  337. // traceback may be NULL for freshly raised exceptions
  338. Py_XINCREF(local_tb);
  339. // exception state may be temporarily empty in parallel loops (race condition)
  340. Py_XINCREF(local_type);
  341. Py_XINCREF(local_value);
  342. *type = local_type;
  343. *value = local_value;
  344. *tb = local_tb;
  345. #if CYTHON_FAST_THREAD_STATE
  346. #if CYTHON_USE_EXC_INFO_STACK
  347. {
  348. _PyErr_StackItem *exc_info = tstate->exc_info;
  349. tmp_type = exc_info->exc_type;
  350. tmp_value = exc_info->exc_value;
  351. tmp_tb = exc_info->exc_traceback;
  352. exc_info->exc_type = local_type;
  353. exc_info->exc_value = local_value;
  354. exc_info->exc_traceback = local_tb;
  355. }
  356. #else
  357. tmp_type = tstate->exc_type;
  358. tmp_value = tstate->exc_value;
  359. tmp_tb = tstate->exc_traceback;
  360. tstate->exc_type = local_type;
  361. tstate->exc_value = local_value;
  362. tstate->exc_traceback = local_tb;
  363. #endif
  364. // Make sure tstate is in a consistent state when we XDECREF
  365. // these objects (DECREF may run arbitrary code).
  366. Py_XDECREF(tmp_type);
  367. Py_XDECREF(tmp_value);
  368. Py_XDECREF(tmp_tb);
  369. #else
  370. PyErr_SetExcInfo(local_type, local_value, local_tb);
  371. #endif
  372. return 0;
  373. bad:
  374. *type = 0;
  375. *value = 0;
  376. *tb = 0;
  377. Py_XDECREF(local_type);
  378. Py_XDECREF(local_value);
  379. Py_XDECREF(local_tb);
  380. return -1;
  381. }
  382. /////////////// ReRaiseException.proto ///////////////
  383. static CYTHON_INLINE void __Pyx_ReraiseException(void); /*proto*/
  384. /////////////// ReRaiseException ///////////////
  385. //@requires: GetTopmostException
  386. static CYTHON_INLINE void __Pyx_ReraiseException(void) {
  387. PyObject *type = NULL, *value = NULL, *tb = NULL;
  388. #if CYTHON_FAST_THREAD_STATE
  389. PyThreadState *tstate = PyThreadState_GET();
  390. #if CYTHON_USE_EXC_INFO_STACK
  391. _PyErr_StackItem *exc_info = __Pyx_PyErr_GetTopmostException(tstate);
  392. type = exc_info->exc_type;
  393. value = exc_info->exc_value;
  394. tb = exc_info->exc_traceback;
  395. #else
  396. type = tstate->exc_type;
  397. value = tstate->exc_value;
  398. tb = tstate->exc_traceback;
  399. #endif
  400. #else
  401. PyErr_GetExcInfo(&type, &value, &tb);
  402. #endif
  403. if (!type || type == Py_None) {
  404. #if !CYTHON_FAST_THREAD_STATE
  405. Py_XDECREF(type);
  406. Py_XDECREF(value);
  407. Py_XDECREF(tb);
  408. #endif
  409. // message copied from Py3
  410. PyErr_SetString(PyExc_RuntimeError,
  411. "No active exception to reraise");
  412. } else {
  413. #if CYTHON_FAST_THREAD_STATE
  414. Py_INCREF(type);
  415. Py_XINCREF(value);
  416. Py_XINCREF(tb);
  417. #endif
  418. PyErr_Restore(type, value, tb);
  419. }
  420. }
  421. /////////////// SaveResetException.proto ///////////////
  422. //@substitute: naming
  423. //@requires: PyThreadStateGet
  424. #if CYTHON_FAST_THREAD_STATE
  425. #define __Pyx_ExceptionSave(type, value, tb) __Pyx__ExceptionSave($local_tstate_cname, type, value, tb)
  426. static CYTHON_INLINE void __Pyx__ExceptionSave(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb); /*proto*/
  427. #define __Pyx_ExceptionReset(type, value, tb) __Pyx__ExceptionReset($local_tstate_cname, type, value, tb)
  428. static CYTHON_INLINE void __Pyx__ExceptionReset(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb); /*proto*/
  429. #else
  430. #define __Pyx_ExceptionSave(type, value, tb) PyErr_GetExcInfo(type, value, tb)
  431. #define __Pyx_ExceptionReset(type, value, tb) PyErr_SetExcInfo(type, value, tb)
  432. #endif
  433. /////////////// SaveResetException ///////////////
  434. //@requires: GetTopmostException
  435. #if CYTHON_FAST_THREAD_STATE
  436. static CYTHON_INLINE void __Pyx__ExceptionSave(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb) {
  437. #if CYTHON_USE_EXC_INFO_STACK
  438. _PyErr_StackItem *exc_info = __Pyx_PyErr_GetTopmostException(tstate);
  439. *type = exc_info->exc_type;
  440. *value = exc_info->exc_value;
  441. *tb = exc_info->exc_traceback;
  442. #else
  443. *type = tstate->exc_type;
  444. *value = tstate->exc_value;
  445. *tb = tstate->exc_traceback;
  446. #endif
  447. Py_XINCREF(*type);
  448. Py_XINCREF(*value);
  449. Py_XINCREF(*tb);
  450. }
  451. static CYTHON_INLINE void __Pyx__ExceptionReset(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb) {
  452. PyObject *tmp_type, *tmp_value, *tmp_tb;
  453. #if CYTHON_USE_EXC_INFO_STACK
  454. _PyErr_StackItem *exc_info = tstate->exc_info;
  455. tmp_type = exc_info->exc_type;
  456. tmp_value = exc_info->exc_value;
  457. tmp_tb = exc_info->exc_traceback;
  458. exc_info->exc_type = type;
  459. exc_info->exc_value = value;
  460. exc_info->exc_traceback = tb;
  461. #else
  462. tmp_type = tstate->exc_type;
  463. tmp_value = tstate->exc_value;
  464. tmp_tb = tstate->exc_traceback;
  465. tstate->exc_type = type;
  466. tstate->exc_value = value;
  467. tstate->exc_traceback = tb;
  468. #endif
  469. Py_XDECREF(tmp_type);
  470. Py_XDECREF(tmp_value);
  471. Py_XDECREF(tmp_tb);
  472. }
  473. #endif
  474. /////////////// SwapException.proto ///////////////
  475. //@substitute: naming
  476. //@requires: PyThreadStateGet
  477. #if CYTHON_FAST_THREAD_STATE
  478. #define __Pyx_ExceptionSwap(type, value, tb) __Pyx__ExceptionSwap($local_tstate_cname, type, value, tb)
  479. static CYTHON_INLINE void __Pyx__ExceptionSwap(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb); /*proto*/
  480. #else
  481. static CYTHON_INLINE void __Pyx_ExceptionSwap(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
  482. #endif
  483. /////////////// SwapException ///////////////
  484. #if CYTHON_FAST_THREAD_STATE
  485. static CYTHON_INLINE void __Pyx__ExceptionSwap(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb) {
  486. PyObject *tmp_type, *tmp_value, *tmp_tb;
  487. #if CYTHON_USE_EXC_INFO_STACK
  488. _PyErr_StackItem *exc_info = tstate->exc_info;
  489. tmp_type = exc_info->exc_type;
  490. tmp_value = exc_info->exc_value;
  491. tmp_tb = exc_info->exc_traceback;
  492. exc_info->exc_type = *type;
  493. exc_info->exc_value = *value;
  494. exc_info->exc_traceback = *tb;
  495. #else
  496. tmp_type = tstate->exc_type;
  497. tmp_value = tstate->exc_value;
  498. tmp_tb = tstate->exc_traceback;
  499. tstate->exc_type = *type;
  500. tstate->exc_value = *value;
  501. tstate->exc_traceback = *tb;
  502. #endif
  503. *type = tmp_type;
  504. *value = tmp_value;
  505. *tb = tmp_tb;
  506. }
  507. #else
  508. static CYTHON_INLINE void __Pyx_ExceptionSwap(PyObject **type, PyObject **value, PyObject **tb) {
  509. PyObject *tmp_type, *tmp_value, *tmp_tb;
  510. PyErr_GetExcInfo(&tmp_type, &tmp_value, &tmp_tb);
  511. PyErr_SetExcInfo(*type, *value, *tb);
  512. *type = tmp_type;
  513. *value = tmp_value;
  514. *tb = tmp_tb;
  515. }
  516. #endif
  517. /////////////// WriteUnraisableException.proto ///////////////
  518. static void __Pyx_WriteUnraisable(const char *name, int clineno,
  519. int lineno, const char *filename,
  520. int full_traceback, int nogil); /*proto*/
  521. /////////////// WriteUnraisableException ///////////////
  522. //@requires: PyErrFetchRestore
  523. //@requires: PyThreadStateGet
  524. static void __Pyx_WriteUnraisable(const char *name, CYTHON_UNUSED int clineno,
  525. CYTHON_UNUSED int lineno, CYTHON_UNUSED const char *filename,
  526. int full_traceback, CYTHON_UNUSED int nogil) {
  527. PyObject *old_exc, *old_val, *old_tb;
  528. PyObject *ctx;
  529. __Pyx_PyThreadState_declare
  530. #ifdef WITH_THREAD
  531. PyGILState_STATE state;
  532. if (nogil)
  533. state = PyGILState_Ensure();
  534. #ifdef _MSC_VER
  535. /* arbitrary, to suppress warning */
  536. else state = (PyGILState_STATE)-1;
  537. #endif
  538. #endif
  539. __Pyx_PyThreadState_assign
  540. __Pyx_ErrFetch(&old_exc, &old_val, &old_tb);
  541. if (full_traceback) {
  542. Py_XINCREF(old_exc);
  543. Py_XINCREF(old_val);
  544. Py_XINCREF(old_tb);
  545. __Pyx_ErrRestore(old_exc, old_val, old_tb);
  546. PyErr_PrintEx(1);
  547. }
  548. #if PY_MAJOR_VERSION < 3
  549. ctx = PyString_FromString(name);
  550. #else
  551. ctx = PyUnicode_FromString(name);
  552. #endif
  553. __Pyx_ErrRestore(old_exc, old_val, old_tb);
  554. if (!ctx) {
  555. PyErr_WriteUnraisable(Py_None);
  556. } else {
  557. PyErr_WriteUnraisable(ctx);
  558. Py_DECREF(ctx);
  559. }
  560. #ifdef WITH_THREAD
  561. if (nogil)
  562. PyGILState_Release(state);
  563. #endif
  564. }
  565. /////////////// CLineInTraceback.proto ///////////////
  566. #ifdef CYTHON_CLINE_IN_TRACEBACK /* 0 or 1 to disable/enable C line display in tracebacks at C compile time */
  567. #define __Pyx_CLineForTraceback(tstate, c_line) (((CYTHON_CLINE_IN_TRACEBACK)) ? c_line : 0)
  568. #else
  569. static int __Pyx_CLineForTraceback(PyThreadState *tstate, int c_line);/*proto*/
  570. #endif
  571. /////////////// CLineInTraceback ///////////////
  572. //@requires: ObjectHandling.c::PyObjectGetAttrStr
  573. //@requires: ObjectHandling.c::PyDictVersioning
  574. //@requires: PyErrFetchRestore
  575. //@substitute: naming
  576. #ifndef CYTHON_CLINE_IN_TRACEBACK
  577. static int __Pyx_CLineForTraceback(CYTHON_NCP_UNUSED PyThreadState *tstate, int c_line) {
  578. PyObject *use_cline;
  579. PyObject *ptype, *pvalue, *ptraceback;
  580. #if CYTHON_COMPILING_IN_CPYTHON
  581. PyObject **cython_runtime_dict;
  582. #endif
  583. if (unlikely(!${cython_runtime_cname})) {
  584. // Very early error where the runtime module is not set up yet.
  585. return c_line;
  586. }
  587. __Pyx_ErrFetchInState(tstate, &ptype, &pvalue, &ptraceback);
  588. #if CYTHON_COMPILING_IN_CPYTHON
  589. cython_runtime_dict = _PyObject_GetDictPtr(${cython_runtime_cname});
  590. if (likely(cython_runtime_dict)) {
  591. __PYX_PY_DICT_LOOKUP_IF_MODIFIED(
  592. use_cline, *cython_runtime_dict,
  593. __Pyx_PyDict_GetItemStr(*cython_runtime_dict, PYIDENT("cline_in_traceback")))
  594. } else
  595. #endif
  596. {
  597. PyObject *use_cline_obj = __Pyx_PyObject_GetAttrStr(${cython_runtime_cname}, PYIDENT("cline_in_traceback"));
  598. if (use_cline_obj) {
  599. use_cline = PyObject_Not(use_cline_obj) ? Py_False : Py_True;
  600. Py_DECREF(use_cline_obj);
  601. } else {
  602. PyErr_Clear();
  603. use_cline = NULL;
  604. }
  605. }
  606. if (!use_cline) {
  607. c_line = 0;
  608. PyObject_SetAttr(${cython_runtime_cname}, PYIDENT("cline_in_traceback"), Py_False);
  609. }
  610. else if (use_cline == Py_False || (use_cline != Py_True && PyObject_Not(use_cline) != 0)) {
  611. c_line = 0;
  612. }
  613. __Pyx_ErrRestoreInState(tstate, ptype, pvalue, ptraceback);
  614. return c_line;
  615. }
  616. #endif
  617. /////////////// AddTraceback.proto ///////////////
  618. static void __Pyx_AddTraceback(const char *funcname, int c_line,
  619. int py_line, const char *filename); /*proto*/
  620. /////////////// AddTraceback ///////////////
  621. //@requires: ModuleSetupCode.c::CodeObjectCache
  622. //@requires: CLineInTraceback
  623. //@substitute: naming
  624. #include "compile.h"
  625. #include "frameobject.h"
  626. #include "traceback.h"
  627. static PyCodeObject* __Pyx_CreateCodeObjectForTraceback(
  628. const char *funcname, int c_line,
  629. int py_line, const char *filename) {
  630. PyCodeObject *py_code = 0;
  631. PyObject *py_srcfile = 0;
  632. PyObject *py_funcname = 0;
  633. #if PY_MAJOR_VERSION < 3
  634. py_srcfile = PyString_FromString(filename);
  635. #else
  636. py_srcfile = PyUnicode_FromString(filename);
  637. #endif
  638. if (!py_srcfile) goto bad;
  639. if (c_line) {
  640. #if PY_MAJOR_VERSION < 3
  641. py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, $cfilenm_cname, c_line);
  642. #else
  643. py_funcname = PyUnicode_FromFormat( "%s (%s:%d)", funcname, $cfilenm_cname, c_line);
  644. #endif
  645. }
  646. else {
  647. #if PY_MAJOR_VERSION < 3
  648. py_funcname = PyString_FromString(funcname);
  649. #else
  650. py_funcname = PyUnicode_FromString(funcname);
  651. #endif
  652. }
  653. if (!py_funcname) goto bad;
  654. py_code = __Pyx_PyCode_New(
  655. 0, /*int argcount,*/
  656. 0, /*int kwonlyargcount,*/
  657. 0, /*int nlocals,*/
  658. 0, /*int stacksize,*/
  659. 0, /*int flags,*/
  660. $empty_bytes, /*PyObject *code,*/
  661. $empty_tuple, /*PyObject *consts,*/
  662. $empty_tuple, /*PyObject *names,*/
  663. $empty_tuple, /*PyObject *varnames,*/
  664. $empty_tuple, /*PyObject *freevars,*/
  665. $empty_tuple, /*PyObject *cellvars,*/
  666. py_srcfile, /*PyObject *filename,*/
  667. py_funcname, /*PyObject *name,*/
  668. py_line, /*int firstlineno,*/
  669. $empty_bytes /*PyObject *lnotab*/
  670. );
  671. Py_DECREF(py_srcfile);
  672. Py_DECREF(py_funcname);
  673. return py_code;
  674. bad:
  675. Py_XDECREF(py_srcfile);
  676. Py_XDECREF(py_funcname);
  677. return NULL;
  678. }
  679. static void __Pyx_AddTraceback(const char *funcname, int c_line,
  680. int py_line, const char *filename) {
  681. PyCodeObject *py_code = 0;
  682. PyFrameObject *py_frame = 0;
  683. PyThreadState *tstate = __Pyx_PyThreadState_Current;
  684. if (c_line) {
  685. c_line = __Pyx_CLineForTraceback(tstate, c_line);
  686. }
  687. // Negate to avoid collisions between py and c lines.
  688. py_code = $global_code_object_cache_find(c_line ? -c_line : py_line);
  689. if (!py_code) {
  690. py_code = __Pyx_CreateCodeObjectForTraceback(
  691. funcname, c_line, py_line, filename);
  692. if (!py_code) goto bad;
  693. $global_code_object_cache_insert(c_line ? -c_line : py_line, py_code);
  694. }
  695. py_frame = PyFrame_New(
  696. tstate, /*PyThreadState *tstate,*/
  697. py_code, /*PyCodeObject *code,*/
  698. $moddict_cname, /*PyObject *globals,*/
  699. 0 /*PyObject *locals*/
  700. );
  701. if (!py_frame) goto bad;
  702. __Pyx_PyFrame_SetLineNumber(py_frame, py_line);
  703. PyTraceBack_Here(py_frame);
  704. bad:
  705. Py_XDECREF(py_code);
  706. Py_XDECREF(py_frame);
  707. }