[Python-checkins] cpython (merge 3.5 -> default): merge 3.5 (closes #27811)

benjamin.peterson python-checkins at python.org
Mon Sep 5 13:18:18 EDT 2016


https://hg.python.org/cpython/rev/1b14dbe9e98b
changeset:   103041:1b14dbe9e98b
parent:      103037:ffc915a55a72
parent:      103040:4d531711cbc7
user:        Benjamin Peterson <benjamin at python.org>
date:        Mon Sep 05 10:16:31 2016 -0700
summary:
  merge 3.5 (closes #27811)

files:
  Lib/test/test_coroutines.py |   8 +++++++
  Misc/NEWS                   |   3 ++
  Objects/genobject.c         |  27 ++++++++++++------------
  3 files changed, 25 insertions(+), 13 deletions(-)


diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py
--- a/Lib/test/test_coroutines.py
+++ b/Lib/test/test_coroutines.py
@@ -1565,6 +1565,14 @@
         finally:
             aw.close()
 
+    def test_fatal_coro_warning(self):
+        # Issue 27811
+        async def func(): pass
+        with warnings.catch_warnings():
+            warnings.filterwarnings("error")
+            func()
+            support.gc_collect()
+
 
 class CoroAsyncIOCompatTest(unittest.TestCase):
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,9 @@
 - Issue #27506: Support passing the bytes/bytearray.translate() "delete"
   argument by keyword.
 
+- Issue #27811: Fix a crash when a coroutine that has not been awaited is
+  finalized with warnings-as-errors enabled.
+
 - Issue #27587: Fix another issue found by PVS-Studio: Null pointer check
   after use of 'def' in _PyState_AddModule().
   Initial patch by Christian Heimes.
diff --git a/Objects/genobject.c b/Objects/genobject.c
--- a/Objects/genobject.c
+++ b/Objects/genobject.c
@@ -24,18 +24,6 @@
     PyObject *res;
     PyObject *error_type, *error_value, *error_traceback;
 
-    /* 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 != NULL
-            && gen->gi_frame->f_lasti == -1
-            && !PyErr_Occurred()
-            && PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
-                                "coroutine '%.50S' was never awaited",
-                                gen->gi_qualname))
-        return;
-
     if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
         /* Generator isn't paused, so no need to close */
         return;
@@ -43,7 +31,20 @@
     /* Save the current exception, if any. */
     PyErr_Fetch(&error_type, &error_value, &error_traceback);
 
-    res = gen_close(gen, NULL);
+    /* 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 */
+    }
+    else {
+        res = gen_close(gen, NULL);
+    }
 
     if (res == NULL)
         PyErr_WriteUnraisable(self);

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list