Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Remove the _create_cancelled_error() helper function.
  • Loading branch information
cjerdonek committed May 15, 2020
commit 22a54cc448747b7e6d0ee57b5c4cc93d2aead540
16 changes: 5 additions & 11 deletions Lib/asyncio/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ def result(self):
the future is done and has an exception set, this exception is raised.
"""
if self._state == _CANCELLED:
raise _create_cancelled_error(self._cancel_message)
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
Expand All @@ -192,7 +194,8 @@ def exception(self):
InvalidStateError.
"""
if self._state == _CANCELLED:
raise _create_cancelled_error(self._cancel_message)
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
Expand Down Expand Up @@ -293,15 +296,6 @@ def _set_result_unless_cancelled(fut, result):
fut.set_result(result)


# This provides a nicer-looking traceback when no msg is passed, which
# has been asyncio's default behavior.
def _create_cancelled_error(msg=None):
if msg is None:
return exceptions.CancelledError()

return exceptions.CancelledError(msg)


def _convert_future_exc(exc):
exc_class = type(exc)
if exc_class is concurrent.futures.CancelledError:
Expand Down
15 changes: 10 additions & 5 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ def __step(self, exc=None):
f'_step(): already done: {self!r}, {exc!r}')
if self._must_cancel:
if not isinstance(exc, exceptions.CancelledError):
exc = futures._create_cancelled_error(self._cancel_message)
exc = exceptions.CancelledError(''
if self._cancel_message is None else self._cancel_message)
self._must_cancel = False
coro = self._coro
self._fut_waiter = None
Expand Down Expand Up @@ -778,7 +779,8 @@ def _done_callback(fut):
# Check if 'fut' is cancelled first, as
# 'fut.exception()' will *raise* a CancelledError
# instead of returning it.
exc = futures._create_cancelled_error(fut._cancel_message)
exc = exceptions.CancelledError(''
if fut._cancel_message is None else fut._cancel_message)
outer.set_exception(exc)
return
else:
Expand All @@ -797,7 +799,9 @@ def _done_callback(fut):
# Check if 'fut' is cancelled first, as
# 'fut.exception()' will *raise* a CancelledError
# instead of returning it.
res = futures._create_cancelled_error(fut._cancel_message)
res = exceptions.CancelledError(
'' if fut._cancel_message is None else
fut._cancel_message)
else:
res = fut.exception()
if res is None:
Expand All @@ -808,8 +812,9 @@ def _done_callback(fut):
# If gather is being cancelled we must propagate the
# cancellation regardless of *return_exceptions* argument.
# See issue 32684.
outer.set_exception(
futures._create_cancelled_error(fut._cancel_message))
exc = exceptions.CancelledError(''
if fut._cancel_message is None else fut._cancel_message)
outer.set_exception(exc)
else:
outer.set_result(results)

Expand Down
12 changes: 6 additions & 6 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,8 @@ async def task():
def test_cancel_with_message_then_future_result(self):
# Test Future.result() after calling cancel() with a message.
cases = [
((), ()),
((None,), ()),
((), ('',)),
((None,), ('',)),
(('my message',), ('my message',)),
# Non-string values should roundtrip.
((5,), (5,)),
Expand Down Expand Up @@ -572,8 +572,8 @@ async def coro():
def test_cancel_with_message_then_future_exception(self):
# Test Future.exception() after calling cancel() with a message.
cases = [
((), ()),
((None,), ()),
((), ('',)),
((None,), ('',)),
(('my message',), ('my message',)),
# Non-string values should roundtrip.
((5,), (5,)),
Expand Down Expand Up @@ -2344,8 +2344,8 @@ def cancelling_callback(_):

def test_cancel_gather_2(self):
cases = [
((), ()),
((None,), ()),
((), ('',)),
((None,), ('',)),
(('my message',), ('my message',)),
# Non-string values should roundtrip.
((5,), (5,)),
Expand Down
4 changes: 3 additions & 1 deletion Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,9 @@ create_cancelled_error(PyObject *msg)
{
PyObject *exc;
if (msg == NULL || msg == Py_None) {
exc = PyObject_CallNoArgs(asyncio_CancelledError);
msg = PyUnicode_FromString("");
exc = PyObject_CallOneArg(asyncio_CancelledError, msg);
Comment thread
1st1 marked this conversation as resolved.
Py_DECREF(msg);
} else {
exc = PyObject_CallOneArg(asyncio_CancelledError, msg);
}
Expand Down