gh-156204: Guard recursion in PyErr_GivenExceptionMatches - #156205
gh-156204: Guard recursion in PyErr_GivenExceptionMatches#156205BHUVANSH855 wants to merge 4 commits into
Conversation
| ctypes = import_module('ctypes') | ||
| lib = ctypes.pythonapi | ||
| lib.PyErr_GivenExceptionMatches.argtypes = [ctypes.py_object, ctypes.py_object] | ||
| lib.PyErr_GivenExceptionMatches.restype = ctypes.c_int |
There was a problem hiding this comment.
Can the test be written without ctypes?
There was a problem hiding this comment.
Switched to _testcapi. Pure Python can't reach it: _PyEval_CheckExceptTypeValid() and get_matcher_type() both reject subtuples one level deep, so nested tuples only arrive via the C API.
Also found the old test asserted assertEqual(res, 0) - true with and without the fix. It now asserts RecursionError via support.run_with_limited_c_stack(); verified it segfaults on reverted errors.c.
One question: _Py_CheckRecursiveCall() replaces the current exception rather than chaining it, which matters for PyErr_ExceptionMatches() callers. Happy to use _Py_ReachedRecursionLimit() instead if you'd prefer.
Documentation build overview
|
Fixes issue gh-156204.
PyErr_GivenExceptionMatchesinPython/errors.crecursively unpacks tuple targets without recursion checks, causing native C stack exhaustion and aSIGSEGVwhen given deeply nested exception tuples.This change:
PyErr_GivenExceptionMatchesviaPy_EnterRecursiveCall()andPy_LeaveRecursiveCall()._testcapi.err_givenexceptionmatches()helper and a regression test inLib/test/test_exceptions.py.blurbNEWS entry.Verified: with
Python/errors.creverted to the pre-fix version the new test segfaults; with the fix it passes.