summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio/futures.py
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2020-05-15 23:55:50 (GMT)
committerGitHub <noreply@github.com>2020-05-15 23:55:50 (GMT)
commit1ce5841eca6d96b1b1e8c213d04f2e92b1619bb5 (patch)
tree9fe492d885e1a0d660be4d96fb92b5f563aec99e /Lib/asyncio/futures.py
parentfe1176e882393b6d3e6a6cfa5ca23657f0b3b4a9 (diff)
downloadcpython-1ce5841eca6d96b1b1e8c213d04f2e92b1619bb5.zip
cpython-1ce5841eca6d96b1b1e8c213d04f2e92b1619bb5.tar.gz
cpython-1ce5841eca6d96b1b1e8c213d04f2e92b1619bb5.tar.bz2
bpo-31033: Add a msg argument to Future.cancel() and Task.cancel() (GH-19979)
Diffstat (limited to 'Lib/asyncio/futures.py')
-rw-r--r--Lib/asyncio/futures.py11
1 files changed, 8 insertions, 3 deletions
diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py
index a3cf379..889f3e6 100644
--- a/Lib/asyncio/futures.py
+++ b/Lib/asyncio/futures.py
@@ -51,6 +51,7 @@ class Future:
_exception = None
_loop = None
_source_traceback = None
+ _cancel_message = None
# This field is used for a dual purpose:
# - Its presence is a marker to declare that a class implements
@@ -123,7 +124,7 @@ class Future:
raise RuntimeError("Future object is not initialized.")
return loop
- def cancel(self):
+ def cancel(self, msg=None):
"""Cancel the future and schedule callbacks.
If the future is already done or cancelled, return False. Otherwise,
@@ -134,6 +135,7 @@ class Future:
if self._state != _PENDING:
return False
self._state = _CANCELLED
+ self._cancel_message = msg
self.__schedule_callbacks()
return True
@@ -173,7 +175,9 @@ class Future:
the future is done and has an exception set, this exception is raised.
"""
if self._state == _CANCELLED:
- raise exceptions.CancelledError
+ raise exceptions.CancelledError(
+ '' if self._cancel_message is None else self._cancel_message)
+
if self._state != _FINISHED:
raise exceptions.InvalidStateError('Result is not ready.')
self.__log_traceback = False
@@ -190,7 +194,8 @@ class Future:
InvalidStateError.
"""
if self._state == _CANCELLED:
- raise exceptions.CancelledError
+ raise exceptions.CancelledError(
+ '' if self._cancel_message is None else self._cancel_message)
if self._state != _FINISHED:
raise exceptions.InvalidStateError('Exception is not set.')
self.__log_traceback = False