Skip to content

Commit f7bf2cc

Browse files
committed
pythongh-128799: Add frame of except* to traceback when wrapping a naked exception (python#128971)
(cherry picked from commit c39ae89)
1 parent e1900db commit f7bf2cc

File tree

7 files changed

+44
-6
lines changed

7 files changed

+44
-6
lines changed

Include/internal/pycore_ceval.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ PyAPI_DATA(const conversion_func) _PyEval_ConversionFuncs[];
247247

248248
PyAPI_FUNC(int) _PyEval_CheckExceptStarTypeValid(PyThreadState *tstate, PyObject* right);
249249
PyAPI_FUNC(int) _PyEval_CheckExceptTypeValid(PyThreadState *tstate, PyObject* right);
250-
PyAPI_FUNC(int) _PyEval_ExceptionGroupMatch(PyObject* exc_value, PyObject *match_type, PyObject **match, PyObject **rest);
250+
PyAPI_FUNC(int) _PyEval_ExceptionGroupMatch(_PyInterpreterFrame *, PyObject* exc_value, PyObject *match_type, PyObject **match, PyObject **rest);
251251
PyAPI_FUNC(void) _PyEval_FormatAwaitableError(PyThreadState *tstate, PyTypeObject *type, int oparg);
252252
PyAPI_FUNC(void) _PyEval_FormatExcCheckArg(PyThreadState *tstate, PyObject *exc, const char *format_str, PyObject *obj);
253253
PyAPI_FUNC(void) _PyEval_FormatExcUnbound(PyThreadState *tstate, PyCodeObject *co, int oparg);

Lib/test/test_traceback.py

+27
Original file line numberDiff line numberDiff line change
@@ -2938,6 +2938,33 @@ def exc():
29382938
report = self.get_report(exc)
29392939
self.assertEqual(report, expected)
29402940

2941+
def test_exception_group_wrapped_naked(self):
2942+
# See gh-128799
2943+
2944+
def exc():
2945+
try:
2946+
raise Exception(42)
2947+
except* Exception as e:
2948+
raise
2949+
2950+
expected = (f' + Exception Group Traceback (most recent call last):\n'
2951+
f' | File "{__file__}", line {self.callable_line}, in get_exception\n'
2952+
f' | exception_or_callable()\n'
2953+
f' | ~~~~~~~~~~~~~~~~~~~~~^^\n'
2954+
f' | File "{__file__}", line {exc.__code__.co_firstlineno + 3}, in exc\n'
2955+
f' | except* Exception as e:\n'
2956+
f' | raise\n'
2957+
f' | ExceptionGroup: (1 sub-exception)\n'
2958+
f' +-+---------------- 1 ----------------\n'
2959+
f' | Traceback (most recent call last):\n'
2960+
f' | File "{__file__}", line {exc.__code__.co_firstlineno + 2}, in exc\n'
2961+
f' | raise Exception(42)\n'
2962+
f' | Exception: 42\n'
2963+
f' +------------------------------------\n')
2964+
2965+
report = self.get_report(exc)
2966+
self.assertEqual(report, expected)
2967+
29412968
def test_KeyboardInterrupt_at_first_line_of_frame(self):
29422969
# see GH-93249
29432970
def f():
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2340,7 +2340,7 @@ dummy_func(
23402340

23412341
match = NULL;
23422342
rest = NULL;
2343-
int res = _PyEval_ExceptionGroupMatch(exc_value, match_type,
2343+
int res = _PyEval_ExceptionGroupMatch(frame, exc_value, match_type,
23442344
&match, &rest);
23452345
DECREF_INPUTS();
23462346
ERROR_IF(res < 0, error);

Python/ceval.c

+12-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "pycore_setobject.h" // _PySet_Update()
2828
#include "pycore_sliceobject.h" // _PyBuildSlice_ConsumeRefs
2929
#include "pycore_sysmodule.h" // _PySys_Audit()
30+
#include "pycore_traceback.h" // _PyTraceBack_FromFrame
3031
#include "pycore_tuple.h" // _PyTuple_ITEMS()
3132
#include "pycore_typeobject.h" // _PySuper_Lookup()
3233
#include "pycore_uop_ids.h" // Uops
@@ -1991,8 +1992,8 @@ do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
19911992
*/
19921993

19931994
int
1994-
_PyEval_ExceptionGroupMatch(PyObject* exc_value, PyObject *match_type,
1995-
PyObject **match, PyObject **rest)
1995+
_PyEval_ExceptionGroupMatch(_PyInterpreterFrame *frame, PyObject* exc_value,
1996+
PyObject *match_type, PyObject **match, PyObject **rest)
19961997
{
19971998
if (Py_IsNone(exc_value)) {
19981999
*match = Py_NewRef(Py_None);
@@ -2018,6 +2019,15 @@ _PyEval_ExceptionGroupMatch(PyObject* exc_value, PyObject *match_type,
20182019
if (wrapped == NULL) {
20192020
return -1;
20202021
}
2022+
PyFrameObject *f = _PyFrame_GetFrameObject(frame);
2023+
if (f != NULL) {
2024+
PyObject *tb = _PyTraceBack_FromFrame(NULL, f);
2025+
if (tb == NULL) {
2026+
return -1;
2027+
}
2028+
PyException_SetTraceback(wrapped, tb);
2029+
Py_DECREF(tb);
2030+
}
20212031
*match = wrapped;
20222032
}
20232033
*rest = Py_NewRef(Py_None);

Python/executor_cases.c.h

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)