cpython: 8d877876aa89 (original) (raw)
Mercurial > cpython
changeset 104417:8d877876aa89 3.5
Issue #27972: Prohibit Tasks to await on themselves. [#27972]
Yury Selivanov yury@magic.io | |
---|---|
date | Sun, 09 Oct 2016 12:19:12 -0400 |
parents | a3b162d5e70a |
children | 41c4f535b5c0 69fe5f2e5aae |
files | Lib/asyncio/tasks.py Lib/test/test_asyncio/test_tasks.py Misc/NEWS |
diffstat | 3 files changed, 27 insertions(+), 7 deletions(-)[+] [-] Lib/asyncio/tasks.py 21 Lib/test/test_asyncio/test_tasks.py 11 Misc/NEWS 2 |
line wrap: on
line diff
--- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -241,7 +241,7 @@ class Task(futures.Future): result = coro.throw(exc) except StopIteration as exc: self.set_result(exc.value)
except futures.CancelledError as exc:[](#l1.7)
except futures.CancelledError:[](#l1.8) super().cancel() # I.e., Future.cancel(self).[](#l1.9) except Exception as exc:[](#l1.10) self.set_exception(exc)[](#l1.11)
@@ -259,12 +259,19 @@ class Task(futures.Future): 'Task {!r} got Future {!r} attached to a ' 'different loop'.format(self, result))) elif blocking:
result._asyncio_future_blocking = False[](#l1.16)
result.add_done_callback(self._wakeup)[](#l1.17)
self._fut_waiter = result[](#l1.18)
if self._must_cancel:[](#l1.19)
if self._fut_waiter.cancel():[](#l1.20)
self._must_cancel = False[](#l1.21)
if result is self:[](#l1.22)
self._loop.call_soon([](#l1.23)
self._step,[](#l1.24)
RuntimeError([](#l1.25)
'Task cannot await on itself: {!r}'.format([](#l1.26)
self)))[](#l1.27)
else:[](#l1.28)
result._asyncio_future_blocking = False[](#l1.29)
result.add_done_callback(self._wakeup)[](#l1.30)
self._fut_waiter = result[](#l1.31)
if self._must_cancel:[](#l1.32)
if self._fut_waiter.cancel():[](#l1.33)
self._must_cancel = False[](#l1.34) else:[](#l1.35) self._loop.call_soon([](#l1.36) self._step,[](#l1.37)
--- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -92,6 +92,17 @@ class TaskTests(test_utils.TestCase): finally: other_loop.close()
- def test_task_awaits_on_itself(self):
@asyncio.coroutine[](#l2.8)
def test():[](#l2.9)
yield from task[](#l2.10)
task = asyncio.ensure_future(test(), loop=self.loop)[](#l2.12)
with self.assertRaisesRegex(RuntimeError,[](#l2.14)
'Task cannot await on itself'):[](#l2.15)
self.loop.run_until_complete(task)[](#l2.16)
+ def test_task_class(self): @asyncio.coroutine def notmuch():