Skip to content

gh-124703: Allow the user to choose how to quit pdb in 'inline' mode #129768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions Lib/bdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 12 additions & 5 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 26 additions & 5 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading