Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`traceback` --- Print or retrieve a stack traceback |
| 3 | ======================================================== |
| 4 | |
| 5 | .. module:: traceback |
| 6 | :synopsis: Print or retrieve a stack traceback. |
| 7 | |
| 8 | |
| 9 | This module provides a standard interface to extract, format and print stack |
| 10 | traces of Python programs. It exactly mimics the behavior of the Python |
| 11 | interpreter when it prints a stack trace. This is useful when you want to print |
| 12 | stack traces under program control, such as in a "wrapper" around the |
| 13 | interpreter. |
| 14 | |
| 15 | .. index:: object: traceback |
| 16 | |
| 17 | The module uses traceback objects --- this is the object type that is stored in |
| 18 | the ``sys.last_traceback`` variable and returned as the third item from |
| 19 | :func:`sys.exc_info`. |
| 20 | |
| 21 | The module defines the following functions: |
| 22 | |
| 23 | |
| 24 | .. function:: print_tb(traceback[, limit[, file]]) |
| 25 | |
| 26 | Print up to *limit* stack trace entries from *traceback*. If *limit* is omitted |
| 27 | or ``None``, all entries are printed. If *file* is omitted or ``None``, the |
| 28 | output goes to ``sys.stderr``; otherwise it should be an open file or file-like |
| 29 | object to receive the output. |
| 30 | |
| 31 | |
| 32 | .. function:: print_exception(type, value, traceback[, limit[, file]]) |
| 33 | |
| 34 | Print exception information and up to *limit* stack trace entries from |
| 35 | *traceback* to *file*. This differs from :func:`print_tb` in the following ways: |
| 36 | (1) if *traceback* is not ``None``, it prints a header ``Traceback (most recent |
| 37 | call last):``; (2) it prints the exception *type* and *value* after the stack |
| 38 | trace; (3) if *type* is :exc:`SyntaxError` and *value* has the appropriate |
| 39 | format, it prints the line where the syntax error occurred with a caret |
| 40 | indicating the approximate position of the error. |
| 41 | |
| 42 | |
| 43 | .. function:: print_exc([limit[, file]]) |
| 44 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 45 | This is a shorthand for ``print_exception(*sys.exc_info())``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | .. function:: format_exc([limit]) |
| 49 | |
| 50 | This is like ``print_exc(limit)`` but returns a string instead of printing to a |
| 51 | file. |
| 52 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | |
| 54 | .. function:: print_last([limit[, file]]) |
| 55 | |
| 56 | This is a shorthand for ``print_exception(sys.last_type, sys.last_value, |
| 57 | sys.last_traceback, limit, file)``. |
| 58 | |
| 59 | |
| 60 | .. function:: print_stack([f[, limit[, file]]]) |
| 61 | |
| 62 | This function prints a stack trace from its invocation point. The optional *f* |
| 63 | argument can be used to specify an alternate stack frame to start. The optional |
| 64 | *limit* and *file* arguments have the same meaning as for |
| 65 | :func:`print_exception`. |
| 66 | |
| 67 | |
| 68 | .. function:: extract_tb(traceback[, limit]) |
| 69 | |
| 70 | Return a list of up to *limit* "pre-processed" stack trace entries extracted |
| 71 | from the traceback object *traceback*. It is useful for alternate formatting of |
| 72 | stack traces. If *limit* is omitted or ``None``, all entries are extracted. A |
| 73 | "pre-processed" stack trace entry is a quadruple (*filename*, *line number*, |
| 74 | *function name*, *text*) representing the information that is usually printed |
| 75 | for a stack trace. The *text* is a string with leading and trailing whitespace |
| 76 | stripped; if the source is not available it is ``None``. |
| 77 | |
| 78 | |
| 79 | .. function:: extract_stack([f[, limit]]) |
| 80 | |
| 81 | Extract the raw traceback from the current stack frame. The return value has |
| 82 | the same format as for :func:`extract_tb`. The optional *f* and *limit* |
| 83 | arguments have the same meaning as for :func:`print_stack`. |
| 84 | |
| 85 | |
| 86 | .. function:: format_list(list) |
| 87 | |
| 88 | Given a list of tuples as returned by :func:`extract_tb` or |
| 89 | :func:`extract_stack`, return a list of strings ready for printing. Each string |
| 90 | in the resulting list corresponds to the item with the same index in the |
| 91 | argument list. Each string ends in a newline; the strings may contain internal |
| 92 | newlines as well, for those items whose source text line is not ``None``. |
| 93 | |
| 94 | |
| 95 | .. function:: format_exception_only(type, value) |
| 96 | |
| 97 | Format the exception part of a traceback. The arguments are the exception type |
| 98 | and value such as given by ``sys.last_type`` and ``sys.last_value``. The return |
| 99 | value is a list of strings, each ending in a newline. Normally, the list |
| 100 | contains a single string; however, for :exc:`SyntaxError` exceptions, it |
| 101 | contains several lines that (when printed) display detailed information about |
| 102 | where the syntax error occurred. The message indicating which exception |
| 103 | occurred is the always last string in the list. |
| 104 | |
| 105 | |
| 106 | .. function:: format_exception(type, value, tb[, limit]) |
| 107 | |
| 108 | Format a stack trace and the exception information. The arguments have the |
| 109 | same meaning as the corresponding arguments to :func:`print_exception`. The |
| 110 | return value is a list of strings, each ending in a newline and some containing |
| 111 | internal newlines. When these lines are concatenated and printed, exactly the |
| 112 | same text is printed as does :func:`print_exception`. |
| 113 | |
| 114 | |
| 115 | .. function:: format_tb(tb[, limit]) |
| 116 | |
| 117 | A shorthand for ``format_list(extract_tb(tb, limit))``. |
| 118 | |
| 119 | |
| 120 | .. function:: format_stack([f[, limit]]) |
| 121 | |
| 122 | A shorthand for ``format_list(extract_stack(f, limit))``. |
| 123 | |
| 124 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 125 | .. _traceback-example: |
| 126 | |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 127 | Traceback Examples |
| 128 | ------------------ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 129 | |
| 130 | This simple example implements a basic read-eval-print loop, similar to (but |
| 131 | less useful than) the standard Python interactive interpreter loop. For a more |
| 132 | complete implementation of the interpreter loop, refer to the :mod:`code` |
| 133 | module. :: |
| 134 | |
| 135 | import sys, traceback |
| 136 | |
| 137 | def run_user_code(envdir): |
Georg Brandl | 8d5c392 | 2007-12-02 22:48:17 +0000 | [diff] [blame] | 138 | source = input(">>> ") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 139 | try: |
| 140 | exec(source, envdir) |
| 141 | except: |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 142 | print("Exception in user code:") |
| 143 | print("-"*60) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 144 | traceback.print_exc(file=sys.stdout) |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 145 | print("-"*60) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 146 | |
| 147 | envdir = {} |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 148 | while True: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 149 | run_user_code(envdir) |
| 150 | |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 151 | |
| 152 | The following example demonstrates the different ways to print and format the |
| 153 | exception and traceback:: |
| 154 | |
| 155 | import sys, traceback |
| 156 | |
| 157 | def lumberjack(): |
| 158 | bright_side_of_death() |
| 159 | |
| 160 | def bright_side_of_death(): |
| 161 | return tuple()[0] |
| 162 | |
| 163 | try: |
| 164 | lumberjack() |
| 165 | except: |
| 166 | exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 167 | print("*** print_tb:") |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 168 | traceback.print_tb(exceptionTraceback, limit=1, file=sys.stdout) |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 169 | print("*** print_exception:") |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 170 | traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback, |
| 171 | limit=2, file=sys.stdout) |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 172 | print("*** print_exc:") |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 173 | traceback.print_exc() |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 174 | print("*** format_exc, first and last line:") |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 175 | formatted_lines = traceback.format_exc().splitlines() |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 176 | print(formatted_lines[0]) |
| 177 | print(formatted_lines[-1]) |
| 178 | print("*** format_exception:") |
| 179 | print(repr(traceback.format_exception(exceptionType, exceptionValue, |
| 180 | exceptionTraceback))) |
| 181 | print("*** extract_tb:") |
| 182 | print(repr(traceback.extract_tb(exceptionTraceback))) |
| 183 | print("*** format_tb:") |
| 184 | print(repr(traceback.format_tb(exceptionTraceback))) |
| 185 | print("*** tb_lineno:", traceback.tb_lineno(exceptionTraceback)) |
| 186 | print("*** print_last:") |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 187 | traceback.print_last() |
| 188 | |
| 189 | |
| 190 | The output for the example would look similar to this:: |
| 191 | |
| 192 | *** print_tb: |
| 193 | File "<doctest>", line 9, in <module> |
| 194 | lumberjack() |
| 195 | *** print_exception: |
| 196 | Traceback (most recent call last): |
| 197 | File "<doctest>", line 9, in <module> |
| 198 | lumberjack() |
| 199 | File "<doctest>", line 3, in lumberjack |
| 200 | bright_side_of_death() |
| 201 | IndexError: tuple index out of range |
| 202 | *** print_exc: |
| 203 | Traceback (most recent call last): |
| 204 | File "<doctest>", line 9, in <module> |
| 205 | lumberjack() |
| 206 | File "<doctest>", line 3, in lumberjack |
| 207 | bright_side_of_death() |
| 208 | IndexError: tuple index out of range |
| 209 | *** format_exc, first and last line: |
| 210 | Traceback (most recent call last): |
| 211 | IndexError: tuple index out of range |
| 212 | *** format_exception: |
| 213 | ['Traceback (most recent call last):\n', |
| 214 | ' File "<doctest>", line 9, in <module>\n lumberjack()\n', |
| 215 | ' File "<doctest>", line 3, in lumberjack\n bright_side_of_death()\n', |
| 216 | ' File "<doctest>", line 6, in bright_side_of_death\n return tuple()[0]\n', |
| 217 | 'IndexError: tuple index out of range\n'] |
| 218 | *** extract_tb: |
| 219 | [('<doctest>', 9, '<module>', 'lumberjack()'), |
| 220 | ('<doctest>', 3, 'lumberjack', 'bright_side_of_death()'), |
| 221 | ('<doctest>', 6, 'bright_side_of_death', 'return tuple()[0]')] |
| 222 | *** format_tb: |
| 223 | [' File "<doctest>", line 9, in <module>\n lumberjack()\n', |
| 224 | ' File "<doctest>", line 3, in lumberjack\n bright_side_of_death()\n', |
| 225 | ' File "<doctest>", line 6, in bright_side_of_death\n return tuple()[0]\n'] |
| 226 | *** tb_lineno: 2 |
| 227 | *** print_last: |
| 228 | Traceback (most recent call last): |
| 229 | File "<doctest>", line 9, in <module> |
| 230 | lumberjack() |
| 231 | File "<doctest>", line 3, in lumberjack |
| 232 | bright_side_of_death() |
| 233 | IndexError: tuple index out of range |
| 234 | |
| 235 | |
| 236 | The following example shows the different ways to print and format the stack:: |
| 237 | |
| 238 | >>> import traceback |
| 239 | >>> def another_function(): |
| 240 | ... lumberstack() |
| 241 | ... |
| 242 | >>> def lumberstack(): |
| 243 | ... traceback.print_stack() |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 244 | ... print(repr(traceback.extract_stack())) |
| 245 | ... print(repr(traceback.format_stack())) |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 246 | ... |
| 247 | >>> another_function() |
| 248 | File "<doctest>", line 10, in <module> |
| 249 | another_function() |
| 250 | File "<doctest>", line 3, in another_function |
| 251 | lumberstack() |
| 252 | File "<doctest>", line 6, in lumberstack |
| 253 | traceback.print_stack() |
| 254 | [('<doctest>', 10, '<module>', 'another_function()'), |
| 255 | ('<doctest>', 3, 'another_function', 'lumberstack()'), |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 256 | ('<doctest>', 7, 'lumberstack', 'print(repr(traceback.extract_stack()))')] |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 257 | [' File "<doctest>", line 10, in <module>\n another_function()\n', |
| 258 | ' File "<doctest>", line 3, in another_function\n lumberstack()\n', |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 259 | ' 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] | 260 | |
| 261 | |
| 262 | This last example demonstrates the final few formatting functions:: |
| 263 | |
| 264 | >>> import traceback |
| 265 | >>> format_list([('spam.py', 3, '<module>', 'spam.eggs()'), |
| 266 | ... ('eggs.py', 42, 'eggs', 'return "bacon"')]) |
| 267 | [' File "spam.py", line 3, in <module>\n spam.eggs()\n', |
| 268 | ' File "eggs.py", line 42, in eggs\n return "bacon"\n'] |
| 269 | >>> theError = IndexError('tuple indx out of range') |
| 270 | >>> traceback.format_exception_only(type(theError), theError) |
| 271 | ['IndexError: tuple index out of range\n'] |