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.