-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-121468: Show asyncio information in pdb #124367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
5c52a71
a51ba04
ae16c8b
490a6d9
297a684
dcb96df
9c66d9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,6 +79,7 @@ | |
| import codeop | ||
| import pprint | ||
| import signal | ||
| import asyncio | ||
| import inspect | ||
| import textwrap | ||
| import tokenize | ||
|
|
@@ -361,6 +362,9 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None, | |
| self._chained_exceptions = tuple() | ||
| self._chained_exception_index = 0 | ||
|
|
||
| self._running_loop = None | ||
| self._current_task = None | ||
|
|
||
| def set_trace(self, frame=None): | ||
| Pdb._last_pdb_instance = self | ||
| if frame is None: | ||
|
|
@@ -407,6 +411,8 @@ def setup(self, f, tb): | |
| self.curframe_locals = self.curframe.f_locals | ||
| self.set_convenience_variable(self.curframe, '_frame', self.curframe) | ||
|
|
||
| self.set_convenience_variable(self.curframe, '_asynctask', self._current_task) | ||
|
|
||
| if self._chained_exceptions: | ||
| self.set_convenience_variable( | ||
| self.curframe, | ||
|
|
@@ -596,6 +602,38 @@ def _hold_exceptions(self, exceptions): | |
| self._chained_exceptions = tuple() | ||
| self._chained_exception_index = 0 | ||
|
|
||
| def _get_asyncio_loop_and_task(self): | ||
| try: | ||
| loop = asyncio.get_event_loop() | ||
|
||
| task = asyncio.current_task(loop) | ||
| except RuntimeError: | ||
| loop = task = None | ||
| return loop, task | ||
|
|
||
| def _asyncio_task_repr(self, task): | ||
| import asyncio.base_futures | ||
|
|
||
| if task.cancelling() and not task.done(): | ||
| status = 'cancelling' | ||
| else: | ||
| info = asyncio.base_futures._future_repr_info(task) | ||
| status = info[0] | ||
|
|
||
| coro = task._coro | ||
|
|
||
| if coro is not None: | ||
| if hasattr(coro, '__qualname__') and coro.__qualname__: | ||
| coro_name = coro.__qualname__ | ||
| elif hasattr(coro, '__name__') and coro.__name__: | ||
| coro_name = coro.__name__ | ||
| else: | ||
| # Stop masking Cython bugs, expose them in a friendly way. | ||
| coro_name = f'<{type(coro).__name__} without __name__>' | ||
| else: | ||
| coro_name = 'unknown' | ||
|
|
||
| return f"{task.get_name()}: <{coro_name} {status}>" | ||
|
|
||
| def interaction(self, frame, tb_or_exc): | ||
| # Restore the previous signal handler at the Pdb prompt. | ||
| if Pdb._previous_sigint_handler: | ||
|
|
@@ -606,6 +644,8 @@ def interaction(self, frame, tb_or_exc): | |
| else: | ||
| Pdb._previous_sigint_handler = None | ||
|
|
||
| self._running_loop, self._current_task = self._get_asyncio_loop_and_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" | ||
|
|
@@ -995,6 +1035,8 @@ def completedefault(self, text, line, begidx, endidx): | |
| # Pdb meta commands, only intended to be used internally by pdb | ||
|
|
||
| def _pdbcmd_print_frame_status(self, arg): | ||
| if self._current_task: | ||
| self.message(self._asyncio_task_repr(self._current_task)) | ||
| self.print_stack_trace(0) | ||
| self._show_display() | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 614 has deferred import of
asyncio.base_futures, so we could consider adding a lazy import ofasyncioat line 606 to improve import time?#109653
#118761
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
asyncio will be used as long as pdb is used.
pdbis not an module that is often imported explicitly or implicitly, I don't think import time ofpdbis specifically critical.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. I didn't realize that at the time