Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98b6391

Browse files
committed
Issue #21163: BaseEventLoop.run_until_complete() and test_utils.run_briefly()
don't log the "destroy pending task" message anymore. The log is redundant for run_until_complete() and useless in run_briefly().
1 parent b75380f commit 98b6391

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

Lib/asyncio/base_events.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,14 @@ def run_until_complete(self, future):
227227
Return the Future's result, or raise its exception.
228228
"""
229229
self._check_closed()
230+
231+
new_task = not isinstance(future, futures.Future)
230232
future = tasks.async(future, loop=self)
233+
if new_task:
234+
# An exception is raised if the future didn't complete, so there
235+
# is no need to log the "destroy pending task" message
236+
future._log_destroy_pending = False
237+
231238
future.add_done_callback(_raise_stop_error)
232239
self.run_forever()
233240
future.remove_done_callback(_raise_stop_error)

Lib/asyncio/tasks.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,16 @@ def __init__(self, coro, *, loop=None):
7575
self._must_cancel = False
7676
self._loop.call_soon(self._step)
7777
self.__class__._all_tasks.add(self)
78+
# If False, don't log a message if the task is destroyed whereas its
79+
# status is still pending
80+
self._log_destroy_pending = True
7881

7982
# On Python 3.3 or older, objects with a destructor part of a reference
8083
# cycle are never destroyed. It's not more the case on Python 3.4 thanks to
8184
# the PEP 442.
8285
if _PY34:
8386
def __del__(self):
84-
if self._state == futures._PENDING:
87+
if self._state == futures._PENDING and self._log_destroy_pending:
8588
context = {
8689
'task': self,
8790
'message': 'Task was destroyed but it is pending!',

Lib/asyncio/test_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ def once():
4949
pass
5050
gen = once()
5151
t = tasks.Task(gen, loop=loop)
52+
# Don't log a warning if the task is not done after run_until_complete().
53+
# It occurs if the loop is stopped or if a task raises a BaseException.
54+
t._log_destroy_pending = False
5255
try:
5356
loop.run_until_complete(t)
5457
finally:

0 commit comments

Comments
 (0)