Skip to content

Commit 2ff758b

Browse files
authored
bpo-45711: [asyncio] Normalize exceptions immediately after Fetch, before they are stored as StackItem, which should be normalized (GH-29890)
1 parent 84ca123 commit 2ff758b

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make :mod:`asyncio` normalize exceptions as soon as they are captured with :c:func:`PyErr_Fetch`, and before they are stored as an exc_info triplet. This brings :mod:`asyncio` in line with the rest of the codebase, where an exc_info triplet is always normalized.

Modules/_asynciomodule.c

+12-7
Original file line numberDiff line numberDiff line change
@@ -2702,6 +2702,11 @@ task_step_impl(TaskObj *task, PyObject *exc)
27022702
if (PyErr_ExceptionMatches(asyncio_CancelledError)) {
27032703
/* CancelledError */
27042704
PyErr_Fetch(&et, &ev, &tb);
2705+
assert(et);
2706+
PyErr_NormalizeException(&et, &ev, &tb);
2707+
if (tb != NULL) {
2708+
PyException_SetTraceback(ev, tb);
2709+
}
27052710

27062711
FutureObj *fut = (FutureObj*)task;
27072712
_PyErr_StackItem *exc_state = &fut->fut_cancelled_exc_state;
@@ -2714,14 +2719,12 @@ task_step_impl(TaskObj *task, PyObject *exc)
27142719

27152720
/* Some other exception; pop it and call Task.set_exception() */
27162721
PyErr_Fetch(&et, &ev, &tb);
2717-
27182722
assert(et);
2719-
if (!ev || !PyObject_TypeCheck(ev, (PyTypeObject *) et)) {
2720-
PyErr_NormalizeException(&et, &ev, &tb);
2721-
}
2723+
PyErr_NormalizeException(&et, &ev, &tb);
27222724
if (tb != NULL) {
27232725
PyException_SetTraceback(ev, tb);
27242726
}
2727+
27252728
o = future_set_exception((FutureObj*)task, ev);
27262729
if (!o) {
27272730
/* An exception in Task.set_exception() */
@@ -2965,7 +2968,7 @@ task_step(TaskObj *task, PyObject *exc)
29652968
PyObject *et, *ev, *tb;
29662969
PyErr_Fetch(&et, &ev, &tb);
29672970
leave_task(task->task_loop, (PyObject*)task);
2968-
_PyErr_ChainExceptions(et, ev, tb);
2971+
_PyErr_ChainExceptions(et, ev, tb); /* Normalizes (et, ev, tb) */
29692972
return NULL;
29702973
}
29712974
else {
@@ -3014,8 +3017,10 @@ task_wakeup(TaskObj *task, PyObject *o)
30143017
}
30153018

30163019
PyErr_Fetch(&et, &ev, &tb);
3017-
if (!ev || !PyObject_TypeCheck(ev, (PyTypeObject *) et)) {
3018-
PyErr_NormalizeException(&et, &ev, &tb);
3020+
assert(et);
3021+
PyErr_NormalizeException(&et, &ev, &tb);
3022+
if (tb != NULL) {
3023+
PyException_SetTraceback(ev, tb);
30193024
}
30203025

30213026
result = task_step(task, ev);

0 commit comments

Comments
 (0)