mirror of
https://github.com/python/cpython.git
synced 2025-10-06 07:02:33 +00:00
bpo-30048: asyncio: fix Task.cancel() was ignored. (GH-1546)
when there are no more `await` or `yield (from)` before return in coroutine,
cancel was ignored.
example:
async def coro():
asyncio.Task.current_task().cancel()
return 42
...
res = await coro() # should raise CancelledError
(cherry picked from commit 991adca012
)
This commit is contained in:
parent
a4465a5bd0
commit
3dc7c52a9f
4 changed files with 39 additions and 1 deletions
|
@ -588,6 +588,24 @@ class BaseTaskTests:
|
|||
self.assertFalse(t._must_cancel) # White-box test.
|
||||
self.assertFalse(t.cancel())
|
||||
|
||||
def test_cancel_at_end(self):
|
||||
"""coroutine end right after task is cancelled"""
|
||||
loop = asyncio.new_event_loop()
|
||||
self.set_event_loop(loop)
|
||||
|
||||
@asyncio.coroutine
|
||||
def task():
|
||||
t.cancel()
|
||||
self.assertTrue(t._must_cancel) # White-box test.
|
||||
return 12
|
||||
|
||||
t = self.new_task(loop, task())
|
||||
self.assertRaises(
|
||||
asyncio.CancelledError, loop.run_until_complete, t)
|
||||
self.assertTrue(t.done())
|
||||
self.assertFalse(t._must_cancel) # White-box test.
|
||||
self.assertFalse(t.cancel())
|
||||
|
||||
def test_stop_while_run_in_complete(self):
|
||||
|
||||
def gen():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue