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