@@ -52,7 +52,7 @@ The module's API can be divided into two parts:
5252Module-Level Functions
5353----------------------
5454
55- .. function :: print_tb(tb, limit=None, file=None)
55+ .. function :: print_tb(tb, limit=None, file=None, *, show_lines=True, recent_first=False )
5656
5757 Print up to *limit * stack trace entries from
5858 :ref: `traceback object <traceback-objects >` *tb * (starting
@@ -63,6 +63,19 @@ Module-Level Functions
6363 :term: `file <file object> ` or :term: `file-like object ` to
6464 receive the output.
6565
66+ If *show_lines * is false, source code lines are not included in the output.
67+
68+ If *recent_first * is true, the most recent stack trace entries are printed
69+ first, otherwise the oldest entries are printed first. The default is false.
70+
71+ .. note ::
72+ ``recent_first=True `` is useful for showing stack traces in places where
73+ people see the top of the stack trace first, such as in a web browser.
74+
75+ ``recent_first=False `` is useful for showing stack traces in places where
76+ people see the bottom of the stack trace first, such as a console or log
77+ files watched with :command: `tail -f `.
78+
6679 .. note ::
6780
6881 The meaning of the *limit * parameter is different than the meaning
@@ -74,9 +87,12 @@ Module-Level Functions
7487 .. versionchanged :: 3.5
7588 Added negative *limit * support.
7689
90+ .. versionchanged :: next
91+ Added *show_lines * and *recent_first * parameters.
92+
7793
7894.. function :: print_exception(exc, /[, value, tb], limit=None, \
79- file=None, chain=True)
95+ file=None, chain=True, * , show_lines=True, recent_first=False )
8096
8197 Print exception information and stack trace entries from
8298 :ref: `traceback object <traceback-objects >`
@@ -103,7 +119,13 @@ Module-Level Functions
103119 :attr: `~BaseException.__cause__ ` or :attr: `~BaseException.__context__ `
104120 attributes of the exception) will be
105121 printed as well, like the interpreter itself does when printing an unhandled
106- exception.
122+ exception. If *show_lines * is ``False ``, source code lines are not included
123+ in the output.
124+
125+ If *show_lines * is false, source code lines are not included in the output.
126+
127+ If *recent_first * is true, the most recent stack trace entries are printed
128+ first, otherwise the oldest entries are printed first. The default is false.
107129
108130 .. versionchanged :: 3.5
109131 The *etype * argument is ignored and inferred from the type of *value *.
@@ -112,32 +134,46 @@ Module-Level Functions
112134 The *etype * parameter has been renamed to *exc * and is now
113135 positional-only.
114136
137+ .. versionchanged :: next
138+ Added *show_lines * and *recent_first * parameters.
115139
116- .. function :: print_exc(limit=None, file=None, chain=True)
140+
141+ .. function :: print_exc(limit=None, file=None, chain=True, *, show_lines=True, recent_first=False)
117142
118143 This is a shorthand for ``print_exception(sys.exception(), limit=limit, file=file,
119- chain=chain) ``.
144+ chain=chain, show_lines=show_lines, recent_first=recent_first) ``.
145+
146+ .. versionchanged :: next
147+ Added *show_lines * and *recent_first * parameters.
120148
121149
122- .. function :: print_last(limit=None, file=None, chain=True)
150+ .. function :: print_last(limit=None, file=None, chain=True, *, show_lines=True, recent_first=False )
123151
124152 This is a shorthand for ``print_exception(sys.last_exc, limit=limit, file=file,
125- chain=chain) ``. In general it will work only after an exception has reached
126- an interactive prompt (see :data: `sys.last_exc `).
153+ chain=chain, show_lines=show_lines, recent_first=recent_first) ``.
154+ In general it will work only after an exception has reached an interactive
155+ prompt (see :data: `sys.last_exc `).
127156
157+ .. versionchanged :: next
158+ Added *show_lines * and *recent_first * parameters.
128159
129- .. function :: print_stack(f=None, limit=None, file=None)
160+
161+ .. function :: print_stack(f=None, limit=None, file=None, *, show_lines=True, recent_first=False)
130162
131163 Print up to *limit * stack trace entries (starting from the invocation
132164 point) if *limit * is positive. Otherwise, print the last ``abs(limit) ``
133165 entries. If *limit * is omitted or ``None ``, all entries are printed.
134166 The optional *f * argument can be used to specify an alternate
135167 :ref: `stack frame <frame-objects >`
136168 to start. The optional *file * argument has the same meaning as for
137- :func: `print_tb `.
169+ :func: `print_tb `. If *show_lines * is ``False ``, source code lines are
170+ not included in the output.
138171
139172 .. versionchanged :: 3.5
140- Added negative *limit * support.
173+ Added negative *limit * support.
174+
175+ .. versionchanged :: next
176+ Added *show_lines * and *recent_first * parameters.
141177
142178
143179.. function :: extract_tb(tb, limit=None)
@@ -161,21 +197,29 @@ Module-Level Functions
161197 arguments have the same meaning as for :func: `print_stack `.
162198
163199
164- .. function :: print_list(extracted_list, file=None)
200+ .. function :: print_list(extracted_list, file=None, *, show_lines=True )
165201
166202 Print the list of tuples as returned by :func: `extract_tb ` or
167203 :func: `extract_stack ` as a formatted stack trace to the given file.
168204 If *file * is ``None ``, the output is written to :data: `sys.stderr `.
205+ If *show_lines * is ``False ``, source code lines are not included in the output.
169206
207+ .. versionchanged :: next
208+ Added *show_lines * parameter.
170209
171- .. function :: format_list(extracted_list)
210+
211+ .. function :: format_list(extracted_list, *, show_lines=True)
172212
173213 Given a list of tuples or :class: `FrameSummary ` objects as returned by
174214 :func: `extract_tb ` or :func: `extract_stack `, return a list of strings ready
175215 for printing. Each string in the resulting list corresponds to the item with
176216 the same index in the argument list. Each string ends in a newline; the
177217 strings may contain internal newlines as well, for those items whose source
178- text line is not ``None ``.
218+ text line is not ``None ``. If *show_lines * is ``False ``, source code lines
219+ are not included in the output.
220+
221+ .. versionchanged :: next
222+ Added *show_lines * parameter.
179223
180224
181225.. function :: format_exception_only(exc, /[, value], *, show_group=False)
@@ -208,36 +252,60 @@ Module-Level Functions
208252 *show_group * parameter was added.
209253
210254
211- .. function :: format_exception(exc, /[, value, tb], limit=None, chain=True)
255+ .. function :: format_exception(exc, /[, value, tb], limit=None, chain=True, *, show_lines=True, recent_first=False, show_group=False )
212256
213257 Format a stack trace and the exception information. The arguments have the
214258 same meaning as the corresponding arguments to :func: `print_exception `. The
215259 return value is a list of strings, each ending in a newline and some
216260 containing internal newlines. When these lines are concatenated and printed,
217261 exactly the same text is printed as does :func: `print_exception `.
218262
263+ If *show_lines * is false, source code lines are not included in the output.
264+ If *recent_first * is true, the most recent stack trace entries are printed
265+ first, otherwise the oldest entries are printed first. The default is false.
266+
219267 .. versionchanged :: 3.5
220268 The *etype * argument is ignored and inferred from the type of *value *.
221269
222270 .. versionchanged :: 3.10
223271 This function's behavior and signature were modified to match
224272 :func: `print_exception `.
225273
274+ .. versionchanged :: next
275+ Added *show_lines * and *recent_first * parameters.
276+
226277
227- .. function :: format_exc(limit=None, chain=True)
278+ .. function :: format_exc(limit=None, chain=True, *, show_lines=True, recent_first=False )
228279
229280 This is like ``print_exc(limit) `` but returns a string instead of printing to
230281 a file.
231282
283+ If *show_lines * is false, source code lines are not included in the output.
284+ If *recent_first * is true, the most recent stack trace entries are printed
285+ first, otherwise the oldest entries are printed first. The default is false.
232286
233- .. function :: format_tb(tb, limit=None)
287+ .. versionchanged :: next
288+ Added *show_lines * and *recent_first * parameters.
234289
235- A shorthand for ``format_list(extract_tb(tb, limit)) ``.
236290
291+ .. function :: format_tb(tb, limit=None, *, show_lines=True, recent_first=False)
237292
238- .. function :: format_stack(f=None , limit=None)
293+ A shorthand for `` format_list(extract_tb(tb , limit), show_lines=show_lines) ``.
239294
240- A shorthand for ``format_list(extract_stack(f, limit)) ``.
295+ If *recent_first * is true, ``reversed(extract_tb(tb, limit)) `` is used.
296+
297+ .. versionchanged :: next
298+ Added *show_lines * and *recent_first * parameters.
299+
300+
301+ .. function :: format_stack(f=None, limit=None, *, show_lines=True, recent_first=False)
302+
303+ A shorthand for ``format_list(extract_stack(f, limit), show_lines=show_lines) ``.
304+
305+ If *recent_first * is true, ``reversed(extract_stack(f, limit)) `` is used.
306+
307+ .. versionchanged :: next
308+ Added *show_lines * and *recent_first * parameters.
241309
242310.. function :: clear_frames(tb)
243311
@@ -398,14 +466,24 @@ the module-level functions described above.
398466
399467 Note that when locals are captured, they are also shown in the traceback.
400468
401- .. method :: print(*, file=None, chain=True)
469+ .. method :: print(*, file=None, chain=True, show_lines=True, recent_first=False )
402470
403471 Print to *file * (default ``sys.stderr ``) the exception information returned by
404472 :meth: `format `.
405473
474+ If *show_lines * is false, source code lines from being included in the output.
475+
476+ If *recent_first * is true, the exception is printed first followed by stack
477+ trace by "most recent call first" order.
478+ Otherwise, the stack trace is printed first by "most recent call last" order
479+ followed by the exception.
480+
406481 .. versionadded :: 3.11
407482
408- .. method :: format(*, chain=True)
483+ .. versionchanged :: next
484+ Added *show_lines * and *recent_first * parameters.
485+
486+ .. method :: format(*, chain=True, show_lines=True, recent_first=False, **kwargs)
409487
410488 Format the exception.
411489
@@ -416,6 +494,16 @@ the module-level functions described above.
416494 some containing internal newlines. :func: `~traceback.print_exception `
417495 is a wrapper around this method which just prints the lines to a file.
418496
497+ If *show_lines * is false, source code lines from being included in the output.
498+
499+ If *recent_first * is true, the exception is printed first followed by stack
500+ trace by "most recent call first" order.
501+ Otherwise, the stack trace is printed first by "most recent call last" order
502+ followed by the exception.
503+
504+ .. versionchanged :: next
505+ Added *show_lines * and *recent_first * parameters.
506+
419507 .. method :: format_exception_only(*, show_group=False)
420508
421509 Format the exception part of the traceback.
@@ -474,7 +562,7 @@ the module-level functions described above.
474562 should be a 4-tuple with *filename *, *lineno *, *name *, *line * as the
475563 elements.
476564
477- .. method :: format()
565+ .. method :: format(*, show_lines=True )
478566
479567 Returns a list of strings ready for printing. Each string in the
480568 resulting list corresponds to a single :ref: `frame <frame-objects >` from
@@ -486,19 +574,31 @@ the module-level functions described above.
486574 repetitions are shown, followed by a summary line stating the exact
487575 number of further repetitions.
488576
577+ The keyword argument *show_lines *, if ``False ``, prevents source code
578+ lines from being included in the output.
579+
489580 .. versionchanged :: 3.6
490581 Long sequences of repeated frames are now abbreviated.
491582
492- .. method :: format_frame_summary(frame_summary)
583+ .. versionchanged :: next
584+ Added the *show_lines * parameter.
585+
586+ .. method :: format_frame_summary(frame_summary, *, show_lines=True, **kwargs)
493587
494588 Returns a string for printing one of the :ref: `frames <frame-objects >`
495589 involved in the stack.
496590 This method is called for each :class: `FrameSummary ` object to be
497591 printed by :meth: `StackSummary.format `. If it returns ``None ``, the
498592 frame is omitted from the output.
499593
594+ The keyword argument *show_lines *, if ``False ``, prevents source code
595+ lines from being included in the output.
596+
500597 .. versionadded :: 3.11
501598
599+ .. versionchanged :: next
600+ Added the *show_lines * parameter.
601+
502602
503603:class: `!FrameSummary ` Objects
504604------------------------------
0 commit comments