Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`traceback` --- Print or retrieve a stack traceback |
| 2 | ======================================================== |
| 3 | |
| 4 | .. module:: traceback |
| 5 | :synopsis: Print or retrieve a stack traceback. |
| 6 | |
| 7 | |
| 8 | This module provides a standard interface to extract, format and print stack |
| 9 | traces of Python programs. It exactly mimics the behavior of the Python |
| 10 | interpreter when it prints a stack trace. This is useful when you want to print |
| 11 | stack traces under program control, such as in a "wrapper" around the |
| 12 | interpreter. |
| 13 | |
| 14 | .. index:: object: traceback |
| 15 | |
| 16 | The module uses traceback objects --- this is the object type that is stored in |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 17 | the :data:`sys.last_traceback` variable and returned as the third item from |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | :func:`sys.exc_info`. |
| 19 | |
| 20 | The module defines the following functions: |
| 21 | |
| 22 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 23 | .. function:: print_tb(traceback, limit=None, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | |
Serhiy Storchaka | 24559e4 | 2015-05-03 13:19:46 +0300 | [diff] [blame] | 25 | Print up to *limit* stack trace entries from *traceback* (starting from |
| 26 | the caller's frame) if *limit* is positive. Otherwise, print the last |
| 27 | ``abs(limit)`` entries. If *limit* is omitted or ``None``, all entries |
| 28 | are printed. If *file* is omitted or ``None``, the output goes to |
| 29 | ``sys.stderr``; otherwise it should be an open file or file-like object |
| 30 | to receive the output. |
| 31 | |
| 32 | .. versionchanged:: 3.5 |
| 33 | Added negative *limit* support. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | |
| 35 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 36 | .. function:: print_exception(type, value, traceback, limit=None, file=None, chain=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 37 | |
Serhiy Storchaka | 24559e4 | 2015-05-03 13:19:46 +0300 | [diff] [blame] | 38 | Print exception information and stack trace entries from |
Georg Brandl | 1aea30a | 2008-07-19 15:51:07 +0000 | [diff] [blame] | 39 | *traceback* to *file*. This differs from :func:`print_tb` in the following |
| 40 | ways: |
| 41 | |
| 42 | * if *traceback* is not ``None``, it prints a header ``Traceback (most recent |
| 43 | call last):`` |
| 44 | * it prints the exception *type* and *value* after the stack trace |
| 45 | * if *type* is :exc:`SyntaxError` and *value* has the appropriate format, it |
| 46 | prints the line where the syntax error occurred with a caret indicating the |
| 47 | approximate position of the error. |
| 48 | |
Serhiy Storchaka | 24559e4 | 2015-05-03 13:19:46 +0300 | [diff] [blame] | 49 | The optional *limit* argument has the same meaning as for :func:`print_tb`. |
Georg Brandl | 1aea30a | 2008-07-19 15:51:07 +0000 | [diff] [blame] | 50 | If *chain* is true (the default), then chained exceptions (the |
| 51 | :attr:`__cause__` or :attr:`__context__` attributes of the exception) will be |
| 52 | printed as well, like the interpreter itself does when printing an unhandled |
| 53 | exception. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 54 | |
| 55 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 56 | .. function:: print_exc(limit=None, file=None, chain=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 57 | |
Serhiy Storchaka | 24559e4 | 2015-05-03 13:19:46 +0300 | [diff] [blame] | 58 | This is a shorthand for ``print_exception(*sys.exc_info(), limit, file, |
| 59 | chain)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | |
| 61 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 62 | .. function:: print_last(limit=None, file=None, chain=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | |
| 64 | This is a shorthand for ``print_exception(sys.last_type, sys.last_value, |
Serhiy Storchaka | 24559e4 | 2015-05-03 13:19:46 +0300 | [diff] [blame] | 65 | sys.last_traceback, limit, file, chain)``. In general it will work only |
| 66 | after an exception has reached an interactive prompt (see |
| 67 | :data:`sys.last_type`). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 68 | |
| 69 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 70 | .. function:: print_stack(f=None, limit=None, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | |
Serhiy Storchaka | 24559e4 | 2015-05-03 13:19:46 +0300 | [diff] [blame] | 72 | Print up to *limit* stack trace entries (starting from the invocation |
| 73 | point) if *limit* is positive. Otherwise, print the last ``abs(limit)`` |
| 74 | entries. If *limit* is omitted or ``None``, all entries are printed. |
| 75 | The optional *f* argument can be used to specify an alternate stack frame |
| 76 | to start. The optional *file* argument has the same meaning as for |
| 77 | :func:`print_tb`. |
| 78 | |
| 79 | .. versionchanged:: 3.5 |
| 80 | Added negative *limit* support. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
| 82 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 83 | .. function:: extract_tb(traceback, limit=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 84 | |
Serhiy Storchaka | 24559e4 | 2015-05-03 13:19:46 +0300 | [diff] [blame] | 85 | Return a list of "pre-processed" stack trace entries extracted from the |
| 86 | traceback object *traceback*. It is useful for alternate formatting of |
| 87 | stack traces. The optional *limit* argument has the same meaning as for |
| 88 | :func:`print_tb`. A "pre-processed" stack trace entry is a 4-tuple |
| 89 | (*filename*, *line number*, *function name*, *text*) representing the |
| 90 | information that is usually printed for a stack trace. The *text* is a |
| 91 | string with leading and trailing whitespace stripped; if the source is |
| 92 | not available it is ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 93 | |
| 94 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 95 | .. function:: extract_stack(f=None, limit=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 | |
| 97 | Extract the raw traceback from the current stack frame. The return value has |
| 98 | the same format as for :func:`extract_tb`. The optional *f* and *limit* |
| 99 | arguments have the same meaning as for :func:`print_stack`. |
| 100 | |
| 101 | |
| 102 | .. function:: format_list(list) |
| 103 | |
| 104 | Given a list of tuples as returned by :func:`extract_tb` or |
| 105 | :func:`extract_stack`, return a list of strings ready for printing. Each string |
| 106 | in the resulting list corresponds to the item with the same index in the |
| 107 | argument list. Each string ends in a newline; the strings may contain internal |
| 108 | newlines as well, for those items whose source text line is not ``None``. |
| 109 | |
| 110 | |
| 111 | .. function:: format_exception_only(type, value) |
| 112 | |
| 113 | Format the exception part of a traceback. The arguments are the exception type |
| 114 | and value such as given by ``sys.last_type`` and ``sys.last_value``. The return |
| 115 | value is a list of strings, each ending in a newline. Normally, the list |
| 116 | contains a single string; however, for :exc:`SyntaxError` exceptions, it |
| 117 | contains several lines that (when printed) display detailed information about |
| 118 | where the syntax error occurred. The message indicating which exception |
| 119 | occurred is the always last string in the list. |
| 120 | |
| 121 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 122 | .. function:: format_exception(type, value, tb, limit=None, chain=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 123 | |
| 124 | Format a stack trace and the exception information. The arguments have the |
| 125 | same meaning as the corresponding arguments to :func:`print_exception`. The |
| 126 | return value is a list of strings, each ending in a newline and some containing |
| 127 | internal newlines. When these lines are concatenated and printed, exactly the |
| 128 | same text is printed as does :func:`print_exception`. |
| 129 | |
| 130 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 131 | .. function:: format_exc(limit=None, chain=True) |
Georg Brandl | 1aea30a | 2008-07-19 15:51:07 +0000 | [diff] [blame] | 132 | |
| 133 | This is like ``print_exc(limit)`` but returns a string instead of printing to a |
| 134 | file. |
| 135 | |
| 136 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 137 | .. function:: format_tb(tb, limit=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 138 | |
| 139 | A shorthand for ``format_list(extract_tb(tb, limit))``. |
| 140 | |
| 141 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 142 | .. function:: format_stack(f=None, limit=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 143 | |
| 144 | A shorthand for ``format_list(extract_stack(f, limit))``. |
| 145 | |
Andrew Kuchling | 173a157 | 2013-09-15 18:15:56 -0400 | [diff] [blame] | 146 | .. function:: clear_frames(tb) |
| 147 | |
| 148 | Clears the local variables of all the stack frames in a traceback *tb* |
| 149 | by calling the :meth:`clear` method of each frame object. |
| 150 | |
| 151 | .. versionadded:: 3.4 |
| 152 | |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 153 | .. function:: walk_stack(f) |
| 154 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 155 | Walk a stack following ``f.f_back`` from the given frame, yielding the frame |
| 156 | and line number for each frame. If *f* is ``None``, the current stack is |
| 157 | used. This helper is used with :meth:`StackSummary.extract`. |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 158 | |
| 159 | .. versionadded:: 3.5 |
| 160 | |
| 161 | .. function:: walk_tb(tb) |
| 162 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 163 | Walk a traceback following ``tb_next`` yielding the frame and line number |
| 164 | for each frame. This helper is used with :meth:`StackSummary.extract`. |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 165 | |
| 166 | .. versionadded:: 3.5 |
| 167 | |
| 168 | The module also defines the following classes: |
| 169 | |
| 170 | :class:`TracebackException` Objects |
| 171 | ----------------------------------- |
| 172 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 173 | .. versionadded:: 3.5 |
| 174 | |
| 175 | :class:`TracebackException` objects are created from actual exceptions to |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 176 | capture data for later printing in a lightweight fashion. |
| 177 | |
Robert Collins | d7c7e0e | 2015-03-05 20:28:52 +1300 | [diff] [blame] | 178 | .. class:: TracebackException(exc_type, exc_value, exc_traceback, *, limit=None, lookup_lines=True, capture_locals=False) |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 179 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 180 | Capture an exception for later rendering. *limit*, *lookup_lines* and |
| 181 | *capture_locals* are as for the :class:`StackSummary` class. |
Robert Collins | d7c7e0e | 2015-03-05 20:28:52 +1300 | [diff] [blame] | 182 | |
| 183 | Note that when locals are captured, they are also shown in the traceback. |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 184 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 185 | .. attribute:: __cause__ |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 186 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 187 | A :class:`TracebackException` of the original ``__cause__``. |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 188 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 189 | .. attribute:: __context__ |
Robert Collins | d7c7e0e | 2015-03-05 20:28:52 +1300 | [diff] [blame] | 190 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 191 | A :class:`TracebackException` of the original ``__context__``. |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 192 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 193 | .. attribute:: __suppress_context__ |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 194 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 195 | The ``__suppress_context__`` value from the original exception. |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 196 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 197 | .. attribute:: stack |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 198 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 199 | A :class:`StackSummary` representing the traceback. |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 200 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 201 | .. attribute:: exc_type |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 202 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 203 | The class of the original traceback. |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 204 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 205 | .. attribute:: filename |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 206 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 207 | For syntax errors - the file name where the error occurred. |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 208 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 209 | .. attribute:: lineno |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 210 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 211 | For syntax errors - the line number where the error occurred. |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 212 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 213 | .. attribute:: text |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 214 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 215 | For syntax errors - the text where the error occurred. |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 216 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 217 | .. attribute:: offset |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 218 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 219 | For syntax errors - the offset into the text where the error occurred. |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 220 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 221 | .. attribute:: msg |
| 222 | |
| 223 | For syntax errors - the compiler error message. |
| 224 | |
| 225 | .. classmethod:: from_exception(exc, *, limit=None, lookup_lines=True, capture_locals=False) |
| 226 | |
| 227 | Capture an exception for later rendering. *limit*, *lookup_lines* and |
| 228 | *capture_locals* are as for the :class:`StackSummary` class. |
| 229 | |
| 230 | Note that when locals are captured, they are also shown in the traceback. |
| 231 | |
| 232 | .. method:: format(*, chain=True) |
| 233 | |
| 234 | Format the exception. |
| 235 | |
| 236 | If *chain* is not ``True``, ``__cause__`` and ``__context__`` will not |
| 237 | be formatted. |
| 238 | |
| 239 | The return value is a generator of strings, each ending in a newline and |
| 240 | some containing internal newlines. :func:`~traceback.print_exception` |
| 241 | is a wrapper around this method which just prints the lines to a file. |
| 242 | |
| 243 | The message indicating which exception occurred is always the last |
| 244 | string in the output. |
| 245 | |
| 246 | .. method:: format_exception_only() |
| 247 | |
| 248 | Format the exception part of the traceback. |
| 249 | |
| 250 | The return value is a generator of strings, each ending in a newline. |
| 251 | |
| 252 | Normally, the generator emits a single string; however, for |
| 253 | :exc:`SyntaxError` exceptions, it emits several lines that (when |
| 254 | printed) display detailed information about where the syntax |
| 255 | error occurred. |
| 256 | |
| 257 | The message indicating which exception occurred is always the last |
| 258 | string in the output. |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 259 | |
| 260 | |
| 261 | :class:`StackSummary` Objects |
| 262 | ----------------------------- |
| 263 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 264 | .. versionadded:: 3.5 |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 265 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 266 | :class:`StackSummary` objects represent a call stack ready for formatting. |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 267 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 268 | .. class:: StackSummary |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 269 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 270 | .. classmethod:: extract(frame_gen, *, limit=None, lookup_lines=True, capture_locals=False) |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 271 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 272 | Construct a :class:`StackSummary` object from a frame generator (such as |
| 273 | is returned by :func:`~traceback.walk_stack` or |
| 274 | :func:`~traceback.walk_tb`). |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 275 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 276 | If *limit* is supplied, only this many frames are taken from *frame_gen*. |
| 277 | If *lookup_lines* is ``False``, the returned :class:`FrameSummary` |
| 278 | objects will not have read their lines in yet, making the cost of |
| 279 | creating the :class:`StackSummary` cheaper (which may be valuable if it |
| 280 | may not actually get formatted). If *capture_locals* is ``True`` the |
| 281 | local variables in each :class:`FrameSummary` are captured as object |
| 282 | representations. |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 283 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 284 | .. classmethod:: from_list(a_list) |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 285 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 286 | Construct a :class:`StackSummary` object from a supplied old-style list |
| 287 | of tuples. Each tuple should be a 4-tuple with filename, lineno, name, |
| 288 | line as the elements. |
| 289 | |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 290 | |
| 291 | :class:`FrameSummary` Objects |
| 292 | ----------------------------- |
| 293 | |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 294 | .. versionadded:: 3.5 |
| 295 | |
| 296 | :class:`FrameSummary` objects represent a single frame in a traceback. |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 297 | |
| 298 | .. class:: FrameSummary(filename, lineno, name, lookup_line=True, locals=None, line=None) |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 299 | |
| 300 | Represent a single frame in the traceback or stack that is being formatted |
| 301 | or printed. It may optionally have a stringified version of the frames |
Berker Peksag | 49f373b | 2015-03-06 12:18:06 +0200 | [diff] [blame] | 302 | locals included in it. If *lookup_line* is ``False``, the source code is not |
| 303 | looked up until the :class:`FrameSummary` has the :attr:`~FrameSummary.line` |
| 304 | attribute accessed (which also happens when casting it to a tuple). |
| 305 | :attr:`~FrameSummary.line` may be directly provided, and will prevent line |
| 306 | lookups happening at all. *locals* is an optional local variable |
| 307 | dictionary, and if supplied the variable representations are stored in the |
| 308 | summary for later display. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 309 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 310 | .. _traceback-example: |
| 311 | |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 312 | Traceback Examples |
| 313 | ------------------ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 314 | |
| 315 | This simple example implements a basic read-eval-print loop, similar to (but |
| 316 | less useful than) the standard Python interactive interpreter loop. For a more |
| 317 | complete implementation of the interpreter loop, refer to the :mod:`code` |
| 318 | module. :: |
| 319 | |
| 320 | import sys, traceback |
| 321 | |
| 322 | def run_user_code(envdir): |
Georg Brandl | 8d5c392 | 2007-12-02 22:48:17 +0000 | [diff] [blame] | 323 | source = input(">>> ") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 324 | try: |
| 325 | exec(source, envdir) |
Andrew Svetlov | 4739561 | 2012-11-02 22:07:26 +0200 | [diff] [blame] | 326 | except Exception: |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 327 | print("Exception in user code:") |
| 328 | print("-"*60) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 329 | traceback.print_exc(file=sys.stdout) |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 330 | print("-"*60) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 331 | |
| 332 | envdir = {} |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 333 | while True: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 334 | run_user_code(envdir) |
| 335 | |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 336 | |
| 337 | The following example demonstrates the different ways to print and format the |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 338 | exception and traceback: |
| 339 | |
| 340 | .. testcode:: |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 341 | |
| 342 | import sys, traceback |
| 343 | |
| 344 | def lumberjack(): |
| 345 | bright_side_of_death() |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 346 | |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 347 | def bright_side_of_death(): |
| 348 | return tuple()[0] |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 349 | |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 350 | try: |
| 351 | lumberjack() |
Ezio Melotti | 5e0110e | 2010-03-13 01:28:34 +0000 | [diff] [blame] | 352 | except IndexError: |
| 353 | exc_type, exc_value, exc_traceback = sys.exc_info() |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 354 | print("*** print_tb:") |
Ezio Melotti | 5e0110e | 2010-03-13 01:28:34 +0000 | [diff] [blame] | 355 | traceback.print_tb(exc_traceback, limit=1, file=sys.stdout) |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 356 | print("*** print_exception:") |
Ezio Melotti | 5e0110e | 2010-03-13 01:28:34 +0000 | [diff] [blame] | 357 | traceback.print_exception(exc_type, exc_value, exc_traceback, |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 358 | limit=2, file=sys.stdout) |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 359 | print("*** print_exc:") |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 360 | traceback.print_exc() |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 361 | print("*** format_exc, first and last line:") |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 362 | formatted_lines = traceback.format_exc().splitlines() |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 363 | print(formatted_lines[0]) |
| 364 | print(formatted_lines[-1]) |
| 365 | print("*** format_exception:") |
Ezio Melotti | 5e0110e | 2010-03-13 01:28:34 +0000 | [diff] [blame] | 366 | print(repr(traceback.format_exception(exc_type, exc_value, |
| 367 | exc_traceback))) |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 368 | print("*** extract_tb:") |
Ezio Melotti | 5e0110e | 2010-03-13 01:28:34 +0000 | [diff] [blame] | 369 | print(repr(traceback.extract_tb(exc_traceback))) |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 370 | print("*** format_tb:") |
Ezio Melotti | 5e0110e | 2010-03-13 01:28:34 +0000 | [diff] [blame] | 371 | print(repr(traceback.format_tb(exc_traceback))) |
| 372 | print("*** tb_lineno:", exc_traceback.tb_lineno) |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 373 | |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 374 | The output for the example would look similar to this: |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 375 | |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 376 | .. testoutput:: |
| 377 | :options: +NORMALIZE_WHITESPACE |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 378 | |
| 379 | *** print_tb: |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 380 | File "<doctest...>", line 10, in <module> |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 381 | lumberjack() |
| 382 | *** print_exception: |
| 383 | Traceback (most recent call last): |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 384 | File "<doctest...>", line 10, in <module> |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 385 | lumberjack() |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 386 | File "<doctest...>", line 4, in lumberjack |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 387 | bright_side_of_death() |
| 388 | IndexError: tuple index out of range |
| 389 | *** print_exc: |
| 390 | Traceback (most recent call last): |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 391 | File "<doctest...>", line 10, in <module> |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 392 | lumberjack() |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 393 | File "<doctest...>", line 4, in lumberjack |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 394 | bright_side_of_death() |
| 395 | IndexError: tuple index out of range |
| 396 | *** format_exc, first and last line: |
| 397 | Traceback (most recent call last): |
| 398 | IndexError: tuple index out of range |
| 399 | *** format_exception: |
| 400 | ['Traceback (most recent call last):\n', |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 401 | ' File "<doctest...>", line 10, in <module>\n lumberjack()\n', |
| 402 | ' File "<doctest...>", line 4, in lumberjack\n bright_side_of_death()\n', |
| 403 | ' File "<doctest...>", line 7, in bright_side_of_death\n return tuple()[0]\n', |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 404 | 'IndexError: tuple index out of range\n'] |
| 405 | *** extract_tb: |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 406 | [('<doctest...>', 10, '<module>', 'lumberjack()'), |
| 407 | ('<doctest...>', 4, 'lumberjack', 'bright_side_of_death()'), |
Benjamin Peterson | 25c95f1 | 2009-05-08 20:42:26 +0000 | [diff] [blame] | 408 | ('<doctest...>', 7, 'bright_side_of_death', 'return tuple()[0]')] |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 409 | *** format_tb: |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 410 | [' File "<doctest...>", line 10, in <module>\n lumberjack()\n', |
| 411 | ' File "<doctest...>", line 4, in lumberjack\n bright_side_of_death()\n', |
| 412 | ' File "<doctest...>", line 7, in bright_side_of_death\n return tuple()[0]\n'] |
| 413 | *** tb_lineno: 10 |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 414 | |
| 415 | |
| 416 | The following example shows the different ways to print and format the stack:: |
| 417 | |
| 418 | >>> import traceback |
| 419 | >>> def another_function(): |
| 420 | ... lumberstack() |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 421 | ... |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 422 | >>> def lumberstack(): |
| 423 | ... traceback.print_stack() |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 424 | ... print(repr(traceback.extract_stack())) |
| 425 | ... print(repr(traceback.format_stack())) |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 426 | ... |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 427 | >>> another_function() |
| 428 | File "<doctest>", line 10, in <module> |
| 429 | another_function() |
| 430 | File "<doctest>", line 3, in another_function |
| 431 | lumberstack() |
| 432 | File "<doctest>", line 6, in lumberstack |
| 433 | traceback.print_stack() |
| 434 | [('<doctest>', 10, '<module>', 'another_function()'), |
| 435 | ('<doctest>', 3, 'another_function', 'lumberstack()'), |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 436 | ('<doctest>', 7, 'lumberstack', 'print(repr(traceback.extract_stack()))')] |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 437 | [' File "<doctest>", line 10, in <module>\n another_function()\n', |
| 438 | ' File "<doctest>", line 3, in another_function\n lumberstack()\n', |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 439 | ' File "<doctest>", line 8, in lumberstack\n print(repr(traceback.format_stack()))\n'] |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 440 | |
| 441 | |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 442 | This last example demonstrates the final few formatting functions: |
| 443 | |
| 444 | .. doctest:: |
| 445 | :options: +NORMALIZE_WHITESPACE |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 446 | |
| 447 | >>> import traceback |
Georg Brandl | 0142d4a | 2009-04-27 16:22:44 +0000 | [diff] [blame] | 448 | >>> traceback.format_list([('spam.py', 3, '<module>', 'spam.eggs()'), |
| 449 | ... ('eggs.py', 42, 'eggs', 'return "bacon"')]) |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 450 | [' File "spam.py", line 3, in <module>\n spam.eggs()\n', |
| 451 | ' File "eggs.py", line 42, in eggs\n return "bacon"\n'] |
Georg Brandl | 0142d4a | 2009-04-27 16:22:44 +0000 | [diff] [blame] | 452 | >>> an_error = IndexError('tuple index out of range') |
| 453 | >>> traceback.format_exception_only(type(an_error), an_error) |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 454 | ['IndexError: tuple index out of range\n'] |