Question
I've written a function to enter PDB when an exception is raised (let's call it trace_on_error). Right now when I call pdb.set_trace(), pdb reasonably enters into the stack frame of trace_on_error, requiring me to have to type the up command before being able to look at the frame of the calling function.
I am trying to make trace_on_error not require users to know how its implemented to use, and thus i would like to have pdb enter into the callers stack frame. As I looked for documentation, i was hoping to find something similar to pdb.set_trace(frame_up=1), but I have not found anything.
Example Code:
- def trace_on_error(f, errors):
- try:
- return f()
- except errors as e:
- pdb.set_trace()
This should do the trick (I've tested it but not sure it works in every possible case):
- def trace_on_error(f, errors):
- try:
- return f()
- except errors as e:
- import sys
- from pdb import Pdb
- Pdb().set_trace(sys._getframe().f_back)
Supplement
* Python standard library: inspect — Inspect live objects
沒有留言:
張貼留言