]> granicus.if.org Git - python/commitdiff
asyncio: Fix formatting of the "Future exception was never retrieved" in
authorVictor Stinner <victor.stinner@gmail.com>
Thu, 20 Nov 2014 13:16:31 +0000 (14:16 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Thu, 20 Nov 2014 13:16:31 +0000 (14:16 +0100)
release mode

Lib/asyncio/futures.py
Lib/test/test_asyncio/test_events.py
Lib/test/test_asyncio/test_futures.py

index 7998fbbcfbf21595238a4df6fa34e80faf58c6d0..40662a328ddf2b05f222e23b3d2813301e8e6b92 100644 (file)
@@ -104,10 +104,11 @@ class _TracebackLogger:
 
     def __del__(self):
         if self.tb:
-            msg = 'Future/Task exception was never retrieved'
+            msg = 'Future/Task exception was never retrieved\n'
             if self.source_traceback:
-                msg += '\nFuture/Task created at (most recent call last):\n'
-                msg += ''.join(traceback.format_list(self.source_traceback))
+                src = ''.join(traceback.format_list(self.source_traceback))
+                msg += 'Future/Task created at (most recent call last):\n'
+                msg += '%s\n' % src.rstrip()
             msg += ''.join(self.tb).rstrip()
             self.loop.call_exception_handler({'message': msg})
 
index 4fe4b4c4f4d0ac52fbed5027ae72ae506b755cda..b05cb7ca972e3dfb9496c3a6e69e6e0d9bf1bcfa 100644 (file)
@@ -606,27 +606,29 @@ class EventLoopTestsMixin:
         self.assertGreater(pr.nbytes, 0)
         tr.close()
 
-    if ssl:
-        def _dummy_ssl_create_context(self, purpose=ssl.Purpose.SERVER_AUTH, *,
-                                      cafile=None, capath=None, cadata=None):
-            """
-            A ssl.create_default_context() replacement that doesn't enable
-            cert validation.
-            """
-            self.assertEqual(purpose, ssl.Purpose.SERVER_AUTH)
-            return test_utils.dummy_ssl_context()
-
     def _test_create_ssl_connection(self, httpd, create_connection,
                                     check_sockname=True):
         conn_fut = create_connection(ssl=test_utils.dummy_ssl_context())
         self._basetest_create_ssl_connection(conn_fut, check_sockname)
 
-        # With ssl=True, ssl.create_default_context() should be called
-        with mock.patch('ssl.create_default_context',
-                        side_effect=self._dummy_ssl_create_context) as m:
-            conn_fut = create_connection(ssl=True)
-            self._basetest_create_ssl_connection(conn_fut, check_sockname)
-            self.assertEqual(m.call_count, 1)
+        # ssl.Purpose was introduced in Python 3.4
+        if hasattr(ssl, 'Purpose'):
+            def _dummy_ssl_create_context(purpose=ssl.Purpose.SERVER_AUTH, *,
+                                          cafile=None, capath=None,
+                                          cadata=None):
+                """
+                A ssl.create_default_context() replacement that doesn't enable
+                cert validation.
+                """
+                self.assertEqual(purpose, ssl.Purpose.SERVER_AUTH)
+                return test_utils.dummy_ssl_context()
+
+            # With ssl=True, ssl.create_default_context() should be called
+            with mock.patch('ssl.create_default_context',
+                            side_effect=_dummy_ssl_create_context) as m:
+                conn_fut = create_connection(ssl=True)
+                self._basetest_create_ssl_connection(conn_fut, check_sockname)
+                self.assertEqual(m.call_count, 1)
 
         # With the real ssl.create_default_context(), certificate
         # validation will fail
index e5002bc825d51c1c9c2f9788c51d329101dfe3ff..371d3518ff55ed006e61893a58b228f6c69d55bd 100644 (file)
@@ -307,8 +307,8 @@ class FutureTests(test_utils.TestCase):
                           'test_future_source_traceback'))
 
     @mock.patch('asyncio.base_events.logger')
-    def test_future_exception_never_retrieved(self, m_log):
-        self.loop.set_debug(True)
+    def check_future_exception_never_retrieved(self, debug, m_log):
+        self.loop.set_debug(debug)
 
         def memory_error():
             try:
@@ -318,40 +318,59 @@ class FutureTests(test_utils.TestCase):
         exc = memory_error()
 
         future = asyncio.Future(loop=self.loop)
-        source_traceback = future._source_traceback
+        if debug:
+            source_traceback = future._source_traceback
         future.set_exception(exc)
         future = None
         test_utils.run_briefly(self.loop)
         support.gc_collect()
 
         if sys.version_info >= (3, 4):
-            frame = source_traceback[-1]
-            regex = (r'^Future exception was never retrieved\n'
-                     r'future: <Future finished exception=MemoryError\(\) created at {filename}:{lineno}>\n'
-                     r'source_traceback: Object created at \(most recent call last\):\n'
-                     r'  File'
-                     r'.*\n'
-                     r'  File "{filename}", line {lineno}, in test_future_exception_never_retrieved\n'
-                     r'    future = asyncio\.Future\(loop=self\.loop\)$'
-                     ).format(filename=re.escape(frame[0]), lineno=frame[1])
+            if debug:
+                frame = source_traceback[-1]
+                regex = (r'^Future exception was never retrieved\n'
+                         r'future: <Future finished exception=MemoryError\(\) created at {filename}:{lineno}>\n'
+                         r'source_traceback: Object created at \(most recent call last\):\n'
+                         r'  File'
+                         r'.*\n'
+                         r'  File "{filename}", line {lineno}, in check_future_exception_never_retrieved\n'
+                         r'    future = asyncio\.Future\(loop=self\.loop\)$'
+                         ).format(filename=re.escape(frame[0]), lineno=frame[1])
+            else:
+                regex = (r'^Future exception was never retrieved\n'
+                         r'future: <Future finished exception=MemoryError\(\)>$'
+                         )
             exc_info = (type(exc), exc, exc.__traceback__)
             m_log.error.assert_called_once_with(mock.ANY, exc_info=exc_info)
         else:
-            frame = source_traceback[-1]
-            regex = (r'^Future/Task exception was never retrieved\n'
-                     r'Future/Task created at \(most recent call last\):\n'
-                     r'  File'
-                     r'.*\n'
-                     r'  File "{filename}", line {lineno}, in test_future_exception_never_retrieved\n'
-                     r'    future = asyncio\.Future\(loop=self\.loop\)\n'
-                     r'Traceback \(most recent call last\):\n'
-                     r'.*\n'
-                     r'MemoryError$'
-                     ).format(filename=re.escape(frame[0]), lineno=frame[1])
+            if debug:
+                frame = source_traceback[-1]
+                regex = (r'^Future/Task exception was never retrieved\n'
+                         r'Future/Task created at \(most recent call last\):\n'
+                         r'  File'
+                         r'.*\n'
+                         r'  File "{filename}", line {lineno}, in check_future_exception_never_retrieved\n'
+                         r'    future = asyncio\.Future\(loop=self\.loop\)\n'
+                         r'Traceback \(most recent call last\):\n'
+                         r'.*\n'
+                         r'MemoryError$'
+                         ).format(filename=re.escape(frame[0]), lineno=frame[1])
+            else:
+                regex = (r'^Future/Task exception was never retrieved\n'
+                         r'Traceback \(most recent call last\):\n'
+                         r'.*\n'
+                         r'MemoryError$'
+                         )
             m_log.error.assert_called_once_with(mock.ANY, exc_info=False)
         message = m_log.error.call_args[0][0]
         self.assertRegex(message, re.compile(regex, re.DOTALL))
 
+    def test_future_exception_never_retrieved(self):
+        self.check_future_exception_never_retrieved(False)
+
+    def test_future_exception_never_retrieved_debug(self):
+        self.check_future_exception_never_retrieved(True)
+
     def test_set_result_unless_cancelled(self):
         fut = asyncio.Future(loop=self.loop)
         fut.cancel()