Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="module-linecache.html" /> | |
13 | <link rel="prev" href="module-inspect.html" /> | |
14 | <link rel="parent" href="python.html" /> | |
15 | <link rel="next" href="traceback-example.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>3.12 traceback -- Print or retrieve a stack traceback</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="3.11.4 The interpreter stack" | |
25 | href="inspect-stack.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" | |
28 | href="python.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="3.12.1 Traceback Example" | |
31 | href="traceback-example.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="inspect-stack.html">3.11.4 The interpreter stack</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="traceback-example.html">3.12.1 Traceback Example</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION0051200000000000000000"> | |
56 | 3.12 <tt class="module">traceback</tt> -- | |
57 | Print or retrieve a stack traceback</A> | |
58 | </H1> | |
59 | ||
60 | <P> | |
61 | <A NAME="module-traceback"></A> | |
62 | ||
63 | <P> | |
64 | This module provides a standard interface to extract, format and print | |
65 | stack traces of Python programs. It exactly mimics the behavior of | |
66 | the Python interpreter when it prints a stack trace. This is useful | |
67 | when you want to print stack traces under program control, such as in a | |
68 | ``wrapper'' around the interpreter. | |
69 | ||
70 | <P> | |
71 | The module uses traceback objects -- this is the object type that is | |
72 | stored in the variables <code>sys.exc_traceback</code> (deprecated) and | |
73 | <code>sys.last_traceback</code> and returned as the third item from | |
74 | <tt class="function">sys.exc_info()</tt>. | |
75 | <a id='l2h-605' xml:id='l2h-605'></a> | |
76 | <P> | |
77 | The module defines the following functions: | |
78 | ||
79 | <P> | |
80 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
81 | <td><nobr><b><tt id='l2h-606' xml:id='l2h-606' class="function">print_tb</tt></b>(</nobr></td> | |
82 | <td><var>traceback</var><big>[</big><var>, limit</var><big>[</big><var>, file</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
83 | <dd> | |
84 | Print up to <var>limit</var> stack trace entries from <var>traceback</var>. If | |
85 | <var>limit</var> is omitted or <code>None</code>, all entries are printed. | |
86 | If <var>file</var> is omitted or <code>None</code>, the output goes to | |
87 | <code>sys.stderr</code>; otherwise it should be an open file or file-like | |
88 | object to receive the output. | |
89 | </dl> | |
90 | ||
91 | <P> | |
92 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
93 | <td><nobr><b><tt id='l2h-607' xml:id='l2h-607' class="function">print_exception</tt></b>(</nobr></td> | |
94 | <td><var>type, value, traceback</var><big>[</big><var>, | |
95 | limit</var><big>[</big><var>, file</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
96 | <dd> | |
97 | Print exception information and up to <var>limit</var> stack trace entries | |
98 | from <var>traceback</var> to <var>file</var>. | |
99 | This differs from <tt class="function">print_tb()</tt> in the | |
100 | following ways: (1) if <var>traceback</var> is not <code>None</code>, it prints a | |
101 | header "<tt class="samp">Traceback (most recent call last):</tt>"; (2) it prints the | |
102 | exception <var>type</var> and <var>value</var> after the stack trace; (3) if | |
103 | <var>type</var> is <tt class="exception">SyntaxError</tt> and <var>value</var> has the | |
104 | appropriate format, it prints the line where the syntax error occurred | |
105 | with a caret indicating the approximate position of the error. | |
106 | </dl> | |
107 | ||
108 | <P> | |
109 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
110 | <td><nobr><b><tt id='l2h-608' xml:id='l2h-608' class="function">print_exc</tt></b>(</nobr></td> | |
111 | <td><var></var><big>[</big><var>limit</var><big>[</big><var>, file</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
112 | <dd> | |
113 | This is a shorthand for <code>print_exception(sys.exc_type, | |
114 | sys.exc_value, sys.exc_traceback, <var>limit</var>, <var>file</var>)</code>. (In | |
115 | fact, it uses <tt class="function">sys.exc_info()</tt> to retrieve the same | |
116 | information in a thread-safe way instead of using the deprecated | |
117 | variables.) | |
118 | </dl> | |
119 | ||
120 | <P> | |
121 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
122 | <td><nobr><b><tt id='l2h-609' xml:id='l2h-609' class="function">format_exc</tt></b>(</nobr></td> | |
123 | <td><var></var><big>[</big><var>limit</var><big>[</big><var>, file</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
124 | <dd> | |
125 | This is like <code>print_exc(<var>limit</var>)</code> but returns a string | |
126 | instead of printing to a file. | |
127 | ||
128 | <span class="versionnote">New in version 2.4.</span> | |
129 | ||
130 | </dl> | |
131 | ||
132 | <P> | |
133 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
134 | <td><nobr><b><tt id='l2h-610' xml:id='l2h-610' class="function">print_last</tt></b>(</nobr></td> | |
135 | <td><var></var><big>[</big><var>limit</var><big>[</big><var>, file</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
136 | <dd> | |
137 | This is a shorthand for <code>print_exception(sys.last_type, | |
138 | sys.last_value, sys.last_traceback, <var>limit</var>, <var>file</var>)</code>. | |
139 | </dl> | |
140 | ||
141 | <P> | |
142 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
143 | <td><nobr><b><tt id='l2h-611' xml:id='l2h-611' class="function">print_stack</tt></b>(</nobr></td> | |
144 | <td><var></var><big>[</big><var>f</var><big>[</big><var>, limit</var><big>[</big><var>, file</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
145 | <dd> | |
146 | This function prints a stack trace from its invocation point. The | |
147 | optional <var>f</var> argument can be used to specify an alternate stack | |
148 | frame to start. The optional <var>limit</var> and <var>file</var> arguments have the | |
149 | same meaning as for <tt class="function">print_exception()</tt>. | |
150 | </dl> | |
151 | ||
152 | <P> | |
153 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
154 | <td><nobr><b><tt id='l2h-612' xml:id='l2h-612' class="function">extract_tb</tt></b>(</nobr></td> | |
155 | <td><var>traceback</var><big>[</big><var>, limit</var><big>]</big><var></var>)</td></tr></table></dt> | |
156 | <dd> | |
157 | Return a list of up to <var>limit</var> ``pre-processed'' stack trace | |
158 | entries extracted from the traceback object <var>traceback</var>. It is | |
159 | useful for alternate formatting of stack traces. If <var>limit</var> is | |
160 | omitted or <code>None</code>, all entries are extracted. A | |
161 | ``pre-processed'' stack trace entry is a quadruple (<var>filename</var>, | |
162 | <var>line number</var>, <var>function name</var>, <var>text</var>) representing | |
163 | the information that is usually printed for a stack trace. The | |
164 | <var>text</var> is a string with leading and trailing whitespace | |
165 | stripped; if the source is not available it is <code>None</code>. | |
166 | </dl> | |
167 | ||
168 | <P> | |
169 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
170 | <td><nobr><b><tt id='l2h-613' xml:id='l2h-613' class="function">extract_stack</tt></b>(</nobr></td> | |
171 | <td><var></var><big>[</big><var>f</var><big>[</big><var>, limit</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
172 | <dd> | |
173 | Extract the raw traceback from the current stack frame. The return | |
174 | value has the same format as for <tt class="function">extract_tb()</tt>. The | |
175 | optional <var>f</var> and <var>limit</var> arguments have the same meaning as | |
176 | for <tt class="function">print_stack()</tt>. | |
177 | </dl> | |
178 | ||
179 | <P> | |
180 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
181 | <td><nobr><b><tt id='l2h-614' xml:id='l2h-614' class="function">format_list</tt></b>(</nobr></td> | |
182 | <td><var>list</var>)</td></tr></table></dt> | |
183 | <dd> | |
184 | Given a list of tuples as returned by <tt class="function">extract_tb()</tt> or | |
185 | <tt class="function">extract_stack()</tt>, return a list of strings ready for | |
186 | printing. Each string in the resulting list corresponds to the item | |
187 | with the same index in the argument list. Each string ends in a | |
188 | newline; the strings may contain internal newlines as well, for those | |
189 | items whose source text line is not <code>None</code>. | |
190 | </dl> | |
191 | ||
192 | <P> | |
193 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
194 | <td><nobr><b><tt id='l2h-615' xml:id='l2h-615' class="function">format_exception_only</tt></b>(</nobr></td> | |
195 | <td><var>type, value</var>)</td></tr></table></dt> | |
196 | <dd> | |
197 | Format the exception part of a traceback. The arguments are the | |
198 | exception type and value such as given by <code>sys.last_type</code> and | |
199 | <code>sys.last_value</code>. The return value is a list of strings, each | |
200 | ending in a newline. Normally, the list contains a single string; | |
201 | however, for <tt class="exception">SyntaxError</tt> exceptions, it contains several | |
202 | lines that (when printed) display detailed information about where the | |
203 | syntax error occurred. The message indicating which exception | |
204 | occurred is the always last string in the list. | |
205 | </dl> | |
206 | ||
207 | <P> | |
208 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
209 | <td><nobr><b><tt id='l2h-616' xml:id='l2h-616' class="function">format_exception</tt></b>(</nobr></td> | |
210 | <td><var>type, value, tb</var><big>[</big><var>, limit</var><big>]</big><var></var>)</td></tr></table></dt> | |
211 | <dd> | |
212 | Format a stack trace and the exception information. The arguments | |
213 | have the same meaning as the corresponding arguments to | |
214 | <tt class="function">print_exception()</tt>. The return value is a list of strings, | |
215 | each ending in a newline and some containing internal newlines. When | |
216 | these lines are concatenated and printed, exactly the same text is | |
217 | printed as does <tt class="function">print_exception()</tt>. | |
218 | </dl> | |
219 | ||
220 | <P> | |
221 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
222 | <td><nobr><b><tt id='l2h-617' xml:id='l2h-617' class="function">format_tb</tt></b>(</nobr></td> | |
223 | <td><var>tb</var><big>[</big><var>, limit</var><big>]</big><var></var>)</td></tr></table></dt> | |
224 | <dd> | |
225 | A shorthand for <code>format_list(extract_tb(<var>tb</var>, <var>limit</var>))</code>. | |
226 | </dl> | |
227 | ||
228 | <P> | |
229 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
230 | <td><nobr><b><tt id='l2h-618' xml:id='l2h-618' class="function">format_stack</tt></b>(</nobr></td> | |
231 | <td><var></var><big>[</big><var>f</var><big>[</big><var>, limit</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
232 | <dd> | |
233 | A shorthand for <code>format_list(extract_stack(<var>f</var>, <var>limit</var>))</code>. | |
234 | </dl> | |
235 | ||
236 | <P> | |
237 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
238 | <td><nobr><b><tt id='l2h-619' xml:id='l2h-619' class="function">tb_lineno</tt></b>(</nobr></td> | |
239 | <td><var>tb</var>)</td></tr></table></dt> | |
240 | <dd> | |
241 | This function returns the current line number set in the traceback | |
242 | object. This function was necessary because in versions of Python | |
243 | prior to 2.3 when the <b class="programopt">-O</b> flag was passed to Python the | |
244 | <code><var>tb</var>.tb_lineno</code> was not updated correctly. This function | |
245 | has no use in versions past 2.3. | |
246 | </dl> | |
247 | ||
248 | <P> | |
249 | ||
250 | <p><br /></p><hr class='online-navigation' /> | |
251 | <div class='online-navigation'> | |
252 | <!--Table of Child-Links--> | |
253 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
254 | ||
255 | <UL CLASS="ChildLinks"> | |
256 | <LI><A href="traceback-example.html">3.12.1 Traceback Example</a> | |
257 | </ul> | |
258 | <!--End of Table of Child-Links--> | |
259 | </div> | |
260 | ||
261 | <DIV CLASS="navigation"> | |
262 | <div class='online-navigation'> | |
263 | <p></p><hr /> | |
264 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
265 | <tr> | |
266 | <td class='online-navigation'><a rel="prev" title="3.11.4 The interpreter stack" | |
267 | href="inspect-stack.html"><img src='../icons/previous.png' | |
268 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
269 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" | |
270 | href="python.html"><img src='../icons/up.png' | |
271 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
272 | <td class='online-navigation'><a rel="next" title="3.12.1 Traceback Example" | |
273 | href="traceback-example.html"><img src='../icons/next.png' | |
274 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
275 | <td align="center" width="100%">Python Library Reference</td> | |
276 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
277 | href="contents.html"><img src='../icons/contents.png' | |
278 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
279 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
280 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
281 | <td class='online-navigation'><a rel="index" title="Index" | |
282 | href="genindex.html"><img src='../icons/index.png' | |
283 | border='0' height='32' alt='Index' width='32' /></A></td> | |
284 | </tr></table> | |
285 | <div class='online-navigation'> | |
286 | <b class="navlabel">Previous:</b> | |
287 | <a class="sectref" rel="prev" href="inspect-stack.html">3.11.4 The interpreter stack</A> | |
288 | <b class="navlabel">Up:</b> | |
289 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> | |
290 | <b class="navlabel">Next:</b> | |
291 | <a class="sectref" rel="next" href="traceback-example.html">3.12.1 Traceback Example</A> | |
292 | </div> | |
293 | </div> | |
294 | <hr /> | |
295 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
296 | </DIV> | |
297 | <!--End of Navigation Panel--> | |
298 | <ADDRESS> | |
299 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
300 | </ADDRESS> | |
301 | </BODY> | |
302 | </HTML> |