[code.view]

[top] / python / PyMOTW / docs / trace / index.html


<!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>trace – Follow Python statements as they are executed &mdash; 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="Debugging and Profiling" href="../profilers.html" />
    <link rel="next" title="Python Runtime Services" href="../runtime_services.html" />
    <link rel="prev" title="timeit – Time the execution of small bits of Python code." href="../timeit/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="../runtime_services.html" title="Python Runtime Services"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../timeit/index.html" title="timeit – Time the execution of small bits of Python code."
             accesskey="P">previous</a> |</li>
        <li><a href="../contents.html">PyMOTW</a> &raquo;</li>
          <li><a href="../profilers.html" accesskey="U">Debugging and Profiling</a> &raquo;</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="#">trace &#8211; Follow Python statements as they are executed</a><ul>
<li><a class="reference internal" href="#command-line-interface">Command Line Interface</a><ul>
<li><a class="reference internal" href="#tracing-execution">Tracing Execution</a></li>
<li><a class="reference internal" href="#code-coverage">Code Coverage</a></li>
<li><a class="reference internal" href="#calling-relationships">Calling Relationships</a></li>
</ul>
</li>
<li><a class="reference internal" href="#programming-interface">Programming Interface</a><ul>
<li><a class="reference internal" href="#saving-result-data">Saving Result Data</a></li>
<li><a class="reference internal" href="#trace-options">Trace Options</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="../timeit/index.html"
                        title="previous chapter">timeit &#8211; Time the execution of small bits of Python code.</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="../runtime_services.html"
                        title="next chapter">Python Runtime Services</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/trace/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-trace">
<span id="trace-follow-python-statements-as-they-are-executed"></span><h1>trace &#8211; Follow Python statements as they are executed<a class="headerlink" href="#module-trace" 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">Monitor which statements and functions are executed as a program runs to produce coverage and call-graph information.</td>
</tr>
<tr class="field"><th class="field-name">Python Version:</th><td class="field-body">2.3 and later</td>
</tr>
</tbody>
</table>
<p>The <a class="reference internal" href="#module-trace" title="trace: Follow Python statements as they are executed."><tt class="xref py py-mod docutils literal"><span class="pre">trace</span></tt></a> module helps you understand the way your program
runs.  You can trace the statements executed, produce coverage
reports, and investigate the relationships between functions that call
each other.</p>
<div class="section" id="command-line-interface">
<h2>Command Line Interface<a class="headerlink" href="#command-line-interface" title="Permalink to this headline">¶</a></h2>
<p>It is easy use <a class="reference internal" href="#module-trace" title="trace: Follow Python statements as they are executed."><tt class="xref py py-mod docutils literal"><span class="pre">trace</span></tt></a> directly from the command line.  Given the
following Python scripts as input:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">recurse</span> <span class="kn">import</span> <span class="n">recurse</span>

<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
    <span class="k">print</span> <span class="s">&#39;This is the main program.&#39;</span>
    <span class="n">recurse</span><span class="p">(</span><span class="mi">2</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">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">recurse</span><span class="p">(</span><span class="n">level</span><span class="p">):</span>
    <span class="k">print</span> <span class="s">&#39;recurse(</span><span class="si">%s</span><span class="s">)&#39;</span> <span class="o">%</span> <span class="n">level</span>
    <span class="k">if</span> <span class="n">level</span><span class="p">:</span>
        <span class="n">recurse</span><span class="p">(</span><span class="n">level</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
    <span class="k">return</span>

<span class="k">def</span> <span class="nf">not_called</span><span class="p">():</span>
    <span class="k">print</span> <span class="s">&#39;This function is never called.&#39;</span>
</pre></div>
</div>
<div class="section" id="tracing-execution">
<h3>Tracing Execution<a class="headerlink" href="#tracing-execution" title="Permalink to this headline">¶</a></h3>
<p>We can see which statements are being executed as the program runs
using the <em class="xref std std-option">--trace</em> option.</p>
<div class="highlight-python"><pre>$ python -m trace --trace trace_example/main.py

 --- modulename: threading, funcname: settrace
threading.py(89):     _trace_hook = func
 --- modulename: trace, funcname: &lt;module&gt;
&lt;string&gt;(1):   --- modulename: trace, funcname: &lt;module&gt;
main.py(7): """
main.py(12): from recurse import recurse
 --- modulename: recurse, funcname: &lt;module&gt;
recurse.py(7): """
recurse.py(12): def recurse(level):
recurse.py(18): def not_called():
main.py(14): def main():
main.py(19): if __name__ == '__main__':
main.py(20):     main()
 --- modulename: trace, funcname: main
main.py(15):     print 'This is the main program.'
This is the main program.
main.py(16):     recurse(2)
 --- modulename: recurse, funcname: recurse
recurse.py(13):     print 'recurse(%s)' % level
recurse(2)
recurse.py(14):     if level:
recurse.py(15):         recurse(level-1)
 --- modulename: recurse, funcname: recurse
recurse.py(13):     print 'recurse(%s)' % level
recurse(1)
recurse.py(14):     if level:
recurse.py(15):         recurse(level-1)
 --- modulename: recurse, funcname: recurse
recurse.py(13):     print 'recurse(%s)' % level
recurse(0)
recurse.py(14):     if level:
recurse.py(16):     return
recurse.py(16):     return
recurse.py(16):     return
main.py(17):     return</pre>
</div>
<p>The first part of the output shows some setup operations performed by
<a class="reference internal" href="#module-trace" title="trace: Follow Python statements as they are executed."><tt class="xref py py-mod docutils literal"><span class="pre">trace</span></tt></a>.  The rest of the output shows the entry into each
function, including the module where the function is located, and then
the lines of the source file as they are executed.  You can see that
<tt class="xref py py-func docutils literal"><span class="pre">recurse()</span></tt> is entered three times, as you would expect from the
way it is called in <tt class="xref py py-func docutils literal"><span class="pre">main()</span></tt>.</p>
</div>
<div class="section" id="code-coverage">
<h3>Code Coverage<a class="headerlink" href="#code-coverage" title="Permalink to this headline">¶</a></h3>
<p>Running <a class="reference internal" href="#module-trace" title="trace: Follow Python statements as they are executed."><tt class="xref py py-mod docutils literal"><span class="pre">trace</span></tt></a> from the command line with the <em class="xref std std-option">--count</em>
option will produce code coverage report information, so you can see
which lines are run and which are skipped.  Since your program is
usually made up of multiple files, a separate coverage report is
produced for each.  By default the coverage report files are written
to the same directory as the module, named after the module but with a
<tt class="docutils literal"><span class="pre">.cover</span></tt> extension instead of <tt class="docutils literal"><span class="pre">.py</span></tt>.</p>
<div class="highlight-python"><pre>$ python -m trace --count trace_example/main.py

This is the main program.
recurse(2)
recurse(1)
recurse(0)</pre>
</div>
<p>And two output files, <tt class="docutils literal"><span class="pre">trace_example/main.cover</span></tt>:</p>
<div class="highlight-python"><pre>
       
    1: from recurse import recurse
       
    1: def main():
    1:     print 'This is the main program.'
    1:     recurse(2)
    1:     return
       
    1: if __name__ == '__main__':
    1:     main()
</pre>
</div>
<p>and <tt class="docutils literal"><span class="pre">trace_example/recurse.cover</span></tt>:</p>
<div class="highlight-python"><pre>
       
    1: def recurse(level):
    3:     print 'recurse(%s)' % level
    3:     if level:
    2:         recurse(level-1)
    3:     return
       
    1: def not_called():
           print 'This function is never called.'
</pre>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Although the line <tt class="docutils literal"><span class="pre">def</span> <span class="pre">recurse(level):</span></tt> has a count of <tt class="docutils literal"><span class="pre">1</span></tt>,
that does not mean the function was only run once.  It means the
function <em>definition</em> was only executed once.</p>
</div>
<p>It is also possible to run the program several times, perhaps with
different options, to save the coverage data and produce a combined
report.</p>
<div class="highlight-python"><pre>$ python -m trace --coverdir coverdir1 --count --file coverdir1/coverage_report.dat trace_example/main.py

Skipping counts file 'coverdir1/coverage_report.dat': [Errno 2] No such file or directory: 'coverdir1/coverage_report.dat'
This is the main program.
recurse(2)
recurse(1)
recurse(0)

$ python -m trace --coverdir coverdir1 --count --file coverdir1/coverage_report.dat trace_example/main.py

This is the main program.
recurse(2)
recurse(1)
recurse(0)

$ python -m trace --coverdir coverdir1 --count --file coverdir1/coverage_report.dat trace_example/main.py

This is the main program.
recurse(2)
recurse(1)
recurse(0)</pre>
</div>
<p>Once the coverage information is recorded to the <tt class="docutils literal"><span class="pre">.cover</span></tt> files, you
can produce reports with the <em class="xref std std-option">--report</em> option.</p>
<div class="highlight-python"><pre>$ python -m trace --coverdir coverdir1 --report --summary --missing --file coverdir1/coverage_report.dat trace_example/main.py

lines   cov%   module   (path)
  599     0%   threading   (/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py)
    8   100%   trace_example.main   (trace_example/main.py)
    8    87%   trace_example.recurse   (trace_example/recurse.py)</pre>
</div>
<p>Since the program ran three times, the coverage report shows values
three times higher than the first report.  The <em class="xref std std-option">--summary</em>
option adds the percent covered information to the output above.  The
<tt class="docutils literal"><span class="pre">recurse</span></tt> module is only 87% covered.  A quick look at the cover
file for recurse shows that the body of <tt class="docutils literal"><span class="pre">not_called()</span></tt> is indeed
never run, indicated by the <tt class="docutils literal"><span class="pre">&gt;&gt;&gt;&gt;&gt;&gt;</span></tt> prefix.</p>
<div class="highlight-python"><pre>
       
    3: def recurse(level):
    9:     print 'recurse(%s)' % level
    9:     if level:
    6:         recurse(level-1)
    9:     return
       
    3: def not_called():
&gt;&gt;&gt;&gt;&gt;&gt;     print 'This function is never called.'
</pre>
</div>
</div>
<div class="section" id="calling-relationships">
<h3>Calling Relationships<a class="headerlink" href="#calling-relationships" title="Permalink to this headline">¶</a></h3>
<p>In addition to coverage information, <a class="reference internal" href="#module-trace" title="trace: Follow Python statements as they are executed."><tt class="xref py py-mod docutils literal"><span class="pre">trace</span></tt></a> will collect and
report on the relationships between functions that call each other.</p>
<p>For a simple list of the functions called, use <em class="xref std std-option">--listfuncs</em>:</p>
<div class="highlight-python"><pre>$ python -m trace --listfuncs trace_example/main.py

This is the main program.
recurse(2)
recurse(1)
recurse(0)

functions called:
filename: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py, modulename: threading, funcname: settrace
filename: &lt;string&gt;, modulename: &lt;string&gt;, funcname: &lt;module&gt;
filename: trace_example/main.py, modulename: main, funcname: &lt;module&gt;
filename: trace_example/main.py, modulename: main, funcname: main
filename: trace_example/recurse.py, modulename: recurse, funcname: &lt;module&gt;
filename: trace_example/recurse.py, modulename: recurse, funcname: recurse</pre>
</div>
<p>For more details about who is doing the calling, use
<em class="xref std std-option">--trackcalls</em>.</p>
<div class="highlight-python"><pre>$ python -m trace --listfuncs --trackcalls trace_example/main.py

This is the main program.
recurse(2)
recurse(1)
recurse(0)

calling relationships:

*** /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/trace.py ***
  --&gt; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py
    trace.Trace.run -&gt; threading.settrace
  --&gt; &lt;string&gt;
    trace.Trace.run -&gt; &lt;string&gt;.&lt;module&gt;

*** &lt;string&gt; ***
  --&gt; trace_example/main.py
    &lt;string&gt;.&lt;module&gt; -&gt; main.&lt;module&gt;

*** trace_example/main.py ***
    main.&lt;module&gt; -&gt; main.main
  --&gt; trace_example/recurse.py
    main.&lt;module&gt; -&gt; recurse.&lt;module&gt;
    main.main -&gt; recurse.recurse

*** trace_example/recurse.py ***
    recurse.recurse -&gt; recurse.recurse</pre>
</div>
</div>
</div>
<div class="section" id="programming-interface">
<h2>Programming Interface<a class="headerlink" href="#programming-interface" title="Permalink to this headline">¶</a></h2>
<p>For a little more control over the trace interface, you can invoke it
from within your program using a <tt class="xref py py-class docutils literal"><span class="pre">Trace</span></tt> object.
<tt class="xref py py-class docutils literal"><span class="pre">Trace</span></tt> lets you set up fixtures and other dependencies before
running a single function or execing a Python command to be traced.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">trace</span>
<span class="kn">from</span> <span class="nn">trace_example.recurse</span> <span class="kn">import</span> <span class="n">recurse</span>

<span class="n">tracer</span> <span class="o">=</span> <span class="n">trace</span><span class="o">.</span><span class="n">Trace</span><span class="p">(</span><span class="n">count</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">trace</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">tracer</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="s">&#39;recurse(2)&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Since the example only traces into the <tt class="xref py py-func docutils literal"><span class="pre">recurse()</span></tt> function, no
information from <tt class="docutils literal"><span class="pre">main.py</span></tt> is included in the output.</p>
<div class="highlight-python"><pre>$ python trace_run.py

 --- modulename: threading, funcname: settrace
threading.py(89):     _trace_hook = func
 --- modulename: trace_run, funcname: &lt;module&gt;
&lt;string&gt;(1):   --- modulename: recurse, funcname: recurse
recurse.py(13):     print 'recurse(%s)' % level
recurse(2)
recurse.py(14):     if level:
recurse.py(15):         recurse(level-1)
 --- modulename: recurse, funcname: recurse
recurse.py(13):     print 'recurse(%s)' % level
recurse(1)
recurse.py(14):     if level:
recurse.py(15):         recurse(level-1)
 --- modulename: recurse, funcname: recurse
recurse.py(13):     print 'recurse(%s)' % level
recurse(0)
recurse.py(14):     if level:
recurse.py(16):     return
recurse.py(16):     return
recurse.py(16):     return</pre>
</div>
<p>That same output could have been produced with the <tt class="xref py py-func docutils literal"><span class="pre">runfunc()</span></tt>
method, too.  <tt class="xref py py-func docutils literal"><span class="pre">runfunc()</span></tt> accepts arbitrary positional and
keyword arguments, which are passed to the function when it is called
by the tracer.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">trace</span>
<span class="kn">from</span> <span class="nn">trace_example.recurse</span> <span class="kn">import</span> <span class="n">recurse</span>

<span class="n">tracer</span> <span class="o">=</span> <span class="n">trace</span><span class="o">.</span><span class="n">Trace</span><span class="p">(</span><span class="n">count</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">trace</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">tracer</span><span class="o">.</span><span class="n">runfunc</span><span class="p">(</span><span class="n">recurse</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><pre>$ python trace_runfunc.py

 --- modulename: recurse, funcname: recurse
recurse.py(13):     print 'recurse(%s)' % level
recurse(2)
recurse.py(14):     if level:
recurse.py(15):         recurse(level-1)
 --- modulename: recurse, funcname: recurse
recurse.py(13):     print 'recurse(%s)' % level
recurse(1)
recurse.py(14):     if level:
recurse.py(15):         recurse(level-1)
 --- modulename: recurse, funcname: recurse
recurse.py(13):     print 'recurse(%s)' % level
recurse(0)
recurse.py(14):     if level:
recurse.py(16):     return
recurse.py(16):     return
recurse.py(16):     return</pre>
</div>
<div class="section" id="saving-result-data">
<h3>Saving Result Data<a class="headerlink" href="#saving-result-data" title="Permalink to this headline">¶</a></h3>
<p>Counts and coverage information can be recorded as well, just as with
the command line interface.  The data must be saved explicitly, using
the <tt class="xref py py-class docutils literal"><span class="pre">CoverageResults</span></tt> instance from the <tt class="xref py py-class docutils literal"><span class="pre">Trace</span></tt> object.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">trace</span>
<span class="kn">from</span> <span class="nn">trace_example.recurse</span> <span class="kn">import</span> <span class="n">recurse</span>

<span class="n">tracer</span> <span class="o">=</span> <span class="n">trace</span><span class="o">.</span><span class="n">Trace</span><span class="p">(</span><span class="n">count</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">trace</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">tracer</span><span class="o">.</span><span class="n">runfunc</span><span class="p">(</span><span class="n">recurse</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>

<span class="n">results</span> <span class="o">=</span> <span class="n">tracer</span><span class="o">.</span><span class="n">results</span><span class="p">()</span>
<span class="n">results</span><span class="o">.</span><span class="n">write_results</span><span class="p">(</span><span class="n">coverdir</span><span class="o">=</span><span class="s">&#39;coverdir2&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><pre>$ python trace_CoverageResults.py

recurse(2)
recurse(1)
recurse(0)

$ find coverdir2

coverdir2
coverdir2/trace_example.recurse.cover</pre>
</div>
<p>And the contents of <tt class="docutils literal"><span class="pre">coverdir2/trace_example.recurse.cover</span></tt>:</p>
<div class="highlight-python"><pre>       #!/usr/bin/env python
       # encoding: utf-8
       #
       # Copyright (c) 2008 Doug Hellmann All rights reserved.
       #
       """
       """
       
       #__version__ = "$Id$"
       #end_pymotw_header
       
&gt;&gt;&gt;&gt;&gt;&gt; def recurse(level):
    3:     print 'recurse(%s)' % level
    3:     if level:
    2:         recurse(level-1)
    3:     return
       
&gt;&gt;&gt;&gt;&gt;&gt; def not_called():
&gt;&gt;&gt;&gt;&gt;&gt;     print 'This function is never called.'
</pre>
</div>
<p>To save the counts data for generating reports, use the <em>infile</em> and
<em>outfile</em> argument to <tt class="xref py py-class docutils literal"><span class="pre">Trace</span></tt>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">trace</span>
<span class="kn">from</span> <span class="nn">trace_example.recurse</span> <span class="kn">import</span> <span class="n">recurse</span>

<span class="n">tracer</span> <span class="o">=</span> <span class="n">trace</span><span class="o">.</span><span class="n">Trace</span><span class="p">(</span><span class="n">count</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">trace</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">outfile</span><span class="o">=</span><span class="s">&#39;trace_report.dat&#39;</span><span class="p">)</span>
<span class="n">tracer</span><span class="o">.</span><span class="n">runfunc</span><span class="p">(</span><span class="n">recurse</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>

<span class="n">report_tracer</span> <span class="o">=</span> <span class="n">trace</span><span class="o">.</span><span class="n">Trace</span><span class="p">(</span><span class="n">count</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">trace</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">infile</span><span class="o">=</span><span class="s">&#39;trace_report.dat&#39;</span><span class="p">)</span>
<span class="n">results</span> <span class="o">=</span> <span class="n">tracer</span><span class="o">.</span><span class="n">results</span><span class="p">()</span>
<span class="n">results</span><span class="o">.</span><span class="n">write_results</span><span class="p">(</span><span class="n">summary</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">coverdir</span><span class="o">=</span><span class="s">&#39;/tmp&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Pass a filename to <em>infile</em> to read previously stored data, and a
filename to <em>outfile</em> to write new results after tracing.  If <em>infile</em>
and <em>outfile</em> are the same, it has the effect of updating the file
with cummulative data.</p>
<div class="highlight-python"><pre>$ python trace_report.py

recurse(2)
recurse(1)
recurse(0)
lines   cov%   module   (path)
    7    57%   trace_example.recurse   (/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/trace/trace_example/recurse.py)</pre>
</div>
</div>
<div class="section" id="trace-options">
<h3>Trace Options<a class="headerlink" href="#trace-options" title="Permalink to this headline">¶</a></h3>
<p>The constructor for <tt class="xref py py-class docutils literal"><span class="pre">Trace</span></tt> takes several optional parameters
to control runtime behavior.</p>
<dl class="docutils">
<dt><em>count</em></dt>
<dd>Boolean.  Turns on line number counting.  Defaults to True.</dd>
<dt><em>countfuncs</em></dt>
<dd>Boolean.  Turns on list of functions called during the run.
Defaults to False.</dd>
<dt><em>countcallers</em></dt>
<dd>Boolean.  Turns on tracking for callers and callees.  Defaults to
False.</dd>
<dt><em>ignoremods</em></dt>
<dd>Sequence.  List of modules or packages to ignore when tracking
coverage.  Defaults to an empty tuple.</dd>
<dt><em>ignoredirs</em></dt>
<dd>Sequence.  List of directories containing modules or packages to be
ignored.  Defaults to an empty tuple.</dd>
<dt><em>infile</em></dt>
<dd>Name of the file containing cached count values.  Defaults to None.</dd>
<dt><em>outfile</em></dt>
<dd>Name of the file to use for storing cached count files.  Defaults to
None, and data is not stored.</dd>
</dl>
<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/lib/module-trace.html">trace</a></dt>
<dd>Standard library documentation for this module.</dd>
<dt><a class="reference internal" href="../sys/tracing.html#sys-tracing"><em>Tracing a Program As It Runs</em></a></dt>
<dd>The <a class="reference internal" href="../sys/index.html#module-sys" title="sys: System-specific configuration"><tt class="xref py py-mod docutils literal"><span class="pre">sys</span></tt></a> module includes facilities for adding your own
tracing function to the interpreter at run-time.</dd>
<dt><a class="reference external" href="http://nedbatchelder.com/code/modules/coverage.html">coverage.py</a></dt>
<dd>Ned Batchelder&#8217;s coverage module.</dd>
<dt><a class="reference external" href="http://darcs.idyll.org/~t/projects/figleaf/doc/">figleaf</a></dt>
<dd>Titus Brown&#8217;s coverage app.</dd>
</dl>
</div>
</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="../runtime_services.html" title="Python Runtime Services"
             >next</a> |</li>
        <li class="right" >
          <a href="../timeit/index.html" title="timeit – Time the execution of small bits of Python code."
             >previous</a> |</li>
        <li><a href="../contents.html">PyMOTW</a> &raquo;</li>
          <li><a href="../profilers.html" >Debugging and Profiling</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; 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>

[top] / python / PyMOTW / docs / trace / index.html

contact | logmethods.com