Source From Here
Question
Is there a way to automatically start break into the debugger (import pdb; pdb.set_trace()) whenever a exception is thrown to inspect the local stack?
HowTo
I found what I was looking for in an answer to What is the simplest way of using Python pdb to in...use of an unhandled exception?:
- test.py
Let's test it:
Is there a way to automatically start break into the debugger (import pdb; pdb.set_trace()) whenever a exception is thrown to inspect the local stack?
HowTo
I found what I was looking for in an answer to What is the simplest way of using Python pdb to in...use of an unhandled exception?:
- test.py
- #!/usr/bin/env python3
- import pdb
- import sys
- import functools
- def debug_on(*exceptions):
- if not exceptions:
- exceptions = (Exception, )
- def decorator(f):
- @functools.wraps(f)
- def wrapper(*args, **kwargs):
- try:
- return f(*args, **kwargs)
- except exceptions:
- pdb.post_mortem(sys.exc_info()[2])
- return wrapper
- return decorator
- @debug_on()
- def buggy_function():
- print("About to cause exception!")
- raise Exception("Something wrong!")
- print("Unreachable...")
- if __name__ == '__main__':
- buggy_function()
沒有留言:
張貼留言