Skip to content

Commit 6b133a2

Browse files
committed
Make None msg result in no arguments.
1 parent 403baa2 commit 6b133a2

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

Lib/asyncio/futures.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def result(self):
175175
the future is done and has an exception set, this exception is raised.
176176
"""
177177
if self._state == _CANCELLED:
178-
raise exceptions.CancelledError(self._cancel_message)
178+
raise _create_cancelled_error(self._cancel_message)
179179
if self._state != _FINISHED:
180180
raise exceptions.InvalidStateError('Result is not ready.')
181181
self.__log_traceback = False
@@ -192,7 +192,7 @@ def exception(self):
192192
InvalidStateError.
193193
"""
194194
if self._state == _CANCELLED:
195-
raise exceptions.CancelledError(self._cancel_message)
195+
raise _create_cancelled_error(self._cancel_message)
196196
if self._state != _FINISHED:
197197
raise exceptions.InvalidStateError('Exception is not set.')
198198
self.__log_traceback = False
@@ -293,6 +293,15 @@ def _set_result_unless_cancelled(fut, result):
293293
fut.set_result(result)
294294

295295

296+
# This provides a nicer-looking traceback when no msg is passed, which
297+
# has been asyncio's default behavior.
298+
def _create_cancelled_error(msg=None):
299+
if msg is None:
300+
return exceptions.CancelledError()
301+
302+
return exceptions.CancelledError(msg)
303+
304+
296305
def _convert_future_exc(exc):
297306
exc_class = type(exc)
298307
if exc_class is concurrent.futures.CancelledError:

Lib/asyncio/tasks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def __step(self, exc=None):
270270
f'_step(): already done: {self!r}, {exc!r}')
271271
if self._must_cancel:
272272
if not isinstance(exc, exceptions.CancelledError):
273-
exc = exceptions.CancelledError(self._cancel_message)
273+
exc = futures._create_cancelled_error(self._cancel_message)
274274
self._must_cancel = False
275275
coro = self._coro
276276
self._fut_waiter = None
@@ -778,7 +778,7 @@ def _done_callback(fut):
778778
# Check if 'fut' is cancelled first, as
779779
# 'fut.exception()' will *raise* a CancelledError
780780
# instead of returning it.
781-
exc = exceptions.CancelledError(fut._cancel_message)
781+
exc = futures._create_cancelled_error(fut._cancel_message)
782782
outer.set_exception(exc)
783783
return
784784
else:
@@ -797,7 +797,7 @@ def _done_callback(fut):
797797
# Check if 'fut' is cancelled first, as
798798
# 'fut.exception()' will *raise* a CancelledError
799799
# instead of returning it.
800-
res = exceptions.CancelledError(fut._cancel_message)
800+
res = futures._create_cancelled_error(fut._cancel_message)
801801
else:
802802
res = fut.exception()
803803
if res is None:
@@ -809,7 +809,7 @@ def _done_callback(fut):
809809
# cancellation regardless of *return_exceptions* argument.
810810
# See issue 32684.
811811
outer.set_exception(
812-
exceptions.CancelledError(fut._cancel_message))
812+
futures._create_cancelled_error(fut._cancel_message))
813813
else:
814814
outer.set_result(results)
815815

Lib/test/test_asyncio/test_tasks.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,8 @@ async def task():
517517
def test_cancel_with_message_then_future_result(self):
518518
# Test Future.result() after calling cancel() with a message.
519519
cases = [
520-
((), (None,)),
521-
((None,), (None,)),
520+
((), ()),
521+
((None,), ()),
522522
(('my message',), ('my message',)),
523523
# Non-string values should roundtrip.
524524
((5,), (5,)),
@@ -547,8 +547,8 @@ async def coro():
547547
def test_cancel_with_message_then_future_exception(self):
548548
# Test Future.exception() after calling cancel() with a message.
549549
cases = [
550-
((), (None,)),
551-
((None,), (None,)),
550+
((), ()),
551+
((None,), ()),
552552
(('my message',), ('my message',)),
553553
# Non-string values should roundtrip.
554554
((5,), (5,)),
@@ -2319,8 +2319,8 @@ def cancelling_callback(_):
23192319

23202320
def test_cancel_gather_2(self):
23212321
cases = [
2322-
((), (None,)),
2323-
((None,), (None,)),
2322+
((), ()),
2323+
((None,), ()),
23242324
(('my message',), ('my message',)),
23252325
# Non-string values should roundtrip.
23262326
((5,), (5,)),

Modules/_asynciomodule.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -599,11 +599,12 @@ future_set_exception(FutureObj *fut, PyObject *exc)
599599
static PyObject *
600600
create_cancelled_error(PyObject *msg)
601601
{
602-
if (msg == NULL) {
603-
msg = Py_None;
602+
PyObject *exc;
603+
if (msg == NULL || msg == Py_None) {
604+
exc = PyObject_CallNoArgs(asyncio_CancelledError);
605+
} else {
606+
exc = PyObject_CallOneArg(asyncio_CancelledError, msg);
604607
}
605-
PyObject *exc = PyObject_CallOneArg(asyncio_CancelledError, msg);
606-
607608
return exc;
608609
}
609610

0 commit comments

Comments
 (0)