diff --git a/Lib/pdb.py b/Lib/pdb.py index 08a941de79ec55..ea6a7890f8c2bc 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -1758,7 +1758,10 @@ def do_quit(self, arg): Quit from the debugger. The program being executed is aborted. """ - if self.mode == 'inline': + # Show prompt to kill process when in 'inline' mode and if pdb was not + # started from an interactive console. The attribute sys.ps1 is only + # defined if the interpreter is in interactive mode. + if self.mode == 'inline' and not hasattr(sys, 'ps1'): while True: try: reply = input('Quitting pdb will kill the process. Quit anyway? [y/n] ') diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 83753279599f76..7a99c1db84b439 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -19,6 +19,7 @@ from test.support import force_not_colorized, os_helper from test.support.import_helper import import_module from test.support.pty_helper import run_pty, FakeInput +from test.support.script_helper import kill_python from unittest.mock import patch SKIP_CORO_TESTS = False @@ -4342,6 +4343,29 @@ def test_quit(self): self.assertEqual(stdout.count("Quit anyway"), 2) +@support.force_not_colorized_test_class +@support.requires_subprocess() +class TestREPLSession(unittest.TestCase): + def test_return_from_inline_mode_to_REPL(self): + # GH-124703: Raise BdbQuit when exiting pdb in REPL session. + # This allows the REPL session to continue. + from test.test_repl import spawn_repl + p = spawn_repl() + user_input = """ + x = 'Spam' + import pdb + pdb.set_trace(commands=['x + "During"', 'q']) + x + 'After' + """ + p.stdin.write(textwrap.dedent(user_input)) + output = kill_python(p) + self.assertIn('SpamDuring', output) + self.assertNotIn("Quit anyway", output) + self.assertIn('BdbQuit', output) + self.assertIn('SpamAfter', output) + self.assertEqual(p.returncode, 0) + + @support.requires_subprocess() class PdbTestReadline(unittest.TestCase): def setUpClass(): diff --git a/Misc/NEWS.d/next/Library/2025-02-21-09-05-44.gh-issue-124703.AMJD4Y.rst b/Misc/NEWS.d/next/Library/2025-02-21-09-05-44.gh-issue-124703.AMJD4Y.rst new file mode 100644 index 00000000000000..0ec9145be6ae63 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-02-21-09-05-44.gh-issue-124703.AMJD4Y.rst @@ -0,0 +1 @@ +Executing ``quit`` command in :mod:`pdb` will raise :exc:`bdb.BdbQuit` when :mod:`pdb` is started from an interactive console using :func:`breakpoint` or :func:`pdb.set_trace`.