]> granicus.if.org Git - python/commitdiff
Fix wrapping into StopIteration of return values in generators and coroutines (#644...
authorYury Selivanov <yselivanov@gmail.com>
Sun, 12 Mar 2017 21:04:06 +0000 (17:04 -0400)
committerGitHub <noreply@github.com>
Sun, 12 Mar 2017 21:04:06 +0000 (17:04 -0400)
Lib/test/test_coroutines.py
Misc/NEWS
Objects/genobject.c

index b4c7b5be6e208c79d040f3a5034d217cd2edf1ae..a69583b5f95ae69a1763e79acd9fdcd721f15bce 100644 (file)
@@ -1103,6 +1103,21 @@ class CoroutineTest(unittest.TestCase):
                                     "coroutine is being awaited already"):
             waiter(coro).send(None)
 
+    def test_await_16(self):
+        # See https://bugs.python.org/issue29600 for details.
+
+        async def f():
+            return ValueError()
+
+        async def g():
+            try:
+                raise KeyError
+            except:
+                return await f()
+
+        _, result = run_async(g())
+        self.assertIsNone(result.__context__)
+
     def test_with_1(self):
         class Manager:
             def __init__(self, name):
index eef4dfa335228655f65ea1887eaa1278211a62da..504b977bc87b4b02af68b507869dc5cfbe95f51b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ What's New in Python 3.6.1 final?
 Core and Builtins
 -----------------
 
+- bpo-29600: Fix wrapping coroutine return values in StopIteration.
+
 - bpo-29723: The ``sys.path[0]`` initialization change for bpo-29139 caused a
   regression by revealing an inconsistency in how sys.path is initialized when
   executing ``__main__`` from a zipfile, directory, or other import location.
index 2680ab0e129da4bf8966156987032db5cc78400e..1c29e296afe2a8df848ad6c5787be9ec98d65360 100644 (file)
@@ -575,8 +575,7 @@ _PyGen_SetStopIterationValue(PyObject *value)
     PyObject *e;
 
     if (value == NULL ||
-        (!PyTuple_Check(value) &&
-         !PyObject_TypeCheck(value, (PyTypeObject *) PyExc_StopIteration)))
+        (!PyTuple_Check(value) && !PyExceptionInstance_Check(value)))
     {
         /* Delay exception instantiation if we can */
         PyErr_SetObject(PyExc_StopIteration, value);