<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>pdb – Interactive Debugger — Python Module of the Week</title> <link rel="stylesheet" href="../_static/sphinxdoc.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '1.132', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <link rel="author" title="About these documents" href="../about.html" /> <link rel="top" title="Python Module of the Week" href="../index.html" /> <link rel="up" title="Development Tools" href="../dev_tools.html" /> <link rel="next" title="Debugging and Profiling" href="../profilers.html" /> <link rel="prev" title="unittest – Automated testing framework" href="../unittest/index.html" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="../profilers.html" title="Debugging and Profiling" accesskey="N">next</a> |</li> <li class="right" > <a href="../unittest/index.html" title="unittest – Automated testing framework" accesskey="P">previous</a> |</li> <li><a href="../contents.html">PyMOTW</a> »</li> <li><a href="../dev_tools.html" accesskey="U">Development Tools</a> »</li> </ul> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">pdb – Interactive Debugger</a><ul> <li><a class="reference internal" href="#starting-the-debugger">Starting the Debugger</a><ul> <li><a class="reference internal" href="#from-the-command-line">From the Command Line</a></li> <li><a class="reference internal" href="#within-the-interpreter">Within the Interpreter</a></li> <li><a class="reference internal" href="#from-within-your-program">From Within Your Program</a></li> <li><a class="reference internal" href="#after-a-failure">After a Failure</a></li> </ul> </li> <li><a class="reference internal" href="#controlling-the-debugger">Controlling the Debugger</a><ul> <li><a class="reference internal" href="#navigating-the-execution-stack">Navigating the Execution Stack</a></li> <li><a class="reference internal" href="#examining-variables-on-the-stack">Examining Variables on the Stack</a></li> <li><a class="reference internal" href="#stepping-through-your-program">Stepping Through Your Program</a></li> </ul> </li> <li><a class="reference internal" href="#breakpoints">Breakpoints</a><ul> <li><a class="reference internal" href="#managing-breakpoints">Managing Breakpoints</a></li> <li><a class="reference internal" href="#temporary-breakpoints">Temporary Breakpoints</a></li> <li><a class="reference internal" href="#conditional-breakpoints">Conditional Breakpoints</a></li> <li><a class="reference internal" href="#ignoring-breakpoints">Ignoring Breakpoints</a></li> <li><a class="reference internal" href="#triggering-actions-on-a-breakpoint">Triggering Actions on a Breakpoint</a></li> </ul> </li> <li><a class="reference internal" href="#changing-execution-flow">Changing Execution Flow</a><ul> <li><a class="reference internal" href="#jump-ahead">Jump Ahead</a></li> <li><a class="reference internal" href="#jump-back">Jump Back</a></li> <li><a class="reference internal" href="#illegal-jumps">Illegal Jumps</a></li> <li><a class="reference internal" href="#restarting-your-program">Restarting Your Program</a></li> </ul> </li> <li><a class="reference internal" href="#customizing-the-debugger-with-aliases">Customizing the Debugger with Aliases</a></li> <li><a class="reference internal" href="#saving-configuration-settings">Saving Configuration Settings</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="../unittest/index.html" title="previous chapter">unittest – Automated testing framework</a></p> <h4>Next topic</h4> <p class="topless"><a href="../profilers.html" title="next chapter">Debugging and Profiling</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/pdb/index.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" size="18" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-pdb"> <span id="pdb-interactive-debugger"></span><h1>pdb – Interactive Debugger<a class="headerlink" href="#module-pdb" title="Permalink to this headline">¶</a></h1> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field"><th class="field-name">Purpose:</th><td class="field-body">Python’s Interactive Debugger</td> </tr> <tr class="field"><th class="field-name">Python Version:</th><td class="field-body">1.4 and later</td> </tr> </tbody> </table> <p><a class="reference internal" href="#module-pdb" title="pdb: Interactive Debugger"><tt class="xref py py-mod docutils literal"><span class="pre">pdb</span></tt></a> implements an interactive debugging environment for Python programs. It includes features to let you pause your program, look at the values of variables, and watch program execution step-by-step, so you can understand what your program actually does and find bugs in the logic.</p> <div class="section" id="starting-the-debugger"> <h2>Starting the Debugger<a class="headerlink" href="#starting-the-debugger" title="Permalink to this headline">¶</a></h2> <p>The first step to using <a class="reference internal" href="#module-pdb" title="pdb: Interactive Debugger"><tt class="xref py py-mod docutils literal"><span class="pre">pdb</span></tt></a> is causing the interpreter to enter the debugger when you want it to. There are a few different ways to do that, depending on your starting conditions and what you need to debug.</p> <div class="section" id="from-the-command-line"> <h3>From the Command Line<a class="headerlink" href="#from-the-command-line" title="Permalink to this headline">¶</a></h3> <p>The most straightforward way to use the debugger is to run it from the command line, giving it your own program as input so it knows what to run.</p> <div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18</pre></div></td><td class="code"><div class="highlight"><pre><span class="c">#!/usr/bin/env python</span> <span class="c"># encoding: utf-8</span> <span class="c">#</span> <span class="c"># Copyright (c) 2010 Doug Hellmann. All rights reserved.</span> <span class="c">#</span> <span class="k">class</span> <span class="nc">MyObj</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num_loops</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="n">num_loops</span> <span class="k">def</span> <span class="nf">go</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">):</span> <span class="k">print</span> <span class="n">i</span> <span class="k">return</span> <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span> <span class="n">MyObj</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span><span class="o">.</span><span class="n">go</span><span class="p">()</span> </pre></div> </td></tr></table></div> <p>Running the debugger from the command line causes it to load your source file and stop execution on the first statement it finds. In this case, it stops before evaluating the definition of the class <tt class="xref py py-class docutils literal"><span class="pre">MyObj</span></tt> on line 7.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_script.py > .../pdb_script.py(7)<module>() -> """ (Pdb)</pre> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Normally <a class="reference internal" href="#module-pdb" title="pdb: Interactive Debugger"><tt class="xref py py-mod docutils literal"><span class="pre">pdb</span></tt></a> includes the full path to each module in the output when printing a filename. In order to maintain clear examples, the sample output in this section replaces the path with <tt class="docutils literal"><span class="pre">...</span></tt>.</p> </div> </div> <div class="section" id="within-the-interpreter"> <h3>Within the Interpreter<a class="headerlink" href="#within-the-interpreter" title="Permalink to this headline">¶</a></h3> <p>Many Python developers work with the interactive interpreter while developing early versions of modules because it lets them experiment more iteratively without the save/run/repeat cycle needed when creating standalone scripts. To run the debugger from within an interactive interpreter, use <tt class="xref py py-func docutils literal"><span class="pre">run()</span></tt> or <tt class="xref py py-func docutils literal"><span class="pre">runeval()</span></tt>.</p> <div class="highlight-python"><pre>$ python Python 2.7 (r27:82508, Jul 3 2010, 21:12:11) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import pdb_script >>> import pdb >>> pdb.run('pdb_script.MyObj(5).go()') > <string>(1)<module>() (Pdb)</pre> </div> <p>The argument to <tt class="xref py py-func docutils literal"><span class="pre">run()</span></tt> is a string expression that can be evaluated by the Python interpreter. The debugger will parse it, then pause execution just before the first expression evaluates. You can use the debugger commands described below to navigate and control the execution.</p> </div> <div class="section" id="from-within-your-program"> <h3>From Within Your Program<a class="headerlink" href="#from-within-your-program" title="Permalink to this headline">¶</a></h3> <p>Both of the previous examples assume you want to start the debugger at the beginning of your program. For a long-running process where the problem appears much later in the program execution, it will be more convenient to start the debugger from inside your program using <tt class="xref py py-func docutils literal"><span class="pre">set_trace()</span></tt>.</p> <div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21</pre></div></td><td class="code"><div class="highlight"><pre><span class="c">#!/usr/bin/env python</span> <span class="c"># encoding: utf-8</span> <span class="c">#</span> <span class="c"># Copyright (c) 2010 Doug Hellmann. All rights reserved.</span> <span class="c">#</span> <span class="kn">import</span> <span class="nn">pdb</span> <span class="k">class</span> <span class="nc">MyObj</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num_loops</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="n">num_loops</span> <span class="k">def</span> <span class="nf">go</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">):</span> <span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span> <span class="k">print</span> <span class="n">i</span> <span class="k">return</span> <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span> <span class="n">MyObj</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span><span class="o">.</span><span class="n">go</span><span class="p">()</span> </pre></div> </td></tr></table></div> <p>Line 16 of the sample script triggers the debugger at that point in execution.</p> <div class="highlight-python"><pre>$ python ./pdb_set_trace.py > .../pdb_set_trace.py(17)go() -> print i (Pdb)</pre> </div> <p><tt class="xref py py-func docutils literal"><span class="pre">set_trace()</span></tt> is just a Python function, so you can call it at any point in your program. This lets you enter the debugger based on conditions inside your program, including from an exception handler or via a specific branch of a control statement.</p> </div> <div class="section" id="after-a-failure"> <h3>After a Failure<a class="headerlink" href="#after-a-failure" title="Permalink to this headline">¶</a></h3> <p>Debugging a failure after a program terminates is called <em>post-mortem</em> debugging. <a class="reference internal" href="#module-pdb" title="pdb: Interactive Debugger"><tt class="xref py py-mod docutils literal"><span class="pre">pdb</span></tt></a> supports post-mortem debugging through the <tt class="xref py py-func docutils literal"><span class="pre">pm()</span></tt> and <tt class="xref py py-func docutils literal"><span class="pre">post_mortem()</span></tt> functions.</p> <div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15</pre></div></td><td class="code"><div class="highlight"><pre><span class="c">#!/usr/bin/env python</span> <span class="c"># encoding: utf-8</span> <span class="c">#</span> <span class="c"># Copyright (c) 2010 Doug Hellmann. All rights reserved.</span> <span class="c">#</span> <span class="k">class</span> <span class="nc">MyObj</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num_loops</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="n">num_loops</span> <span class="k">def</span> <span class="nf">go</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">num_loops</span><span class="p">):</span> <span class="k">print</span> <span class="n">i</span> <span class="k">return</span> </pre></div> </td></tr></table></div> <p>Here the incorrect attribute name on line 13 triggers an <a class="reference internal" href="../exceptions/index.html#exceptions-attributeerror"><em>AttributeError</em></a> exception, causing execution to stop. <tt class="xref py py-func docutils literal"><span class="pre">pm()</span></tt> looks for the active traceback and starts the debugger at the point in the call stack where the exception occurred.</p> <div class="highlight-python"><pre>$ python Python 2.7 (r27:82508, Jul 3 2010, 21:12:11) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from pdb_post_mortem import MyObj >>> MyObj(5).go() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "pdb_post_mortem.py", line 13, in go for i in range(self.num_loops): AttributeError: 'MyObj' object has no attribute 'num_loops' >>> import pdb >>> pdb.pm() > .../pdb_post_mortem.py(13)go() -> for i in range(self.num_loops): (Pdb)</pre> </div> </div> </div> <div class="section" id="controlling-the-debugger"> <h2>Controlling the Debugger<a class="headerlink" href="#controlling-the-debugger" title="Permalink to this headline">¶</a></h2> <p>You interact with the debugger using a small command language that lets you move around the call stack, examine and change the values of variables, and control how the debugger executes your program. The interactive debugger uses <a class="reference internal" href="../readline/index.html#module-readline" title="readline: Interface to the GNU readline library"><tt class="xref py py-mod docutils literal"><span class="pre">readline</span></tt></a> to accept commands. Entering a blank line re-runs the previous command again, unless it was a <strong class="command">list</strong> operation.</p> <div class="section" id="navigating-the-execution-stack"> <h3>Navigating the Execution Stack<a class="headerlink" href="#navigating-the-execution-stack" title="Permalink to this headline">¶</a></h3> <p>At any point while the debugger is running you can use <strong class="command">where</strong> (abbreviated <strong class="command">w</strong>) to find out exactly what line is being executed and where on the call stack you are. In this case, the module <tt class="docutils literal"><span class="pre">pdb_set_trace.py</span></tt> line 17 in the <tt class="xref py py-func docutils literal"><span class="pre">go()</span></tt> method.</p> <div class="highlight-python"><pre>$ python pdb_set_trace.py > .../pdb_set_trace.py(17)go() -> print i (Pdb) where .../pdb_set_trace.py(21)<module>() -> MyObj(5).go() > .../pdb_set_trace.py(17)go() -> print i</pre> </div> <p>To add more context around the current location, use <strong class="command">list</strong> (<strong class="command">l</strong>).</p> <div class="highlight-python"><pre>(Pdb) list 12 self.count = num_loops 13 14 def go(self): 15 for i in range(self.count): 16 pdb.set_trace() 17 -> print i 18 return 19 20 if __name__ == '__main__': 21 MyObj(5).go() [EOF] (Pdb)</pre> </div> <p>The default is to list 11 lines around the current line (five before and five after). Using <strong class="command">list</strong> with a single numerical argument lists 11 lines around that line instead of the current line.</p> <div class="highlight-python"><pre>(Pdb) list 14 9 class MyObj(object): 10 11 def __init__(self, num_loops): 12 self.count = num_loops 13 14 def go(self): 15 for i in range(self.count): 16 pdb.set_trace() 17 -> print i 18 return 19</pre> </div> <p>If <strong class="command">list</strong> receives two arguments, it interprets them as the first and last lines to include in its output.</p> <div class="highlight-python"><pre>(Pdb) list 5, 19 5 # 6 7 import pdb 8 9 class MyObj(object): 10 11 def __init__(self, num_loops): 12 self.count = num_loops 13 14 def go(self): 15 for i in range(self.count): 16 pdb.set_trace() 17 -> print i 18 return 19</pre> </div> <p>Move between frames within the current call stack using <strong class="command">up</strong> and down. <strong class="command">up</strong> (abbreviated <strong class="command">u</strong>) moves towards older frames on the stack. <strong class="command">down</strong> (abbreviated <strong class="command">d</strong>) moves towards newer frames.</p> <div class="highlight-python"><pre>(Pdb) up > .../pdb_set_trace.py(21)<module>() -> MyObj(5).go() (Pdb) down > .../pdb_set_trace.py(17)go() -> print i</pre> </div> <p>Each time you move up or down the debugger prints the current location in the same format as produced by <strong class="command">where</strong>.</p> </div> <div class="section" id="examining-variables-on-the-stack"> <h3>Examining Variables on the Stack<a class="headerlink" href="#examining-variables-on-the-stack" title="Permalink to this headline">¶</a></h3> <p>Each frame on the stack maintains a set of variables, including values local to the function being executed and global state information. <a class="reference internal" href="#module-pdb" title="pdb: Interactive Debugger"><tt class="xref py py-mod docutils literal"><span class="pre">pdb</span></tt></a> provides several ways to examine the contents of those variables.</p> <div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18</pre></div></td><td class="code"><div class="highlight"><pre><span class="c">#!/usr/bin/env python</span> <span class="c"># encoding: utf-8</span> <span class="c">#</span> <span class="c"># Copyright (c) 2010 Doug Hellmann. All rights reserved.</span> <span class="c">#</span> <span class="kn">import</span> <span class="nn">pdb</span> <span class="k">def</span> <span class="nf">recursive_function</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">output</span><span class="o">=</span><span class="s">'to be printed'</span><span class="p">):</span> <span class="k">if</span> <span class="n">n</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span> <span class="n">recursive_function</span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span> <span class="k">print</span> <span class="n">output</span> <span class="k">return</span> <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span> <span class="n">recursive_function</span><span class="p">()</span> </pre></div> </td></tr></table></div> <p>The <strong class="command">args</strong> command (abbreviated <strong class="command">a</strong>) prints all of the arguments to the function active in the current frame. This example also uses a recursive function to show what a deeper stack looks like when printed by <strong class="command">where</strong>.</p> <div class="highlight-python"><pre>$ python pdb_function_arguments.py > .../pdb_function_arguments.py(14)recursive_function() -> return (Pdb) where .../pdb_function_arguments.py(17)<module>() -> recursive_function() .../pdb_function_arguments.py(11)recursive_function() -> recursive_function(n-1) .../pdb_function_arguments.py(11)recursive_function() -> recursive_function(n-1) .../pdb_function_arguments.py(11)recursive_function() -> recursive_function(n-1) .../pdb_function_arguments.py(11)recursive_function() -> recursive_function(n-1) .../pdb_function_arguments.py(11)recursive_function() -> recursive_function(n-1) > .../pdb_function_arguments.py(14)recursive_function() -> return (Pdb) args n = 0 output = to be printed (Pdb) up > .../pdb_function_arguments.py(11)recursive_function() -> recursive_function(n-1) (Pdb) args n = 1 output = to be printed (Pdb)</pre> </div> <p>The <strong class="command">p</strong> command evaluates an expression given as argument and prints the result. You can also use Python’s <tt class="docutils literal"><span class="pre">print</span></tt> statement, but that is passed through to the interpreter to be executed rather than running as a command in the debugger.</p> <div class="highlight-python"><pre>(Pdb) p n 1 (Pdb) print n 1</pre> </div> <p>Similarly, prefixing an expression with <strong class="command">!</strong> passes it to the Python interpreter to be evaluated. You can use this feature to execute arbitrary Python statements, including modifying variables. This example changes the value of <em>output</em> before letting the debugger continue running the program. The next statement after the call to <tt class="xref py py-func docutils literal"><span class="pre">set_trace()</span></tt> prints the value of <em>output</em>, showing the modified value.</p> <div class="highlight-python"><pre>$ python pdb_function_arguments.py > .../pdb_function_arguments.py(14)recursive_function() -> print output (Pdb) !output 'to be printed' (Pdb) !output='changed value' (Pdb) continue changed value</pre> </div> <p>For more complicated values such as nested or large data structures, use <strong class="command">pp</strong> to “pretty print” them. This program reads several lines of text from a file.</p> <div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12</pre></div></td><td class="code"><div class="highlight"><pre><span class="c">#!/usr/bin/env python</span> <span class="c"># encoding: utf-8</span> <span class="c">#</span> <span class="c"># Copyright (c) 2010 Doug Hellmann. All rights reserved.</span> <span class="c">#</span> <span class="kn">import</span> <span class="nn">pdb</span> <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">'lorem.txt'</span><span class="p">,</span> <span class="s">'rt'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span> <span class="n">lines</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span> <span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span> </pre></div> </td></tr></table></div> <p>Printing the variable <tt class="docutils literal"><span class="pre">lines</span></tt> with <strong class="command">p</strong> results in output that is difficult to read because it wraps awkwardly. <strong class="command">pp</strong> uses <a class="reference internal" href="../pprint/index.html#module-pprint" title="pprint: Pretty-print data structures"><tt class="xref py py-mod docutils literal"><span class="pre">pprint</span></tt></a> to format the value for clean printing.</p> <div class="highlight-python"><pre>$ python pdb_pp.py --Return-- > .../pdb_pp.py(12)<module>()->None -> pdb.set_trace() (Pdb) p lines ['Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec\n', 'egestas, enim et consectetuer ullamcorper, lectus ligula rutrum leo, a\n', 'elementum elit tortor eu quam.\n'] (Pdb) pp lines ['Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec\n', 'egestas, enim et consectetuer ullamcorper, lectus ligula rutrum leo, a\n', 'elementum elit tortor eu quam.\n'] (Pdb)</pre> </div> </div> <div class="section" id="stepping-through-your-program"> <h3>Stepping Through Your Program<a class="headerlink" href="#stepping-through-your-program" title="Permalink to this headline">¶</a></h3> <p>In addition to navigating up and down the call stack when the program is paused, you can also step through execution of the program past the point where it enters the debugger.</p> <div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17</pre></div></td><td class="code"><div class="highlight"><pre><span class="c">#!/usr/bin/env python</span> <span class="c"># encoding: utf-8</span> <span class="c">#</span> <span class="c"># Copyright (c) 2010 Doug Hellmann. All rights reserved.</span> <span class="c">#</span> <span class="kn">import</span> <span class="nn">pdb</span> <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> <span class="n">j</span> <span class="o">=</span> <span class="n">i</span> <span class="o">*</span> <span class="n">n</span> <span class="k">print</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span> <span class="k">return</span> <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span> <span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span> <span class="n">f</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> </pre></div> </td></tr></table></div> <p>Use <strong class="command">step</strong> to execute the current line and then stop at the next execution point – either the first statement inside a function being called or the next line of the current function.</p> <div class="highlight-python"><pre>$ python pdb_step.py > /Users/dhellmann/Documents/PyMOTW/src.pdb/PyMOTW/pdb/pdb_step.py(17)<module>() -> f(5)</pre> </div> <p>The interpreter pauses at the call to <tt class="xref py py-func docutils literal"><span class="pre">set_trace()</span></tt> and gives control to the debugger. The first step causes the execution to enter <tt class="xref py py-func docutils literal"><span class="pre">f()</span></tt>.</p> <div class="highlight-python"><pre>(Pdb) step --Call-- > .../pdb_step.py(9)f() -> def f(n):</pre> </div> <p>One more step moves execution to the first line of <tt class="xref py py-func docutils literal"><span class="pre">f()</span></tt> and starts the loop.</p> <div class="highlight-python"><pre>(Pdb) step > /Users/dhellmann/Documents/PyMOTW/src.pdb/PyMOTW/pdb/pdb_step.py(10)f() -> for i in range(n):</pre> </div> <p>Stepping again moves to the first line inside the loop where <tt class="docutils literal"><span class="pre">j</span></tt> is defined.</p> <div class="highlight-python"><pre>(Pdb) step > /Users/dhellmann/Documents/PyMOTW/src.pdb/PyMOTW/pdb/pdb_step.py(11)f() -> j = i * n (Pdb) p i 0</pre> </div> <p>The value of <tt class="docutils literal"><span class="pre">i</span></tt> is <tt class="docutils literal"><span class="pre">0</span></tt>, so after one more step the value of <tt class="docutils literal"><span class="pre">j</span></tt> should also be <tt class="docutils literal"><span class="pre">0</span></tt>.</p> <div class="highlight-python"><pre>(Pdb) step > /Users/dhellmann/Documents/PyMOTW/src.pdb/PyMOTW/pdb/pdb_step.py(12)f() -> print i, j (Pdb) p j 0 (Pdb)</pre> </div> <p>Stepping one line at a time like this can become tedious if there is a lot of code to cover before the point where the error occurs, or if the same function is called repeatedly.</p> <div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21</pre></div></td><td class="code"><div class="highlight"><pre><span class="c">#!/usr/bin/env python</span> <span class="c"># encoding: utf-8</span> <span class="c">#</span> <span class="c"># Copyright (c) 2010 Doug Hellmann. All rights reserved.</span> <span class="c">#</span> <span class="kn">import</span> <span class="nn">pdb</span> <span class="k">def</span> <span class="nf">calc</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span> <span class="n">j</span> <span class="o">=</span> <span class="n">i</span> <span class="o">*</span> <span class="n">n</span> <span class="k">return</span> <span class="n">j</span> <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> <span class="n">j</span> <span class="o">=</span> <span class="n">calc</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="k">print</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span> <span class="k">return</span> <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span> <span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span> <span class="n">f</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> </pre></div> </td></tr></table></div> <p>In this example, there is nothing wrong with <tt class="xref py py-func docutils literal"><span class="pre">calc()</span></tt>, so stepping through it each time it is called in the loop in <tt class="xref py py-func docutils literal"><span class="pre">f()</span></tt> obscures the useful output by showing all of the lines of <tt class="xref py py-func docutils literal"><span class="pre">calc()</span></tt> as they are executed.</p> <div class="highlight-python"><pre>$ python pdb_next.py > .../pdb_next.py(21)<module>() -> f(5) (Pdb) step --Call-- > .../pdb_next.py(13)f() -> def f(n): (Pdb) step > .../pdb_next.py(14)f() -> for i in range(n): (Pdb) step > .../pdb_next.py(15)f() -> j = calc(i, n) (Pdb) step --Call-- > .../pdb_next.py(9)calc() -> def calc(i, n): (Pdb) step > .../pdb_next.py(10)calc() -> j = i * n (Pdb) step > .../pdb_next.py(11)calc() -> return j (Pdb) step --Return-- > .../pdb_next.py(11)calc()->0 -> return j (Pdb) step > .../pdb_next.py(16)f() -> print i, j (Pdb) step 0 0</pre> </div> <p>The <strong class="command">next</strong> command is like step, but does not enter functions called from the statement being executed. In effect, it steps all the way through the function call to the next statement in the current function in a single operation.</p> <div class="highlight-python"><pre>> .../pdb_next.py(14)f() -> for i in range(n): (Pdb) step > .../pdb_next.py(15)f() -> j = calc(i, n) (Pdb) next > .../pdb_next.py(16)f() -> print i, j (Pdb)</pre> </div> <p>The <strong class="command">until</strong> command is like <strong class="command">next</strong>, except it explicitly continues until execution reaches a line in the same function with a line number higher than the current value. That means, for example, that <strong class="command">until</strong> can be used to step past the end of a loop.</p> <div class="highlight-python"><pre>$ python pdb_next.py > .../pdb_next.py(21)<module>() -> f(5) (Pdb) step --Call-- > .../pdb_next.py(13)f() -> def f(n): (Pdb) step > .../pdb_next.py(14)f() -> for i in range(n): (Pdb) step > .../pdb_next.py(15)f() -> j = calc(i, n) (Pdb) next > .../pdb_next.py(16)f() -> print i, j (Pdb) until 0 0 1 5 2 10 3 15 4 20 > .../pdb_next.py(17)f() -> return (Pdb)</pre> </div> <p>Before <strong class="command">until</strong> was run, the current line was 16, the last line of the loop. After <strong class="command">until</strong> ran, execution was on line 17, and the loop had been exhausted.</p> <p><strong class="command">return</strong> is another short-cut for bypassing parts of a function. It continues executing until the function is about to execute a <tt class="docutils literal"><span class="pre">return</span></tt> statement, and then it pauses. This gives you time to look at the return value before the function returns.</p> <div class="highlight-python"><pre>$ python pdb_next.py > .../pdb_next.py(21)<module>() -> f(5) (Pdb) step --Call-- > .../pdb_next.py(13)f() -> def f(n): (Pdb) step > .../pdb_next.py(14)f() -> for i in range(n): (Pdb) return 0 0 1 5 2 10 3 15 4 20 --Return-- > .../pdb_next.py(17)f()->None -> return (Pdb)</pre> </div> </div> </div> <div class="section" id="breakpoints"> <h2>Breakpoints<a class="headerlink" href="#breakpoints" title="Permalink to this headline">¶</a></h2> <p>As programs grow even longer, even using <strong class="command">next</strong> and <strong class="command">until</strong> will become slow and cumbersome. Instead of stepping through the program by hand, a better solution is to let it run normally until it reaches a point where you want the debugger to interrupt it. You could use <tt class="xref py py-func docutils literal"><span class="pre">set_trace()</span></tt> to start the debugger, but that only works if there is only one point you want to pause the program. It is more convenient to run the program through the debugger, but tell the debugger where to stop in advance using <em>breakpoints</em>. The debugger monitors your program, and when it reaches the location described by a breakpoint the program is paused before the line is executed.</p> <div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21</pre></div></td><td class="code"><div class="highlight"><pre><span class="c">#!/usr/bin/env python</span> <span class="c"># encoding: utf-8</span> <span class="c">#</span> <span class="c"># Copyright (c) 2010 Doug Hellmann. All rights reserved.</span> <span class="c">#</span> <span class="k">def</span> <span class="nf">calc</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span> <span class="n">j</span> <span class="o">=</span> <span class="n">i</span> <span class="o">*</span> <span class="n">n</span> <span class="k">print</span> <span class="s">'j ='</span><span class="p">,</span> <span class="n">j</span> <span class="k">if</span> <span class="n">j</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span> <span class="k">print</span> <span class="s">'Positive!'</span> <span class="k">return</span> <span class="n">j</span> <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> <span class="k">print</span> <span class="s">'i ='</span><span class="p">,</span> <span class="n">i</span> <span class="n">j</span> <span class="o">=</span> <span class="n">calc</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="k">return</span> <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span> <span class="n">f</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> </pre></div> </td></tr></table></div> <p>There are several options to the <strong class="command">break</strong> command used for setting break points. You can specify the line number, file, and function where processing should pause. To set a breakpoint on a specific line of the current file, use <tt class="docutils literal"><span class="pre">break</span> <span class="pre">lineno</span></tt>:</p> <div class="highlight-python"><pre>$ python -m pdb pdb_break.py > .../pdb_break.py(7)<module>() -> def calc(i, n): (Pdb) break 11 Breakpoint 1 at .../pdb_break.py:11 (Pdb) continue i = 0 j = 0 i = 1 j = 5 > .../pdb_break.py(11)calc() -> print 'Positive!' (Pdb)</pre> </div> <p>The command <strong class="command">continue</strong> tells the debugger to keep running your program until the next breakpoint. In this case, it runs through the first iteration of the <tt class="docutils literal"><span class="pre">for</span></tt> loop in <tt class="xref py py-func docutils literal"><span class="pre">f()</span></tt> and stops inside <tt class="xref py py-func docutils literal"><span class="pre">calc()</span></tt> during the second iteration.</p> <p>Breakpoints can also be set to the first line of a function by specifying the function name instead of a line number. This example shows what happens if a breakpoint is added for the <tt class="xref py py-func docutils literal"><span class="pre">calc()</span></tt> function.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_break.py > .../pdb_break.py(7)<module>() -> def calc(i, n): (Pdb) break calc Breakpoint 1 at .../pdb_break.py:7 (Pdb) continue i = 0 > .../pdb_break.py(8)calc() -> j = i * n (Pdb) where .../pdb_break.py(21)<module>() -> f(5) .../pdb_break.py(17)f() -> j = calc(i, n) > .../pdb_break.py(8)calc() -> j = i * n (Pdb)</pre> </div> <p>To specify a breakpoint in another file, prefix the line or function argument with a filename.</p> <div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2 3 4 5 6 7 8 9</pre></div></td><td class="code"><div class="highlight"><pre><span class="c">#!/usr/bin/env python</span> <span class="c"># encoding: utf-8</span> <span class="c">#</span> <span class="c"># Copyright (c) 2010 Doug Hellmann. All rights reserved.</span> <span class="c">#</span> <span class="kn">from</span> <span class="nn">pdb_break</span> <span class="kn">import</span> <span class="n">f</span> <span class="n">f</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> </pre></div> </td></tr></table></div> <p>Here a breakpoint is set for line 11 of <tt class="docutils literal"><span class="pre">pdb_break.py</span></tt> after starting the main program <tt class="docutils literal"><span class="pre">pdb_break_remote.py</span></tt>.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_break_remote.py > .../pdb_break_remote.py(7)<module>() -> from pdb_break import f (Pdb) break pdb_break.py:11 Breakpoint 1 at .../pdb_break.py:11 (Pdb) continue i = 0 j = 0 i = 1 j = 5 > .../pdb_break.py(11)calc() -> print 'Positive!' (Pdb)</pre> </div> <p>The filename can be a full path to the source file, or a relative path to a file available on <tt class="docutils literal"><span class="pre">sys.path</span></tt>.</p> <p>To list the breakpoints currently set, use <strong class="command">break</strong> without any arguments. The output includes the file and line number of each break point, as well as information about how many times it has been encountered.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_break.py > .../pdb_break.py(7)<module>() -> def calc(i, n): (Pdb) break 11 Breakpoint 1 at .../pdb_break.py:11 (Pdb) break Num Type Disp Enb Where 1 breakpoint keep yes at .../pdb_break.py:11 (Pdb) continue i = 0 j = 0 i = 1 j = 5 > .../pdb/pdb_break.py(11)calc() -> print 'Positive!' (Pdb) continue Positive! i = 2 j = 10 > .../pdb_break.py(11)calc() -> print 'Positive!' (Pdb) break Num Type Disp Enb Where 1 breakpoint keep yes at .../pdb_break.py:11 breakpoint already hit 2 times (Pdb)</pre> </div> <div class="section" id="managing-breakpoints"> <h3>Managing Breakpoints<a class="headerlink" href="#managing-breakpoints" title="Permalink to this headline">¶</a></h3> <p>As each new breakpoint is added, it is assigned a numerical identifier. These ID numbers are used to enable, disable, and remove the breakpoints interactively.</p> <p>Turning off a breakpoint with <strong class="command">disable</strong> tells the debugger not to stop when that line is reached. The breakpoint is remembered, but ignored.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_break.py > .../pdb_break.py(7)<module>() -> def calc(i, n): (Pdb) break calc Breakpoint 1 at .../pdb_break.py:7 (Pdb) break 11 Breakpoint 2 at .../pdb_break.py:11 (Pdb) break Num Type Disp Enb Where 1 breakpoint keep yes at .../pdb_break.py:7 2 breakpoint keep yes at .../pdb_break.py:11 (Pdb) disable 1 (Pdb) break Num Type Disp Enb Where 1 breakpoint keep no at .../pdb_break.py:7 2 breakpoint keep yes at .../pdb_break.py:11 (Pdb) continue i = 0 j = 0 i = 1 j = 5 > .../pdb_break.py(11)calc() -> print 'Positive!' (Pdb)</pre> </div> <p>The debugging session below sets two breakpoints in the program, then disables one. The program is run until the remaining breakpoint is encountered, and then the other breakpoint is turned back on with <strong class="command">enable</strong> before execution continues.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_break.py > .../pdb_break.py(7)<module>() -> def calc(i, n): (Pdb) break calc Breakpoint 1 at .../pdb_break.py:7 (Pdb) break 16 Breakpoint 2 at .../pdb_break.py:16 (Pdb) disable 1 (Pdb) continue > .../pdb_break.py(16)f() -> print 'i =', i (Pdb) list 11 print 'Positive!' 12 return j 13 14 def f(n): 15 for i in range(n): 16 B-> print 'i =', i 17 j = calc(i, n) 18 return 19 20 if __name__ == '__main__': 21 f(5) (Pdb) continue i = 0 j = 0 > .../pdb_break.py(16)f() -> print 'i =', i (Pdb) list 11 print 'Positive!' 12 return j 13 14 def f(n): 15 for i in range(n): 16 B-> print 'i =', i 17 j = calc(i, n) 18 return 19 20 if __name__ == '__main__': 21 f(5) (Pdb) p i 1 (Pdb) enable 1 (Pdb) continue i = 1 > .../pdb_break.py(8)calc() -> j = i * n (Pdb) list 3 # 4 # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 # 6 7 B def calc(i, n): 8 -> j = i * n 9 print 'j =', j 10 if j > 0: 11 print 'Positive!' 12 return j 13 (Pdb)</pre> </div> <p>The lines prefixed with <tt class="docutils literal"><span class="pre">B</span></tt> in the output from <strong class="command">list</strong> show where the breakpoints are set in the program (lines 9 and 18).</p> <p>Use <strong class="command">clear</strong> to delete a breakpoint entirely.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_break.py > .../pdb_break.py(7)<module>() -> def calc(i, n): (Pdb) break calc Breakpoint 1 at .../pdb_break.py:7 (Pdb) break 11 Breakpoint 2 at .../pdb_break.py:11 (Pdb) break 16 Breakpoint 3 at .../pdb_break.py:16 (Pdb) break Num Type Disp Enb Where 1 breakpoint keep yes at .../pdb_break.py:7 2 breakpoint keep yes at .../pdb_break.py:11 3 breakpoint keep yes at .../pdb_break.py:16 (Pdb) clear 2 Deleted breakpoint 2 (Pdb) break Num Type Disp Enb Where 1 breakpoint keep yes at .../pdb_break.py:7 3 breakpoint keep yes at .../pdb_break.py:16 (Pdb)</pre> </div> <p>The other breakpoints retain their original identifiers and are not renumbered.</p> </div> <div class="section" id="temporary-breakpoints"> <h3>Temporary Breakpoints<a class="headerlink" href="#temporary-breakpoints" title="Permalink to this headline">¶</a></h3> <p>A temporary breakpoint is automatically cleared the first time program execution hits it. Using a temporary breakpoint lets you reach a particular spot in the program flow quickly, just as with a regular breakpoint, but since it is cleared immediately it does not interfere with subsequent progress if that part of the program is run repeatedly.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_break.py > .../pdb_break.py(7)<module>() -> def calc(i, n): (Pdb) tbreak 11 Breakpoint 1 at .../pdb_break.py:11 (Pdb) continue i = 0 j = 0 i = 1 j = 5 Deleted breakpoint 1 > .../pdb_break.py(11)calc() -> print 'Positive!' (Pdb) break (Pdb) continue Positive! i = 2 j = 10 Positive! i = 3 j = 15 Positive! i = 4 j = 20 Positive! The program finished and will be restarted > .../pdb_break.py(7)<module>() -> def calc(i, n): (Pdb)</pre> </div> <p>After the program reaches line 11 the first time, the breakpoint is removed and execution does not stop again until the program finishes.</p> </div> <div class="section" id="conditional-breakpoints"> <h3>Conditional Breakpoints<a class="headerlink" href="#conditional-breakpoints" title="Permalink to this headline">¶</a></h3> <p>Rules can be applied to breakpoints so that execution only stops when the conditions are met. Using conditional breakpoints gives you finer control over how the debugger pauses your program than manually enabling and disabling breakpoints yourself.</p> <p>Conditional breakpoints can be set in two ways. The first is to specify the condition when the breakpoint is set using <strong class="command">break</strong>.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_break.py > .../pdb_break.py(7)<module>() -> def calc(i, n): (Pdb) break 9, j>0 Breakpoint 1 at .../pdb_break.py:9 (Pdb) break Num Type Disp Enb Where 1 breakpoint keep yes at .../pdb_break.py:9 stop only if j>0 (Pdb) continue i = 0 j = 0 i = 1 > .../pdb_break.py(9)calc() -> print 'j =', j (Pdb)</pre> </div> <p>The condition argument must be an expression using values visible in the stack frame where the breakpoint is defined. If the expression evaluates as true, execution stops at the breakpoint.</p> <p>A condition can also be applied to an existing breakpoint using the <strong class="command">condition</strong> command. The arguments are the breakpoint ID and the expression.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_break.py > .../pdb_break.py(7)<module>() -> def calc(i, n): (Pdb) break 9 Breakpoint 1 at .../pdb_break.py:9 (Pdb) break Num Type Disp Enb Where 1 breakpoint keep yes at .../pdb_break.py:9 (Pdb) condition 1 j>0 (Pdb) break Num Type Disp Enb Where 1 breakpoint keep yes at .../pdb_break.py:9 stop only if j>0 (Pdb)</pre> </div> </div> <div class="section" id="ignoring-breakpoints"> <h3>Ignoring Breakpoints<a class="headerlink" href="#ignoring-breakpoints" title="Permalink to this headline">¶</a></h3> <p>Programs with a lot of looping or recursive calls to the same function are often easier to debug by “skipping ahead” in the execution, instead of watching every call or breakpoint. The <strong class="command">ignore</strong> command tells the debugger to pass over a breakpoint without stopping. Each time processing encounteres the breakpoint, it decrements the ignore counter. When the counter is zero, the breakpoint is re-activated.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_break.py > .../pdb_break.py(7)<module>() -> def calc(i, n): (Pdb) break 17 Breakpoint 1 at .../pdb_break.py:17 (Pdb) continue i = 0 > .../pdb_break.py(17)f() -> j = calc(i, n) (Pdb) next j = 0 > .../pdb_break.py(15)f() -> for i in range(n): (Pdb) ignore 1 2 Will ignore next 2 crossings of breakpoint 1. (Pdb) break Num Type Disp Enb Where 1 breakpoint keep yes at .../pdb_break.py:17 ignore next 2 hits breakpoint already hit 1 time (Pdb) continue i = 1 j = 5 Positive! i = 2 j = 10 Positive! i = 3 > .../pdb_break.py(17)f() -> j = calc(i, n) (Pdb) break Num Type Disp Enb Where 1 breakpoint keep yes at .../pdb_break.py:17 breakpoint already hit 4 times</pre> </div> <p>Explicitly resetting the ignore count to zero re-enables the breakpoint immediately.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_break.py > .../pdb_break.py(7)<module>() -> def calc(i, n): (Pdb) break 17 Breakpoint 1 at .../pdb_break.py:17 (Pdb) ignore 1 2 Will ignore next 2 crossings of breakpoint 1. (Pdb) break Num Type Disp Enb Where 1 breakpoint keep yes at .../pdb_break.py:17 ignore next 2 hits (Pdb) ignore 1 0 Will stop next time breakpoint 1 is reached. (Pdb) break Num Type Disp Enb Where 1 breakpoint keep yes at .../pdb_break.py:17</pre> </div> </div> <div class="section" id="triggering-actions-on-a-breakpoint"> <h3>Triggering Actions on a Breakpoint<a class="headerlink" href="#triggering-actions-on-a-breakpoint" title="Permalink to this headline">¶</a></h3> <p>In addition to the purely interactive mode, <a class="reference internal" href="#module-pdb" title="pdb: Interactive Debugger"><tt class="xref py py-mod docutils literal"><span class="pre">pdb</span></tt></a> supports basic scripting. Using <strong class="command">commands</strong>, you can define a series of interpreter commands, including Python statements, to be executed when a specific breakpoint is encountered. After running <strong class="command">commands</strong> with the breakpoint number as argument, the debugger prompt changes to <tt class="docutils literal"><span class="pre">(com)</span></tt>. Enter commands one a time, and finish the list with <tt class="docutils literal"><span class="pre">end</span></tt> to save the script and return to the main debugger prompt.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_break.py > .../pdb_break.py(7)<module>() -> def calc(i, n): (Pdb) break 9 Breakpoint 1 at .../pdb_break.py:9 (Pdb) commands 1 (com) print 'debug i =', i (com) print 'debug j =', j (com) print 'debug n =', n (com) end (Pdb) continue i = 0 debug i = 0 debug j = 0 debug n = 5 > .../pdb_break.py(9)calc() -> print 'j =', j (Pdb) continue j = 0 i = 1 debug i = 1 debug j = 5 debug n = 5 > .../pdb_break.py(9)calc() -> print 'j =', j (Pdb)</pre> </div> <p>This feature is especially useful for debugging code that uses a lot of data structures or variables, since you can have the debugger print out all of the values automatically, instead of doing it manually each time the breakpoint is encountered.</p> </div> </div> <div class="section" id="changing-execution-flow"> <h2>Changing Execution Flow<a class="headerlink" href="#changing-execution-flow" title="Permalink to this headline">¶</a></h2> <p>The <strong class="command">jump</strong> command lets you alter the flow of your program at runtime, without modifying the code. You can skip forwards to avoid running some code, or backwards to run it again. This sample program generates a list of numbers.</p> <div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17</pre></div></td><td class="code"><div class="highlight"><pre><span class="c">#!/usr/bin/env python</span> <span class="c"># encoding: utf-8</span> <span class="c">#</span> <span class="c"># Copyright (c) 2010 Doug Hellmann. All rights reserved.</span> <span class="c">#</span> <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> <span class="n">j</span> <span class="o">=</span> <span class="n">i</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">j</span> <span class="n">j</span> <span class="o">+=</span> <span class="n">n</span> <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">j</span><span class="p">)</span> <span class="k">return</span> <span class="n">result</span> <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span> <span class="k">print</span> <span class="n">f</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> </pre></div> </td></tr></table></div> <p>When run without interference the output is a sequence of increasing numbers divisible by <tt class="docutils literal"><span class="pre">5</span></tt>.</p> <div class="highlight-python"><pre>$ python pdb_jump.py [5, 15, 30, 50, 75]</pre> </div> <div class="section" id="jump-ahead"> <h3>Jump Ahead<a class="headerlink" href="#jump-ahead" title="Permalink to this headline">¶</a></h3> <p>Jumping ahead moves the point of execution past the current location without evaluating any of the statements in between. By skipping over line 13 in the example below, the value of <tt class="docutils literal"><span class="pre">j</span></tt> is not incremented and all of the subsequent values that depend on it are a little smaller.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_jump.py > .../pdb_jump.py(7)<module>() -> def f(n): (Pdb) break 12 Breakpoint 1 at .../pdb_jump.py:12 (Pdb) continue > .../pdb_jump.py(12)f() -> j += n (Pdb) p j 0 (Pdb) step > .../pdb_jump.py(13)f() -> result.append(j) (Pdb) p j 5 (Pdb) continue > .../pdb_jump.py(12)f() -> j += n (Pdb) jump 13 > .../pdb_jump.py(13)f() -> result.append(j) (Pdb) p j 10 (Pdb) disable 1 (Pdb) continue [5, 10, 25, 45, 70] The program finished and will be restarted > .../pdb_jump.py(7)<module>() -> def f(n): (Pdb)</pre> </div> </div> <div class="section" id="jump-back"> <h3>Jump Back<a class="headerlink" href="#jump-back" title="Permalink to this headline">¶</a></h3> <p>Jumps can also move the program execution to a statement that has already been executed, to run it again. Here, the value of <tt class="docutils literal"><span class="pre">j</span></tt> is incremented an extra time, so the numbers in the result sequence are all larger than they would otherwise be.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_jump.py > .../pdb_jump.py(7)<module>() -> def f(n): (Pdb) break 13 Breakpoint 1 at .../pdb_jump.py:13 (Pdb) continue > .../pdb_jump.py(13)f() -> result.append(j) (Pdb) p j 5 (Pdb) jump 12 > .../pdb_jump.py(12)f() -> j += n (Pdb) continue > .../pdb_jump.py(13)f() -> result.append(j) (Pdb) p j 10 (Pdb) disable 1 (Pdb) continue [10, 20, 35, 55, 80] The program finished and will be restarted > .../pdb_jump.py(7)<module>() -> def f(n): (Pdb)</pre> </div> </div> <div class="section" id="illegal-jumps"> <h3>Illegal Jumps<a class="headerlink" href="#illegal-jumps" title="Permalink to this headline">¶</a></h3> <p>Jumping in and out of certain flow control statements is dangerous or undefined, and therefore prevented by the debugger.</p> <div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32</pre></div></td><td class="code"><div class="highlight"><pre><span class="c">#!/usr/bin/env python</span> <span class="c"># encoding: utf-8</span> <span class="c">#</span> <span class="c"># Copyright (c) 2010 Doug Hellmann. All rights reserved.</span> <span class="c">#</span> <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> <span class="k">if</span> <span class="n">n</span> <span class="o"><</span> <span class="mi">0</span><span class="p">:</span> <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">'Invalid n: </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="n">n</span><span class="p">)</span> <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> <span class="n">j</span> <span class="o">=</span> <span class="n">i</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">j</span> <span class="n">j</span> <span class="o">+=</span> <span class="n">n</span> <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">j</span><span class="p">)</span> <span class="k">return</span> <span class="n">result</span> <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span> <span class="k">try</span><span class="p">:</span> <span class="k">print</span> <span class="n">f</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="k">finally</span><span class="p">:</span> <span class="k">print</span> <span class="s">'Always printed'</span> <span class="k">try</span><span class="p">:</span> <span class="k">print</span> <span class="n">f</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">)</span> <span class="k">except</span><span class="p">:</span> <span class="k">print</span> <span class="s">'There was an error'</span> <span class="k">else</span><span class="p">:</span> <span class="k">print</span> <span class="s">'There was no error'</span> <span class="k">print</span> <span class="s">'Last statement'</span> </pre></div> </td></tr></table></div> <p>You can jump into a function, but if you do the arguments are not defined and the code is unlikely to work.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_no_jump.py > .../pdb_no_jump.py(7)<module>() -> def f(n): (Pdb) break 21 Breakpoint 1 at .../pdb_no_jump.py:21 (Pdb) jump 8 > .../pdb_no_jump.py(8)<module>() -> if n < 0: (Pdb) p n *** NameError: NameError("name 'n' is not defined",) (Pdb) args (Pdb)</pre> </div> <p>You cannot jump into the middle of a block such as a <tt class="docutils literal"><span class="pre">for</span></tt> loop or <tt class="docutils literal"><span class="pre">try:except</span></tt> statement.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_no_jump.py > .../pdb_no_jump.py(7)<module>() -> def f(n): (Pdb) break 21 Breakpoint 1 at .../pdb_no_jump.py:21 (Pdb) continue > .../pdb_no_jump.py(21)<module>() -> print f(5) (Pdb) jump 26 *** Jump failed: can't jump into the middle of a block (Pdb)</pre> </div> <p>The code in a <tt class="docutils literal"><span class="pre">finally</span></tt> block must all be executed, so you cannot jump out of the block.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_no_jump.py > .../pdb_no_jump.py(7)<module>() -> def f(n): (Pdb) break 23 Breakpoint 1 at .../pdb_no_jump.py:23 (Pdb) continue [5, 15, 30, 50, 75] > .../pdb_no_jump.py(23)<module>() -> print 'Always printed' (Pdb) jump 25 *** Jump failed: can't jump into or out of a 'finally' block (Pdb)</pre> </div> <p>And the most basic restriction is that jumping is constrained to the bottom frame on the call stack. If you move up the stack to examine variables, you cannot change the execution flow at that point.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_no_jump.py > .../pdb_no_jump.py(7)<module>() -> def f(n): (Pdb) break 11 Breakpoint 1 at .../pdb_no_jump.py:11 (Pdb) continue > .../pdb_no_jump.py(11)f() -> j = 0 (Pdb) where /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/bdb.py(379)run() -> exec cmd in globals, locals <string>(1)<module>() .../pdb_no_jump.py(21)<module>() -> print f(5) > .../pdb_no_jump.py(11)f() -> j = 0 (Pdb) up > .../pdb_no_jump.py(21)<module>() -> print f(5) (Pdb) jump 25 *** You can only jump within the bottom frame (Pdb)</pre> </div> </div> <div class="section" id="restarting-your-program"> <h3>Restarting Your Program<a class="headerlink" href="#restarting-your-program" title="Permalink to this headline">¶</a></h3> <p>When the debugger reaches the end of your program, it automatically starts it over, but you can also restart it explicitly without leaving the debugger and losing your breakpoints or other settings.</p> <div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14</pre></div></td><td class="code"><div class="highlight"><pre><span class="c">#!/usr/bin/env python</span> <span class="c"># encoding: utf-8</span> <span class="c">#</span> <span class="c"># Copyright (c) 2010 Doug Hellmann. All rights reserved.</span> <span class="c">#</span> <span class="kn">import</span> <span class="nn">sys</span> <span class="k">def</span> <span class="nf">f</span><span class="p">():</span> <span class="k">print</span> <span class="s">'Command line args:'</span><span class="p">,</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span> <span class="k">return</span> <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span> <span class="n">f</span><span class="p">()</span> </pre></div> </td></tr></table></div> <p>Running the above program to completion within the debugger prints the name of the script file, since no other arguments were given on the command line.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_run.py > .../pdb_run.py(7)<module>() -> import sys (Pdb) continue Command line args: ['pdb_run.py'] The program finished and will be restarted > .../pdb_run.py(7)<module>() -> import sys (Pdb)</pre> </div> <p>The program can be restarted using <strong class="command">run</strong>. Arguments passed to <strong class="command">run</strong> are parsed with <a class="reference internal" href="../shlex/index.html#module-shlex" title="shlex: Lexical analysis of shell-style syntaxes."><tt class="xref py py-mod docutils literal"><span class="pre">shlex</span></tt></a> and passed to the program as though they were command line arguments, so you can restart the program with different settings.</p> <div class="highlight-python"><pre>(Pdb) run a b c "this is a long value" Restarting pdb_run.py with arguments: a b c this is a long value > .../pdb_run.py(7)<module>() -> import sys (Pdb) continue Command line args: ['pdb_run.py', 'a', 'b', 'c', 'this is a long value'] The program finished and will be restarted > .../pdb_run.py(7)<module>() -> import sys (Pdb)</pre> </div> <p><strong class="command">run</strong> can also be used at any other point in processing to restart the program.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_run.py > .../pdb_run.py(7)<module>() -> import sys (Pdb) break 10 Breakpoint 1 at .../pdb_run.py:10 (Pdb) continue > .../pdb_run.py(10)f() -> print 'Command line args:', sys.argv (Pdb) run one two three Restarting pdb_run.py with arguments: one two three > .../pdb_run.py(7)<module>() -> import sys (Pdb)</pre> </div> </div> </div> <div class="section" id="customizing-the-debugger-with-aliases"> <h2>Customizing the Debugger with Aliases<a class="headerlink" href="#customizing-the-debugger-with-aliases" title="Permalink to this headline">¶</a></h2> <p>You can avoid typing complex commands repeatedly by using <strong class="command">alias</strong> to define a shortcut. Alias expansion is applied to the first word of each command. The body of the alias can consist of any command that is legal to type at the debugger prompt, including other debugger commands and pure Python expressions. Recursion is allowed in alias definitions, so one alias can even invoke another.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_function_arguments.py > .../pdb_function_arguments.py(7)<module>() -> import pdb (Pdb) break 10 Breakpoint 1 at .../pdb_function_arguments.py:10 (Pdb) continue > .../pdb_function_arguments.py(10)recursive_function() -> if n > 0: (Pdb) pp locals().keys() ['output', 'n'] (Pdb) alias pl pp locals().keys() (Pdb) pl ['output', 'n']</pre> </div> <p>Running <strong class="command">alias</strong> without any arguments shows the list of defined aliases. A single argument is assumed to be the name of an alias, and its definition is printed.</p> <div class="highlight-python"><pre>(Pdb) alias pl = pp locals().keys() (Pdb) alias pl pl = pp locals().keys() (Pdb)</pre> </div> <p>Arguments to the alias are referenced using <tt class="docutils literal"><span class="pre">%n</span></tt> where <em>n</em> is replaced with a number indicating the position of the argument, starting with <tt class="docutils literal"><span class="pre">1</span></tt>. To consume all of the arguments, use <tt class="docutils literal"><span class="pre">%*</span></tt>.</p> <div class="highlight-python"><pre>$ python -m pdb pdb_function_arguments.py > .../pdb_function_arguments.py(7)<module>() -> import pdb (Pdb) alias ph !help(%1) (Pdb) ph locals Help on built-in function locals in module __builtin__: locals(...) locals() -> dictionary Update and return a dictionary containing the current scope's local variables.</pre> </div> <p>Clear the definition of an alias with <strong class="command">unalias</strong>.</p> <div class="highlight-python"><pre>(Pdb) unalias ph (Pdb) ph locals *** SyntaxError: invalid syntax (<stdin>, line 1) (Pdb)</pre> </div> </div> <div class="section" id="saving-configuration-settings"> <h2>Saving Configuration Settings<a class="headerlink" href="#saving-configuration-settings" title="Permalink to this headline">¶</a></h2> <p>Debugging a program involves a lot of repetition; running the code, observing the output, adjusting the code or inputs, and running it again. <a class="reference internal" href="#module-pdb" title="pdb: Interactive Debugger"><tt class="xref py py-mod docutils literal"><span class="pre">pdb</span></tt></a> attempts to cut down on the amount of repetition you need to use to control the debugging experience, to let you concentrate on your code instead of the debugger. To help reduce the number of times you issue the same commands to the debugger, <a class="reference internal" href="#module-pdb" title="pdb: Interactive Debugger"><tt class="xref py py-mod docutils literal"><span class="pre">pdb</span></tt></a> lets you save configuration using text files read and interpreted on startup.</p> <p>The file <tt class="docutils literal"><span class="pre">~/.pdbrc</span></tt> is read first, allowing you to set global personal preferences for all of your debugging sessions. Then <tt class="docutils literal"><span class="pre">./.pdbrc</span></tt> is read from the current working directory, so you can set local preferences for a particular project.</p> <div class="highlight-python"><pre>$ cat ~/.pdbrc # Show python help alias ph !help(%1) # Overridden alias alias redefined p 'home definition' $ cat .pdbrc # Breakpoints break 10 # Overridden alias alias redefined p 'local definition' $ python -m pdb pdb_function_arguments.py Breakpoint 1 at .../pdb_function_arguments.py:10 > .../pdb_function_arguments.py(7)<module>() -> import pdb (Pdb) alias ph = !help(%1) redefined = p 'local definition' (Pdb) break Num Type Disp Enb Where 1 breakpoint keep yes at .../pdb_function_arguments.py:10 (Pdb)</pre> </div> <p>Any configuration commands that can be typed at the debugger prompt can be saved in one of the startup files, but most commands that control the execution (<strong class="command">continue</strong>, <strong class="command">jump</strong>, etc.) cannot. The exception is <strong class="command">run</strong>, which means you can set the command line arguments for a debugging session in <tt class="docutils literal"><span class="pre">./.pdbrc</span></tt> so they are consistent across several runs.</p> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt><a class="reference external" href="http://docs.python.org/library/pdb.html">pdb</a></dt> <dd>The standard library documentation for this module.</dd> <dt><a class="reference internal" href="../readline/index.html#module-readline" title="readline: Interface to the GNU readline library"><tt class="xref py py-mod docutils literal"><span class="pre">readline</span></tt></a></dt> <dd>Interactive prompt editing library.</dd> <dt><a class="reference internal" href="../cmd/index.html#module-cmd" title="cmd: Create line-oriented command processors."><tt class="xref py py-mod docutils literal"><span class="pre">cmd</span></tt></a></dt> <dd>Build interactive programs.</dd> <dt><a class="reference internal" href="../shlex/index.html#module-shlex" title="shlex: Lexical analysis of shell-style syntaxes."><tt class="xref py py-mod docutils literal"><span class="pre">shlex</span></tt></a></dt> <dd>Shell command line parsing.</dd> </dl> </div> </div> </div> </div> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="../profilers.html" title="Debugging and Profiling" >next</a> |</li> <li class="right" > <a href="../unittest/index.html" title="unittest – Automated testing framework" >previous</a> |</li> <li><a href="../contents.html">PyMOTW</a> »</li> <li><a href="../dev_tools.html" >Development Tools</a> »</li> </ul> </div> <div class="footer"> © Copyright Doug Hellmann. Last updated on Oct 24, 2010. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>. <br/><a href="http://creativecommons.org/licenses/by-nc-sa/3.0/us/" rel="license"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/3.0/us/88x31.png"/></a> </div> </body> </html>