@@ -18,7 +18,8 @@ interpreter.
1818
1919The module uses :ref: `traceback objects <traceback-objects >` --- these are
2020objects 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.
2223
2324.. seealso ::
2425
@@ -32,11 +33,13 @@ The module defines the following functions:
3233
3334.. function :: print_tb(tb, limit=None, file=None)
3435
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
3638 from the caller's frame) if *limit * is positive. Otherwise, print the last
3739 ``abs(limit) `` entries. If *limit * is omitted or ``None ``, all entries are
3840 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
4043 receive the output.
4144
4245 .. versionchanged :: 3.5
@@ -46,7 +49,8 @@ The module defines the following functions:
4649.. function :: print_exception(exc, /[, value, tb], limit=None, \
4750 file=None, chain=True)
4851
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 >`
5054 *tb * to *file *. This differs from :func: `print_tb ` in the following
5155 ways:
5256
@@ -98,7 +102,8 @@ The module defines the following functions:
98102 Print up to *limit * stack trace entries (starting from the invocation
99103 point) if *limit * is positive. Otherwise, print the last ``abs(limit) ``
100104 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 >`
102107 to start. The optional *file * argument has the same meaning as for
103108 :func: `print_tb `.
104109
@@ -109,20 +114,20 @@ The module defines the following functions:
109114.. function :: extract_tb(tb, limit=None)
110115
111116 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
113119 for alternate formatting of stack traces. The optional *limit * argument has
114120 the same meaning as for :func: `print_tb `. A "pre-processed" stack trace
115121 entry is a :class: `FrameSummary ` object containing attributes
116122 :attr: `~FrameSummary.filename `, :attr: `~FrameSummary.lineno `,
117123 :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.
121125
122126
123127.. function :: extract_stack(f=None, limit=None)
124128
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
126131 the same format as for :func: `extract_tb `. The optional *f * and *limit *
127132 arguments have the same meaning as for :func: `print_stack `.
128133
@@ -140,7 +145,7 @@ The module defines the following functions:
140145.. function :: format_exception_only(exc, /[, value], *, show_group=False)
141146
142147 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
144149 ending in a newline. The list contains the exception's message, which is
145150 normally a single string; however, for :exc: `SyntaxError ` exceptions, it
146151 contains several lines that (when printed) display detailed information
@@ -160,7 +165,8 @@ The module defines the following functions:
160165 positional-only.
161166
162167 .. 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.
164170
165171 .. versionchanged :: 3.13
166172 *show_group * parameter was added.
@@ -199,14 +205,17 @@ The module defines the following functions:
199205
200206.. function :: clear_frames(tb)
201207
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 >`.
204212
205213 .. versionadded :: 3.4
206214
207215.. function :: walk_stack(f)
208216
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
210219 and line number for each frame. If *f * is ``None ``, the current stack is
211220 used. This helper is used with :meth: `StackSummary.extract `.
212221
@@ -222,12 +231,12 @@ The module defines the following functions:
222231
223232The module also defines the following classes:
224233
225- :class: `TracebackException ` Objects
226- -----------------------------------
234+ :class: `! TracebackException ` Objects
235+ ------------------------------------
227236
228237.. versionadded :: 3.5
229238
230- :class: `TracebackException ` objects are created from actual exceptions to
239+ :class: `! TracebackException ` objects are created from actual exceptions to
231240capture data for later printing in a lightweight fashion.
232241
233242.. 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.
379388 well, recursively, with indentation relative to their nesting depth.
380389
381390 .. 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.
383393
384394 .. versionchanged :: 3.13
385395 Added the *show_group * parameter.
386396
387397
388- :class: `StackSummary ` Objects
389- -----------------------------
398+ :class: `! StackSummary ` Objects
399+ ------------------------------
390400
391401.. versionadded :: 3.5
392402
393- :class: `StackSummary ` objects represent a call stack ready for formatting.
403+ :class: `! StackSummary ` objects represent a call stack ready for formatting.
394404
395405.. class :: StackSummary
396406
397407 .. classmethod :: extract(frame_gen, *, limit=None, lookup_lines=True, capture_locals=False)
398408
399- Construct a :class: `StackSummary ` object from a frame generator (such as
409+ Construct a :class: `! StackSummary ` object from a frame generator (such as
400410 is returned by :func: `~traceback.walk_stack ` or
401411 :func: `~traceback.walk_tb `).
402412
403413 If *limit * is supplied, only this many frames are taken from *frame_gen *.
404414 If *lookup_lines * is ``False ``, the returned :class: `FrameSummary `
405415 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
407417 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
409419 representations.
410420
411421 .. versionchanged :: 3.12
@@ -414,14 +424,16 @@ capture data for later printing in a lightweight fashion.
414424
415425 .. classmethod :: from_list(a_list)
416426
417- Construct a :class: `StackSummary ` object from a supplied list of
427+ Construct a :class: `! StackSummary ` object from a supplied list of
418428 :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.
420431
421432 .. method :: format()
422433
423434 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.
425437 Each string ends in a newline; the strings may contain internal
426438 newlines as well, for those items with source text lines.
427439
@@ -434,33 +446,59 @@ capture data for later printing in a lightweight fashion.
434446
435447 .. method :: format_frame_summary(frame_summary)
436448
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.
438451 This method is called for each :class: `FrameSummary ` object to be
439452 printed by :meth: `StackSummary.format `. If it returns ``None ``, the
440453 frame is omitted from the output.
441454
442455 .. versionadded :: 3.11
443456
444457
445- :class: `FrameSummary ` Objects
446- -----------------------------
458+ :class: `! FrameSummary ` Objects
459+ ------------------------------
447460
448461.. versionadded :: 3.5
449462
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 >`.
451465
452466.. class :: FrameSummary(filename, lineno, name, lookup_line=True, locals=None, line=None)
453467
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
456471 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 ` ).
459474 :attr: `~FrameSummary.line ` may be directly provided, and will prevent line
460475 lookups happening at all. *locals * is an optional local variable
461476 dictionary, and if supplied the variable representations are stored in the
462477 summary for later display.
463478
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+
464502.. _traceback-example :
465503
466504Traceback Examples
0 commit comments