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 | |
| 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 Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 31 | .. function:: print_exception(type, value, traceback, limit=None, file=None, chain=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
| 33 | Print exception information and up to *limit* stack trace entries from |
Georg Brandl | 1aea30a | 2008-07-19 15:51:07 +0000 | [diff] [blame] | 34 | *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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 48 | |
| 49 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 50 | .. function:: print_exc(limit=None, file=None, chain=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 52 | This is a shorthand for ``print_exception(*sys.exc_info())``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | |
| 54 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 55 | .. function:: print_last(limit=None, file=None, chain=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 56 | |
| 57 | This is a shorthand for ``print_exception(sys.last_type, sys.last_value, |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 58 | 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 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_stack(f=None, limit=None, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | |
| 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 Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 70 | .. function:: extract_tb(traceback, limit=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | |
| 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 |
| 75 | "pre-processed" stack trace entry is a quadruple (*filename*, *line number*, |
| 76 | *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 Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 81 | .. function:: extract_stack(f=None, limit=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 82 | |
| 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 Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 108 | .. function:: format_exception(type, value, tb, limit=None, chain=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 | |
| 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 Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 117 | .. function:: format_exc(limit=None, chain=True) |
Georg Brandl | 1aea30a | 2008-07-19 15:51:07 +0000 | [diff] [blame] | 118 | |
| 119 | This is like ``print_exc(limit)`` but returns a string instead of printing to a |
| 120 | file. |
| 121 | |
| 122 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 123 | .. function:: format_tb(tb, limit=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 | |
| 125 | A shorthand for ``format_list(extract_tb(tb, limit))``. |
| 126 | |
| 127 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 128 | .. function:: format_stack(f=None, limit=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 129 | |
| 130 | A shorthand for ``format_list(extract_stack(f, limit))``. |
| 131 | |
| 132 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 133 | .. _traceback-example: |
| 134 | |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 135 | Traceback Examples |
| 136 | ------------------ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 137 | |
| 138 | This simple example implements a basic read-eval-print loop, similar to (but |
| 139 | less useful than) the standard Python interactive interpreter loop. For a more |
| 140 | complete implementation of the interpreter loop, refer to the :mod:`code` |
| 141 | module. :: |
| 142 | |
| 143 | import sys, traceback |
| 144 | |
| 145 | def run_user_code(envdir): |
Georg Brandl | 8d5c392 | 2007-12-02 22:48:17 +0000 | [diff] [blame] | 146 | source = input(">>> ") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 147 | try: |
| 148 | exec(source, envdir) |
Andrew Svetlov | 4739561 | 2012-11-02 22:07:26 +0200 | [diff] [blame] | 149 | except Exception: |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 150 | print("Exception in user code:") |
| 151 | print("-"*60) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 152 | traceback.print_exc(file=sys.stdout) |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 153 | print("-"*60) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 154 | |
| 155 | envdir = {} |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 156 | while True: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 157 | run_user_code(envdir) |
| 158 | |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 159 | |
| 160 | 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] | 161 | exception and traceback: |
| 162 | |
| 163 | .. testcode:: |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 164 | |
| 165 | import sys, traceback |
| 166 | |
| 167 | def lumberjack(): |
| 168 | bright_side_of_death() |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 169 | |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 170 | def bright_side_of_death(): |
| 171 | return tuple()[0] |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 172 | |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 173 | try: |
| 174 | lumberjack() |
Ezio Melotti | 5e0110e | 2010-03-13 01:28:34 +0000 | [diff] [blame] | 175 | except IndexError: |
| 176 | exc_type, exc_value, exc_traceback = sys.exc_info() |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 177 | print("*** print_tb:") |
Ezio Melotti | 5e0110e | 2010-03-13 01:28:34 +0000 | [diff] [blame] | 178 | traceback.print_tb(exc_traceback, limit=1, file=sys.stdout) |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 179 | print("*** print_exception:") |
Ezio Melotti | 5e0110e | 2010-03-13 01:28:34 +0000 | [diff] [blame] | 180 | traceback.print_exception(exc_type, exc_value, exc_traceback, |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 181 | limit=2, file=sys.stdout) |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 182 | print("*** print_exc:") |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 183 | traceback.print_exc() |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 184 | print("*** format_exc, first and last line:") |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 185 | formatted_lines = traceback.format_exc().splitlines() |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 186 | print(formatted_lines[0]) |
| 187 | print(formatted_lines[-1]) |
| 188 | print("*** format_exception:") |
Ezio Melotti | 5e0110e | 2010-03-13 01:28:34 +0000 | [diff] [blame] | 189 | print(repr(traceback.format_exception(exc_type, exc_value, |
| 190 | exc_traceback))) |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 191 | print("*** extract_tb:") |
Ezio Melotti | 5e0110e | 2010-03-13 01:28:34 +0000 | [diff] [blame] | 192 | print(repr(traceback.extract_tb(exc_traceback))) |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 193 | print("*** format_tb:") |
Ezio Melotti | 5e0110e | 2010-03-13 01:28:34 +0000 | [diff] [blame] | 194 | print(repr(traceback.format_tb(exc_traceback))) |
| 195 | print("*** tb_lineno:", exc_traceback.tb_lineno) |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 196 | |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 197 | The output for the example would look similar to this: |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 198 | |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 199 | .. testoutput:: |
| 200 | :options: +NORMALIZE_WHITESPACE |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 201 | |
| 202 | *** print_tb: |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 203 | File "<doctest...>", line 10, in <module> |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 204 | lumberjack() |
| 205 | *** print_exception: |
| 206 | Traceback (most recent call last): |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 207 | File "<doctest...>", line 10, in <module> |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 208 | lumberjack() |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 209 | File "<doctest...>", line 4, in lumberjack |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 210 | bright_side_of_death() |
| 211 | IndexError: tuple index out of range |
| 212 | *** print_exc: |
| 213 | Traceback (most recent call last): |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 214 | File "<doctest...>", line 10, in <module> |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 215 | lumberjack() |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 216 | File "<doctest...>", line 4, in lumberjack |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 217 | bright_side_of_death() |
| 218 | IndexError: tuple index out of range |
| 219 | *** format_exc, first and last line: |
| 220 | Traceback (most recent call last): |
| 221 | IndexError: tuple index out of range |
| 222 | *** format_exception: |
| 223 | ['Traceback (most recent call last):\n', |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 224 | ' File "<doctest...>", line 10, in <module>\n lumberjack()\n', |
| 225 | ' File "<doctest...>", line 4, in lumberjack\n bright_side_of_death()\n', |
| 226 | ' 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] | 227 | 'IndexError: tuple index out of range\n'] |
| 228 | *** extract_tb: |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 229 | [('<doctest...>', 10, '<module>', 'lumberjack()'), |
| 230 | ('<doctest...>', 4, 'lumberjack', 'bright_side_of_death()'), |
Benjamin Peterson | 25c95f1 | 2009-05-08 20:42:26 +0000 | [diff] [blame] | 231 | ('<doctest...>', 7, 'bright_side_of_death', 'return tuple()[0]')] |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 232 | *** format_tb: |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 233 | [' File "<doctest...>", line 10, in <module>\n lumberjack()\n', |
| 234 | ' File "<doctest...>", line 4, in lumberjack\n bright_side_of_death()\n', |
| 235 | ' File "<doctest...>", line 7, in bright_side_of_death\n return tuple()[0]\n'] |
| 236 | *** tb_lineno: 10 |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 237 | |
| 238 | |
| 239 | The following example shows the different ways to print and format the stack:: |
| 240 | |
| 241 | >>> import traceback |
| 242 | >>> def another_function(): |
| 243 | ... lumberstack() |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 244 | ... |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 245 | >>> def lumberstack(): |
| 246 | ... traceback.print_stack() |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 247 | ... print(repr(traceback.extract_stack())) |
| 248 | ... print(repr(traceback.format_stack())) |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 249 | ... |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 250 | >>> another_function() |
| 251 | File "<doctest>", line 10, in <module> |
| 252 | another_function() |
| 253 | File "<doctest>", line 3, in another_function |
| 254 | lumberstack() |
| 255 | File "<doctest>", line 6, in lumberstack |
| 256 | traceback.print_stack() |
| 257 | [('<doctest>', 10, '<module>', 'another_function()'), |
| 258 | ('<doctest>', 3, 'another_function', 'lumberstack()'), |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 259 | ('<doctest>', 7, 'lumberstack', 'print(repr(traceback.extract_stack()))')] |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 260 | [' File "<doctest>", line 10, in <module>\n another_function()\n', |
| 261 | ' File "<doctest>", line 3, in another_function\n lumberstack()\n', |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 262 | ' 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] | 263 | |
| 264 | |
R. David Murray | e02a301 | 2009-04-27 18:38:19 +0000 | [diff] [blame] | 265 | This last example demonstrates the final few formatting functions: |
| 266 | |
| 267 | .. doctest:: |
| 268 | :options: +NORMALIZE_WHITESPACE |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 269 | |
| 270 | >>> import traceback |
Georg Brandl | 0142d4a | 2009-04-27 16:22:44 +0000 | [diff] [blame] | 271 | >>> traceback.format_list([('spam.py', 3, '<module>', 'spam.eggs()'), |
| 272 | ... ('eggs.py', 42, 'eggs', 'return "bacon"')]) |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 273 | [' File "spam.py", line 3, in <module>\n spam.eggs()\n', |
| 274 | ' File "eggs.py", line 42, in eggs\n return "bacon"\n'] |
Georg Brandl | 0142d4a | 2009-04-27 16:22:44 +0000 | [diff] [blame] | 275 | >>> an_error = IndexError('tuple index out of range') |
| 276 | >>> traceback.format_exception_only(type(an_error), an_error) |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 277 | ['IndexError: tuple index out of range\n'] |