1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>28.10. traceback — Print or retrieve a stack traceback — Python 2.7.11 documentation</title>
11
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
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.11 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.11 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>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="__future__.html" title="28.11. __future__ — Future statement definitions"
55
accesskey="N">next</a> |</li>
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> »</li>
63
<span class="version_switcher_placeholder">2.7.11</span>
64
<a href="../index.html">Documentation</a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
68
<li class="nav-item nav-item-2"><a href="python.html" accesskey="U">28. Python Runtime Services</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
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> — 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 “wrapper” around the
84
<p id="index-0">The module uses traceback objects — 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>
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>
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>
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>
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
124
<div class="versionadded">
125
<p><span class="versionmodified">New in version 2.4.</span></p>
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>
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>
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> “pre-processed” 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 “pre-processed” 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>
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>
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>
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
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>
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>
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>
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>
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>
227
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">traceback</span>
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">">>> "</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">"Exception in user code:"</span>
235
<span class="k">print</span> <span class="s1">'-'</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">'-'</span><span class="o">*</span><span class="mi">60</span>
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>
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 class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">traceback</span>
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>
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>
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">"*** print_tb:"</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">"*** print_exception:"</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">"*** print_exc:"</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">"*** format_exc, first and last line:"</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">"*** format_exception:"</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">"*** extract_tb:"</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">"*** format_tb:"</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">"*** tb_lineno:"</span><span class="p">,</span> <span class="n">exc_traceback</span><span class="o">.</span><span class="n">tb_lineno</span>
279
<p>The output for the example would look similar to this:</p>
280
<div class="highlight-python"><div class="highlight"><pre>*** print_tb:
281
File "<doctest...>", line 10, in <module>
284
Traceback (most recent call last):
285
File "<doctest...>", line 10, in <module>
287
File "<doctest...>", line 4, in lumberjack
288
bright_side_of_death()
289
IndexError: tuple index out of range
291
Traceback (most recent call last):
292
File "<doctest...>", line 10, in <module>
294
File "<doctest...>", 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
['Traceback (most recent call last):\n',
302
' File "<doctest...>", line 10, in <module>\n lumberjack()\n',
303
' File "<doctest...>", line 4, in lumberjack\n bright_side_of_death()\n',
304
' File "<doctest...>", line 7, in bright_side_of_death\n return tuple()[0]\n',
305
'IndexError: tuple index out of range\n']
307
[('<doctest...>', 10, '<module>', 'lumberjack()'),
308
('<doctest...>', 4, 'lumberjack', 'bright_side_of_death()'),
309
('<doctest...>', 7, 'bright_side_of_death', 'return tuple()[0]')]
311
[' File "<doctest...>", line 10, in <module>\n lumberjack()\n',
312
' File "<doctest...>", line 4, in lumberjack\n bright_side_of_death()\n',
313
' File "<doctest...>", line 7, in bright_side_of_death\n return tuple()[0]\n']
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 class="gp">>>> </span><span class="kn">import</span> <span class="nn">traceback</span>
319
<span class="gp">>>> </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">>>> </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">>>> </span><span class="n">another_function</span><span class="p">()</span>
328
<span class="go"> File "<doctest>", line 10, in <module></span>
329
<span class="go"> another_function()</span>
330
<span class="go"> File "<doctest>", line 3, in another_function</span>
331
<span class="go"> lumberstack()</span>
332
<span class="go"> File "<doctest>", line 6, in lumberstack</span>
333
<span class="go"> traceback.print_stack()</span>
334
<span class="go">[('<doctest>', 10, '<module>', 'another_function()'),</span>
335
<span class="go"> ('<doctest>', 3, 'another_function', 'lumberstack()'),</span>
336
<span class="go"> ('<doctest>', 7, 'lumberstack', 'print repr(traceback.extract_stack())')]</span>
337
<span class="go">[' File "<doctest>", line 10, in <module>\n another_function()\n',</span>
338
<span class="go"> ' File "<doctest>", line 3, in another_function\n lumberstack()\n',</span>
339
<span class="go"> ' File "<doctest>", line 8, in lumberstack\n print repr(traceback.format_stack())\n']</span>
342
<p>This last example demonstrates the final few formatting functions:</p>
343
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">traceback</span>
344
<span class="gp">>>> </span><span class="n">traceback</span><span class="o">.</span><span class="n">format_list</span><span class="p">([(</span><span class="s1">'spam.py'</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="s1">'<module>'</span><span class="p">,</span> <span class="s1">'spam.eggs()'</span><span class="p">),</span>
345
<span class="gp">... </span> <span class="p">(</span><span class="s1">'eggs.py'</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="s1">'eggs'</span><span class="p">,</span> <span class="s1">'return "bacon"'</span><span class="p">)])</span>
346
<span class="go">[' File "spam.py", line 3, in <module>\n spam.eggs()\n',</span>
347
<span class="go"> ' File "eggs.py", line 42, in eggs\n return "bacon"\n']</span>
348
<span class="gp">>>> </span><span class="n">an_error</span> <span class="o">=</span> <span class="ne">IndexError</span><span class="p">(</span><span class="s1">'tuple index out of range'</span><span class="p">)</span>
349
<span class="gp">>>> </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">['IndexError: tuple index out of range\n']</span>
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>
364
<li><a class="reference internal" href="#">28.10. <code class="docutils literal"><span class="pre">traceback</span></code> — Print or retrieve a stack traceback</a><ul>
365
<li><a class="reference internal" href="#traceback-examples">28.10.1. Traceback Examples</a></li>
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> — Exit handlers</a></p>
374
<p class="topless"><a href="__future__.html"
375
title="next chapter">28.11. <code class="docutils literal"><span class="pre">__future__</span></code> — Future statement definitions</a></p>
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>
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" />
391
<p class="searchtip" style="font-size: 90%">
392
Enter search terms or a module, class or function name.
395
<script type="text/javascript">$('#searchbox').show(0);</script>
398
<div class="clearer"></div>
400
<div class="related" role="navigation" aria-label="related navigation">
403
<li class="right" style="margin-right: 10px">
404
<a href="../genindex.html" title="General Index"
407
<a href="../py-modindex.html" title="Python Module Index"
410
<a href="__future__.html" title="28.11. __future__ — Future statement definitions"
413
<a href="atexit.html" title="28.9. atexit — Exit handlers"
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> »</li>
419
<span class="version_switcher_placeholder">2.7.11</span>
420
<a href="../index.html">Documentation</a> »
423
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
424
<li class="nav-item nav-item-2"><a href="python.html" >28. Python Runtime Services</a> »</li>
428
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
430
The Python Software Foundation is a non-profit corporation.
431
<a href="https://www.python.org/psf/donations/">Please donate.</a>
433
Last updated on Jan 23, 2016.
434
<a href="../bugs.html">Found a bug</a>?
436
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'