When pdb / ipdb is used in post-mortem mode, it drops you into an interactive shell where you can explore the environment at the stack frame of the most deeply nested code. I'm usually not interested in the the situation several levels down into some library -- I'm interested in my function because that's what really caused the problem. So, most of the time I have to press "u" several times to get back up to the level of the code I wrote.
Is there a way I can jump to the "top" to speed this up? Or even better, a shortcut to go straight to a particular stack frame?
(By the way, the stack feels a little "upside down" to me here. A function that calls another function puts the new call on the top of the stack, right? So I feel like the pdb u(p) command is actually moving you down into the stack...)
Starting Python Debugger To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().
The docs say: j(ump) lineno Set the next line that will be executed. Only available in the bottom-most frame. This lets you jump back and execute code again, or jump forward to skip code that you don't want to run.
pdb. set_trace (*, header=None) Enter the debugger at the calling stack frame. This is useful to hard-code a breakpoint at a given point in a program, even if the code is not otherwise being debugged (e.g. when an assertion fails). If given, header is printed to the console just before debugging begins.
Once you get you pdb prompt . Just hit n (next) 10 times to exit the loop.
You could do "up n" with an arbitrarily high value for n, like 99: https://docs.python.org/3/library/pdb.html#pdbcommand-up
PS. It was mentioned by Chris in a comment on the OP. I do re-post it as an answer to make it more visible, and with a link to the documentation.
You can use a PdbExtension for this.
See https://github.com/fschulze/pytest-pdb/pull/5 where this is about to be added to pytest-pdb.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With