https://github.com/python/cpython/commit/4b7b581193073d100253bef746e9e148309514e2
commit: 4b7b581193073d100253bef746e9e148309514e2
branch: main
author: Timofei Ivankov <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-11-05T11:28:56+05:30
summary:

gh-161166: Fix incorrect exception line in asyncio.Task.print_stack() (#161167)

files:
A Misc/NEWS.d/next/Library/2026-11-02-13-11-30.gh-issue-161166.zfFDlW.rst
M Lib/asyncio/base_tasks.py
M Lib/test/test_asyncio/test_tasks.py

diff --git a/Lib/asyncio/base_tasks.py b/Lib/asyncio/base_tasks.py
index 2e297c6e0802be..ae5c9a514bad24 100644
--- a/Lib/asyncio/base_tasks.py
+++ b/Lib/asyncio/base_tasks.py
@@ -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
@@ -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:
diff --git a/Lib/test/test_asyncio/test_tasks.py 
b/Lib/test/test_asyncio/test_tasks.py
index 277b856f57a136..f16815474e2d19 100644
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -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():
diff --git 
a/Misc/NEWS.d/next/Library/2026-11-02-13-11-30.gh-issue-161166.zfFDlW.rst 
b/Misc/NEWS.d/next/Library/2026-11-02-13-11-30.gh-issue-161166.zfFDlW.rst
new file mode 100644
index 00000000000000..4d66a399f5e655
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-11-02-13-11-30.gh-issue-161166.zfFDlW.rst
@@ -0,0 +1,2 @@
+Fix :meth:`asyncio.Task.print_stack` displaying an incorrect exception line
+after a ``finally`` block. Patch by Timofei Ivankov.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to