From 6a22709db142028e6bf5b54421483c153c69980d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?tonghuaroot=20=28=E7=AB=A5=E8=AF=9D=29?= Date: Sun, 25 Oct 2026 14:52:29 +0800 Subject: [PATCH] gh-160643: Reset Bdb.enterframe when trace dispatch raises An exception escaping trace_dispatch(), such as BdbQuit after q in the interactive interpreter, left enterframe set, so every later breakpoint() returned early. Reset it in a finally block. --- Lib/bdb.py | 6 ++++-- Lib/test/test_pdb.py | 21 +++++++++++++++++++ ...-10-24-17-46-57.gh-issue-160643.xOwxMs.rst | 3 +++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-10-24-17-46-57.gh-issue-160643.xOwxMs.rst diff --git a/Lib/bdb.py b/Lib/bdb.py index 2ff52aebfc9822..3749078f4d8e70 100644 --- a/Lib/bdb.py +++ b/Lib/bdb.py @@ -251,8 +251,10 @@ def reset(self): @contextmanager def set_enterframe(self, frame): self.enterframe = frame - yield - self.enterframe = None + try: + yield + finally: + self.enterframe = None def trace_dispatch(self, frame, event, arg): """Dispatch a trace function for debugged frames based on the event. diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index cfcc8c6df57b94..699cb95ba8bdbc 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -5047,6 +5047,27 @@ def test_nested_set_trace(self): self.assertIn('42', stdout) self.assertEqual(stderr, '') + def test_set_trace_after_quit(self): + script = """ + import bdb + import sys + # Make quit raise BdbQuit as in the interactive interpreter. + sys.ps1 = '>>> ' + try: + breakpoint() + except bdb.BdbQuit: + pass + breakpoint() + """ + commands = """ + q + p 'stopped' + 'again' + c + """ + stdout, stderr = self._run_script(script, commands) + self.assertIn("'stoppedagain'", 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-24-17-46-57.gh-issue-160643.xOwxMs.rst b/Misc/NEWS.d/next/Library/2026-10-24-17-46-57.gh-issue-160643.xOwxMs.rst new file mode 100644 index 00000000000000..7ceec016acca8d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-24-17-46-57.gh-issue-160643.xOwxMs.rst @@ -0,0 +1,3 @@ +Fix :func:`breakpoint` and :func:`pdb.set_trace` being ignored after a +previous debugger session was ended by an exception, for example by quitting +with ``q`` in the interactive interpreter.