summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2016-10-09 16:21:03 (GMT)
committerYury Selivanov <yury@magic.io>2016-10-09 16:21:03 (GMT)
commitd4ea2ec38a6d51266425ef5a8947d4fb0772250c (patch)
tree6960e83b91cd015a885ffd954d464d866f642d42 /Lib
parentcfa6b72a47466daf144a58a42e5d905d351aab12 (diff)
parent917c1c3ee1b4cbe7a7a78ffde053fb5628eee486 (diff)
downloadcpython-d4ea2ec38a6d51266425ef5a8947d4fb0772250c.zip
cpython-d4ea2ec38a6d51266425ef5a8947d4fb0772250c.tar.gz
cpython-d4ea2ec38a6d51266425ef5a8947d4fb0772250c.tar.bz2
Merge 3.6 (issue #27972)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/asyncio/tasks.py21
-rw-r--r--Lib/test/test_asyncio/test_tasks.py11
2 files changed, 25 insertions, 7 deletions
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index f735b44..14949d1 100644
--- 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:
+ except futures.CancelledError:
super().cancel() # I.e., Future.cancel(self).
except Exception as exc:
self.set_exception(exc)
@@ -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
- result.add_done_callback(self._wakeup)
- self._fut_waiter = result
- if self._must_cancel:
- if self._fut_waiter.cancel():
- self._must_cancel = False
+ if result is self:
+ self._loop.call_soon(
+ self._step,
+ RuntimeError(
+ 'Task cannot await on itself: {!r}'.format(
+ self)))
+ else:
+ result._asyncio_future_blocking = False
+ result.add_done_callback(self._wakeup)
+ self._fut_waiter = result
+ if self._must_cancel:
+ if self._fut_waiter.cancel():
+ self._must_cancel = False
else:
self._loop.call_soon(
self._step,
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
index 2863c42..a5af7d1 100644
--- 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
+ def test():
+ yield from task
+
+ task = asyncio.ensure_future(test(), loop=self.loop)
+
+ with self.assertRaisesRegex(RuntimeError,
+ 'Task cannot await on itself'):
+ self.loop.run_until_complete(task)
+
def test_task_class(self):
@asyncio.coroutine
def notmuch():