]> granicus.if.org Git - python/commitdiff
asyncio.tasks: Fix CoroWrapper to workaround yield-from bug in CPython < 3.4.1
authorYury Selivanov <yselivanov@sprymix.com>
Tue, 15 Apr 2014 02:21:52 +0000 (22:21 -0400)
committerYury Selivanov <yselivanov@sprymix.com>
Tue, 15 Apr 2014 02:21:52 +0000 (22:21 -0400)
Closes issue #21209.

Lib/asyncio/tasks.py
Lib/test/test_asyncio/test_tasks.py
Misc/NEWS

index 153f731a76d156d5e42959bea8ed247c072558f4..0366da35fee140314ff1ec757d4fd7beac1d43b6 100644 (file)
@@ -49,7 +49,10 @@ class CoroWrapper:
     def __next__(self):
         return next(self.gen)
 
-    def send(self, value):
+    def send(self, *value):
+        # We use `*value` because of a bug in CPythons prior
+        # to 3.4.1. See issue #21209 and test_yield_from_corowrapper
+        # for details.  This workaround should be removed in 3.5.0.
         return self.gen.send(value)
 
     def throw(self, exc):
index ced34312f79751de8b76d164bae18710c4085556..45de8acc1a3bd90816fb3112ae30fd8584ae2092 100644 (file)
@@ -1386,6 +1386,31 @@ class TaskTests(unittest.TestCase):
         self.assertRaises(ValueError, self.loop.run_until_complete,
             asyncio.wait([], loop=self.loop))
 
+    def test_yield_from_corowrapper(self):
+        old_debug = asyncio.tasks._DEBUG
+        asyncio.tasks._DEBUG = True
+        try:
+            @asyncio.coroutine
+            def t1():
+                return (yield from t2())
+
+            @asyncio.coroutine
+            def t2():
+                f = asyncio.Future(loop=self.loop)
+                asyncio.Task(t3(f), loop=self.loop)
+                return (yield from f)
+
+            @asyncio.coroutine
+            def t3(f):
+                f.set_result((1, 2, 3))
+
+            task = asyncio.Task(t1(), loop=self.loop)
+            val = self.loop.run_until_complete(task)
+            self.assertEqual(val, (1, 2, 3))
+        finally:
+            asyncio.tasks._DEBUG = old_debug
+
+
 class GatherTestsBase:
 
     def setUp(self):
index 8519b6196ce6ab0f66513a1edeb605f4f27a203b..9cdc6059c15ffee36155b77444833c7f3cf91b22 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -147,6 +147,9 @@ Library
   positional-or-keyword arguments passed as keyword arguments become
   keyword-only.
 
+- Issue #21209: Fix asyncio.tasks.CoroWrapper to workaround a bug
+  in yield-from implementation in CPythons prior to 3.4.1.
+
 IDLE
 ----