Exceptions.c 28 KB

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