|
4 | 4 | #include "Python.h" |
5 | 5 | #include "pycore_audit.h" // _PySys_Audit() |
6 | 6 | #include "pycore_call.h" // _PyObject_CallNoArgs() |
| 7 | +#include "pycore_ceval.h" // _Py_ReachedRecursionLimitWithMargin() |
7 | 8 | #include "pycore_fileutils.h" // _PyFile_Flush |
8 | 9 | #include "pycore_initconfig.h" // _PyStatus_ERR() |
9 | 10 | #include "pycore_pyerrors.h" // _PyErr_Format() |
@@ -335,17 +336,27 @@ PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc) |
335 | 336 | return 0; |
336 | 337 | } |
337 | 338 | if (PyTuple_Check(exc)) { |
338 | | - Py_ssize_t i, n; |
339 | | - n = PyTuple_Size(exc); |
340 | | - for (i = 0; i < n; i++) { |
| 339 | + PyThreadState *tstate = _PyThreadState_GET(); |
| 340 | + if (_Py_ReachedRecursionLimitWithMargin(tstate, 2)) { |
| 341 | + PyObject *exc_value = _PyErr_GetRaisedException(tstate); |
| 342 | + _PyErr_SetString(tstate, PyExc_RecursionError, |
| 343 | + "maximum recursion depth exceeded while " |
| 344 | + "checking exception tuple"); |
| 345 | + PyErr_FormatUnraisable("Exception ignored while " |
| 346 | + "checking exception tuple"); |
| 347 | + _PyErr_SetRaisedException(tstate, exc_value); |
| 348 | + return 0; |
| 349 | + } |
| 350 | + int res = 0; |
| 351 | + Py_ssize_t n = PyTuple_GET_SIZE(exc); |
| 352 | + for (Py_ssize_t i = 0; i < n; i++) { |
341 | 353 | /* Test recursively */ |
342 | | - if (PyErr_GivenExceptionMatches( |
343 | | - err, PyTuple_GET_ITEM(exc, i))) |
344 | | - { |
345 | | - return 1; |
346 | | - } |
| 354 | + if (PyErr_GivenExceptionMatches(err, PyTuple_GET_ITEM(exc, i))) { |
| 355 | + res = 1; |
| 356 | + break; |
| 357 | + } |
347 | 358 | } |
348 | | - return 0; |
| 359 | + return res; |
349 | 360 | } |
350 | 361 | /* err might be an instance, so check its class. */ |
351 | 362 | if (PyExceptionInstance_Check(err)) |
|
0 commit comments