Skip to content

Commit 04407ce

Browse files
committed
bpo-45711: Change exc_info related APIs to derive type and traceback from the exception instance
1 parent 4dd8219 commit 04407ce

File tree

4 files changed

+79
-28
lines changed

4 files changed

+79
-28
lines changed

Doc/c-api/exceptions.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,6 @@ Querying the error indicator
482482
to an exception that was *already caught*, not to an exception that was
483483
freshly raised. This function steals the references of the arguments.
484484
To clear the exception state, pass ``NULL`` for all three arguments.
485-
For general rules about the three arguments, see :c:func:`PyErr_Restore`.
486485
487486
.. note::
488487
@@ -493,6 +492,12 @@ Querying the error indicator
493492
494493
.. versionadded:: 3.3
495494
495+
.. versionchanged:: 3.11
496+
The ``type`` and ``traceback`` arguments are no longer used, the
497+
interpreter now derives them the exception instance (the ``value``
498+
argument). The function still steals references of all three
499+
arguments.
500+
496501
497502
Signal Handling
498503
===============

Doc/library/sys.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,14 @@ always available.
396396
``(type, value, traceback)``. Their meaning is: *type* gets the type of the
397397
exception being handled (a subclass of :exc:`BaseException`); *value* gets
398398
the exception instance (an instance of the exception type); *traceback* gets
399-
a :ref:`traceback object <traceback-objects>` which encapsulates the call
400-
stack at the point where the exception originally occurred.
401-
399+
a :ref:`traceback object <traceback-objects>` which typically encapsulates
400+
the call stack at the point where the exception last occurred.
401+
402+
.. versionchanged:: 3.11
403+
The ``type`` and ``traceback`` fields are now derived from the ``value``
404+
(the exception instance), so when an exception is modified while it is
405+
being handled, the changes are reflected in the results of subsequent
406+
calls to :func:`exc_info`.
402407

403408
.. data:: exec_prefix
404409

Doc/whatsnew/3.11.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,14 @@ sqlite3
258258
threading mode the underlying SQLite library has been compiled with.
259259
(Contributed by Erlend E. Aasland in :issue:`45613`.)
260260

261+
sys
262+
---
263+
264+
* :func:`sys.exc_info` now derives the ``type`` and ``traceback`` fields
265+
from the ``value`` (the exception instance), so when an exception is
266+
modified while it is being handled, the changes are reflected in
267+
the results of subsequent calls to :func:`exc_info`.
268+
(Contributed by Irit Katriel in :issue:`45711`.)
261269

262270
threading
263271
---------
@@ -572,6 +580,17 @@ New Features
572580
suspend and resume tracing and profiling.
573581
(Contributed by Victor Stinner in :issue:`43760`.)
574582

583+
* :c:func:`PyErr_SetExcInfo()` no longer uses the ``type`` and ``traceback``
584+
arguments, the interpreter now derives those values from the exception
585+
instance (the ``value`` argument). The function still steals references
586+
of all three arguments.
587+
(Contributed by Irit Katriel in :issue:`45711`.)
588+
589+
* :c:func:`PyErr_GetExcInfo()` now derives the ``type`` and ``traceback``
590+
fields of the result from the exception instance (the ``value`` field).
591+
(Contributed by Irit Katriel in :issue:`45711`.)
592+
593+
575594
Porting to Python 3.11
576595
----------------------
577596

Python/errors.c

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -470,25 +470,43 @@ PyErr_Clear(void)
470470
_PyErr_Clear(tstate);
471471
}
472472

473+
static PyObject*
474+
get_exc_type(PyObject *exc_value) /* returns a borrowed ref */
475+
{
476+
if (exc_value == NULL || exc_value == Py_None) {
477+
return exc_value;
478+
}
479+
else {
480+
assert(PyExceptionInstance_Check(exc_value));
481+
PyObject *type = PyExceptionInstance_Class(exc_value);
482+
assert(type != NULL);
483+
return type;
484+
}
485+
}
486+
487+
static PyObject*
488+
get_exc_traceback(PyObject *exc_value) /* returns a borrowed ref */
489+
{
490+
if (exc_value == NULL || exc_value == Py_None) {
491+
return Py_None;
492+
}
493+
else {
494+
assert(PyExceptionInstance_Check(exc_value));
495+
PyObject *tb = PyException_GetTraceback(exc_value);
496+
Py_XDECREF(tb);
497+
return tb ? tb : Py_None;
498+
}
499+
}
473500

474501
void
475502
_PyErr_GetExcInfo(PyThreadState *tstate,
476503
PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
477504
{
478505
_PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
479506

507+
*p_type = get_exc_type(exc_info->exc_value);
480508
*p_value = exc_info->exc_value;
481-
*p_traceback = exc_info->exc_traceback;
482-
483-
if (*p_value == NULL || *p_value == Py_None) {
484-
assert(exc_info->exc_type == NULL || exc_info->exc_type == Py_None);
485-
*p_type = Py_None;
486-
}
487-
else {
488-
assert(PyExceptionInstance_Check(*p_value));
489-
assert(exc_info->exc_type == PyExceptionInstance_Class(*p_value));
490-
*p_type = PyExceptionInstance_Class(*p_value);
491-
}
509+
*p_traceback = get_exc_traceback(exc_info->exc_value);
492510

493511
Py_XINCREF(*p_type);
494512
Py_XINCREF(*p_value);
@@ -513,9 +531,16 @@ PyErr_SetExcInfo(PyObject *p_type, PyObject *p_value, PyObject *p_traceback)
513531
oldvalue = tstate->exc_info->exc_value;
514532
oldtraceback = tstate->exc_info->exc_traceback;
515533

516-
tstate->exc_info->exc_type = p_type;
534+
535+
tstate->exc_info->exc_type = get_exc_type(p_value);
536+
Py_XINCREF(tstate->exc_info->exc_type);
517537
tstate->exc_info->exc_value = p_value;
518-
tstate->exc_info->exc_traceback = p_traceback;
538+
tstate->exc_info->exc_traceback = get_exc_traceback(p_value);
539+
Py_XINCREF(tstate->exc_info->exc_traceback);
540+
541+
/* These args are no longer used, but we still need to steal a ref */
542+
Py_XDECREF(p_type);
543+
Py_XDECREF(p_traceback);
519544

520545
Py_XDECREF(oldtype);
521546
Py_XDECREF(oldvalue);
@@ -527,22 +552,19 @@ PyObject*
527552
_PyErr_StackItemToExcInfoTuple(_PyErr_StackItem *err_info)
528553
{
529554
PyObject *exc_value = err_info->exc_value;
530-
if (exc_value == NULL) {
531-
exc_value = Py_None;
532-
}
533555

534-
assert(exc_value == Py_None || PyExceptionInstance_Check(exc_value));
556+
assert(exc_value == NULL ||
557+
exc_value == Py_None ||
558+
PyExceptionInstance_Check(exc_value));
535559

536-
PyObject *exc_type = PyExceptionInstance_Check(exc_value) ?
537-
PyExceptionInstance_Class(exc_value) :
538-
Py_None;
560+
PyObject *exc_type = get_exc_type(exc_value);
561+
PyObject *exc_traceback = get_exc_traceback(exc_value);
539562

540563
return Py_BuildValue(
541564
"(OOO)",
542-
exc_type,
543-
exc_value,
544-
err_info->exc_traceback != NULL ?
545-
err_info->exc_traceback : Py_None);
565+
exc_type ? exc_type : Py_None,
566+
exc_value ? exc_value : Py_None,
567+
exc_traceback ? exc_traceback : Py_None);
546568
}
547569

548570

0 commit comments

Comments
 (0)