@@ -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], *, show_group=False)
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
@@ -160,7 +165,8 @@ The module defines the following functions:
160
165
positional-only.
161
166
162
167
.. versionchanged :: 3.11
163
- The returned list now includes any notes attached to the exception.
168
+ The returned list now includes any
169
+ :attr: `notes <BaseException.__notes__> ` attached to the exception.
164
170
165
171
.. versionchanged :: 3.13
166
172
*show_group * parameter was added.
@@ -199,14 +205,17 @@ The module defines the following functions:
199
205
200
206
.. function :: clear_frames(tb)
201
207
202
- Clears the local variables of all the stack frames in a traceback *tb *
203
- by calling the :meth: `clear ` method of each frame object.
208
+ Clears the local variables of all the stack frames in a
209
+ :ref: `traceback <traceback-objects >` *tb *
210
+ by calling the :meth: `~frame.clear ` method of each
211
+ :ref: `frame object <frame-objects >`.
204
212
205
213
.. versionadded :: 3.4
206
214
207
215
.. function :: walk_stack(f)
208
216
209
- Walk a stack following ``f.f_back `` from the given frame, yielding the frame
217
+ Walk a stack following :attr: `f.f_back <frame.f_back> ` from the given frame,
218
+ yielding the frame
210
219
and line number for each frame. If *f * is ``None ``, the current stack is
211
220
used. This helper is used with :meth: `StackSummary.extract `.
212
221
@@ -222,12 +231,12 @@ The module defines the following functions:
222
231
223
232
The module also defines the following classes:
224
233
225
- :class: `TracebackException ` Objects
226
- -----------------------------------
234
+ :class: `! TracebackException ` Objects
235
+ ------------------------------------
227
236
228
237
.. versionadded :: 3.5
229
238
230
- :class: `TracebackException ` objects are created from actual exceptions to
239
+ :class: `! TracebackException ` objects are created from actual exceptions to
231
240
capture data for later printing in a lightweight fashion.
232
241
233
242
.. 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)
@@ -379,33 +388,34 @@ capture data for later printing in a lightweight fashion.
379
388
well, recursively, with indentation relative to their nesting depth.
380
389
381
390
.. versionchanged :: 3.11
382
- The exception's notes are now included in the output.
391
+ The exception's :attr: `notes <BaseException.__notes__> ` are now
392
+ included in the output.
383
393
384
394
.. versionchanged :: 3.13
385
395
Added the *show_group * parameter.
386
396
387
397
388
- :class: `StackSummary ` Objects
389
- -----------------------------
398
+ :class: `! StackSummary ` Objects
399
+ ------------------------------
390
400
391
401
.. versionadded :: 3.5
392
402
393
- :class: `StackSummary ` objects represent a call stack ready for formatting.
403
+ :class: `! StackSummary ` objects represent a call stack ready for formatting.
394
404
395
405
.. class :: StackSummary
396
406
397
407
.. classmethod :: extract(frame_gen, *, limit=None, lookup_lines=True, capture_locals=False)
398
408
399
- Construct a :class: `StackSummary ` object from a frame generator (such as
409
+ Construct a :class: `! StackSummary ` object from a frame generator (such as
400
410
is returned by :func: `~traceback.walk_stack ` or
401
411
:func: `~traceback.walk_tb `).
402
412
403
413
If *limit * is supplied, only this many frames are taken from *frame_gen *.
404
414
If *lookup_lines * is ``False ``, the returned :class: `FrameSummary `
405
415
objects will not have read their lines in yet, making the cost of
406
- creating the :class: `StackSummary ` cheaper (which may be valuable if it
416
+ creating the :class: `! StackSummary ` cheaper (which may be valuable if it
407
417
may not actually get formatted). If *capture_locals * is ``True `` the
408
- local variables in each :class: `FrameSummary ` are captured as object
418
+ local variables in each :class: `! FrameSummary ` are captured as object
409
419
representations.
410
420
411
421
.. versionchanged :: 3.12
@@ -414,14 +424,16 @@ capture data for later printing in a lightweight fashion.
414
424
415
425
.. classmethod :: from_list(a_list)
416
426
417
- Construct a :class: `StackSummary ` object from a supplied list of
427
+ Construct a :class: `! StackSummary ` object from a supplied list of
418
428
:class: `FrameSummary ` objects or old-style list of tuples. Each tuple
419
- should be a 4-tuple with filename, lineno, name, line as the elements.
429
+ should be a 4-tuple with *filename *, *lineno *, *name *, *line * as the
430
+ elements.
420
431
421
432
.. method :: format()
422
433
423
434
Returns a list of strings ready for printing. Each string in the
424
- resulting list corresponds to a single frame from the stack.
435
+ resulting list corresponds to a single :ref: `frame <frame-objects >` from
436
+ the stack.
425
437
Each string ends in a newline; the strings may contain internal
426
438
newlines as well, for those items with source text lines.
427
439
@@ -434,33 +446,59 @@ capture data for later printing in a lightweight fashion.
434
446
435
447
.. method :: format_frame_summary(frame_summary)
436
448
437
- Returns a string for printing one of the frames involved in the stack.
449
+ Returns a string for printing one of the :ref: `frames <frame-objects >`
450
+ involved in the stack.
438
451
This method is called for each :class: `FrameSummary ` object to be
439
452
printed by :meth: `StackSummary.format `. If it returns ``None ``, the
440
453
frame is omitted from the output.
441
454
442
455
.. versionadded :: 3.11
443
456
444
457
445
- :class: `FrameSummary ` Objects
446
- -----------------------------
458
+ :class: `! FrameSummary ` Objects
459
+ ------------------------------
447
460
448
461
.. versionadded :: 3.5
449
462
450
- A :class: `FrameSummary ` object represents a single frame in a traceback.
463
+ A :class: `!FrameSummary ` object represents a single :ref: `frame <frame-objects >`
464
+ in a :ref: `traceback <traceback-objects >`.
451
465
452
466
.. class :: FrameSummary(filename, lineno, name, lookup_line=True, locals=None, line=None)
453
467
454
- Represent a single frame in the traceback or stack that is being formatted
455
- or printed. It may optionally have a stringified version of the frames
468
+ Represents a single :ref: `frame <frame-objects >` in the
469
+ :ref: `traceback <traceback-objects >` or stack that is being formatted
470
+ or printed. It may optionally have a stringified version of the frame's
456
471
locals included in it. If *lookup_line * is ``False ``, the source code is not
457
- looked up until the :class: `FrameSummary ` has the :attr: `~FrameSummary.line `
458
- attribute accessed (which also happens when casting it to a tuple).
472
+ looked up until the :class: `! FrameSummary ` has the :attr: `~FrameSummary.line `
473
+ attribute accessed (which also happens when casting it to a :class: ` tuple ` ).
459
474
:attr: `~FrameSummary.line ` may be directly provided, and will prevent line
460
475
lookups happening at all. *locals * is an optional local variable
461
476
dictionary, and if supplied the variable representations are stored in the
462
477
summary for later display.
463
478
479
+ :class: `!FrameSummary ` instances have the following attributes:
480
+
481
+ .. attribute :: FrameSummary.filename
482
+
483
+ The filename of the source code for this frame. Equivalent to accessing
484
+ :attr: `f.f_code.co_filename <codeobject.co_filename> ` on a
485
+ :ref: `frame object <frame-objects >` *f *.
486
+
487
+ .. attribute :: FrameSummary.lineno
488
+
489
+ The line number of the source code for this frame.
490
+
491
+ .. attribute :: FrameSummary.name
492
+
493
+ Equivalent to accessing :attr: `f.f_code.co_name <codeobject.co_name> ` on
494
+ a :ref: `frame object <frame-objects >` *f *.
495
+
496
+ .. attribute :: FrameSummary.line
497
+
498
+ A string representing the source code for this frame, with leading and
499
+ trailing whitespace stripped.
500
+ If the source is not available, it is ``None ``.
501
+
464
502
.. _traceback-example :
465
503
466
504
Traceback Examples
0 commit comments