diff --git a/Lib/bdb.py b/Lib/bdb.py index f0601913ab3c8d..179111a6ae9901 100644 --- a/Lib/bdb.py +++ b/Lib/bdb.py @@ -600,6 +600,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 8876b3f5ce37a3..697fb22e6ea8c2 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -4950,6 +4950,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.