]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
supress coroutine warning when an exception is pending (#27968)
authorBenjamin Peterson <benjamin@python.org>
Wed, 7 Sep 2016 15:46:59 +0000 (08:46 -0700)
committerBenjamin Peterson <benjamin@python.org>
Wed, 7 Sep 2016 15:46:59 +0000 (08:46 -0700)
Objects/genobject.c

index 01c59c24a968c717806fe18f23b2975c22fe226b..9172e6a01025948977c14626b89820eadf2ff83a 100644 (file)
@@ -21,7 +21,7 @@ void
 _PyGen_Finalize(PyObject *self)
 {
     PyGenObject *gen = (PyGenObject *)self;
-    PyObject *res;
+    PyObject *res = NULL;
     PyObject *error_type, *error_value, *error_traceback;
 
     if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
@@ -33,23 +33,26 @@ _PyGen_Finalize(PyObject *self)
 
     /* If `gen` is a coroutine, and if it was never awaited on,
        issue a RuntimeWarning. */
-    if (gen->gi_code != NULL
-            && ((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE
-            && gen->gi_frame->f_lasti == -1
-            && !PyErr_Occurred()
-            && PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
-                                "coroutine '%.50S' was never awaited",
-                                gen->gi_qualname)) {
-        res = NULL;  /* oops, exception */
+    if (gen->gi_code != NULL &&
+        ((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE &&
+        gen->gi_frame->f_lasti == -1) {
+        if (!error_value) {
+            PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
+                             "coroutine '%.50S' was never awaited",
+                             gen->gi_qualname);
+        }
     }
     else {
         res = gen_close(gen, NULL);
     }
 
-    if (res == NULL)
-        PyErr_WriteUnraisable(self);
-    else
+    if (res == NULL) {
+        if (PyErr_Occurred())
+            PyErr_WriteUnraisable(self);
+    }
+    else {
         Py_DECREF(res);
+    }
 
     /* Restore the saved exception. */
     PyErr_Restore(error_type, error_value, error_traceback);