From 68d5c6e8ff77a0ac7df8d4cb1ac1fc5ccbd47f46 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 932fe45ba7f587..da179e72eb3604 100644 --- a/Lib/bdb.py +++ b/Lib/bdb.py @@ -610,6 +610,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 12f33bf4f99855..bafbe2f086e85c 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -4875,6 +4875,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.