summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio/tasks.py
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2017-12-25 15:48:15 (GMT)
committerGitHub <noreply@github.com>2017-12-25 15:48:15 (GMT)
commit0cf16f9ea014b17d398ee3971d4976c698533318 (patch)
treec0a2ec1cc06a2519ea5b8a254de844ec1afb3955 /Lib/asyncio/tasks.py
parent3dfbaf51f0d90646e0414ddbd3b513ee8e5ffe9b (diff)
downloadcpython-0cf16f9ea014b17d398ee3971d4976c698533318.zip
cpython-0cf16f9ea014b17d398ee3971d4976c698533318.tar.gz
cpython-0cf16f9ea014b17d398ee3971d4976c698533318.tar.bz2
bpo-32363: Disable Task.set_exception() and Task.set_result() (#4923)
Diffstat (limited to 'Lib/asyncio/tasks.py')
-rw-r--r--Lib/asyncio/tasks.py24
1 files changed, 17 insertions, 7 deletions
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index 572e707..b118088 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -37,7 +37,9 @@ def all_tasks(loop=None):
return {t for t in _all_tasks if futures._get_loop(t) is loop}
-class Task(futures.Future):
+class Task(futures._PyFuture): # Inherit Python Task implementation
+ # from a Python Future implementation.
+
"""A coroutine wrapped in a Future."""
# An important invariant maintained while a Task not done:
@@ -107,11 +109,17 @@ class Task(futures.Future):
if self._source_traceback:
context['source_traceback'] = self._source_traceback
self._loop.call_exception_handler(context)
- futures.Future.__del__(self)
+ super().__del__()
def _repr_info(self):
return base_tasks._task_repr_info(self)
+ def set_result(self, result):
+ raise RuntimeError('Task does not support set_result operation')
+
+ def set_exception(self, exception):
+ raise RuntimeError('Task does not support set_exception operation')
+
def get_stack(self, *, limit=None):
"""Return the list of stack frames for this task's coroutine.
@@ -180,7 +188,9 @@ class Task(futures.Future):
return True
def _step(self, exc=None):
- assert not self.done(), f'_step(): already done: {self!r}, {exc!r}'
+ if self.done():
+ raise futures.InvalidStateError(
+ f'_step(): already done: {self!r}, {exc!r}')
if self._must_cancel:
if not isinstance(exc, futures.CancelledError):
exc = futures.CancelledError()
@@ -201,15 +211,15 @@ class Task(futures.Future):
if self._must_cancel:
# Task is cancelled right before coro stops.
self._must_cancel = False
- self.set_exception(futures.CancelledError())
+ super().set_exception(futures.CancelledError())
else:
- self.set_result(exc.value)
+ super().set_result(exc.value)
except futures.CancelledError:
super().cancel() # I.e., Future.cancel(self).
except Exception as exc:
- self.set_exception(exc)
+ super().set_exception(exc)
except BaseException as exc:
- self.set_exception(exc)
+ super().set_exception(exc)
raise
else:
blocking = getattr(result, '_asyncio_future_blocking', None)