Skip to content

Commit 05af230

Browse files
authored
Use traceback from exception instead of exc_info in reraise
1 parent 7bc139f commit 05af230

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

Doc/reference/simple_stmts.rst

+6
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,12 @@ and information about handling exceptions is in section :ref:`try`.
655655
The ``__suppress_context__`` attribute to suppress automatic display of the
656656
exception context.
657657

658+
.. versionchanged:: 3.11
659+
If the traceback of the active exception is modified in an :keyword:`except`
660+
clause, a subsequent ``raise`` statement re-raises the exception with the
661+
modified traceback. Previously, the exception was re-raised with the
662+
traceback it had when it was caught.
663+
658664
.. _break:
659665

660666
The :keyword:`!break` statement

Doc/whatsnew/3.11.rst

+6
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ Other CPython Implementation Changes
181181
hash-based pyc files now use ``siphash13``, too.
182182
(Contributed by Inada Naoki in :issue:`29410`.)
183183

184+
* When an active exception is re-raised by a :keyword:`raise` statement with no parameters,
185+
the traceback attached to this exception is now always ``sys.exc_info()[1].__traceback__``.
186+
This means that changes made to the traceback in the current :keyword:`except` clause are
187+
reflected in the re-raised exception.
188+
(Contributed by Irit Katriel in :issue:`45711`.)
189+
184190
New Modules
185191
===========
186192

Python/ceval.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -5920,20 +5920,17 @@ do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
59205920
if (exc == NULL) {
59215921
/* Reraise */
59225922
_PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
5923-
PyObject *tb;
5924-
type = exc_info->exc_type;
59255923
value = exc_info->exc_value;
5926-
tb = exc_info->exc_traceback;
5927-
assert(((Py_IsNone(value) || value == NULL)) ==
5928-
((Py_IsNone(type) || type == NULL)));
59295924
if (Py_IsNone(value) || value == NULL) {
59305925
_PyErr_SetString(tstate, PyExc_RuntimeError,
59315926
"No active exception to reraise");
59325927
return 0;
59335928
}
5929+
assert(PyExceptionInstance_Check(value));
5930+
type = PyExceptionInstance_Class(value);
59345931
Py_XINCREF(type);
59355932
Py_XINCREF(value);
5936-
Py_XINCREF(tb);
5933+
PyObject *tb = PyException_GetTraceback(value); /* new ref */
59375934
_PyErr_Restore(tstate, type, value, tb);
59385935
return 1;
59395936
}

0 commit comments

Comments
 (0)