~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.12-docs-html/library/traceback.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
2
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
3
 
 
4
 
 
5
<html xmlns="http://www.w3.org/1999/xhtml">
 
6
  <head>
 
7
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
8
    
 
9
    <title>28.10. traceback — Print or retrieve a stack traceback &mdash; Python 2.7.12 documentation</title>
 
10
    
 
11
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
 
12
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
 
13
    
 
14
    <script type="text/javascript">
 
15
      var DOCUMENTATION_OPTIONS = {
 
16
        URL_ROOT:    '../',
 
17
        VERSION:     '2.7.12',
 
18
        COLLAPSE_INDEX: false,
 
19
        FILE_SUFFIX: '.html',
 
20
        HAS_SOURCE:  true
 
21
      };
 
22
    </script>
 
23
    <script type="text/javascript" src="../_static/jquery.js"></script>
 
24
    <script type="text/javascript" src="../_static/underscore.js"></script>
 
25
    <script type="text/javascript" src="../_static/doctools.js"></script>
 
26
    <script type="text/javascript" src="../_static/sidebar.js"></script>
 
27
    <link rel="search" type="application/opensearchdescription+xml"
 
28
          title="Search within Python 2.7.12 documentation"
 
29
          href="../_static/opensearch.xml"/>
 
30
    <link rel="author" title="About these documents" href="../about.html" />
 
31
    <link rel="copyright" title="Copyright" href="../copyright.html" />
 
32
    <link rel="top" title="Python 2.7.12 documentation" href="../contents.html" />
 
33
    <link rel="up" title="28. Python Runtime Services" href="python.html" />
 
34
    <link rel="next" title="28.11. __future__ — Future statement definitions" href="__future__.html" />
 
35
    <link rel="prev" title="28.9. atexit — Exit handlers" href="atexit.html" />
 
36
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 
37
    <script type="text/javascript" src="../_static/copybutton.js"></script>
 
38
    <script type="text/javascript" src="../_static/version_switch.js"></script>
 
39
 
 
40
    
 
41
 
 
42
  </head>
 
43
  <body role="document">  
 
44
    <div class="related" role="navigation" aria-label="related navigation">
 
45
      <h3>Navigation</h3>
 
46
      <ul>
 
47
        <li class="right" style="margin-right: 10px">
 
48
          <a href="../genindex.html" title="General Index"
 
49
             accesskey="I">index</a></li>
 
50
        <li class="right" >
 
51
          <a href="../py-modindex.html" title="Python Module Index"
 
52
             >modules</a> |</li>
 
53
        <li class="right" >
 
54
          <a href="__future__.html" title="28.11. __future__ — Future statement definitions"
 
55
             accesskey="N">next</a> |</li>
 
56
        <li class="right" >
 
57
          <a href="atexit.html" title="28.9. atexit — Exit handlers"
 
58
             accesskey="P">previous</a> |</li>
 
59
        <li><img src="../_static/py.png" alt=""
 
60
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
61
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
62
        <li>
 
63
          <span class="version_switcher_placeholder">2.7.12</span>
 
64
          <a href="../index.html">Documentation</a> &raquo;
 
65
        </li>
 
66
 
 
67
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
68
          <li class="nav-item nav-item-2"><a href="python.html" accesskey="U">28. Python Runtime Services</a> &raquo;</li> 
 
69
      </ul>
 
70
    </div>    
 
71
 
 
72
    <div class="document">
 
73
      <div class="documentwrapper">
 
74
        <div class="bodywrapper">
 
75
          <div class="body" role="main">
 
76
            
 
77
  <div class="section" id="module-traceback">
 
78
<span id="traceback-print-or-retrieve-a-stack-traceback"></span><h1>28.10. <a class="reference internal" href="#module-traceback" title="traceback: Print or retrieve a stack traceback."><code class="xref py py-mod docutils literal"><span class="pre">traceback</span></code></a> &#8212; Print or retrieve a stack traceback<a class="headerlink" href="#module-traceback" title="Permalink to this headline">¶</a></h1>
 
79
<p>This module provides a standard interface to extract, format and print stack
 
80
traces of Python programs.  It exactly mimics the behavior of the Python
 
81
interpreter when it prints a stack trace.  This is useful when you want to print
 
82
stack traces under program control, such as in a &#8220;wrapper&#8221; around the
 
83
interpreter.</p>
 
84
<p id="index-0">The module uses traceback objects &#8212; this is the object type that is stored in
 
85
the variables <a class="reference internal" href="sys.html#sys.exc_traceback" title="sys.exc_traceback"><code class="xref py py-data docutils literal"><span class="pre">sys.exc_traceback</span></code></a> (deprecated) and
 
86
<a class="reference internal" href="sys.html#sys.last_traceback" title="sys.last_traceback"><code class="xref py py-data docutils literal"><span class="pre">sys.last_traceback</span></code></a> and returned as the third item from
 
87
<a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal"><span class="pre">sys.exc_info()</span></code></a>.</p>
 
88
<p>The module defines the following functions:</p>
 
89
<dl class="function">
 
90
<dt id="traceback.print_tb">
 
91
<code class="descclassname">traceback.</code><code class="descname">print_tb</code><span class="sig-paren">(</span><em>tb</em><span class="optional">[</span>, <em>limit</em><span class="optional">[</span>, <em>file</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#traceback.print_tb" title="Permalink to this definition">¶</a></dt>
 
92
<dd><p>Print up to <em>limit</em> stack trace entries from the traceback object <em>tb</em>. If
 
93
<em>limit</em> is omitted or <code class="docutils literal"><span class="pre">None</span></code>, all entries are printed. If <em>file</em> is omitted
 
94
or <code class="docutils literal"><span class="pre">None</span></code>, the output goes to <code class="docutils literal"><span class="pre">sys.stderr</span></code>; otherwise it should be an
 
95
open file or file-like object to receive the output.</p>
 
96
</dd></dl>
 
97
 
 
98
<dl class="function">
 
99
<dt id="traceback.print_exception">
 
100
<code class="descclassname">traceback.</code><code class="descname">print_exception</code><span class="sig-paren">(</span><em>etype</em>, <em>value</em>, <em>tb</em><span class="optional">[</span>, <em>limit</em><span class="optional">[</span>, <em>file</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#traceback.print_exception" title="Permalink to this definition">¶</a></dt>
 
101
<dd><p>Print exception information and up to <em>limit</em> stack trace entries from the
 
102
traceback <em>tb</em> to <em>file</em>. This differs from <a class="reference internal" href="#traceback.print_tb" title="traceback.print_tb"><code class="xref py py-func docutils literal"><span class="pre">print_tb()</span></code></a> in the following
 
103
ways: (1) if <em>tb</em> is not <code class="docutils literal"><span class="pre">None</span></code>, it prints a header <code class="docutils literal"><span class="pre">Traceback</span> <span class="pre">(most</span>
 
104
<span class="pre">recent</span> <span class="pre">call</span> <span class="pre">last):</span></code>; (2) it prints the exception <em>etype</em> and <em>value</em> after
 
105
the stack trace; (3) if <em>etype</em> is <a class="reference internal" href="exceptions.html#exceptions.SyntaxError" title="exceptions.SyntaxError"><code class="xref py py-exc docutils literal"><span class="pre">SyntaxError</span></code></a> and <em>value</em> has the
 
106
appropriate format, it prints the line where the syntax error occurred with a
 
107
caret indicating the approximate position of the error.</p>
 
108
</dd></dl>
 
109
 
 
110
<dl class="function">
 
111
<dt id="traceback.print_exc">
 
112
<code class="descclassname">traceback.</code><code class="descname">print_exc</code><span class="sig-paren">(</span><span class="optional">[</span><em>limit</em><span class="optional">[</span>, <em>file</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#traceback.print_exc" title="Permalink to this definition">¶</a></dt>
 
113
<dd><p>This is a shorthand for <code class="docutils literal"><span class="pre">print_exception(sys.exc_type,</span> <span class="pre">sys.exc_value,</span>
 
114
<span class="pre">sys.exc_traceback,</span> <span class="pre">limit,</span> <span class="pre">file)</span></code>.  (In fact, it uses <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal"><span class="pre">sys.exc_info()</span></code></a> to
 
115
retrieve the same information in a thread-safe way instead of using the
 
116
deprecated variables.)</p>
 
117
</dd></dl>
 
118
 
 
119
<dl class="function">
 
120
<dt id="traceback.format_exc">
 
121
<code class="descclassname">traceback.</code><code class="descname">format_exc</code><span class="sig-paren">(</span><span class="optional">[</span><em>limit</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#traceback.format_exc" title="Permalink to this definition">¶</a></dt>
 
122
<dd><p>This is like <code class="docutils literal"><span class="pre">print_exc(limit)</span></code> but returns a string instead of printing to
 
123
a file.</p>
 
124
<div class="versionadded">
 
125
<p><span class="versionmodified">New in version 2.4.</span></p>
 
126
</div>
 
127
</dd></dl>
 
128
 
 
129
<dl class="function">
 
130
<dt id="traceback.print_last">
 
131
<code class="descclassname">traceback.</code><code class="descname">print_last</code><span class="sig-paren">(</span><span class="optional">[</span><em>limit</em><span class="optional">[</span>, <em>file</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#traceback.print_last" title="Permalink to this definition">¶</a></dt>
 
132
<dd><p>This is a shorthand for <code class="docutils literal"><span class="pre">print_exception(sys.last_type,</span> <span class="pre">sys.last_value,</span>
 
133
<span class="pre">sys.last_traceback,</span> <span class="pre">limit,</span> <span class="pre">file)</span></code>.  In general it will work only after
 
134
an exception has reached an interactive prompt (see <a class="reference internal" href="sys.html#sys.last_type" title="sys.last_type"><code class="xref py py-data docutils literal"><span class="pre">sys.last_type</span></code></a>).</p>
 
135
</dd></dl>
 
136
 
 
137
<dl class="function">
 
138
<dt id="traceback.print_stack">
 
139
<code class="descclassname">traceback.</code><code class="descname">print_stack</code><span class="sig-paren">(</span><span class="optional">[</span><em>f</em><span class="optional">[</span>, <em>limit</em><span class="optional">[</span>, <em>file</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#traceback.print_stack" title="Permalink to this definition">¶</a></dt>
 
140
<dd><p>This function prints a stack trace from its invocation point. The optional
 
141
<em>f</em> argument can be used to specify an alternate stack frame to start. The
 
142
optional limit* and <em>file</em> arguments have the same meaning as for
 
143
<a class="reference internal" href="#traceback.print_exception" title="traceback.print_exception"><code class="xref py py-func docutils literal"><span class="pre">print_exception()</span></code></a>.</p>
 
144
</dd></dl>
 
145
 
 
146
<dl class="function">
 
147
<dt id="traceback.extract_tb">
 
148
<code class="descclassname">traceback.</code><code class="descname">extract_tb</code><span class="sig-paren">(</span><em>tb</em><span class="optional">[</span>, <em>limit</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#traceback.extract_tb" title="Permalink to this definition">¶</a></dt>
 
149
<dd><p>Return a list of up to <em>limit</em> &#8220;pre-processed&#8221; stack trace entries extracted
 
150
from the traceback object <em>tb</em>.  It is useful for alternate formatting of
 
151
stack traces.  If <em>limit</em> is omitted or <code class="docutils literal"><span class="pre">None</span></code>, all entries are extracted.
 
152
A &#8220;pre-processed&#8221; stack trace entry is a 4-tuple (<em>filename</em>, <em>line number</em>,
 
153
function name*, <em>text</em>) representing the information that is usually printed
 
154
for a stack trace.  The <em>text</em> is a string with leading and trailing
 
155
whitespace stripped; if the source is not available it is <code class="docutils literal"><span class="pre">None</span></code>.</p>
 
156
</dd></dl>
 
157
 
 
158
<dl class="function">
 
159
<dt id="traceback.extract_stack">
 
160
<code class="descclassname">traceback.</code><code class="descname">extract_stack</code><span class="sig-paren">(</span><span class="optional">[</span><em>f</em><span class="optional">[</span>, <em>limit</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#traceback.extract_stack" title="Permalink to this definition">¶</a></dt>
 
161
<dd><p>Extract the raw traceback from the current stack frame.  The return value has
 
162
the same format as for <a class="reference internal" href="#traceback.extract_tb" title="traceback.extract_tb"><code class="xref py py-func docutils literal"><span class="pre">extract_tb()</span></code></a>.  The optional <em>f</em> and <em>limit</em>
 
163
arguments have the same meaning as for <a class="reference internal" href="#traceback.print_stack" title="traceback.print_stack"><code class="xref py py-func docutils literal"><span class="pre">print_stack()</span></code></a>.</p>
 
164
</dd></dl>
 
165
 
 
166
<dl class="function">
 
167
<dt id="traceback.format_list">
 
168
<code class="descclassname">traceback.</code><code class="descname">format_list</code><span class="sig-paren">(</span><em>extracted_list</em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.format_list" title="Permalink to this definition">¶</a></dt>
 
169
<dd><p>Given a list of tuples as returned by <a class="reference internal" href="#traceback.extract_tb" title="traceback.extract_tb"><code class="xref py py-func docutils literal"><span class="pre">extract_tb()</span></code></a> or
 
170
<a class="reference internal" href="#traceback.extract_stack" title="traceback.extract_stack"><code class="xref py py-func docutils literal"><span class="pre">extract_stack()</span></code></a>, return a list of strings ready for printing.  Each
 
171
string in the resulting list corresponds to the item with the same index in
 
172
the argument list.  Each string ends in a newline; the strings may contain
 
173
internal newlines as well, for those items whose source text line is not
 
174
<code class="docutils literal"><span class="pre">None</span></code>.</p>
 
175
</dd></dl>
 
176
 
 
177
<dl class="function">
 
178
<dt id="traceback.format_exception_only">
 
179
<code class="descclassname">traceback.</code><code class="descname">format_exception_only</code><span class="sig-paren">(</span><em>etype</em>, <em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.format_exception_only" title="Permalink to this definition">¶</a></dt>
 
180
<dd><p>Format the exception part of a traceback.  The arguments are the exception
 
181
type, <em>etype</em> and <em>value</em> such as given by <code class="docutils literal"><span class="pre">sys.last_type</span></code> and
 
182
<code class="docutils literal"><span class="pre">sys.last_value</span></code>.  The return value is a list of strings, each ending in a
 
183
newline.  Normally, the list contains a single string; however, for
 
184
<a class="reference internal" href="exceptions.html#exceptions.SyntaxError" title="exceptions.SyntaxError"><code class="xref py py-exc docutils literal"><span class="pre">SyntaxError</span></code></a> exceptions, it contains several lines that (when printed)
 
185
display detailed information about where the syntax error occurred.  The
 
186
message indicating which exception occurred is the always last string in the
 
187
list.</p>
 
188
</dd></dl>
 
189
 
 
190
<dl class="function">
 
191
<dt id="traceback.format_exception">
 
192
<code class="descclassname">traceback.</code><code class="descname">format_exception</code><span class="sig-paren">(</span><em>etype</em>, <em>value</em>, <em>tb</em><span class="optional">[</span>, <em>limit</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#traceback.format_exception" title="Permalink to this definition">¶</a></dt>
 
193
<dd><p>Format a stack trace and the exception information.  The arguments  have the
 
194
same meaning as the corresponding arguments to <a class="reference internal" href="#traceback.print_exception" title="traceback.print_exception"><code class="xref py py-func docutils literal"><span class="pre">print_exception()</span></code></a>.  The
 
195
return value is a list of strings, each ending in a newline and some
 
196
containing internal newlines.  When these lines are concatenated and printed,
 
197
exactly the same text is printed as does <a class="reference internal" href="#traceback.print_exception" title="traceback.print_exception"><code class="xref py py-func docutils literal"><span class="pre">print_exception()</span></code></a>.</p>
 
198
</dd></dl>
 
199
 
 
200
<dl class="function">
 
201
<dt id="traceback.format_tb">
 
202
<code class="descclassname">traceback.</code><code class="descname">format_tb</code><span class="sig-paren">(</span><em>tb</em><span class="optional">[</span>, <em>limit</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#traceback.format_tb" title="Permalink to this definition">¶</a></dt>
 
203
<dd><p>A shorthand for <code class="docutils literal"><span class="pre">format_list(extract_tb(tb,</span> <span class="pre">limit))</span></code>.</p>
 
204
</dd></dl>
 
205
 
 
206
<dl class="function">
 
207
<dt id="traceback.format_stack">
 
208
<code class="descclassname">traceback.</code><code class="descname">format_stack</code><span class="sig-paren">(</span><span class="optional">[</span><em>f</em><span class="optional">[</span>, <em>limit</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#traceback.format_stack" title="Permalink to this definition">¶</a></dt>
 
209
<dd><p>A shorthand for <code class="docutils literal"><span class="pre">format_list(extract_stack(f,</span> <span class="pre">limit))</span></code>.</p>
 
210
</dd></dl>
 
211
 
 
212
<dl class="function">
 
213
<dt id="traceback.tb_lineno">
 
214
<code class="descclassname">traceback.</code><code class="descname">tb_lineno</code><span class="sig-paren">(</span><em>tb</em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.tb_lineno" title="Permalink to this definition">¶</a></dt>
 
215
<dd><p>This function returns the current line number set in the traceback object.
 
216
This function was necessary because in versions of Python prior to 2.3 when
 
217
the <a class="reference internal" href="../using/cmdline.html#cmdoption-O"><code class="xref std std-option docutils literal"><span class="pre">-O</span></code></a> flag was passed to Python the <code class="docutils literal"><span class="pre">tb.tb_lineno</span></code> was not
 
218
updated correctly.  This function has no use in versions past 2.3.</p>
 
219
</dd></dl>
 
220
 
 
221
<div class="section" id="traceback-examples">
 
222
<span id="traceback-example"></span><h2>28.10.1. Traceback Examples<a class="headerlink" href="#traceback-examples" title="Permalink to this headline">¶</a></h2>
 
223
<p>This simple example implements a basic read-eval-print loop, similar to (but
 
224
less useful than) the standard Python interactive interpreter loop.  For a more
 
225
complete implementation of the interpreter loop, refer to the <a class="reference internal" href="code.html#module-code" title="code: Facilities to implement read-eval-print loops."><code class="xref py py-mod docutils literal"><span class="pre">code</span></code></a>
 
226
module.</p>
 
227
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">traceback</span>
 
228
 
 
229
<span class="k">def</span> <span class="nf">run_user_code</span><span class="p">(</span><span class="n">envdir</span><span class="p">):</span>
 
230
    <span class="n">source</span> <span class="o">=</span> <span class="nb">raw_input</span><span class="p">(</span><span class="s2">&quot;&gt;&gt;&gt; &quot;</span><span class="p">)</span>
 
231
    <span class="k">try</span><span class="p">:</span>
 
232
        <span class="k">exec</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">envdir</span>
 
233
    <span class="k">except</span><span class="p">:</span>
 
234
        <span class="k">print</span> <span class="s2">&quot;Exception in user code:&quot;</span>
 
235
        <span class="k">print</span> <span class="s1">&#39;-&#39;</span><span class="o">*</span><span class="mi">60</span>
 
236
        <span class="n">traceback</span><span class="o">.</span><span class="n">print_exc</span><span class="p">(</span><span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>
 
237
        <span class="k">print</span> <span class="s1">&#39;-&#39;</span><span class="o">*</span><span class="mi">60</span>
 
238
 
 
239
<span class="n">envdir</span> <span class="o">=</span> <span class="p">{}</span>
 
240
<span class="k">while</span> <span class="mi">1</span><span class="p">:</span>
 
241
    <span class="n">run_user_code</span><span class="p">(</span><span class="n">envdir</span><span class="p">)</span>
 
242
</pre></div>
 
243
</div>
 
244
<p>The following example demonstrates the different ways to print and format the
 
245
exception and traceback:</p>
 
246
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">traceback</span>
 
247
 
 
248
<span class="k">def</span> <span class="nf">lumberjack</span><span class="p">():</span>
 
249
    <span class="n">bright_side_of_death</span><span class="p">()</span>
 
250
 
 
251
<span class="k">def</span> <span class="nf">bright_side_of_death</span><span class="p">():</span>
 
252
    <span class="k">return</span> <span class="nb">tuple</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
 
253
 
 
254
<span class="k">try</span><span class="p">:</span>
 
255
    <span class="n">lumberjack</span><span class="p">()</span>
 
256
<span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
 
257
    <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">exc_traceback</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()</span>
 
258
    <span class="k">print</span> <span class="s2">&quot;*** print_tb:&quot;</span>
 
259
    <span class="n">traceback</span><span class="o">.</span><span class="n">print_tb</span><span class="p">(</span><span class="n">exc_traceback</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>
 
260
    <span class="k">print</span> <span class="s2">&quot;*** print_exception:&quot;</span>
 
261
    <span class="n">traceback</span><span class="o">.</span><span class="n">print_exception</span><span class="p">(</span><span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">exc_traceback</span><span class="p">,</span>
 
262
                              <span class="n">limit</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>
 
263
    <span class="k">print</span> <span class="s2">&quot;*** print_exc:&quot;</span>
 
264
    <span class="n">traceback</span><span class="o">.</span><span class="n">print_exc</span><span class="p">()</span>
 
265
    <span class="k">print</span> <span class="s2">&quot;*** format_exc, first and last line:&quot;</span>
 
266
    <span class="n">formatted_lines</span> <span class="o">=</span> <span class="n">traceback</span><span class="o">.</span><span class="n">format_exc</span><span class="p">()</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()</span>
 
267
    <span class="k">print</span> <span class="n">formatted_lines</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
 
268
    <span class="k">print</span> <span class="n">formatted_lines</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
 
269
    <span class="k">print</span> <span class="s2">&quot;*** format_exception:&quot;</span>
 
270
    <span class="k">print</span> <span class="nb">repr</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">format_exception</span><span class="p">(</span><span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span>
 
271
                                          <span class="n">exc_traceback</span><span class="p">))</span>
 
272
    <span class="k">print</span> <span class="s2">&quot;*** extract_tb:&quot;</span>
 
273
    <span class="k">print</span> <span class="nb">repr</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">extract_tb</span><span class="p">(</span><span class="n">exc_traceback</span><span class="p">))</span>
 
274
    <span class="k">print</span> <span class="s2">&quot;*** format_tb:&quot;</span>
 
275
    <span class="k">print</span> <span class="nb">repr</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">format_tb</span><span class="p">(</span><span class="n">exc_traceback</span><span class="p">))</span>
 
276
    <span class="k">print</span> <span class="s2">&quot;*** tb_lineno:&quot;</span><span class="p">,</span> <span class="n">exc_traceback</span><span class="o">.</span><span class="n">tb_lineno</span>
 
277
</pre></div>
 
278
</div>
 
279
<p>The output for the example would look similar to this:</p>
 
280
<div class="highlight-python"><div class="highlight"><pre><span></span>*** print_tb:
 
281
  File &quot;&lt;doctest...&gt;&quot;, line 10, in &lt;module&gt;
 
282
    lumberjack()
 
283
*** print_exception:
 
284
Traceback (most recent call last):
 
285
  File &quot;&lt;doctest...&gt;&quot;, line 10, in &lt;module&gt;
 
286
    lumberjack()
 
287
  File &quot;&lt;doctest...&gt;&quot;, line 4, in lumberjack
 
288
    bright_side_of_death()
 
289
IndexError: tuple index out of range
 
290
*** print_exc:
 
291
Traceback (most recent call last):
 
292
  File &quot;&lt;doctest...&gt;&quot;, line 10, in &lt;module&gt;
 
293
    lumberjack()
 
294
  File &quot;&lt;doctest...&gt;&quot;, line 4, in lumberjack
 
295
    bright_side_of_death()
 
296
IndexError: tuple index out of range
 
297
*** format_exc, first and last line:
 
298
Traceback (most recent call last):
 
299
IndexError: tuple index out of range
 
300
*** format_exception:
 
301
[&#39;Traceback (most recent call last):\n&#39;,
 
302
 &#39;  File &quot;&lt;doctest...&gt;&quot;, line 10, in &lt;module&gt;\n    lumberjack()\n&#39;,
 
303
 &#39;  File &quot;&lt;doctest...&gt;&quot;, line 4, in lumberjack\n    bright_side_of_death()\n&#39;,
 
304
 &#39;  File &quot;&lt;doctest...&gt;&quot;, line 7, in bright_side_of_death\n    return tuple()[0]\n&#39;,
 
305
 &#39;IndexError: tuple index out of range\n&#39;]
 
306
*** extract_tb:
 
307
[(&#39;&lt;doctest...&gt;&#39;, 10, &#39;&lt;module&gt;&#39;, &#39;lumberjack()&#39;),
 
308
 (&#39;&lt;doctest...&gt;&#39;, 4, &#39;lumberjack&#39;, &#39;bright_side_of_death()&#39;),
 
309
 (&#39;&lt;doctest...&gt;&#39;, 7, &#39;bright_side_of_death&#39;, &#39;return tuple()[0]&#39;)]
 
310
*** format_tb:
 
311
[&#39;  File &quot;&lt;doctest...&gt;&quot;, line 10, in &lt;module&gt;\n    lumberjack()\n&#39;,
 
312
 &#39;  File &quot;&lt;doctest...&gt;&quot;, line 4, in lumberjack\n    bright_side_of_death()\n&#39;,
 
313
 &#39;  File &quot;&lt;doctest...&gt;&quot;, line 7, in bright_side_of_death\n    return tuple()[0]\n&#39;]
 
314
*** tb_lineno: 10
 
315
</pre></div>
 
316
</div>
 
317
<p>The following example shows the different ways to print and format the stack:</p>
 
318
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">traceback</span>
 
319
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">another_function</span><span class="p">():</span>
 
320
<span class="gp">... </span>    <span class="n">lumberstack</span><span class="p">()</span>
 
321
<span class="gp">...</span>
 
322
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">lumberstack</span><span class="p">():</span>
 
323
<span class="gp">... </span>    <span class="n">traceback</span><span class="o">.</span><span class="n">print_stack</span><span class="p">()</span>
 
324
<span class="gp">... </span>    <span class="k">print</span> <span class="nb">repr</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">extract_stack</span><span class="p">())</span>
 
325
<span class="gp">... </span>    <span class="k">print</span> <span class="nb">repr</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">format_stack</span><span class="p">())</span>
 
326
<span class="gp">...</span>
 
327
<span class="gp">&gt;&gt;&gt; </span><span class="n">another_function</span><span class="p">()</span>
 
328
<span class="go">  File &quot;&lt;doctest&gt;&quot;, line 10, in &lt;module&gt;</span>
 
329
<span class="go">    another_function()</span>
 
330
<span class="go">  File &quot;&lt;doctest&gt;&quot;, line 3, in another_function</span>
 
331
<span class="go">    lumberstack()</span>
 
332
<span class="go">  File &quot;&lt;doctest&gt;&quot;, line 6, in lumberstack</span>
 
333
<span class="go">    traceback.print_stack()</span>
 
334
<span class="go">[(&#39;&lt;doctest&gt;&#39;, 10, &#39;&lt;module&gt;&#39;, &#39;another_function()&#39;),</span>
 
335
<span class="go"> (&#39;&lt;doctest&gt;&#39;, 3, &#39;another_function&#39;, &#39;lumberstack()&#39;),</span>
 
336
<span class="go"> (&#39;&lt;doctest&gt;&#39;, 7, &#39;lumberstack&#39;, &#39;print repr(traceback.extract_stack())&#39;)]</span>
 
337
<span class="go">[&#39;  File &quot;&lt;doctest&gt;&quot;, line 10, in &lt;module&gt;\n    another_function()\n&#39;,</span>
 
338
<span class="go"> &#39;  File &quot;&lt;doctest&gt;&quot;, line 3, in another_function\n    lumberstack()\n&#39;,</span>
 
339
<span class="go"> &#39;  File &quot;&lt;doctest&gt;&quot;, line 8, in lumberstack\n    print repr(traceback.format_stack())\n&#39;]</span>
 
340
</pre></div>
 
341
</div>
 
342
<p>This last example demonstrates the final few formatting functions:</p>
 
343
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">traceback</span>
 
344
<span class="gp">&gt;&gt;&gt; </span><span class="n">traceback</span><span class="o">.</span><span class="n">format_list</span><span class="p">([(</span><span class="s1">&#39;spam.py&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="s1">&#39;&lt;module&gt;&#39;</span><span class="p">,</span> <span class="s1">&#39;spam.eggs()&#39;</span><span class="p">),</span>
 
345
<span class="gp">... </span>                       <span class="p">(</span><span class="s1">&#39;eggs.py&#39;</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="s1">&#39;eggs&#39;</span><span class="p">,</span> <span class="s1">&#39;return &quot;bacon&quot;&#39;</span><span class="p">)])</span>
 
346
<span class="go">[&#39;  File &quot;spam.py&quot;, line 3, in &lt;module&gt;\n    spam.eggs()\n&#39;,</span>
 
347
<span class="go"> &#39;  File &quot;eggs.py&quot;, line 42, in eggs\n    return &quot;bacon&quot;\n&#39;]</span>
 
348
<span class="gp">&gt;&gt;&gt; </span><span class="n">an_error</span> <span class="o">=</span> <span class="ne">IndexError</span><span class="p">(</span><span class="s1">&#39;tuple index out of range&#39;</span><span class="p">)</span>
 
349
<span class="gp">&gt;&gt;&gt; </span><span class="n">traceback</span><span class="o">.</span><span class="n">format_exception_only</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">an_error</span><span class="p">),</span> <span class="n">an_error</span><span class="p">)</span>
 
350
<span class="go">[&#39;IndexError: tuple index out of range\n&#39;]</span>
 
351
</pre></div>
 
352
</div>
 
353
</div>
 
354
</div>
 
355
 
 
356
 
 
357
          </div>
 
358
        </div>
 
359
      </div>
 
360
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
 
361
        <div class="sphinxsidebarwrapper">
 
362
  <h3><a href="../contents.html">Table Of Contents</a></h3>
 
363
  <ul>
 
364
<li><a class="reference internal" href="#">28.10. <code class="docutils literal"><span class="pre">traceback</span></code> &#8212; Print or retrieve a stack traceback</a><ul>
 
365
<li><a class="reference internal" href="#traceback-examples">28.10.1. Traceback Examples</a></li>
 
366
</ul>
 
367
</li>
 
368
</ul>
 
369
 
 
370
  <h4>Previous topic</h4>
 
371
  <p class="topless"><a href="atexit.html"
 
372
                        title="previous chapter">28.9. <code class="docutils literal"><span class="pre">atexit</span></code> &#8212; Exit handlers</a></p>
 
373
  <h4>Next topic</h4>
 
374
  <p class="topless"><a href="__future__.html"
 
375
                        title="next chapter">28.11. <code class="docutils literal"><span class="pre">__future__</span></code> &#8212; Future statement definitions</a></p>
 
376
<h3>This Page</h3>
 
377
<ul class="this-page-menu">
 
378
  <li><a href="../bugs.html">Report a Bug</a></li>
 
379
  <li><a href="../_sources/library/traceback.txt"
 
380
         rel="nofollow">Show Source</a></li>
 
381
</ul>
 
382
 
 
383
<div id="searchbox" style="display: none" role="search">
 
384
  <h3>Quick search</h3>
 
385
    <form class="search" action="../search.html" method="get">
 
386
      <input type="text" name="q" />
 
387
      <input type="submit" value="Go" />
 
388
      <input type="hidden" name="check_keywords" value="yes" />
 
389
      <input type="hidden" name="area" value="default" />
 
390
    </form>
 
391
    <p class="searchtip" style="font-size: 90%">
 
392
    Enter search terms or a module, class or function name.
 
393
    </p>
 
394
</div>
 
395
<script type="text/javascript">$('#searchbox').show(0);</script>
 
396
        </div>
 
397
      </div>
 
398
      <div class="clearer"></div>
 
399
    </div>  
 
400
    <div class="related" role="navigation" aria-label="related navigation">
 
401
      <h3>Navigation</h3>
 
402
      <ul>
 
403
        <li class="right" style="margin-right: 10px">
 
404
          <a href="../genindex.html" title="General Index"
 
405
             >index</a></li>
 
406
        <li class="right" >
 
407
          <a href="../py-modindex.html" title="Python Module Index"
 
408
             >modules</a> |</li>
 
409
        <li class="right" >
 
410
          <a href="__future__.html" title="28.11. __future__ — Future statement definitions"
 
411
             >next</a> |</li>
 
412
        <li class="right" >
 
413
          <a href="atexit.html" title="28.9. atexit — Exit handlers"
 
414
             >previous</a> |</li>
 
415
        <li><img src="../_static/py.png" alt=""
 
416
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
417
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
418
        <li>
 
419
          <span class="version_switcher_placeholder">2.7.12</span>
 
420
          <a href="../index.html">Documentation</a> &raquo;
 
421
        </li>
 
422
 
 
423
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
424
          <li class="nav-item nav-item-2"><a href="python.html" >28. Python Runtime Services</a> &raquo;</li> 
 
425
      </ul>
 
426
    </div>  
 
427
    <div class="footer">
 
428
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
 
429
    <br />
 
430
    The Python Software Foundation is a non-profit corporation.
 
431
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
 
432
    <br />
 
433
    Last updated on Sep 20, 2016.
 
434
    <a href="../bugs.html">Found a bug</a>?
 
435
    <br />
 
436
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
 
437
    </div>
 
438
 
 
439
  </body>
 
440
</html>
 
 
b'\\ No newline at end of file'