https://github.com/python/cpython/commit/514cfe739e8559d964a216e20358ae6c0f8831fa
commit: 514cfe739e8559d964a216e20358ae6c0f8831fa
branch: main
author: Tian Gao <[email protected]>
committer: gaogaotiantian <[email protected]>
date: 2026-10-20T15:47:31-07:00
summary:

gh-138641: Ignore set_trace() when we are already in pdb (#159918)

files:
A Misc/NEWS.d/next/Library/2026-10-20-17-29-14.gh-issue-138641.FN6qM0.rst
M Lib/bdb.py
M Lib/test/test_pdb.py

diff --git a/Lib/bdb.py b/Lib/bdb.py
index d654f27e5b90e0..7acd359855ffc6 100644
--- a/Lib/bdb.py
+++ b/Lib/bdb.py
@@ -601,6 +601,13 @@ def set_trace(self, frame=None):
 
         If frame is not specified, debugging starts from caller's frame.
         """
+
+        if self.enterframe is not None:
+            # If self.enterframe is not None, it means we are already in
+            # dispatched functions. Tracing is disabled when calling a
+            # trace function, so we just skip this set_trace() call.
+            return
+
         self.stop_trace()
         if frame is None:
             frame = sys._getframe().f_back
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index 8a9c55838ededb..b3c11db79a7b93 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -4979,6 +4979,18 @@ def foo():
         stdout, _ = self._run_script(script, commands)
         self.assertIn("42", stdout)
 
+    def test_nested_set_trace(self):
+        # Calling breakpoint() in pdb prompt should just be ignored.
+        script = "breakpoint()"
+        commands = """
+            breakpoint()
+            p 40 + 2
+            c
+        """
+        stdout, stderr = self._run_script(script, commands)
+        self.assertIn('42', stdout)
+        self.assertEqual(stderr, '')
+
     def test_readline_not_imported(self):
         """GH-138860
         Directly or indirectly importing readline might deadlock a subprocess
diff --git 
a/Misc/NEWS.d/next/Library/2026-10-20-17-29-14.gh-issue-138641.FN6qM0.rst 
b/Misc/NEWS.d/next/Library/2026-10-20-17-29-14.gh-issue-138641.FN6qM0.rst
new file mode 100644
index 00000000000000..6d9ac174282839
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-10-20-17-29-14.gh-issue-138641.FN6qM0.rst
@@ -0,0 +1 @@
+Ignore :func:`pdb.set_trace` when called directly at the :mod:`pdb` prompt.

_______________________________________________
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