Skip to content

Commit 22a54cc

Browse files
committed
Remove the _create_cancelled_error() helper function.
1 parent a0434c9 commit 22a54cc

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

Lib/asyncio/futures.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ 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 _create_cancelled_error(self._cancel_message)
178+
raise exceptions.CancelledError(
179+
'' if self._cancel_message is None else self._cancel_message)
180+
179181
if self._state != _FINISHED:
180182
raise exceptions.InvalidStateError('Result is not ready.')
181183
self.__log_traceback = False
@@ -192,7 +194,8 @@ def exception(self):
192194
InvalidStateError.
193195
"""
194196
if self._state == _CANCELLED:
195-
raise _create_cancelled_error(self._cancel_message)
197+
raise exceptions.CancelledError(
198+
'' if self._cancel_message is None else self._cancel_message)
196199
if self._state != _FINISHED:
197200
raise exceptions.InvalidStateError('Exception is not set.')
198201
self.__log_traceback = False
@@ -293,15 +296,6 @@ def _set_result_unless_cancelled(fut, result):
293296
fut.set_result(result)
294297

295298

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-
305299
def _convert_future_exc(exc):
306300
exc_class = type(exc)
307301
if exc_class is concurrent.futures.CancelledError:

Lib/asyncio/tasks.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ 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 = futures._create_cancelled_error(self._cancel_message)
273+
exc = exceptions.CancelledError(''
274+
if self._cancel_message is None else self._cancel_message)
274275
self._must_cancel = False
275276
coro = self._coro
276277
self._fut_waiter = None
@@ -778,7 +779,8 @@ def _done_callback(fut):
778779
# Check if 'fut' is cancelled first, as
779780
# 'fut.exception()' will *raise* a CancelledError
780781
# instead of returning it.
781-
exc = futures._create_cancelled_error(fut._cancel_message)
782+
exc = exceptions.CancelledError(''
783+
if fut._cancel_message is None else fut._cancel_message)
782784
outer.set_exception(exc)
783785
return
784786
else:
@@ -797,7 +799,9 @@ def _done_callback(fut):
797799
# Check if 'fut' is cancelled first, as
798800
# 'fut.exception()' will *raise* a CancelledError
799801
# instead of returning it.
800-
res = futures._create_cancelled_error(fut._cancel_message)
802+
res = exceptions.CancelledError(
803+
'' if fut._cancel_message is None else
804+
fut._cancel_message)
801805
else:
802806
res = fut.exception()
803807
if res is None:
@@ -808,8 +812,9 @@ def _done_callback(fut):
808812
# If gather is being cancelled we must propagate the
809813
# cancellation regardless of *return_exceptions* argument.
810814
# See issue 32684.
811-
outer.set_exception(
812-
futures._create_cancelled_error(fut._cancel_message))
815+
exc = exceptions.CancelledError(''
816+
if fut._cancel_message is None else fut._cancel_message)
817+
outer.set_exception(exc)
813818
else:
814819
outer.set_result(results)
815820

Lib/test/test_asyncio/test_tasks.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,8 @@ async def task():
542542
def test_cancel_with_message_then_future_result(self):
543543
# Test Future.result() after calling cancel() with a message.
544544
cases = [
545-
((), ()),
546-
((None,), ()),
545+
((), ('',)),
546+
((None,), ('',)),
547547
(('my message',), ('my message',)),
548548
# Non-string values should roundtrip.
549549
((5,), (5,)),
@@ -572,8 +572,8 @@ async def coro():
572572
def test_cancel_with_message_then_future_exception(self):
573573
# Test Future.exception() after calling cancel() with a message.
574574
cases = [
575-
((), ()),
576-
((None,), ()),
575+
((), ('',)),
576+
((None,), ('',)),
577577
(('my message',), ('my message',)),
578578
# Non-string values should roundtrip.
579579
((5,), (5,)),
@@ -2344,8 +2344,8 @@ def cancelling_callback(_):
23442344

23452345
def test_cancel_gather_2(self):
23462346
cases = [
2347-
((), ()),
2348-
((None,), ()),
2347+
((), ('',)),
2348+
((None,), ('',)),
23492349
(('my message',), ('my message',)),
23502350
# Non-string values should roundtrip.
23512351
((5,), (5,)),

Modules/_asynciomodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,9 @@ create_cancelled_error(PyObject *msg)
601601
{
602602
PyObject *exc;
603603
if (msg == NULL || msg == Py_None) {
604-
exc = PyObject_CallNoArgs(asyncio_CancelledError);
604+
msg = PyUnicode_FromString("");
605+
exc = PyObject_CallOneArg(asyncio_CancelledError, msg);
606+
Py_DECREF(msg);
605607
} else {
606608
exc = PyObject_CallOneArg(asyncio_CancelledError, msg);
607609
}

0 commit comments

Comments
 (0)