In pdb how do you reset the list (l) command line count?

Question:

From PDB

(Pdb) help l
l(ist) [first [,last]]
  List source code for the current file.
  Without arguments, list 11 lines around the current line
  or continue the previous listing.
  With one argument, list 11 lines starting at that line.
  With two arguments, list the given range;
  if the second argument is less than the first, it is a count.

The “continue the previous listing” feature is really nice, but how do you turn it off?

Asked By: Jorge Vargas

||

Answers:

I don’t think there is a way to turn it off. It’s annoyed me enough that once I went looking in the pdb source to see if there was an undocumented syntax, but I didn’t find any.

There really needs to be a syntax that means, “List the lines near the current execution pointer.”

Answered By: Ned Batchelder

You could monkey patch it for the behavior you want. For example, here is a full script which adds a “reset_list” or “rl” command to pdb:

import pdb

def Pdb_reset_list(self, arg):
    self.lineno = None
    print >>self.stdout, "Reset list position."
pdb.Pdb.do_reset = Pdb_reset_list
pdb.Pdb.do_rl = Pdb_reset_list

a = 1
b = 2

pdb.set_trace()

print a, b

One could conceivably monkey patch the standard list command to not retain the lineno history.

edit: And here is such a patch:

import pdb
Pdb = pdb.Pdb

Pdb._do_list = Pdb.do_list
def pdb_list_wrapper(self, arg):
    if arg.strip().lower() in ('r', 'reset', 'c', 'current'):
        self.lineno = None
        arg = ''
    self._do_list(arg)
Pdb.do_list = Pdb.do_l = pdb_list_wrapper

a = 1
b = 2

pdb.set_trace()

print a, b
Answered By: Mike Boers

If you use epdb instead of pdb, you can use "l" to go forward like in pdb, but then "l." goes back to the current line number, and "l-" goes backwards through the file. You can also use until # to continue until a given line. Epdb offers a bunch of other niceties too. Need to debug remotely? Try serve() instead of set_trace() and then telnet in (port 8080 is the default port).

import epdb
epdb.serve()
Answered By: Joseph Tate

Late but hopefully still helpful. In pdb, make the following alias (which you can add to your .pdbrc file so it’s always available):

alias ll u;;d;;l

Then whenever you type ll, pdb will list from the current position. It works by going up the stack and then down the stack, which resets ‘l’ to show from the current position. (This won’t work if you are at the top of the stack trace.)

Answered By: Ghopper21

Try this.

(pdb) l .

Maybe you can always type the dot.

ps.
You may consider to use pudb. This is a nice UI to pdb what gdbtui is to gdb.

Answered By: plhn
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.