diff --git a/Lib/pdb.py b/Lib/pdb.py index 458eb835265236..130e05bc3b6af2 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -84,7 +84,7 @@ import signal import socket import typing -import asyncio +lazy import asyncio import inspect import weakref import builtins @@ -902,6 +902,10 @@ def _hold_exceptions(self, exceptions): self._chained_exception_index = 0 def _get_asyncio_task(self): + # If asyncio has never been imported there cannot be a running task, + # so skip the import rather than pay for it on every interaction. + if 'asyncio' not in sys.modules: + return None try: task = asyncio.current_task() except RuntimeError: diff --git a/Misc/NEWS.d/next/Library/2026-08-31-11-40-00.gh-issue-156774.Pd8Lz1.rst b/Misc/NEWS.d/next/Library/2026-08-31-11-40-00.gh-issue-156774.Pd8Lz1.rst new file mode 100644 index 00000000000000..85104694a6b2ef --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-31-11-40-00.gh-issue-156774.Pd8Lz1.rst @@ -0,0 +1,3 @@ +Speed up :mod:`pdb` startup by deferring the :mod:`asyncio` import, and by +skipping it entirely in ``Pdb._get_asyncio_task()`` when no event loop has +been used.