|
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() |
@@ -337,17 +338,27 @@ PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc) |
337 | 338 | return 0; |
338 | 339 | } |
339 | 340 | if (PyTuple_Check(exc)) { |
340 | | - Py_ssize_t i, n; |
341 | | - n = PyTuple_Size(exc); |
342 | | - for (i = 0; i < n; i++) { |
| 341 | + PyThreadState *tstate = _PyThreadState_GET(); |
| 342 | + if (_Py_ReachedRecursionLimitWithMargin(tstate, 2)) { |
| 343 | + PyObject *exc_value = _PyErr_GetRaisedException(tstate); |
| 344 | + _PyErr_SetString(tstate, PyExc_RecursionError, |
| 345 | + "maximum recursion depth exceeded while " |
| 346 | + "checking exception tuple"); |
| 347 | + PyErr_FormatUnraisable("Exception ignored while " |
| 348 | + "checking exception tuple"); |
| 349 | + _PyErr_SetRaisedException(tstate, exc_value); |
| 350 | + return 0; |
| 351 | + } |
| 352 | + int res = 0; |
| 353 | + Py_ssize_t n = PyTuple_GET_SIZE(exc); |
| 354 | + for (Py_ssize_t i = 0; i < n; i++) { |
343 | 355 | /* Test recursively */ |
344 | | - if (PyErr_GivenExceptionMatches( |
345 | | - err, PyTuple_GET_ITEM(exc, i))) |
346 | | - { |
347 | | - return 1; |
348 | | - } |
| 356 | + if (PyErr_GivenExceptionMatches(err, PyTuple_GET_ITEM(exc, i))) { |
| 357 | + res = 1; |
| 358 | + break; |
| 359 | + } |
349 | 360 | } |
350 | | - return 0; |
| 361 | + return res; |
351 | 362 | } |
352 | 363 | /* err might be an instance, so check its class. */ |
353 | 364 | if (PyExceptionInstance_Check(err)) |
|
0 commit comments