From: Yury Selivanov Date: Sun, 11 Jun 2017 14:11:47 +0000 (+0000) Subject: bpo-30508: Don't log exceptions if Task/Future "cancel()" method was called. (#2110) X-Git-Tag: v3.5.4rc1~88 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d24c8287e226ac9983caf6bb826a7b53142ee31f;p=thirdparty%2FPython%2Fcpython.git bpo-30508: Don't log exceptions if Task/Future "cancel()" method was called. (#2110) --- diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py index 9ca8d8458bcf..60b0d3133494 100644 --- a/Lib/asyncio/futures.py +++ b/Lib/asyncio/futures.py @@ -240,6 +240,7 @@ class Future: change the future's state to cancelled, schedule the callbacks and return True. """ + self._log_traceback = False if self._state != _PENDING: return False self._state = _CANCELLED diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 89d0989c614a..6c43e6830a10 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -208,6 +208,7 @@ class Task(futures.Future): terminates with a CancelledError exception (even if cancel() was not called). """ + self._log_traceback = False if self.done(): return False if self._fut_waiter is not None: diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py index c306b77e6532..195a006fe67e 100644 --- a/Lib/test/test_asyncio/test_futures.py +++ b/Lib/test/test_asyncio/test_futures.py @@ -314,6 +314,14 @@ class FutureTests(test_utils.TestCase): del fut self.assertFalse(m_log.error.called) + @mock.patch('asyncio.base_events.logger') + def test_tb_logger_not_called_after_cancel(self, m_log): + fut = asyncio.Future(loop=self.loop) + fut.set_exception(Exception()) + fut.cancel() + del fut + self.assertFalse(m_log.error.called) + @mock.patch('asyncio.base_events.logger') def test_tb_logger_result_unretrieved(self, m_log): fut = asyncio.Future(loop=self.loop) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index b3d0653e82e5..c419015a088b 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -1828,6 +1828,25 @@ class TaskTests(test_utils.TestCase): }) mock_handler.reset_mock() + @mock.patch('asyncio.base_events.logger') + def test_tb_logger_not_called_after_cancel(self, m_log): + loop = asyncio.new_event_loop() + self.set_event_loop(loop) + + @asyncio.coroutine + def coro(): + raise TypeError + + @asyncio.coroutine + def runner(): + task = loop.create_task(coro()) + yield from asyncio.sleep(0.05, loop=loop) + task.cancel() + task = None + + loop.run_until_complete(runner()) + self.assertFalse(m_log.error.called) + @mock.patch('asyncio.coroutines.logger') def test_coroutine_never_yielded(self, m_log): with set_coroutine_debug(True): diff --git a/Misc/NEWS b/Misc/NEWS index 33b7b9ae9df8..e8f94451f8c5 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -56,6 +56,9 @@ Extension Modules Library ------- +- bpo-30508: Don't log exceptions if Task/Future "cancel()" method was + called. + - bpo-28556: Updates to typing module: Add generic AsyncContextManager, add support for ContextManager on all versions. Original PRs by Jelle Zijlstra and Ivan Levkivskyi