bpo-31033: Add a msg argument to Future.cancel() and Task.cancel() (GH-19979)
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 @@
_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 @@
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 @@
if self._state != _PENDING:
return False
self._state = _CANCELLED
+ self._cancel_message = msg
self.__schedule_callbacks()
return True
@@ -173,7 +175,9 @@
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 @@
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