blob: c6b5d0c9568dd9d809d8205938df2cc76cb812b4 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`traceback` --- Print or retrieve a stack traceback
2========================================================
3
4.. module:: traceback
5 :synopsis: Print or retrieve a stack traceback.
6
7
8This module provides a standard interface to extract, format and print stack
9traces of Python programs. It exactly mimics the behavior of the Python
10interpreter when it prints a stack trace. This is useful when you want to print
11stack traces under program control, such as in a "wrapper" around the
12interpreter.
13
14.. index:: object: traceback
15
16The module uses traceback objects --- this is the object type that is stored in
R. David Murraye02a3012009-04-27 18:38:19 +000017the :data:`sys.last_traceback` variable and returned as the third item from
Georg Brandl116aa622007-08-15 14:28:22 +000018:func:`sys.exc_info`.
19
20The module defines the following functions:
21
22
Georg Brandl7f01a132009-09-16 15:58:14 +000023.. function:: print_tb(traceback, limit=None, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +000024
25 Print up to *limit* stack trace entries from *traceback*. If *limit* is omitted
26 or ``None``, all entries are printed. If *file* is omitted or ``None``, the
27 output goes to ``sys.stderr``; otherwise it should be an open file or file-like
28 object to receive the output.
29
30
Georg Brandl7f01a132009-09-16 15:58:14 +000031.. function:: print_exception(type, value, traceback, limit=None, file=None, chain=True)
Georg Brandl116aa622007-08-15 14:28:22 +000032
33 Print exception information and up to *limit* stack trace entries from
Georg Brandl1aea30a2008-07-19 15:51:07 +000034 *traceback* to *file*. This differs from :func:`print_tb` in the following
35 ways:
36
37 * if *traceback* is not ``None``, it prints a header ``Traceback (most recent
38 call last):``
39 * it prints the exception *type* and *value* after the stack trace
40 * if *type* is :exc:`SyntaxError` and *value* has the appropriate format, it
41 prints the line where the syntax error occurred with a caret indicating the
42 approximate position of the error.
43
44 If *chain* is true (the default), then chained exceptions (the
45 :attr:`__cause__` or :attr:`__context__` attributes of the exception) will be
46 printed as well, like the interpreter itself does when printing an unhandled
47 exception.
Georg Brandl116aa622007-08-15 14:28:22 +000048
49
Georg Brandl7f01a132009-09-16 15:58:14 +000050.. function:: print_exc(limit=None, file=None, chain=True)
Georg Brandl116aa622007-08-15 14:28:22 +000051
Georg Brandlf6945182008-02-01 11:56:49 +000052 This is a shorthand for ``print_exception(*sys.exc_info())``.
Georg Brandl116aa622007-08-15 14:28:22 +000053
54
Georg Brandl7f01a132009-09-16 15:58:14 +000055.. function:: print_last(limit=None, file=None, chain=True)
Georg Brandl116aa622007-08-15 14:28:22 +000056
57 This is a shorthand for ``print_exception(sys.last_type, sys.last_value,
R. David Murraye02a3012009-04-27 18:38:19 +000058 sys.last_traceback, limit, file)``. In general it will work only after
59 an exception has reached an interactive prompt (see :data:`sys.last_type`).
Georg Brandl116aa622007-08-15 14:28:22 +000060
61
Georg Brandl7f01a132009-09-16 15:58:14 +000062.. function:: print_stack(f=None, limit=None, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +000063
64 This function prints a stack trace from its invocation point. The optional *f*
65 argument can be used to specify an alternate stack frame to start. The optional
66 *limit* and *file* arguments have the same meaning as for
67 :func:`print_exception`.
68
69
Georg Brandl7f01a132009-09-16 15:58:14 +000070.. function:: extract_tb(traceback, limit=None)
Georg Brandl116aa622007-08-15 14:28:22 +000071
72 Return a list of up to *limit* "pre-processed" stack trace entries extracted
73 from the traceback object *traceback*. It is useful for alternate formatting of
74 stack traces. If *limit* is omitted or ``None``, all entries are extracted. A
Raymond Hettingerda4bf8f2014-04-01 22:17:33 -070075 "pre-processed" stack trace entry is a 4-tuple (*filename*, *line number*,
Georg Brandl116aa622007-08-15 14:28:22 +000076 *function name*, *text*) representing the information that is usually printed
77 for a stack trace. The *text* is a string with leading and trailing whitespace
78 stripped; if the source is not available it is ``None``.
79
80
Georg Brandl7f01a132009-09-16 15:58:14 +000081.. function:: extract_stack(f=None, limit=None)
Georg Brandl116aa622007-08-15 14:28:22 +000082
83 Extract the raw traceback from the current stack frame. The return value has
84 the same format as for :func:`extract_tb`. The optional *f* and *limit*
85 arguments have the same meaning as for :func:`print_stack`.
86
87
88.. function:: format_list(list)
89
90 Given a list of tuples as returned by :func:`extract_tb` or
91 :func:`extract_stack`, return a list of strings ready for printing. Each string
92 in the resulting list corresponds to the item with the same index in the
93 argument list. Each string ends in a newline; the strings may contain internal
94 newlines as well, for those items whose source text line is not ``None``.
95
96
97.. function:: format_exception_only(type, value)
98
99 Format the exception part of a traceback. The arguments are the exception type
100 and value such as given by ``sys.last_type`` and ``sys.last_value``. The return
101 value is a list of strings, each ending in a newline. Normally, the list
102 contains a single string; however, for :exc:`SyntaxError` exceptions, it
103 contains several lines that (when printed) display detailed information about
104 where the syntax error occurred. The message indicating which exception
105 occurred is the always last string in the list.
106
107
Georg Brandl7f01a132009-09-16 15:58:14 +0000108.. function:: format_exception(type, value, tb, limit=None, chain=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000109
110 Format a stack trace and the exception information. The arguments have the
111 same meaning as the corresponding arguments to :func:`print_exception`. The
112 return value is a list of strings, each ending in a newline and some containing
113 internal newlines. When these lines are concatenated and printed, exactly the
114 same text is printed as does :func:`print_exception`.
115
116
Georg Brandl7f01a132009-09-16 15:58:14 +0000117.. function:: format_exc(limit=None, chain=True)
Georg Brandl1aea30a2008-07-19 15:51:07 +0000118
119 This is like ``print_exc(limit)`` but returns a string instead of printing to a
120 file.
121
122
Georg Brandl7f01a132009-09-16 15:58:14 +0000123.. function:: format_tb(tb, limit=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125 A shorthand for ``format_list(extract_tb(tb, limit))``.
126
127
Georg Brandl7f01a132009-09-16 15:58:14 +0000128.. function:: format_stack(f=None, limit=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000129
130 A shorthand for ``format_list(extract_stack(f, limit))``.
131
Andrew Kuchling173a1572013-09-15 18:15:56 -0400132.. function:: clear_frames(tb)
133
134 Clears the local variables of all the stack frames in a traceback *tb*
135 by calling the :meth:`clear` method of each frame object.
136
137 .. versionadded:: 3.4
138
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300139.. function:: walk_stack(f)
140
Berker Peksag49f373b2015-03-06 12:18:06 +0200141 Walk a stack following ``f.f_back`` from the given frame, yielding the frame
142 and line number for each frame. If *f* is ``None``, the current stack is
143 used. This helper is used with :meth:`StackSummary.extract`.
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300144
145 .. versionadded:: 3.5
146
147.. function:: walk_tb(tb)
148
Berker Peksag49f373b2015-03-06 12:18:06 +0200149 Walk a traceback following ``tb_next`` yielding the frame and line number
150 for each frame. This helper is used with :meth:`StackSummary.extract`.
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300151
152 .. versionadded:: 3.5
153
154The module also defines the following classes:
155
156:class:`TracebackException` Objects
157-----------------------------------
158
Berker Peksag49f373b2015-03-06 12:18:06 +0200159.. versionadded:: 3.5
160
161:class:`TracebackException` objects are created from actual exceptions to
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300162capture data for later printing in a lightweight fashion.
163
Robert Collinsd7c7e0e2015-03-05 20:28:52 +1300164.. class:: TracebackException(exc_type, exc_value, exc_traceback, *, limit=None, lookup_lines=True, capture_locals=False)
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300165
Berker Peksag49f373b2015-03-06 12:18:06 +0200166 Capture an exception for later rendering. *limit*, *lookup_lines* and
167 *capture_locals* are as for the :class:`StackSummary` class.
Robert Collinsd7c7e0e2015-03-05 20:28:52 +1300168
169 Note that when locals are captured, they are also shown in the traceback.
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300170
Berker Peksag49f373b2015-03-06 12:18:06 +0200171 .. attribute:: __cause__
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300172
Berker Peksag49f373b2015-03-06 12:18:06 +0200173 A :class:`TracebackException` of the original ``__cause__``.
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300174
Berker Peksag49f373b2015-03-06 12:18:06 +0200175 .. attribute:: __context__
Robert Collinsd7c7e0e2015-03-05 20:28:52 +1300176
Berker Peksag49f373b2015-03-06 12:18:06 +0200177 A :class:`TracebackException` of the original ``__context__``.
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300178
Berker Peksag49f373b2015-03-06 12:18:06 +0200179 .. attribute:: __suppress_context__
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300180
Berker Peksag49f373b2015-03-06 12:18:06 +0200181 The ``__suppress_context__`` value from the original exception.
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300182
Berker Peksag49f373b2015-03-06 12:18:06 +0200183 .. attribute:: stack
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300184
Berker Peksag49f373b2015-03-06 12:18:06 +0200185 A :class:`StackSummary` representing the traceback.
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300186
Berker Peksag49f373b2015-03-06 12:18:06 +0200187 .. attribute:: exc_type
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300188
Berker Peksag49f373b2015-03-06 12:18:06 +0200189 The class of the original traceback.
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300190
Berker Peksag49f373b2015-03-06 12:18:06 +0200191 .. attribute:: filename
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300192
Berker Peksag49f373b2015-03-06 12:18:06 +0200193 For syntax errors - the file name where the error occurred.
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300194
Berker Peksag49f373b2015-03-06 12:18:06 +0200195 .. attribute:: lineno
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300196
Berker Peksag49f373b2015-03-06 12:18:06 +0200197 For syntax errors - the line number where the error occurred.
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300198
Berker Peksag49f373b2015-03-06 12:18:06 +0200199 .. attribute:: text
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300200
Berker Peksag49f373b2015-03-06 12:18:06 +0200201 For syntax errors - the text where the error occurred.
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300202
Berker Peksag49f373b2015-03-06 12:18:06 +0200203 .. attribute:: offset
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300204
Berker Peksag49f373b2015-03-06 12:18:06 +0200205 For syntax errors - the offset into the text where the error occurred.
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300206
Berker Peksag49f373b2015-03-06 12:18:06 +0200207 .. attribute:: msg
208
209 For syntax errors - the compiler error message.
210
211 .. classmethod:: from_exception(exc, *, limit=None, lookup_lines=True, capture_locals=False)
212
213 Capture an exception for later rendering. *limit*, *lookup_lines* and
214 *capture_locals* are as for the :class:`StackSummary` class.
215
216 Note that when locals are captured, they are also shown in the traceback.
217
218 .. method:: format(*, chain=True)
219
220 Format the exception.
221
222 If *chain* is not ``True``, ``__cause__`` and ``__context__`` will not
223 be formatted.
224
225 The return value is a generator of strings, each ending in a newline and
226 some containing internal newlines. :func:`~traceback.print_exception`
227 is a wrapper around this method which just prints the lines to a file.
228
229 The message indicating which exception occurred is always the last
230 string in the output.
231
232 .. method:: format_exception_only()
233
234 Format the exception part of the traceback.
235
236 The return value is a generator of strings, each ending in a newline.
237
238 Normally, the generator emits a single string; however, for
239 :exc:`SyntaxError` exceptions, it emits several lines that (when
240 printed) display detailed information about where the syntax
241 error occurred.
242
243 The message indicating which exception occurred is always the last
244 string in the output.
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300245
246
247:class:`StackSummary` Objects
248-----------------------------
249
Berker Peksag49f373b2015-03-06 12:18:06 +0200250.. versionadded:: 3.5
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300251
Berker Peksag49f373b2015-03-06 12:18:06 +0200252:class:`StackSummary` objects represent a call stack ready for formatting.
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300253
Berker Peksag49f373b2015-03-06 12:18:06 +0200254.. class:: StackSummary
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300255
Berker Peksag49f373b2015-03-06 12:18:06 +0200256 .. classmethod:: extract(frame_gen, *, limit=None, lookup_lines=True, capture_locals=False)
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300257
Berker Peksag49f373b2015-03-06 12:18:06 +0200258 Construct a :class:`StackSummary` object from a frame generator (such as
259 is returned by :func:`~traceback.walk_stack` or
260 :func:`~traceback.walk_tb`).
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300261
Berker Peksag49f373b2015-03-06 12:18:06 +0200262 If *limit* is supplied, only this many frames are taken from *frame_gen*.
263 If *lookup_lines* is ``False``, the returned :class:`FrameSummary`
264 objects will not have read their lines in yet, making the cost of
265 creating the :class:`StackSummary` cheaper (which may be valuable if it
266 may not actually get formatted). If *capture_locals* is ``True`` the
267 local variables in each :class:`FrameSummary` are captured as object
268 representations.
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300269
Berker Peksag49f373b2015-03-06 12:18:06 +0200270 .. classmethod:: from_list(a_list)
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300271
Berker Peksag49f373b2015-03-06 12:18:06 +0200272 Construct a :class:`StackSummary` object from a supplied old-style list
273 of tuples. Each tuple should be a 4-tuple with filename, lineno, name,
274 line as the elements.
275
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300276
277:class:`FrameSummary` Objects
278-----------------------------
279
Berker Peksag49f373b2015-03-06 12:18:06 +0200280.. versionadded:: 3.5
281
282:class:`FrameSummary` objects represent a single frame in a traceback.
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300283
284.. class:: FrameSummary(filename, lineno, name, lookup_line=True, locals=None, line=None)
Robert Collins6bc2c1e2015-03-05 12:07:57 +1300285
286 Represent a single frame in the traceback or stack that is being formatted
287 or printed. It may optionally have a stringified version of the frames
Berker Peksag49f373b2015-03-06 12:18:06 +0200288 locals included in it. If *lookup_line* is ``False``, the source code is not
289 looked up until the :class:`FrameSummary` has the :attr:`~FrameSummary.line`
290 attribute accessed (which also happens when casting it to a tuple).
291 :attr:`~FrameSummary.line` may be directly provided, and will prevent line
292 lookups happening at all. *locals* is an optional local variable
293 dictionary, and if supplied the variable representations are stored in the
294 summary for later display.
Georg Brandl116aa622007-08-15 14:28:22 +0000295
Georg Brandl116aa622007-08-15 14:28:22 +0000296.. _traceback-example:
297
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000298Traceback Examples
299------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000300
301This simple example implements a basic read-eval-print loop, similar to (but
302less useful than) the standard Python interactive interpreter loop. For a more
303complete implementation of the interpreter loop, refer to the :mod:`code`
304module. ::
305
306 import sys, traceback
307
308 def run_user_code(envdir):
Georg Brandl8d5c3922007-12-02 22:48:17 +0000309 source = input(">>> ")
Georg Brandl116aa622007-08-15 14:28:22 +0000310 try:
311 exec(source, envdir)
Andrew Svetlov47395612012-11-02 22:07:26 +0200312 except Exception:
Collin Winterc79461b2007-09-01 23:34:30 +0000313 print("Exception in user code:")
314 print("-"*60)
Georg Brandl116aa622007-08-15 14:28:22 +0000315 traceback.print_exc(file=sys.stdout)
Collin Winterc79461b2007-09-01 23:34:30 +0000316 print("-"*60)
Georg Brandl116aa622007-08-15 14:28:22 +0000317
318 envdir = {}
Collin Winterc79461b2007-09-01 23:34:30 +0000319 while True:
Georg Brandl116aa622007-08-15 14:28:22 +0000320 run_user_code(envdir)
321
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000322
323The following example demonstrates the different ways to print and format the
R. David Murraye02a3012009-04-27 18:38:19 +0000324exception and traceback:
325
326.. testcode::
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000327
328 import sys, traceback
329
330 def lumberjack():
331 bright_side_of_death()
Georg Brandl48310cd2009-01-03 21:18:54 +0000332
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000333 def bright_side_of_death():
334 return tuple()[0]
Georg Brandl48310cd2009-01-03 21:18:54 +0000335
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000336 try:
337 lumberjack()
Ezio Melotti5e0110e2010-03-13 01:28:34 +0000338 except IndexError:
339 exc_type, exc_value, exc_traceback = sys.exc_info()
Georg Brandlf6945182008-02-01 11:56:49 +0000340 print("*** print_tb:")
Ezio Melotti5e0110e2010-03-13 01:28:34 +0000341 traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
Georg Brandlf6945182008-02-01 11:56:49 +0000342 print("*** print_exception:")
Ezio Melotti5e0110e2010-03-13 01:28:34 +0000343 traceback.print_exception(exc_type, exc_value, exc_traceback,
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000344 limit=2, file=sys.stdout)
Georg Brandlf6945182008-02-01 11:56:49 +0000345 print("*** print_exc:")
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000346 traceback.print_exc()
Georg Brandlf6945182008-02-01 11:56:49 +0000347 print("*** format_exc, first and last line:")
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000348 formatted_lines = traceback.format_exc().splitlines()
Georg Brandlf6945182008-02-01 11:56:49 +0000349 print(formatted_lines[0])
350 print(formatted_lines[-1])
351 print("*** format_exception:")
Ezio Melotti5e0110e2010-03-13 01:28:34 +0000352 print(repr(traceback.format_exception(exc_type, exc_value,
353 exc_traceback)))
Georg Brandlf6945182008-02-01 11:56:49 +0000354 print("*** extract_tb:")
Ezio Melotti5e0110e2010-03-13 01:28:34 +0000355 print(repr(traceback.extract_tb(exc_traceback)))
Georg Brandlf6945182008-02-01 11:56:49 +0000356 print("*** format_tb:")
Ezio Melotti5e0110e2010-03-13 01:28:34 +0000357 print(repr(traceback.format_tb(exc_traceback)))
358 print("*** tb_lineno:", exc_traceback.tb_lineno)
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000359
R. David Murraye02a3012009-04-27 18:38:19 +0000360The output for the example would look similar to this:
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000361
R. David Murraye02a3012009-04-27 18:38:19 +0000362.. testoutput::
363 :options: +NORMALIZE_WHITESPACE
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000364
365 *** print_tb:
R. David Murraye02a3012009-04-27 18:38:19 +0000366 File "<doctest...>", line 10, in <module>
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000367 lumberjack()
368 *** print_exception:
369 Traceback (most recent call last):
R. David Murraye02a3012009-04-27 18:38:19 +0000370 File "<doctest...>", line 10, in <module>
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000371 lumberjack()
R. David Murraye02a3012009-04-27 18:38:19 +0000372 File "<doctest...>", line 4, in lumberjack
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000373 bright_side_of_death()
374 IndexError: tuple index out of range
375 *** print_exc:
376 Traceback (most recent call last):
R. David Murraye02a3012009-04-27 18:38:19 +0000377 File "<doctest...>", line 10, in <module>
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000378 lumberjack()
R. David Murraye02a3012009-04-27 18:38:19 +0000379 File "<doctest...>", line 4, in lumberjack
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000380 bright_side_of_death()
381 IndexError: tuple index out of range
382 *** format_exc, first and last line:
383 Traceback (most recent call last):
384 IndexError: tuple index out of range
385 *** format_exception:
386 ['Traceback (most recent call last):\n',
R. David Murraye02a3012009-04-27 18:38:19 +0000387 ' File "<doctest...>", line 10, in <module>\n lumberjack()\n',
388 ' File "<doctest...>", line 4, in lumberjack\n bright_side_of_death()\n',
389 ' File "<doctest...>", line 7, in bright_side_of_death\n return tuple()[0]\n',
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000390 'IndexError: tuple index out of range\n']
391 *** extract_tb:
R. David Murraye02a3012009-04-27 18:38:19 +0000392 [('<doctest...>', 10, '<module>', 'lumberjack()'),
393 ('<doctest...>', 4, 'lumberjack', 'bright_side_of_death()'),
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000394 ('<doctest...>', 7, 'bright_side_of_death', 'return tuple()[0]')]
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000395 *** format_tb:
R. David Murraye02a3012009-04-27 18:38:19 +0000396 [' File "<doctest...>", line 10, in <module>\n lumberjack()\n',
397 ' File "<doctest...>", line 4, in lumberjack\n bright_side_of_death()\n',
398 ' File "<doctest...>", line 7, in bright_side_of_death\n return tuple()[0]\n']
399 *** tb_lineno: 10
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000400
401
402The following example shows the different ways to print and format the stack::
403
404 >>> import traceback
405 >>> def another_function():
406 ... lumberstack()
Georg Brandl48310cd2009-01-03 21:18:54 +0000407 ...
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000408 >>> def lumberstack():
409 ... traceback.print_stack()
Georg Brandlf6945182008-02-01 11:56:49 +0000410 ... print(repr(traceback.extract_stack()))
411 ... print(repr(traceback.format_stack()))
Georg Brandl48310cd2009-01-03 21:18:54 +0000412 ...
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000413 >>> another_function()
414 File "<doctest>", line 10, in <module>
415 another_function()
416 File "<doctest>", line 3, in another_function
417 lumberstack()
418 File "<doctest>", line 6, in lumberstack
419 traceback.print_stack()
420 [('<doctest>', 10, '<module>', 'another_function()'),
421 ('<doctest>', 3, 'another_function', 'lumberstack()'),
Georg Brandlf6945182008-02-01 11:56:49 +0000422 ('<doctest>', 7, 'lumberstack', 'print(repr(traceback.extract_stack()))')]
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000423 [' File "<doctest>", line 10, in <module>\n another_function()\n',
424 ' File "<doctest>", line 3, in another_function\n lumberstack()\n',
Georg Brandlf6945182008-02-01 11:56:49 +0000425 ' File "<doctest>", line 8, in lumberstack\n print(repr(traceback.format_stack()))\n']
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000426
427
R. David Murraye02a3012009-04-27 18:38:19 +0000428This last example demonstrates the final few formatting functions:
429
430.. doctest::
431 :options: +NORMALIZE_WHITESPACE
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000432
433 >>> import traceback
Georg Brandl0142d4a2009-04-27 16:22:44 +0000434 >>> traceback.format_list([('spam.py', 3, '<module>', 'spam.eggs()'),
435 ... ('eggs.py', 42, 'eggs', 'return "bacon"')])
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000436 [' File "spam.py", line 3, in <module>\n spam.eggs()\n',
437 ' File "eggs.py", line 42, in eggs\n return "bacon"\n']
Georg Brandl0142d4a2009-04-27 16:22:44 +0000438 >>> an_error = IndexError('tuple index out of range')
439 >>> traceback.format_exception_only(type(an_error), an_error)
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000440 ['IndexError: tuple index out of range\n']