From a7dfe0fd07c58e7ba4085d9d47a00793d1395e28 Mon Sep 17 00:00:00 2001 From: alperyoney Date: Wed, 16 Apr 2025 13:45:10 -0700 Subject: [PATCH] gh-131173: Improve exception handling during take_ownership processing Save and restore exceptions during take_ownership processing to preserve exceptions currently being raised. --- Python/frame.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Python/frame.c b/Python/frame.c index 558f92055bbfc1..ce216797e47cda 100644 --- a/Python/frame.c +++ b/Python/frame.c @@ -69,6 +69,7 @@ take_ownership(PyFrameObject *f, _PyInterpreterFrame *frame) _PyInterpreterFrame *prev = _PyFrame_GetFirstComplete(frame->previous); if (prev) { assert(prev->owner < FRAME_OWNED_BY_INTERPRETER); + PyObject *exc = PyErr_GetRaisedException(); /* Link PyFrameObjects.f_back and remove link through _PyInterpreterFrame.previous */ PyFrameObject *back = _PyFrame_GetFrameObject(prev); if (back == NULL) { @@ -80,6 +81,7 @@ take_ownership(PyFrameObject *f, _PyInterpreterFrame *frame) else { f->f_back = (PyFrameObject *)Py_NewRef(back); } + PyErr_SetRaisedException(exc); } if (!_PyObject_GC_IS_TRACKED((PyObject *)f)) { _PyObject_GC_TRACK((PyObject *)f);