https://github.com/python/cpython/commit/024b6bceb1b1b3c52ccaa431f154d28d94b0bef5
commit: 024b6bceb1b1b3c52ccaa431f154d28d94b0bef5
branch: main
author: Brittany Reynoso <[email protected]>
committer: hugovk <[email protected]>
date: 2026-09-08T13:22:45+03:00
summary:

gh-156774: Speed up pdb startup with asyncio guard (#156775)

files:
A Misc/NEWS.d/next/Library/2026-08-31-11-40-00.gh-issue-156774.Pd8Lz1.rst
M Lib/pdb.py

diff --git a/Lib/pdb.py b/Lib/pdb.py
index 458eb835265236..c3440a5d11a588 100644
--- a/Lib/pdb.py
+++ b/Lib/pdb.py
@@ -84,7 +84,6 @@
 import signal
 import socket
 import typing
-import asyncio
 import inspect
 import weakref
 import builtins
@@ -102,6 +101,8 @@
 from types import CodeType
 from warnings import deprecated
 
+lazy import asyncio
+
 try:
     import _pyrepl.utils
 except ModuleNotFoundError:
@@ -902,6 +903,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..f5f64c494296cc
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-08-31-11-40-00.gh-issue-156774.Pd8Lz1.rst
@@ -0,0 +1,2 @@
+Speed up :mod:`pdb` startup by only importing :mod:`asyncio` when the program
+being debugged has already imported it.

_______________________________________________
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