@@ -18,7 +18,8 @@ interpreter.
18
18
19
19
The module uses :ref: `traceback objects <traceback-objects >` --- these are
20
20
objects of type :class: `types.TracebackType `,
21
- which are assigned to the ``__traceback__ `` field of :class: `BaseException ` instances.
21
+ which are assigned to the :attr: `~BaseException.__traceback__ ` field of
22
+ :class: `BaseException ` instances.
22
23
23
24
.. seealso ::
24
25
@@ -32,11 +33,13 @@ The module defines the following functions:
32
33
33
34
.. function :: print_tb(tb, limit=None, file=None)
34
35
35
- Print up to *limit * stack trace entries from traceback object *tb * (starting
36
+ Print up to *limit * stack trace entries from
37
+ :ref: `traceback object <traceback-objects >` *tb * (starting
36
38
from the caller's frame) if *limit * is positive. Otherwise, print the last
37
39
``abs(limit) `` entries. If *limit * is omitted or ``None ``, all entries are
38
40
printed. If *file * is omitted or ``None ``, the output goes to
39
- ``sys.stderr ``; otherwise it should be an open file or file-like object to
41
+ :data: `sys.stderr `; otherwise it should be an open
42
+ :term: `file <file object> ` or :term: `file-like object ` to
40
43
receive the output.
41
44
42
45
.. versionchanged :: 3.5
@@ -46,7 +49,8 @@ The module defines the following functions:
46
49
.. function :: print_exception(exc, /[, value, tb], limit=None, \
47
50
file=None, chain=True)
48
51
49
- Print exception information and stack trace entries from traceback object
52
+ Print exception information and stack trace entries from
53
+ :ref: `traceback object <traceback-objects >`
50
54
*tb * to *file *. This differs from :func: `print_tb ` in the following
51
55
ways:
52
56
@@ -98,7 +102,8 @@ The module defines the following functions:
98
102
Print up to *limit * stack trace entries (starting from the invocation
99
103
point) if *limit * is positive. Otherwise, print the last ``abs(limit) ``
100
104
entries. If *limit * is omitted or ``None ``, all entries are printed.
101
- The optional *f * argument can be used to specify an alternate stack frame
105
+ The optional *f * argument can be used to specify an alternate
106
+ :ref: `stack frame <frame-objects >`
102
107
to start. The optional *file * argument has the same meaning as for
103
108
:func: `print_tb `.
104
109
@@ -109,20 +114,20 @@ The module defines the following functions:
109
114
.. function :: extract_tb(tb, limit=None)
110
115
111
116
Return a :class: `StackSummary ` object representing a list of "pre-processed"
112
- stack trace entries extracted from the traceback object *tb *. It is useful
117
+ stack trace entries extracted from the
118
+ :ref: `traceback object <traceback-objects >` *tb *. It is useful
113
119
for alternate formatting of stack traces. The optional *limit * argument has
114
120
the same meaning as for :func: `print_tb `. A "pre-processed" stack trace
115
121
entry is a :class: `FrameSummary ` object containing attributes
116
122
:attr: `~FrameSummary.filename `, :attr: `~FrameSummary.lineno `,
117
123
:attr: `~FrameSummary.name `, and :attr: `~FrameSummary.line ` representing the
118
- information that is usually printed for a stack trace. The
119
- :attr: `~FrameSummary.line ` is a string with leading and trailing
120
- whitespace stripped; if the source is not available it is ``None ``.
124
+ information that is usually printed for a stack trace.
121
125
122
126
123
127
.. function :: extract_stack(f=None, limit=None)
124
128
125
- Extract the raw traceback from the current stack frame. The return value has
129
+ Extract the raw traceback from the current
130
+ :ref: `stack frame <frame-objects >`. The return value has
126
131
the same format as for :func: `extract_tb `. The optional *f * and *limit *
127
132
arguments have the same meaning as for :func: `print_stack `.
128
133
@@ -140,7 +145,7 @@ The module defines the following functions:
140
145
.. function :: format_exception_only(exc, /[, value])
141
146
142
147
Format the exception part of a traceback using an exception value such as
143
- given by `` sys.last_value ` `. The return value is a list of strings, each
148
+ given by :data: ` sys.last_value `. The return value is a list of strings, each
144
149
ending in a newline. The list contains the exception's message, which is
145
150
normally a single string; however, for :exc: `SyntaxError ` exceptions, it
146
151
contains several lines that (when printed) display detailed information
@@ -156,7 +161,8 @@ The module defines the following functions:
156
161
positional-only.
157
162
158
163
.. versionchanged :: 3.11
159
- The returned list now includes any notes attached to the exception.
164
+ The returned list now includes any
165
+ :attr: `notes <BaseException.__notes__> ` attached to the exception.
160
166
161
167
162
168
.. function :: format_exception(exc, /[, value, tb], limit=None, chain=True)
@@ -192,14 +198,17 @@ The module defines the following functions:
192
198
193
199
.. function :: clear_frames(tb)
194
200
195
- Clears the local variables of all the stack frames in a traceback *tb *
196
- by calling the :meth: `clear ` method of each frame object.
201
+ Clears the local variables of all the stack frames in a
202
+ :ref: `traceback <traceback-objects >` *tb *
203
+ by calling the :meth: `~frame.clear ` method of each
204
+ :ref: `frame object <frame-objects >`.
197
205
198
206
.. versionadded :: 3.4
199
207
200
208
.. function :: walk_stack(f)
201
209
202
- Walk a stack following ``f.f_back `` from the given frame, yielding the frame
210
+ Walk a stack following :attr: `f.f_back <frame.f_back> ` from the given frame,
211
+ yielding the frame
203
212
and line number for each frame. If *f * is ``None ``, the current stack is
204
213
used. This helper is used with :meth: `StackSummary.extract `.
205
214
@@ -215,12 +224,12 @@ The module defines the following functions:
215
224
216
225
The module also defines the following classes:
217
226
218
- :class: `TracebackException ` Objects
219
- -----------------------------------
227
+ :class: `! TracebackException ` Objects
228
+ ------------------------------------
220
229
221
230
.. versionadded :: 3.5
222
231
223
- :class: `TracebackException ` objects are created from actual exceptions to
232
+ :class: `! TracebackException ` objects are created from actual exceptions to
224
233
capture data for later printing in a lightweight fashion.
225
234
226
235
.. class :: TracebackException(exc_type, exc_value, exc_traceback, *, limit=None, lookup_lines=True, capture_locals=False, compact=False, max_group_width=15, max_group_depth=10)
@@ -360,31 +369,32 @@ capture data for later printing in a lightweight fashion.
360
369
the syntax error occurred.
361
370
362
371
.. versionchanged :: 3.11
363
- The exception's notes are now included in the output.
372
+ The exception's :attr: `notes <BaseException.__notes__> ` are now
373
+ included in the output.
364
374
365
375
366
376
367
- :class: `StackSummary ` Objects
368
- -----------------------------
377
+ :class: `! StackSummary ` Objects
378
+ ------------------------------
369
379
370
380
.. versionadded :: 3.5
371
381
372
- :class: `StackSummary ` objects represent a call stack ready for formatting.
382
+ :class: `! StackSummary ` objects represent a call stack ready for formatting.
373
383
374
384
.. class :: StackSummary
375
385
376
386
.. classmethod :: extract(frame_gen, *, limit=None, lookup_lines=True, capture_locals=False)
377
387
378
- Construct a :class: `StackSummary ` object from a frame generator (such as
388
+ Construct a :class: `! StackSummary ` object from a frame generator (such as
379
389
is returned by :func: `~traceback.walk_stack ` or
380
390
:func: `~traceback.walk_tb `).
381
391
382
392
If *limit * is supplied, only this many frames are taken from *frame_gen *.
383
393
If *lookup_lines * is ``False ``, the returned :class: `FrameSummary `
384
394
objects will not have read their lines in yet, making the cost of
385
- creating the :class: `StackSummary ` cheaper (which may be valuable if it
395
+ creating the :class: `! StackSummary ` cheaper (which may be valuable if it
386
396
may not actually get formatted). If *capture_locals * is ``True `` the
387
- local variables in each :class: `FrameSummary ` are captured as object
397
+ local variables in each :class: `! FrameSummary ` are captured as object
388
398
representations.
389
399
390
400
.. versionchanged :: 3.12
@@ -393,14 +403,16 @@ capture data for later printing in a lightweight fashion.
393
403
394
404
.. classmethod :: from_list(a_list)
395
405
396
- Construct a :class: `StackSummary ` object from a supplied list of
406
+ Construct a :class: `! StackSummary ` object from a supplied list of
397
407
:class: `FrameSummary ` objects or old-style list of tuples. Each tuple
398
- should be a 4-tuple with filename, lineno, name, line as the elements.
408
+ should be a 4-tuple with *filename *, *lineno *, *name *, *line * as the
409
+ elements.
399
410
400
411
.. method :: format()
401
412
402
413
Returns a list of strings ready for printing. Each string in the
403
- resulting list corresponds to a single frame from the stack.
414
+ resulting list corresponds to a single :ref: `frame <frame-objects >` from
415
+ the stack.
404
416
Each string ends in a newline; the strings may contain internal
405
417
newlines as well, for those items with source text lines.
406
418
@@ -413,33 +425,59 @@ capture data for later printing in a lightweight fashion.
413
425
414
426
.. method :: format_frame_summary(frame_summary)
415
427
416
- Returns a string for printing one of the frames involved in the stack.
428
+ Returns a string for printing one of the :ref: `frames <frame-objects >`
429
+ involved in the stack.
417
430
This method is called for each :class: `FrameSummary ` object to be
418
431
printed by :meth: `StackSummary.format `. If it returns ``None ``, the
419
432
frame is omitted from the output.
420
433
421
434
.. versionadded :: 3.11
422
435
423
436
424
- :class: `FrameSummary ` Objects
425
- -----------------------------
437
+ :class: `! FrameSummary ` Objects
438
+ ------------------------------
426
439
427
440
.. versionadded :: 3.5
428
441
429
- A :class: `FrameSummary ` object represents a single frame in a traceback.
442
+ A :class: `!FrameSummary ` object represents a single :ref: `frame <frame-objects >`
443
+ in a :ref: `traceback <traceback-objects >`.
430
444
431
445
.. class :: FrameSummary(filename, lineno, name, lookup_line=True, locals=None, line=None)
432
446
433
- Represent a single frame in the traceback or stack that is being formatted
434
- or printed. It may optionally have a stringified version of the frames
447
+ Represents a single :ref: `frame <frame-objects >` in the
448
+ :ref: `traceback <traceback-objects >` or stack that is being formatted
449
+ or printed. It may optionally have a stringified version of the frame's
435
450
locals included in it. If *lookup_line * is ``False ``, the source code is not
436
- looked up until the :class: `FrameSummary ` has the :attr: `~FrameSummary.line `
437
- attribute accessed (which also happens when casting it to a tuple).
451
+ looked up until the :class: `! FrameSummary ` has the :attr: `~FrameSummary.line `
452
+ attribute accessed (which also happens when casting it to a :class: ` tuple ` ).
438
453
:attr: `~FrameSummary.line ` may be directly provided, and will prevent line
439
454
lookups happening at all. *locals * is an optional local variable
440
455
dictionary, and if supplied the variable representations are stored in the
441
456
summary for later display.
442
457
458
+ :class: `!FrameSummary ` instances have the following attributes:
459
+
460
+ .. attribute :: FrameSummary.filename
461
+
462
+ The filename of the source code for this frame. Equivalent to accessing
463
+ :attr: `f.f_code.co_filename <codeobject.co_filename> ` on a
464
+ :ref: `frame object <frame-objects >` *f *.
465
+
466
+ .. attribute :: FrameSummary.lineno
467
+
468
+ The line number of the source code for this frame.
469
+
470
+ .. attribute :: FrameSummary.name
471
+
472
+ Equivalent to accessing :attr: `f.f_code.co_name <codeobject.co_name> ` on
473
+ a :ref: `frame object <frame-objects >` *f *.
474
+
475
+ .. attribute :: FrameSummary.line
476
+
477
+ A string representing the source code for this frame, with leading and
478
+ trailing whitespace stripped.
479
+ If the source is not available, it is ``None ``.
480
+
443
481
.. _traceback-example :
444
482
445
483
Traceback Examples
0 commit comments