[Python-checkins] cpython (3.5): Issue #24867: Fix asyncio.Task.get_stack() for 'async def' coroutines

larry.hastings python-checkins at python.org
Tue Aug 25 23:25:20 CEST 2015


https://hg.python.org/cpython/rev/595614c08eeb
changeset: 97495:595614c08eeb
branch: 3.5
parent: 97475:5a9ac801f9b4
user: Yury Selivanov <yselivanov at sprymix.com>
date: Mon Aug 17 14:46:51 2015 -0400
summary:
 Issue #24867: Fix asyncio.Task.get_stack() for 'async def' coroutines
files:
 Lib/asyncio/tasks.py | 6 ++-
 Lib/test/test_asyncio/test_pep492.py | 17 ++++++++
 Lib/test/test_asyncio/test_tasks.py | 32 ++++++++++++++++
 Misc/NEWS | 2 +
 4 files changed, 56 insertions(+), 1 deletions(-)
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -128,7 +128,11 @@
 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:
diff --git a/Lib/test/test_asyncio/test_pep492.py b/Lib/test/test_asyncio/test_pep492.py
--- a/Lib/test/test_asyncio/test_pep492.py
+++ b/Lib/test/test_asyncio/test_pep492.py
@@ -186,6 +186,23 @@
 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()
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -2,6 +2,7 @@
 
 import contextlib
 import functools
+import io
 import os
 import re
 import sys
@@ -162,6 +163,37 @@
 '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)
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -74,6 +74,8 @@
 
 - Issue #24791: Fix grammar regression for call syntax: 'g(*a or b)'.
 
+- Issue #24867: Fix Task.get_stack() for 'async def' coroutines
+
 Documentation
 -------------
 
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /