https://github.com/python/cpython/commit/041150c34c13644547e06c7084035172ab501241
commit: 041150c34c13644547e06c7084035172ab501241
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: gaogaotiantian <[email protected]>
date: 2026-10-21T00:29:22Z
summary:

[3.15] gh-138641: Ignore set_trace() when we are already in pdb (GH-159918) 
(#159970)

gh-138641: Ignore set_trace() when we are already in pdb (GH-159918)
(cherry picked from commit 514cfe739e8559d964a216e20358ae6c0f8831fa)

Co-authored-by: Tian Gao <[email protected]>

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 d969dcfe3acde5..7da8660710a78e 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 d8f022a6f718c2..1e6ca24b08af1d 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