Skip to content

Instantly share code, notes, and snippets.

@abhayraw1
Created April 15, 2019 06:42
Show Gist options
  • Save abhayraw1/19d783aa403cc3720c84d1a796742a87 to your computer and use it in GitHub Desktop.
Save abhayraw1/19d783aa403cc3720c84d1a796742a87 to your computer and use it in GitHub Desktop.
import sys, code, traceback, pdb
from contextlib import contextmanager
""" Context Manager for launching pdb interactive right at the point of failure.
Mixed and matched from:
[1]: ieure/error_debug.py [https://gist.github.com/ieure/193277]
[2]: Florian Bosch's answer on SO [https://stackoverflow.com/questions/242485/starting-python-debugger-automatically-on-error]
"""
@contextmanager
def debug(use_pdb=True):
try:
yield
except Exception as e:
if not use_pdb:
raise
debug_here()
def debug_here():
type, value, tb = sys.exc_info()
traceback.print_exc()
last_frame = lambda tb=tb: last_frame(tb.tb_next) if tb.tb_next else tb
frame = last_frame().tb_frame
ns = dict(frame.f_globals)
ns.update(frame.f_locals)
code.interact(local=ns)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment