|
| 1 | +# Item 57: Consider interactive debugging with pdb |
| 2 | + |
| 3 | + |
| 4 | +# Everyone encounters bugs in their code while developing programs. Using the |
| 5 | +# print function can help you track down the source of many issues (see Item |
| 6 | +# 55: "Use repr strings for debugging output"). Writing tests for specific |
| 7 | +# cases that cause trouble is another great way to isolate problems (see Item |
| 8 | +# 56: "Test everything with unittest"). |
| 9 | + |
| 10 | +# But these tools aren't enough to find every root cause. When you need |
| 11 | +# something more powerful, it's time to try Python's built-in interactive |
| 12 | +# debugger. The debugger lets you inspect program state, print local |
| 13 | +# variables, and set through a Python program one statement at a time. |
| 14 | + |
| 15 | +# In most other programming language, you use a debugger by specifying what |
| 16 | +# line of a source file you'd like to stop on, then execute the program. In |
| 17 | +# contrast, with Python the easiest way to use the debugger is by modifying |
| 18 | +# your program to directly initiate the debugger just before you think you'll |
| 19 | +# have an issue worth investigating. There is no difference between running a |
| 20 | +# Python program under a debugger and running it normally. |
| 21 | + |
| 22 | +# To initiate the debugger, all you have to do is import the pdb built-in |
| 23 | +# module and run its set_trace function. You'll often see this done in a |
| 24 | +# single line so programmers can comment it out with a single # character. |
| 25 | + |
| 26 | + |
| 27 | +def complex_func(a, b, c): |
| 28 | + # ... |
| 29 | + import pdb |
| 30 | + pdb.set_trace() |
| 31 | + |
| 32 | + |
| 33 | +# As soon as this statement runs, the program will pause its execution. The |
| 34 | +# terminal that started you program will turn into an interactive Python |
| 35 | +# shell. |
| 36 | + |
| 37 | +# -> import pdb; pdb.set_trace() |
| 38 | +# (Pdb) |
| 39 | + |
| 40 | +# At the (Pdb) prompt, you can type in the same of local variables to see |
| 41 | +# their values printed out. You can see a list of all local variables by |
| 42 | +# calling the locals built-in function. YOu can import modules, inspect global |
| 43 | +# state, construct new objects, run the help built-in function, and even |
| 44 | +# modify parts of the program--whatever you need to to to aid in your |
| 45 | +# debugging. In addition, the debugger has three commands that make inspecting |
| 46 | +# the running program easier. |
| 47 | +# 1. bt: Print the trackback of the current execution call back. This lets you |
| 48 | +# figure out where you are in your program anc how you arrived at the |
| 49 | +# pdb.set_trace trigger point. |
| 50 | +# 2. up: Move your scope up the function call stack to the caller of the |
| 51 | +# current function. This allows you to inspect the local variables in |
| 52 | +# higher levels of the call stack. |
| 53 | +# 3. down: Move your scope back down the function call stack one level. |
| 54 | + |
| 55 | +# Once you're done inspecting the current state, you can use debugger commands |
| 56 | +# to resume the program's execution under precise control. |
| 57 | +# 1. step: Run the program until the next line of execution in the program, |
| 58 | +# then return control back to the debugger. If the next line of execution |
| 59 | +# includes calling a function, the debugger will stop in the function that |
| 60 | +# was called. |
| 61 | +# 2. next: Run the program until the line of execution in the current |
| 62 | +# function, then return control back to the debugger. If the next line of |
| 63 | +# execution includes calling a function, the debugger will not stop until |
| 64 | +# the called function has returned. |
| 65 | +# 3. return: Run the program until the current function returns, then return |
| 66 | +# control back to the debugger. |
| 67 | +# 4. continue: Continue running the program until the next breakpoint (or |
| 68 | +# set_trace is called again). |
| 69 | + |
| 70 | + |
| 71 | +# Things to remember |
| 72 | + |
| 73 | +# 1. You can initiate the Python interactive debugger at a point of interest |
| 74 | +# directly in your program with the import pdb; pdb.set_trace() statements. |
| 75 | +# 2. The Python debugger prompt is a full Python shell that lets you inspect |
| 76 | +# and modify the state of a running program. |
| 77 | +# 3. pdb shell commands let you precisely control program execution, allowing |
| 78 | +# you to alternate between inspecting program state and progressing program |
| 79 | +# execution. |
0 commit comments