diff options
author | Yury Selivanov <yury@magic.io> | 2017-06-11 14:00:14 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-11 14:00:14 (GMT) |
commit | 176f2ebdad93f20876c08efabd364a0e6c86de14 (patch) | |
tree | 63eaa99f8e662418e60d07fec138f33c3bd504a6 /Lib | |
parent | 59422a29ee3a95866c4f7e037bdfffd5768afddd (diff) | |
download | cpython-176f2ebdad93f20876c08efabd364a0e6c86de14.zip cpython-176f2ebdad93f20876c08efabd364a0e6c86de14.tar.gz cpython-176f2ebdad93f20876c08efabd364a0e6c86de14.tar.bz2 |
bpo-30508: Don't log exceptions if Task/Future "cancel()" method was called. (#2109)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/asyncio/futures.py | 1 | ||||
-rw-r--r-- | Lib/asyncio/tasks.py | 1 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_futures.py | 8 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_tasks.py | 19 |
4 files changed, 29 insertions, 0 deletions
diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py index d11d289..cff9590 100644 --- a/Lib/asyncio/futures.py +++ b/Lib/asyncio/futures.py @@ -191,6 +191,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 d7867d1..9fe2a2f 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -148,6 +148,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 99336f8..5d4b2d2 100644 --- a/Lib/test/test_asyncio/test_futures.py +++ b/Lib/test/test_asyncio/test_futures.py @@ -319,6 +319,14 @@ class BaseFutureTests: self.assertFalse(m_log.error.called) @mock.patch('asyncio.base_events.logger') + def test_tb_logger_not_called_after_cancel(self, m_log): + fut = self._new_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 = self._new_future(loop=self.loop) fut.set_result(42) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 5462c80..195a1ed 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -1865,6 +1865,25 @@ class BaseTaskTests: }) 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 = self.new_task(loop, 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): |