@@ -52,7 +52,7 @@ The module's API can be divided into two parts:
52
52
Module-Level Functions
53
53
----------------------
54
54
55
- .. function :: print_tb(tb, limit=None, file=None)
55
+ .. function :: print_tb(tb, limit=None, file=None, *, show_lines=True, recent_first=False )
56
56
57
57
Print up to *limit * stack trace entries from
58
58
:ref: `traceback object <traceback-objects >` *tb * (starting
@@ -63,6 +63,19 @@ Module-Level Functions
63
63
:term: `file <file object> ` or :term: `file-like object ` to
64
64
receive the output.
65
65
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
+
66
79
.. note ::
67
80
68
81
The meaning of the *limit * parameter is different than the meaning
@@ -74,9 +87,12 @@ Module-Level Functions
74
87
.. versionchanged :: 3.5
75
88
Added negative *limit * support.
76
89
90
+ .. versionchanged :: next
91
+ Added *show_lines * and *recent_first * parameters.
92
+
77
93
78
94
.. function :: print_exception(exc, /[, value, tb], limit=None, \
79
- file=None, chain=True)
95
+ file=None, chain=True, * , show_lines=True, recent_first=False )
80
96
81
97
Print exception information and stack trace entries from
82
98
:ref: `traceback object <traceback-objects >`
@@ -103,7 +119,13 @@ Module-Level Functions
103
119
:attr: `~BaseException.__cause__ ` or :attr: `~BaseException.__context__ `
104
120
attributes of the exception) will be
105
121
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.
107
129
108
130
.. versionchanged :: 3.5
109
131
The *etype * argument is ignored and inferred from the type of *value *.
@@ -112,32 +134,46 @@ Module-Level Functions
112
134
The *etype * parameter has been renamed to *exc * and is now
113
135
positional-only.
114
136
137
+ .. versionchanged :: next
138
+ Added *show_lines * and *recent_first * parameters.
115
139
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)
117
142
118
143
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.
120
148
121
149
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 )
123
151
124
152
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 `).
127
156
157
+ .. versionchanged :: next
158
+ Added *show_lines * and *recent_first * parameters.
128
159
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)
130
162
131
163
Print up to *limit * stack trace entries (starting from the invocation
132
164
point) if *limit * is positive. Otherwise, print the last ``abs(limit) ``
133
165
entries. If *limit * is omitted or ``None ``, all entries are printed.
134
166
The optional *f * argument can be used to specify an alternate
135
167
:ref: `stack frame <frame-objects >`
136
168
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.
138
171
139
172
.. 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.
141
177
142
178
143
179
.. function :: extract_tb(tb, limit=None)
@@ -161,21 +197,29 @@ Module-Level Functions
161
197
arguments have the same meaning as for :func: `print_stack `.
162
198
163
199
164
- .. function :: print_list(extracted_list, file=None)
200
+ .. function :: print_list(extracted_list, file=None, *, show_lines=True )
165
201
166
202
Print the list of tuples as returned by :func: `extract_tb ` or
167
203
:func: `extract_stack ` as a formatted stack trace to the given file.
168
204
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.
169
206
207
+ .. versionchanged :: next
208
+ Added *show_lines * parameter.
170
209
171
- .. function :: format_list(extracted_list)
210
+
211
+ .. function :: format_list(extracted_list, *, show_lines=True)
172
212
173
213
Given a list of tuples or :class: `FrameSummary ` objects as returned by
174
214
:func: `extract_tb ` or :func: `extract_stack `, return a list of strings ready
175
215
for printing. Each string in the resulting list corresponds to the item with
176
216
the same index in the argument list. Each string ends in a newline; the
177
217
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.
179
223
180
224
181
225
.. function :: format_exception_only(exc, /[, value], *, show_group=False)
@@ -208,36 +252,60 @@ Module-Level Functions
208
252
*show_group * parameter was added.
209
253
210
254
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 )
212
256
213
257
Format a stack trace and the exception information. The arguments have the
214
258
same meaning as the corresponding arguments to :func: `print_exception `. The
215
259
return value is a list of strings, each ending in a newline and some
216
260
containing internal newlines. When these lines are concatenated and printed,
217
261
exactly the same text is printed as does :func: `print_exception `.
218
262
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
+
219
267
.. versionchanged :: 3.5
220
268
The *etype * argument is ignored and inferred from the type of *value *.
221
269
222
270
.. versionchanged :: 3.10
223
271
This function's behavior and signature were modified to match
224
272
:func: `print_exception `.
225
273
274
+ .. versionchanged :: next
275
+ Added *show_lines * and *recent_first * parameters.
276
+
226
277
227
- .. function :: format_exc(limit=None, chain=True)
278
+ .. function :: format_exc(limit=None, chain=True, *, show_lines=True, recent_first=False )
228
279
229
280
This is like ``print_exc(limit) `` but returns a string instead of printing to
230
281
a file.
231
282
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.
232
286
233
- .. function :: format_tb(tb, limit=None)
287
+ .. versionchanged :: next
288
+ Added *show_lines * and *recent_first * parameters.
234
289
235
- A shorthand for ``format_list(extract_tb(tb, limit)) ``.
236
290
291
+ .. function :: format_tb(tb, limit=None, *, show_lines=True, recent_first=False)
237
292
238
- .. function :: format_stack(f=None , limit=None)
293
+ A shorthand for `` format_list(extract_tb(tb , limit), show_lines=show_lines) ``.
239
294
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.
241
309
242
310
.. function :: clear_frames(tb)
243
311
@@ -398,14 +466,24 @@ the module-level functions described above.
398
466
399
467
Note that when locals are captured, they are also shown in the traceback.
400
468
401
- .. method :: print(*, file=None, chain=True)
469
+ .. method :: print(*, file=None, chain=True, show_lines=True, recent_first=False )
402
470
403
471
Print to *file * (default ``sys.stderr ``) the exception information returned by
404
472
:meth: `format `.
405
473
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
+
406
481
.. versionadded :: 3.11
407
482
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)
409
487
410
488
Format the exception.
411
489
@@ -416,6 +494,16 @@ the module-level functions described above.
416
494
some containing internal newlines. :func: `~traceback.print_exception `
417
495
is a wrapper around this method which just prints the lines to a file.
418
496
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
+
419
507
.. method :: format_exception_only(*, show_group=False)
420
508
421
509
Format the exception part of the traceback.
@@ -474,7 +562,7 @@ the module-level functions described above.
474
562
should be a 4-tuple with *filename *, *lineno *, *name *, *line * as the
475
563
elements.
476
564
477
- .. method :: format()
565
+ .. method :: format(*, show_lines=True )
478
566
479
567
Returns a list of strings ready for printing. Each string in the
480
568
resulting list corresponds to a single :ref: `frame <frame-objects >` from
@@ -486,19 +574,31 @@ the module-level functions described above.
486
574
repetitions are shown, followed by a summary line stating the exact
487
575
number of further repetitions.
488
576
577
+ The keyword argument *show_lines *, if ``False ``, prevents source code
578
+ lines from being included in the output.
579
+
489
580
.. versionchanged :: 3.6
490
581
Long sequences of repeated frames are now abbreviated.
491
582
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)
493
587
494
588
Returns a string for printing one of the :ref: `frames <frame-objects >`
495
589
involved in the stack.
496
590
This method is called for each :class: `FrameSummary ` object to be
497
591
printed by :meth: `StackSummary.format `. If it returns ``None ``, the
498
592
frame is omitted from the output.
499
593
594
+ The keyword argument *show_lines *, if ``False ``, prevents source code
595
+ lines from being included in the output.
596
+
500
597
.. versionadded :: 3.11
501
598
599
+ .. versionchanged :: next
600
+ Added the *show_lines * parameter.
601
+
502
602
503
603
:class: `!FrameSummary ` Objects
504
604
------------------------------
0 commit comments