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