How can I force PDB to quit when the signal is being caught?
Question:
PDB’s quit command works by raising an exception (Bdb.BdbQuit
). If that exception gets caught, I cannot figure out a way to kill the program short of killing the entire shell. CTRL+C works by raising a KeyboardInterrupt
exception, which can also be caught.
You can recreate this problem with this simple script.
foo = 0
while True:
try:
import pdb; pdb.set_trace()
foo += 1
except:
pass
This script cannot be stopped from within PDB with the quit command or CTRL+C.
I’m aware this is bad programming and you should never use an except
without an exception type. I ask because I ran into this issue while debugging and a third-party library trapped me in the loop.
Answers:
You can try killing the python process with os._exit
.
import os
try:
print("Exiting")
os._exit(1)
except:
print("Caught!")
Output:
Exiting
Why not re-raise the exception:
import bdb
try:
something_i_might_want_to_debug_with_pdb()
except bdb.BdbQuit as exc:
raise exc
except:
print("Caught!")
PDB’s quit command works by raising an exception (Bdb.BdbQuit
). If that exception gets caught, I cannot figure out a way to kill the program short of killing the entire shell. CTRL+C works by raising a KeyboardInterrupt
exception, which can also be caught.
You can recreate this problem with this simple script.
foo = 0
while True:
try:
import pdb; pdb.set_trace()
foo += 1
except:
pass
This script cannot be stopped from within PDB with the quit command or CTRL+C.
I’m aware this is bad programming and you should never use an except
without an exception type. I ask because I ran into this issue while debugging and a third-party library trapped me in the loop.
You can try killing the python process with os._exit
.
import os
try:
print("Exiting")
os._exit(1)
except:
print("Caught!")
Output:
Exiting
Why not re-raise the exception:
import bdb
try:
something_i_might_want_to_debug_with_pdb()
except bdb.BdbQuit as exc:
raise exc
except:
print("Caught!")