Show context after every move using Python pdb

0

Can I show the context any time I step through code using Python's pdb debugger without having to explicitly call the list command?

I've tried chaining commands with something like n & l or n && l or nl or n + l or n; l. I can't find any documentation regarding this.

The trouble is, any time I step through the code, I end up typing n RET and then either l RET or l l RET...every..single..time to see some context.

Steve Ferg's guide describes the apparent intended pdb workflow accurately:

So a typical interaction with pdb might go like this

  • The pdb.set_trace() statement is encountered, and you start tracing with the (Pdb) prompt
  • You press “n” and then ENTER, to start stepping through your code.
  • You just press ENTER to step again.
  • You just press ENTER to step again.
  • You just press ENTER to step again. etc. etc. etc.
  • Eventually, you realize that you are a bit lost. You’re not exactly sure where you are in your program any more. So…
  • You press “l” and then ENTER. This lists the area of your program that is currently being executed.
  • You inspect the display, get your bearings, and are ready to start again. So….
  • You press “n” and then ENTER, to start stepping through your code.
  • You just press ENTER to step again.
  • You just press ENTER to step again. etc. etc. etc.

It seems obvious to me that showing the context after every move would be useful and desired by the user. However, since there doesn't seem to be a simple option to do so, it makes me think that I'm using pdb incorrectly. Maybe my constant need to see the context is an indication of misuse? But how else would I use pdb?

Lorem Ipsum

Posted 2018-11-01T16:39:55.173

Reputation: 185

Answers

0

This can be done by creating a .pdbrc file and using the alias command.

In your .pdbrc file put,

alias n next ;; l
alias s step ;; l

Then when you press n, the command next ;; l will be issued instead and similarly for s. The double-semi colons separate commands so that it's like you pressed next RET l RET.

You can read more about the alias command, and others, in the pdb documentation.

Be warned that getting pdb to read the .pdbrc file is a bit of a hassle on Windows. pdb looks for the HOME system variable which is not available by default on Windows. You'll have to manually create a HOME system variable and put the folder containing .pdcrc into it. I have documented specifically how to do this in another response: How can I define .pdbrc on a Windows machine?

Lorem Ipsum

Posted 2018-11-01T16:39:55.173

Reputation: 185