Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Lib/asyncio/base_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,16 @@ def _task_get_stack(task, limit):


def _task_print_stack(task, limit, file):
exc = task._exception
tb = exc.__traceback__ if exc is not None else None
extracted_list = []
checked = set()
for f in task.get_stack(limit=limit):
lineno = f.f_lineno
if tb is not None:
lineno = tb.tb_lineno
tb = tb.tb_next
else:
lineno = f.f_lineno
co = f.f_code
filename = co.co_filename
name = co.co_name
Expand All @@ -83,7 +89,6 @@ def _task_print_stack(task, limit, file):
line = linecache.getline(filename, lineno, f.f_globals)
extracted_list.append((filename, lineno, name, line))

exc = task._exception
if not extracted_list:
print(f'No stack for {task!r}', file=file)
elif exc is not None:
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,21 @@ async def runner():

self.loop.run_until_complete(runner())

def test_print_stack_exception_lineno(self):
# gh-161166
async def coro():
try:
1 / 0
finally:
cleaned = True

task = self.new_task(self.loop, coro())
test_utils.run_briefly(self.loop)
task.exception()
file = io.StringIO()
task.print_stack(file=file)
self.assertIn(' 1 / 0\n', file.getvalue())

def test_get_stack_async_generator(self):
# gh-161178
async def gen():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :meth:`asyncio.Task.print_stack` displaying an incorrect exception line
after a ``finally`` block. Patch by Timofei Ivankov.
Loading