[Python-checkins] cpython (merge 3.4 -> default): Merge 3.4 (asyncio)

victor.stinner python-checkins at python.org
Thu Jan 15 16:31:09 CET 2015


https://hg.python.org/cpython/rev/e05646ea3c40
changeset:   94178:e05646ea3c40
parent:      94176:78fb5c4e3129
parent:      94177:8adf1896712d
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Thu Jan 15 16:29:23 2015 +0100
summary:
  Merge 3.4 (asyncio)

files:
  Lib/asyncio/tasks.py                |  12 +++++--
  Lib/test/test_asyncio/test_tasks.py |  27 +++++++++++++++++
  2 files changed, 35 insertions(+), 4 deletions(-)


diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -347,10 +347,9 @@
     it cancels the task and raises TimeoutError.  To avoid the task
     cancellation, wrap it in shield().
 
-    Usage:
+    If the wait is cancelled, the task is also cancelled.
 
-        result = yield from asyncio.wait_for(fut, 10.0)
-
+    This function is a coroutine.
     """
     if loop is None:
         loop = events.get_event_loop()
@@ -367,7 +366,12 @@
 
     try:
         # wait until the future completes or the timeout
-        yield from waiter
+        try:
+            yield from waiter
+        except futures.CancelledError:
+            fut.remove_done_callback(cb)
+            fut.cancel()
+            raise
 
         if fut.done():
             return fut.result()
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -1705,6 +1705,33 @@
                           'test_task_source_traceback'))
         self.loop.run_until_complete(task)
 
+    def _test_cancel_wait_for(self, timeout):
+        loop = asyncio.new_event_loop()
+        self.addCleanup(loop.close)
+
+        @asyncio.coroutine
+        def blocking_coroutine():
+            fut = asyncio.Future(loop=loop)
+            # Block: fut result is never set
+            yield from fut
+
+        task = loop.create_task(blocking_coroutine())
+
+        wait = loop.create_task(asyncio.wait_for(task, timeout, loop=loop))
+        loop.call_soon(wait.cancel)
+
+        self.assertRaises(asyncio.CancelledError,
+                          loop.run_until_complete, wait)
+
+        # Python issue #23219: cancelling the wait must also cancel the task
+        self.assertTrue(task.cancelled())
+
+    def test_cancel_blocking_wait_for(self):
+        self._test_cancel_wait_for(None)
+
+    def test_cancel_wait_for(self):
+        self._test_cancel_wait_for(60.0)
+
 
 class GatherTestsBase:
 

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


More information about the Python-checkins mailing list