Sub topic 3
Reading the (Pdb) prompt and getting help
This chapter explains what pdb prints when it stops your program, how it decides whether a line you type is a debugger command or Python code, and how to get help from inside a session. It assumes you can run Python scripts from a terminal, read a traceback, and have already started a session with breakpoint() or python -m pdb. You need nothing beyond that.
Anatomy of a stop
Save this file as mean.py. It is the same example used when you first paused a program with breakpoint().
def mean(values):
total = sum(values)
breakpoint()
return total / len(values)
print(mean([]))Run python mean.py and pdb prints this:
> /home/user/mean.py(4)mean()
-> return total / len(values)
(Pdb)The output has three parts.
- The line starting with
>names the current frame. It gives the file path, the line number in parentheses, and the function name. Here that is line 4 ofmean.py, insidemean(). At module level the name is<module>. - The line starting with
->is the source line that is about to run. It has not run yet. (Pdb)is the prompt. pdb is waiting for a command.
The stop is at line 4, not line 3, even though breakpoint() is written on line 3. The call itself has already happened, so pdb pauses at the next line of the caller. If breakpoint() is the last statement in a function, the stop lands on the function's return instead.
Other markers you will see
pdb adds a marker line when the stop is not an ordinary line event.
| Marker | Meaning |
|---|---|
| `--Call--` | You stepped into a function and are about to run its first line. |
| `--Return--` | The current function is about to return. The header ends with `->value`, the return value. |
| `--Exception--` | An exception is propagating through this frame. The exception type and message follow. |
| `[EOF]` | The `list` command reached the end of the file. |
For example, stepping over return n in a function that returns 2 prints this:
--Return--
> /home/user/words.py(4)count_words()->2
-> return nThe ->2 after count_words() is the value being returned. This is a quick way to check what a function returns without adding a print.
Commands versus Python code
Everything you type at (Pdb) goes through one rule. If the first word is a pdb command or one of its aliases, pdb runs that command. Otherwise pdb treats the line as Python and runs it in the current frame, with the frame's local and global variables in scope.
This lets you inspect state without any special command:
(Pdb) values
[]
(Pdb) len(values)
0
(Pdb) total
0
(Pdb) total / 1
0.0Expressions print their value, as in the interactive interpreter. Statements such as assignments also work, and they change the paused program's variables.
When a variable has a command's name
The one-letter commands a, b, c, d, h, l, n, p, q, r, s, u, and w are easy to collide with. A variable named n is common. Consider this script:
def count_words(words):
n = len(words)
breakpoint()
return n
count_words(["a", "b"])At the prompt, n does not show the variable. It runs the next command:
> /home/user/words.py(4)count_words()
-> return n
(Pdb) n
--Return--
> /home/user/words.py(4)count_words()->2
-> return n
(Pdb) !n
2
(Pdb) p n
2A leading ! forces pdb to run the line as Python, so !n prints the variable. p n works too, because p is the command and n is its argument. The same applies to variables named c, l, s, and the rest.
Do not name a variable help, list, or next and then print it as a bare word. The words refer to pdb commands first. The ! prefix gets around this too.
Repeating commands and chaining them
Pressing Enter on an empty line repeats the previous command. After n, pressing Enter again steps another line, which is faster than retyping n. The exception is list. After a list command, Enter shows the next eleven lines instead of the same ones.
You can put several commands on one line by separating them with ;;:
(Pdb) p total ;; p values ;; nA single ; would not work, because Python would take it as a statement separator. Use ;; for pdb commands.
Getting help inside the debugger
You do not need to memorize the command list. pdb documents itself.
h(orhelp) with no argument lists every documented command.h commandprints the documentation for one command, such ash untilorh commands.help pdbprints the full pdb documentation, the same text as the module's reference page.help execexplains the!prefix.
The no-argument listing looks like this. The exact set varies a little between Python versions.
(Pdb) h
Documented commands (type help <topic>):
========================================
EOF c d h list q rv undisplay
a cl debug help ll quit s unt
alias clear disable ignore longlist r source until
args commands display interact n restart step up
b condition down j next return tbreak w
break cont enable jump p retval u whatis
bt continue exit l pp run unalias where
Miscellaneous help topics:
==========================
exec pdbNames on the same row are not related. Most commands have a short alias, such as n for next, and h n shows both forms together.
Commands used most at first
| Short form | Full name | What it does |
|---|---|---|
| `n` | `next` | Run the current line, stepping over calls. |
| `s` | `step` | Run the current line, stepping into calls. |
| `c` | `continue` | Resume until the next breakpoint or the end. |
| `l` | `list` | Show source around the current line. |
| `w` | `where` | Print the call stack. |
| `p expr` | `p` | Print the value of an expression. |
| `pp expr` | `pp` | Pretty-print the value of an expression. |
| `h` | `help` | Show help. |
| `q` | `quit` | Leave the debugger and stop the program. |
Later chapters cover each of these in depth. For now, you only need to be able to find them.
Leaving a session
There are two ways out, and they behave differently.
c(continue) lets the program run on. pdb stays active and will stop again at the nextbreakpoint()call or breakpoint you set.q(quit) ends the session and raisesBdbQuitinside your program, which stops it. Pressing Ctrl-D at an empty prompt has the same effect.
If you only want to stop being interrupted, use PYTHONBREAKPOINT=0 (covered in a later chapter). If you hit q by accident, the program is gone and you must rerun it.
Related prompts
pdb changes its prompt in a few situations. Recognizing them tells you which mode you are in.
(Pdb)is the normal command prompt.(com)appears while you define a breakpoint'scommandslist. It accepts commands until you typeend.>>>appears afterinteract, which opens a full Python shell with the current frame's variables. Press Ctrl-D to return to(Pdb).
If you see (Pdb) but nothing responds the way you expect, check the > line first. A common mistake is debugging the wrong frame after moving up the stack, and the frame header shows that at once.
Exercise
A paused function has a local variable named l that holds a list. Typing l prints source code instead of the list. Give two ways to see the variable's value, and say what pressing Enter on an empty line does right after a list command.
Show answer
Use p l or !l (pp l also works). The p command receives l as an argument, and the ! prefix forces Python evaluation. After a list command, an empty line shows the next eleven lines of source instead of repeating the same listing.