]> granicus.if.org Git - python/commitdiff
Issue #24867: Fix asyncio.Task.get_stack() for 'async def' coroutines
authorYury Selivanov <yselivanov@sprymix.com>
Mon, 17 Aug 2015 18:46:51 +0000 (14:46 -0400)
committerYury Selivanov <yselivanov@sprymix.com>
Mon, 17 Aug 2015 18:46:51 +0000 (14:46 -0400)
Lib/asyncio/tasks.py
Lib/test/test_asyncio/test_pep492.py
Lib/test/test_asyncio/test_tasks.py
Misc/NEWS

index 9bfc1cf81479c68b271ab81a1812436e9201401f..a235e742e234d5fcd4e449a33c7e9095499a820e 100644 (file)
@@ -128,7 +128,11 @@ class Task(futures.Future):
         returned for a suspended coroutine.
         """
         frames = []
-        f = self._coro.gi_frame
+        try:
+            # 'async def' coroutines
+            f = self._coro.cr_frame
+        except AttributeError:
+            f = self._coro.gi_frame
         if f is not None:
             while f is not None:
                 if limit is not None:
index b702efcdfda2d38f3533106d05b9193b4656012e..41e1b8a9e0cc97c6d26710a92c90b23fb879aee0 100644 (file)
@@ -186,6 +186,23 @@ class CoroutineTests(BaseTest):
         data = self.loop.run_until_complete(coro())
         self.assertEqual(data, 'spam')
 
+    def test_task_print_stack(self):
+        T = None
+
+        async def foo():
+            f = T.get_stack(limit=1)
+            try:
+                self.assertEqual(f[0].f_code.co_name, 'foo')
+            finally:
+                f = None
+
+        async def runner():
+            nonlocal T
+            T = asyncio.ensure_future(foo(), loop=self.loop)
+            await T
+
+        self.loop.run_until_complete(runner())
+
 
 if __name__ == '__main__':
     unittest.main()
index 251192acee4029b9c5c19580e4de1b922471e771..04267873103a0bdea1be26abdeb64c7a6d0c8c71 100644 (file)
@@ -2,6 +2,7 @@
 
 import contextlib
 import functools
+import io
 import os
 import re
 import sys
@@ -162,6 +163,37 @@ class TaskTests(test_utils.TestCase):
                                    'function is deprecated, use ensure_'):
             self.assertIs(f, asyncio.async(f))
 
+    def test_get_stack(self):
+        T = None
+
+        @asyncio.coroutine
+        def foo():
+            yield from bar()
+
+        @asyncio.coroutine
+        def bar():
+            # test get_stack()
+            f = T.get_stack(limit=1)
+            try:
+                self.assertEqual(f[0].f_code.co_name, 'foo')
+            finally:
+                f = None
+
+            # test print_stack()
+            file = io.StringIO()
+            T.print_stack(limit=1, file=file)
+            file.seek(0)
+            tb = file.read()
+            self.assertRegex(tb, r'foo\(\) running')
+
+        @asyncio.coroutine
+        def runner():
+            nonlocal T
+            T = asyncio.ensure_future(foo(), loop=self.loop)
+            yield from T
+
+        self.loop.run_until_complete(runner())
+
     def test_task_repr(self):
         self.loop.set_debug(False)
 
index 27a05648cc6f98a87b583c1634eaa333b6b348be..a6f39498dc4e3552da5f4f14fe069db3b6a936a1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -74,6 +74,8 @@ Library
 
 - Issue #24791: Fix grammar regression for call syntax: 'g(*a or b)'.
 
+- Issue #24867: Fix Task.get_stack() for 'async def' coroutines
+
 Documentation
 -------------