Context Managers for Debugging (Python)
A small helper I use to debug some python programs:
from contextlib import contextmanager
@contextmanager
def debug_error():
try:
yield
except Exception as e:
breakpoint()
quit()
### usage
with debug_error():
x = 1
y = 0
x/y # x and y will be avail in pdb
You can also put things before the yield
(which will run before the with
block), and after it (to run at the end of the block.) Anything put after it will run at the end of the block. See @contextmanager for more info.