From 4f910af37df383ab3f222741a1c04bbe12c633f3 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Tue, 20 Oct 2026 15:47:31 -0700 Subject: [PATCH] gh-138641: Ignore set_trace() when we are already in pdb (GH-159918) (cherry picked from commit 514cfe739e8559d964a216e20358ae6c0f8831fa) Co-authored-by: Tian Gao --- Lib/bdb.py | 7 +++++++ Lib/test/test_pdb.py | 12 ++++++++++++ .../2026-10-20-17-29-14.gh-issue-138641.FN6qM0.rst | 1 + 3 files changed, 20 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-10-20-17-29-14.gh-issue-138641.FN6qM0.rst 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.