Skip to content

Commit 9716caa

Browse files
committed
gh-128799: Add frame of except* to traceback when wrapping a naked exception
1 parent d4544cb commit 9716caa

File tree

6 files changed

+17
-6
lines changed

6 files changed

+17
-6
lines changed

Include/internal/pycore_ceval.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ PyAPI_DATA(const size_t) _Py_FunctionAttributeOffsets[];
264264

265265
PyAPI_FUNC(int) _PyEval_CheckExceptStarTypeValid(PyThreadState *tstate, PyObject* right);
266266
PyAPI_FUNC(int) _PyEval_CheckExceptTypeValid(PyThreadState *tstate, PyObject* right);
267-
PyAPI_FUNC(int) _PyEval_ExceptionGroupMatch(PyObject* exc_value, PyObject *match_type, PyObject **match, PyObject **rest);
267+
PyAPI_FUNC(int) _PyEval_ExceptionGroupMatch(_PyInterpreterFrame *, PyObject* exc_value, PyObject *match_type, PyObject **match, PyObject **rest);
268268
PyAPI_FUNC(void) _PyEval_FormatAwaitableError(PyThreadState *tstate, PyTypeObject *type, int oparg);
269269
PyAPI_FUNC(void) _PyEval_FormatExcCheckArg(PyThreadState *tstate, PyObject *exc, const char *format_str, PyObject *obj);
270270
PyAPI_FUNC(void) _PyEval_FormatExcUnbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add frame of `except*` to traceback when it wraps a naked exception.

Python/bytecodes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2717,7 +2717,7 @@ dummy_func(
27172717

27182718
PyObject *match_o = NULL;
27192719
PyObject *rest_o = NULL;
2720-
int res = _PyEval_ExceptionGroupMatch(exc_value, match_type,
2720+
int res = _PyEval_ExceptionGroupMatch(frame, exc_value, match_type,
27212721
&match_o, &rest_o);
27222722
DECREF_INPUTS();
27232723
ERROR_IF(res < 0, error);

Python/ceval.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "pycore_range.h" // _PyRangeIterObject
2828
#include "pycore_setobject.h" // _PySet_Update()
2929
#include "pycore_sliceobject.h" // _PyBuildSlice_ConsumeRefs
30+
#include "pycore_traceback.h" // _PyTraceBack_FromFrame
3031
#include "pycore_tuple.h" // _PyTuple_ITEMS()
3132
#include "pycore_uop_ids.h" // Uops
3233
#include "pycore_pyerrors.h"
@@ -2094,8 +2095,8 @@ do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
20942095
*/
20952096

20962097
int
2097-
_PyEval_ExceptionGroupMatch(PyObject* exc_value, PyObject *match_type,
2098-
PyObject **match, PyObject **rest)
2098+
_PyEval_ExceptionGroupMatch(_PyInterpreterFrame *frame, PyObject* exc_value,
2099+
PyObject *match_type, PyObject **match, PyObject **rest)
20992100
{
21002101
if (Py_IsNone(exc_value)) {
21012102
*match = Py_NewRef(Py_None);
@@ -2121,6 +2122,15 @@ _PyEval_ExceptionGroupMatch(PyObject* exc_value, PyObject *match_type,
21212122
if (wrapped == NULL) {
21222123
return -1;
21232124
}
2125+
PyFrameObject *f = _PyFrame_GetFrameObject(frame);
2126+
if (f != NULL) {
2127+
PyObject *tb = _PyTraceBack_FromFrame(NULL, f);
2128+
if (tb == NULL) {
2129+
return -1;
2130+
}
2131+
PyException_SetTraceback(wrapped, tb);
2132+
Py_DECREF(tb);
2133+
}
21242134
*match = wrapped;
21252135
}
21262136
*rest = Py_NewRef(Py_None);

Python/executor_cases.c.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)