summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2017-06-11 14:11:47 (GMT)
committerGitHub <noreply@github.com>2017-06-11 14:11:47 (GMT)
commitd24c8287e226ac9983caf6bb826a7b53142ee31f (patch)
treec041358ff6a9e78e64dc7e6cf3e5e0952ade5cd9
parentea8b34868c3a765ac9a5eed7321af8f4548a9654 (diff)
downloadcpython-d24c8287e226ac9983caf6bb826a7b53142ee31f.zip
cpython-d24c8287e226ac9983caf6bb826a7b53142ee31f.tar.gz
cpython-d24c8287e226ac9983caf6bb826a7b53142ee31f.tar.bz2
bpo-30508: Don't log exceptions if Task/Future "cancel()" method was called. (#2110)
-rw-r--r--Lib/asyncio/futures.py1
-rw-r--r--Lib/asyncio/tasks.py1
-rw-r--r--Lib/test/test_asyncio/test_futures.py8
-rw-r--r--Lib/test/test_asyncio/test_tasks.py19
-rw-r--r--Misc/NEWS3
5 files changed, 32 insertions, 0 deletions
diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py
index 9ca8d84..60b0d313 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 89d0989..6c43e68 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 c306b77..195a006 100644
--- a/Lib/test/test_asyncio/test_futures.py
+++ b/Lib/test/test_asyncio/test_futures.py
@@ -315,6 +315,14 @@ class FutureTests(test_utils.TestCase):
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)
fut.set_result(42)
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
index b3d0653..c419015 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 33b7b9a..e8f9445 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