Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7250d02

Browse files
committed
Merged in 1st1/cpython350 (pull request #5)
Issue #24867: Fix asyncio.Task.get_stack() for 'async def' coroutines
2 parents 90f5bca + 7ca6c55 commit 7250d02

4 files changed

Lines changed: 56 additions & 1 deletion

File tree

Lib/asyncio/tasks.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ def get_stack(self, *, limit=None):
128128
returned for a suspended coroutine.
129129
"""
130130
frames = []
131-
f = self._coro.gi_frame
131+
try:
132+
# 'async def' coroutines
133+
f = self._coro.cr_frame
134+
except AttributeError:
135+
f = self._coro.gi_frame
132136
if f is not None:
133137
while f is not None:
134138
if limit is not None:

Lib/test/test_asyncio/test_pep492.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,23 @@ async def coro():
186186
data = self.loop.run_until_complete(coro())
187187
self.assertEqual(data, 'spam')
188188

189+
def test_task_print_stack(self):
190+
T = None
191+
192+
async def foo():
193+
f = T.get_stack(limit=1)
194+
try:
195+
self.assertEqual(f[0].f_code.co_name, 'foo')
196+
finally:
197+
f = None
198+
199+
async def runner():
200+
nonlocal T
201+
T = asyncio.ensure_future(foo(), loop=self.loop)
202+
await T
203+
204+
self.loop.run_until_complete(runner())
205+
189206

190207
if __name__ == '__main__':
191208
unittest.main()

Lib/test/test_asyncio/test_tasks.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import contextlib
44
import functools
5+
import io
56
import os
67
import re
78
import sys
@@ -162,6 +163,37 @@ def test_async_warning(self):
162163
'function is deprecated, use ensure_'):
163164
self.assertIs(f, asyncio.async(f))
164165

166+
def test_get_stack(self):
167+
T = None
168+
169+
@asyncio.coroutine
170+
def foo():
171+
yield from bar()
172+
173+
@asyncio.coroutine
174+
def bar():
175+
# test get_stack()
176+
f = T.get_stack(limit=1)
177+
try:
178+
self.assertEqual(f[0].f_code.co_name, 'foo')
179+
finally:
180+
f = None
181+
182+
# test print_stack()
183+
file = io.StringIO()
184+
T.print_stack(limit=1, file=file)
185+
file.seek(0)
186+
tb = file.read()
187+
self.assertRegex(tb, r'foo\(\) running')
188+
189+
@asyncio.coroutine
190+
def runner():
191+
nonlocal T
192+
T = asyncio.ensure_future(foo(), loop=self.loop)
193+
yield from T
194+
195+
self.loop.run_until_complete(runner())
196+
165197
def test_task_repr(self):
166198
self.loop.set_debug(False)
167199

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ Library
7979

8080
- Issue #24791: Fix grammar regression for call syntax: 'g(*a or b)'.
8181

82+
- Issue #24867: Fix Task.get_stack() for 'async def' coroutines
83+
8284
Documentation
8385
-------------
8486

0 commit comments

Comments
 (0)