gh-121468: Add current asyncio task as a convenience variable in pdb (#124367)

This commit is contained in:
Tian Gao 2025-03-14 14:46:26 -04:00 committed by GitHub
parent c107b15ab8
commit 27fc62cf4f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 49 additions and 3 deletions

View file

@ -79,6 +79,7 @@ import types
import codeop
import pprint
import signal
import asyncio
import inspect
import textwrap
import tokenize
@ -363,6 +364,8 @@ class Pdb(bdb.Bdb, cmd.Cmd):
self._chained_exceptions = tuple()
self._chained_exception_index = 0
self._current_task = None
def set_trace(self, frame=None, *, commands=None):
Pdb._last_pdb_instance = self
if frame is None:
@ -405,7 +408,8 @@ class Pdb(bdb.Bdb, cmd.Cmd):
tb = tb.tb_next
self.curframe = self.stack[self.curindex][0]
self.set_convenience_variable(self.curframe, '_frame', self.curframe)
if self._current_task:
self.set_convenience_variable(self.curframe, '_asynctask', self._current_task)
self._save_initial_file_mtime(self.curframe)
if self._chained_exceptions:
@ -616,6 +620,13 @@ class Pdb(bdb.Bdb, cmd.Cmd):
self._chained_exceptions = tuple()
self._chained_exception_index = 0
def _get_asyncio_task(self):
try:
task = asyncio.current_task()
except RuntimeError:
task = None
return task
def interaction(self, frame, tb_or_exc):
# Restore the previous signal handler at the Pdb prompt.
if Pdb._previous_sigint_handler:
@ -626,6 +637,8 @@ class Pdb(bdb.Bdb, cmd.Cmd):
else:
Pdb._previous_sigint_handler = None
self._current_task = self._get_asyncio_task()
_chained_exceptions, tb = self._get_tb_and_exceptions(tb_or_exc)
if isinstance(tb_or_exc, BaseException):
assert tb is not None, "main exception must have a traceback"