Python – Using sys.exit() in the debugger

Using sys.exit() in the debugger… here is a solution to the problem.

Using sys.exit() in the debugger

My program ended unexpectedly. When I run it via pdb, it starts with :

The program exited via sys.exit(). Exit status:                                                                         

But there is no stack record in the instance it exits. I don’t know why this is happening. Missing step and next iterating through everything, is there a way to instruct pdb to go into the debugger when trying this instead of obeying sys.exit()?

Solution

A simple solution is to monkey patch sys.exit() before running :

$ python -m pdb my_script.py

(Pdb) def no_exit(code): raise RuntimeError('halt')
(Pdb) import sys
(Pdb) sys.exit = no_exit
(Pdb) cont

Related Problems and Solutions