diff --git a/Lib/bdb.py b/Lib/bdb.py index a741628e32a981..0e4ae91079377a 100644 --- a/Lib/bdb.py +++ b/Lib/bdb.py @@ -97,27 +97,30 @@ def trace_dispatch(self, frame, event, arg): The arg parameter depends on the previous event. """ - with self.set_enterframe(frame): - if self.quitting: - return # None - if event == 'line': - return self.dispatch_line(frame) - if event == 'call': - return self.dispatch_call(frame, arg) - if event == 'return': - return self.dispatch_return(frame, arg) - if event == 'exception': - return self.dispatch_exception(frame, arg) - if event == 'c_call': - return self.trace_dispatch - if event == 'c_exception': - return self.trace_dispatch - if event == 'c_return': + try: + with self.set_enterframe(frame): + if self.quitting: + return # None + if event == 'line': + return self.dispatch_line(frame) + if event == 'call': + return self.dispatch_call(frame, arg) + if event == 'return': + return self.dispatch_return(frame, arg) + if event == 'exception': + return self.dispatch_exception(frame, arg) + if event == 'c_call': + return self.trace_dispatch + if event == 'c_exception': + return self.trace_dispatch + if event == 'c_return': + return self.trace_dispatch + if event == 'opcode': + return self.dispatch_opcode(frame, arg) + print('bdb.Bdb.dispatch: unknown debugging event:', repr(event)) return self.trace_dispatch - if event == 'opcode': - return self.dispatch_opcode(frame, arg) - print('bdb.Bdb.dispatch: unknown debugging event:', repr(event)) - return self.trace_dispatch + except BdbQuit: # Make stacktrace shorter + raise BdbQuit from None def dispatch_line(self, frame): """Invoke user function and return trace function for line event. diff --git a/Lib/pdb.py b/Lib/pdb.py index beef74d792250b..9eac94c6555395 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -1723,19 +1723,26 @@ def do_debug(self, arg): def do_quit(self, arg): """q(uit) | exit - Quit from the debugger. The program being executed is aborted. + Quit from the debugger. End pdb session and possibly abort the process. """ if self.mode == 'inline': while True: try: - reply = input('Quitting pdb will kill the process. Quit anyway? [y/n] ') + reply = input('Exit pdb? [e/k/c] (end session/kill process/cancel) ') reply = reply.lower().strip() except EOFError: - reply = 'y' + reply = 'e' self.message('') - if reply == 'y' or reply == '': + + if reply in ('e', 'q', ''): + # Will raise BdbQuit and allow returning to REPL + break + elif reply == 'k': + # Kill process. Allows user to break out of infinite loop + # or exit both pdb and REPL when interactive work finished. sys.exit(0) - elif reply.lower() == 'n': + elif reply.lower() == 'c': + # Cancel and return to debugger return self._user_requested_quit = True diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 4d371a6e754b96..c63dd7588a8e9c 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -4274,23 +4274,44 @@ def _run_script(self, script, commands, ) return stdout, stderr - def test_quit(self): + def test_quit_with_BdbQuit(self): + # Test quitting with a bdb.BdbQuit exception, which allows control to + # return to the repl. script = """ x = 1 breakpoint() """ commands = """ + exit + c + p x + 1 quit - n + e + """ + + stdout, stderr = self._run_script(script, commands, expected_returncode=1) + self.assertIn("2", stdout) + self.assertIn("Exit pdb? [e/k/c]", stdout) + self.assertIn("BdbQuit", stderr) + + def test_quit_abort_process(self): + script = """ + x = 1 + breakpoint() + """ + + commands = """ + exit + c p x + 1 quit - y + k """ - stdout, stderr = self._run_script(script, commands) + stdout, stderr = self._run_script(script, commands, expected_returncode=0) self.assertIn("2", stdout) - self.assertIn("Quit anyway", stdout) + self.assertIn("Exit pdb? [e/k/c]", stdout) @support.requires_subprocess() diff --git a/Misc/NEWS.d/next/Library/2025-02-07-05-10-33.gh-issue-124703.Wyw9JQ.rst b/Misc/NEWS.d/next/Library/2025-02-07-05-10-33.gh-issue-124703.Wyw9JQ.rst new file mode 100644 index 00000000000000..eae90a0a8f2a20 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-02-07-05-10-33.gh-issue-124703.Wyw9JQ.rst @@ -0,0 +1 @@ +Allow the user to choose how to quit pdb in 'inline' mode. The user may now choose between killing the process and raising BdbQuit. This change restores the ability to return to the REPL from a pdb session.