Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b88db87

Browse files
committed
supress coroutine warning when an exception is pending (#27968)
1 parent 32d3742 commit b88db87

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

Objects/genobject.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void
2121
_PyGen_Finalize(PyObject *self)
2222
{
2323
PyGenObject *gen = (PyGenObject *)self;
24-
PyObject *res;
24+
PyObject *res = NULL;
2525
PyObject *error_type, *error_value, *error_traceback;
2626

2727
if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
@@ -33,23 +33,26 @@ _PyGen_Finalize(PyObject *self)
3333

3434
/* If `gen` is a coroutine, and if it was never awaited on,
3535
issue a RuntimeWarning. */
36-
if (gen->gi_code != NULL
37-
&& ((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE
38-
&& gen->gi_frame->f_lasti == -1
39-
&& !PyErr_Occurred()
40-
&& PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
41-
"coroutine '%.50S' was never awaited",
42-
gen->gi_qualname)) {
43-
res = NULL; /* oops, exception */
36+
if (gen->gi_code != NULL &&
37+
((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE &&
38+
gen->gi_frame->f_lasti == -1) {
39+
if (!error_value) {
40+
PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
41+
"coroutine '%.50S' was never awaited",
42+
gen->gi_qualname);
43+
}
4444
}
4545
else {
4646
res = gen_close(gen, NULL);
4747
}
4848

49-
if (res == NULL)
50-
PyErr_WriteUnraisable(self);
51-
else
49+
if (res == NULL) {
50+
if (PyErr_Occurred())
51+
PyErr_WriteUnraisable(self);
52+
}
53+
else {
5254
Py_DECREF(res);
55+
}
5356

5457
/* Restore the saved exception. */
5558
PyErr_Restore(error_type, error_value, error_traceback);

0 commit comments

Comments
 (0)