From ccb4ad92199f2a60690fe6f62f22ebd12d6232df Mon Sep 17 00:00:00 2001 From: "Adam D. Thomas" Date: Tue, 25 Feb 2025 13:27:26 +1100 Subject: gh-124703: Change back to raising bdb.BdbQuit when exiting pdb in 'inline' mode in a REPL session (#130395) --- Lib/pdb.py | 5 ++++- Lib/test/test_pdb.py | 24 ++++++++++++++++++++++ .../2025-02-21-09-05-44.gh-issue-124703.AMJD4Y.rst | 1 + 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-02-21-09-05-44.gh-issue-124703.AMJD4Y.rst diff --git a/Lib/pdb.py b/Lib/pdb.py index 08a941d..ea6a789 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -1758,7 +1758,10 @@ class Pdb(bdb.Bdb, cmd.Cmd): 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 8375327..7a99c1d 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -19,6 +19,7 @@ from test import support 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 @@ class PdbTestInline(unittest.TestCase): 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 0000000..0ec9145 --- /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`. -- cgit v0.12