[Python-checkins] cpython (merge 3.4 -> 3.5): asyncio: Error if awaiting in parallel on the same coroutine

yury.selivanov python-checkins at python.org
Wed Nov 18 12:40:51 EST 2015


https://hg.python.org/cpython/rev/6875b0bd3cd0
changeset:   99202:6875b0bd3cd0
branch:      3.5
parent:      99198:d64bb25cf797
parent:      99201:89d66f912671
user:        Yury Selivanov <yselivanov at sprymix.com>
date:        Wed Nov 18 12:40:26 2015 -0500
summary:
  asyncio: Error if awaiting in parallel on the same coroutine

See https://github.com/python/asyncio/pull/293 for details.

files:
  Lib/asyncio/coroutines.py            |   8 +++++-
  Lib/test/test_asyncio/test_pep492.py |  20 ++++++++++++++++
  2 files changed, 27 insertions(+), 1 deletions(-)


diff --git a/Lib/asyncio/coroutines.py b/Lib/asyncio/coroutines.py
--- a/Lib/asyncio/coroutines.py
+++ b/Lib/asyncio/coroutines.py
@@ -140,7 +140,13 @@
 
     if compat.PY35:
 
-        __await__ = __iter__ # make compatible with 'await' expression
+        def __await__(self):
+            cr_await = getattr(self.gen, 'cr_await', None)
+            if cr_await is not None:
+                raise RuntimeError(
+                    "Cannot await on coroutine {!r} while it's "
+                    "awaiting for {!r}".format(self.gen, cr_await))
+            return self
 
         @property
         def gi_yieldfrom(self):
diff --git a/Lib/test/test_asyncio/test_pep492.py b/Lib/test/test_asyncio/test_pep492.py
--- a/Lib/test/test_asyncio/test_pep492.py
+++ b/Lib/test/test_asyncio/test_pep492.py
@@ -203,6 +203,26 @@
 
         self.loop.run_until_complete(runner())
 
+    def test_double_await(self):
+        async def afunc():
+            await asyncio.sleep(0.1, loop=self.loop)
+
+        async def runner():
+            coro = afunc()
+            t = asyncio.Task(coro, loop=self.loop)
+            try:
+                await asyncio.sleep(0, loop=self.loop)
+                await coro
+            finally:
+                t.cancel()
+
+        self.loop.set_debug(True)
+        with self.assertRaisesRegex(
+            RuntimeError,
+            r'Cannot await.*test_double_await.*\bafunc\b.*while.*\bsleep\b'):
+
+            self.loop.run_until_complete(runner())
+
 
 if __name__ == '__main__':
     unittest.main()

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


More information about the Python-checkins mailing list