26
26
#
27
27
28
28
29
- def print_list (extracted_list , file = None ):
29
+ def print_list (extracted_list , file = None , * , show_lines = True ):
30
30
"""Print the list of tuples as returned by extract_tb() or
31
- extract_stack() as a formatted stack trace to the given file."""
31
+ extract_stack() as a formatted stack trace to the given file.
32
+
33
+ If show_lines is False, source code lines are not included in the output.
34
+ """
32
35
if file is None :
33
36
file = sys .stderr
34
- for item in StackSummary .from_list (extracted_list ).format ():
37
+ for item in StackSummary .from_list (extracted_list ).format (show_lines = show_lines ):
35
38
print (item , file = file , end = "" )
36
39
37
- def format_list (extracted_list ):
40
+ def format_list (extracted_list , * , show_lines = True ):
38
41
"""Format a list of tuples or FrameSummary objects for printing.
39
42
40
43
Given a list of tuples or FrameSummary objects as returned by
@@ -45,26 +48,33 @@ def format_list(extracted_list):
45
48
same index in the argument list. Each string ends in a newline;
46
49
the strings may contain internal newlines as well, for those items
47
50
whose source text line is not None.
51
+
52
+ If show_lines is False, source code lines are not included in the output.
48
53
"""
49
- return StackSummary .from_list (extracted_list ).format ()
54
+ return StackSummary .from_list (extracted_list ).format (show_lines = show_lines )
50
55
51
56
#
52
57
# Printing and Extracting Tracebacks.
53
58
#
54
59
55
- def print_tb (tb , limit = None , file = None ):
60
+ def print_tb (tb , limit = None , file = None , * , show_lines = True ):
56
61
"""Print up to 'limit' stack trace entries from the traceback 'tb'.
57
62
58
63
If 'limit' is omitted or None, all entries are printed. If 'file'
59
64
is omitted or None, the output goes to sys.stderr; otherwise
60
65
'file' should be an open file or file-like object with a write()
61
66
method.
67
+
68
+ If show_lines is False, source code lines are not included in the output.
62
69
"""
63
- print_list (extract_tb (tb , limit = limit ), file = file )
70
+ print_list (extract_tb (tb , limit = limit ), file = file , show_lines = show_lines )
71
+
72
+ def format_tb (tb , limit = None , * , show_lines = True ):
73
+ """A shorthand for 'format_list(extract_tb(tb, limit))'.
64
74
65
- def format_tb ( tb , limit = None ):
66
- """A shorthand for 'format_list(extract_tb(tb, limit))'."""
67
- return extract_tb (tb , limit = limit ).format ()
75
+ If show_lines is False, source code lines are not included in the output.
76
+ """
77
+ return extract_tb (tb , limit = limit ).format (show_lines = show_lines )
68
78
69
79
def extract_tb (tb , limit = None ):
70
80
"""
@@ -80,7 +90,7 @@ def extract_tb(tb, limit=None):
80
90
whitespace stripped; if the source is not available it is None.
81
91
"""
82
92
return StackSummary ._extract_from_extended_frame_gen (
83
- _walk_tb_with_full_positions (tb ), limit = limit )
93
+ _walk_tb_with_full_positions (tb ), limit = limit , lookup_lines = False )
84
94
85
95
#
86
96
# Exception formatting and output.
@@ -117,7 +127,7 @@ def _parse_value_tb(exc, value, tb):
117
127
118
128
119
129
def print_exception (exc , / , value = _sentinel , tb = _sentinel , limit = None , \
120
- file = None , chain = True , ** kwargs ):
130
+ file = None , chain = True , show_lines = True , ** kwargs ):
121
131
"""Print exception up to 'limit' stack trace entries from 'tb' to 'file'.
122
132
123
133
This differs from print_tb() in the following ways: (1) if
@@ -127,11 +137,14 @@ def print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \
127
137
appropriate format, it prints the line where the syntax error
128
138
occurred with a caret on the next line indicating the approximate
129
139
position of the error.
140
+
141
+ If show_lines is False, source code lines are not included in the output.
130
142
"""
131
143
colorize = kwargs .get ("colorize" , False )
132
144
value , tb = _parse_value_tb (exc , value , tb )
133
- te = TracebackException (type (value ), value , tb , limit = limit , compact = True )
134
- te .print (file = file , chain = chain , colorize = colorize )
145
+ te = TracebackException (type (value ), value , tb , limit = limit , compact = True ,
146
+ lookup_lines = show_lines )
147
+ te .print (file = file , chain = chain , colorize = colorize , show_lines = show_lines )
135
148
136
149
137
150
BUILTIN_EXCEPTION_LIMIT = object ()
@@ -144,19 +157,22 @@ def _print_exception_bltin(exc, /):
144
157
145
158
146
159
def format_exception (exc , / , value = _sentinel , tb = _sentinel , limit = None , \
147
- chain = True , ** kwargs ):
160
+ chain = True , show_lines = True , ** kwargs ):
148
161
"""Format a stack trace and the exception information.
149
162
150
163
The arguments have the same meaning as the corresponding arguments
151
164
to print_exception(). The return value is a list of strings, each
152
165
ending in a newline and some containing internal newlines. When
153
166
these lines are concatenated and printed, exactly the same text is
154
167
printed as does print_exception().
168
+
169
+ If show_lines is False, source code lines are not included in the output.
155
170
"""
156
171
colorize = kwargs .get ("colorize" , False )
157
172
value , tb = _parse_value_tb (exc , value , tb )
158
- te = TracebackException (type (value ), value , tb , limit = limit , compact = True )
159
- return list (te .format (chain = chain , colorize = colorize ))
173
+ te = TracebackException (type (value ), value , tb , limit = limit , compact = True ,
174
+ lookup_lines = show_lines )
175
+ return list (te .format (chain = chain , colorize = colorize , show_lines = show_lines ))
160
176
161
177
162
178
def format_exception_only (exc , / , value = _sentinel , * , show_group = False , ** kwargs ):
@@ -205,47 +221,61 @@ def _safe_string(value, what, func=str):
205
221
206
222
# --
207
223
208
- def print_exc (limit = None , file = None , chain = True ):
209
- """Shorthand for 'print_exception(sys.exception(), limit=limit, file=file, chain=chain)'."""
210
- print_exception (sys .exception (), limit = limit , file = file , chain = chain )
224
+ def print_exc (limit = None , file = None , chain = True , * , show_lines = True ):
225
+ """Shorthand for 'print_exception(sys.exception(), limit=limit, file=file, chain=chain)'.
226
+
227
+ If show_lines is False, source code lines are not included in the output.
228
+ """
229
+ print_exception (sys .exception (), limit = limit , file = file , chain = chain , show_lines = show_lines )
230
+
231
+ def format_exc (limit = None , chain = True , * , show_lines = True ):
232
+ """Like print_exc() but return a string.
211
233
212
- def format_exc (limit = None , chain = True ):
213
- """Like print_exc() but return a string."""
214
- return "" .join (format_exception (sys .exception (), limit = limit , chain = chain ))
234
+ If show_lines is False, source code lines are not included in the output.
235
+ """
236
+ return "" .join (format_exception (sys .exception (), limit = limit , chain = chain , show_lines = show_lines ))
237
+
238
+ def print_last (limit = None , file = None , chain = True , * , show_lines = True ):
239
+ """This is a shorthand for 'print_exception(sys.last_exc, limit=limit, file=file, chain=chain)'.
215
240
216
- def print_last ( limit = None , file = None , chain = True ):
217
- """This is a shorthand for 'print_exception(sys.last_exc, limit=limit, file=file, chain=chain)'."""
241
+ If show_lines is False, source code lines are not included in the output.
242
+ """
218
243
if not hasattr (sys , "last_exc" ) and not hasattr (sys , "last_type" ):
219
244
raise ValueError ("no last exception" )
220
245
221
246
if hasattr (sys , "last_exc" ):
222
- print_exception (sys .last_exc , limit = limit , file = file , chain = chain )
247
+ print_exception (sys .last_exc , limit = limit , file = file , chain = chain , show_lines = show_lines )
223
248
else :
224
249
print_exception (sys .last_type , sys .last_value , sys .last_traceback ,
225
- limit = limit , file = file , chain = chain )
250
+ limit = limit , file = file , chain = chain , show_lines = show_lines )
226
251
227
252
228
253
#
229
254
# Printing and Extracting Stacks.
230
255
#
231
256
232
- def print_stack (f = None , limit = None , file = None ):
257
+ def print_stack (f = None , limit = None , file = None , * , show_lines = True ):
233
258
"""Print a stack trace from its invocation point.
234
259
235
260
The optional 'f' argument can be used to specify an alternate
236
261
stack frame at which to start. The optional 'limit' and 'file'
237
262
arguments have the same meaning as for print_exception().
263
+
264
+ If show_lines is False, source code lines are not included in the output.
238
265
"""
239
266
if f is None :
240
267
f = sys ._getframe ().f_back
241
- print_list (extract_stack (f , limit = limit ), file = file )
268
+ print_list (extract_stack (f , limit = limit ), file = file , show_lines = show_lines )
269
+
242
270
271
+ def format_stack (f = None , limit = None , * , show_lines = True ):
272
+ """Shorthand for 'format_list(extract_stack(f, limit))'.
243
273
244
- def format_stack ( f = None , limit = None ):
245
- """Shorthand for 'format_list(extract_stack(f, limit))'."""
274
+ If show_lines is False, source code lines are not included in the output.
275
+ """
246
276
if f is None :
247
277
f = sys ._getframe ().f_back
248
- return format_list (extract_stack (f , limit = limit ))
278
+ return format_list (extract_stack (f , limit = limit ), show_lines = show_lines )
249
279
250
280
251
281
def extract_stack (f = None , limit = None ):
@@ -259,7 +289,7 @@ def extract_stack(f=None, limit=None):
259
289
"""
260
290
if f is None :
261
291
f = sys ._getframe ().f_back
262
- stack = StackSummary .extract (walk_stack (f ), limit = limit )
292
+ stack = StackSummary .extract (walk_stack (f ), limit = limit , lookup_lines = False )
263
293
stack .reverse ()
264
294
return stack
265
295
@@ -436,7 +466,7 @@ class StackSummary(list):
436
466
437
467
@classmethod
438
468
def extract (klass , frame_gen , * , limit = None , lookup_lines = True ,
439
- capture_locals = False ):
469
+ capture_locals = False ):
440
470
"""Create a StackSummary from a traceback or stack object.
441
471
442
472
:param frame_gen: A generator that yields (frame, lineno) tuples
@@ -525,11 +555,13 @@ def from_list(klass, a_list):
525
555
result .append (FrameSummary (filename , lineno , name , line = line ))
526
556
return result
527
557
528
- def format_frame_summary (self , frame_summary , ** kwargs ):
558
+ def format_frame_summary (self , frame_summary , * , show_lines = True , * *kwargs ):
529
559
"""Format the lines for a single FrameSummary.
530
560
531
561
Returns a string representing one frame involved in the stack. This
532
562
gets called for every frame to be printed in the stack summary.
563
+
564
+ If show_lines is False, source code lines are not included in the output.
533
565
"""
534
566
colorize = kwargs .get ("colorize" , False )
535
567
row = []
@@ -553,7 +585,7 @@ def format_frame_summary(self, frame_summary, **kwargs):
553
585
theme .reset ,
554
586
)
555
587
)
556
- if frame_summary ._dedented_lines and frame_summary ._dedented_lines .strip ():
588
+ if show_lines and frame_summary ._dedented_lines and frame_summary ._dedented_lines .strip ():
557
589
if (
558
590
frame_summary .colno is None or
559
591
frame_summary .end_colno is None
@@ -742,7 +774,7 @@ def _spawns_full_line(value):
742
774
return True
743
775
return False
744
776
745
- def format (self , ** kwargs ):
777
+ def format (self , * , show_lines = True , * *kwargs ):
746
778
"""Format the stack ready for printing.
747
779
748
780
Returns a list of strings ready for printing. Each string in the
@@ -753,6 +785,8 @@ def format(self, **kwargs):
753
785
For long sequences of the same frame and line, the first few
754
786
repetitions are shown, followed by a summary line stating the exact
755
787
number of further repetitions.
788
+
789
+ If show_lines is False, source code lines are not included in the output.
756
790
"""
757
791
colorize = kwargs .get ("colorize" , False )
758
792
result = []
@@ -761,7 +795,7 @@ def format(self, **kwargs):
761
795
last_name = None
762
796
count = 0
763
797
for frame_summary in self :
764
- formatted_frame = self .format_frame_summary (frame_summary , colorize = colorize )
798
+ formatted_frame = self .format_frame_summary (frame_summary , show_lines = show_lines , colorize = colorize )
765
799
if formatted_frame is None :
766
800
continue
767
801
if (last_file is None or last_file != frame_summary .filename or
@@ -1465,7 +1499,7 @@ def _format_syntax_error(self, stype, **kwargs):
1465
1499
filename_suffix ,
1466
1500
)
1467
1501
1468
- def format (self , * , chain = True , _ctx = None , ** kwargs ):
1502
+ def format (self , * , chain = True , show_lines = True , _ctx = None , ** kwargs ):
1469
1503
"""Format the exception.
1470
1504
1471
1505
If chain is not *True*, *__cause__* and *__context__* will not be formatted.
@@ -1476,6 +1510,8 @@ def format(self, *, chain=True, _ctx=None, **kwargs):
1476
1510
1477
1511
The message indicating which exception occurred is always the last
1478
1512
string in the output.
1513
+
1514
+ If show_lines is False, source code lines are not included in the output.
1479
1515
"""
1480
1516
colorize = kwargs .get ("colorize" , False )
1481
1517
if _ctx is None :
@@ -1507,7 +1543,7 @@ def format(self, *, chain=True, _ctx=None, **kwargs):
1507
1543
if exc .exceptions is None :
1508
1544
if exc .stack :
1509
1545
yield from _ctx .emit ('Traceback (most recent call last):\n ' )
1510
- yield from _ctx .emit (exc .stack .format (colorize = colorize ))
1546
+ yield from _ctx .emit (exc .stack .format (show_lines = show_lines , colorize = colorize ))
1511
1547
yield from _ctx .emit (exc .format_exception_only (colorize = colorize ))
1512
1548
elif _ctx .exception_group_depth > self .max_group_depth :
1513
1549
# exception group, but depth exceeds limit
@@ -1523,7 +1559,7 @@ def format(self, *, chain=True, _ctx=None, **kwargs):
1523
1559
yield from _ctx .emit (
1524
1560
'Exception Group Traceback (most recent call last):\n ' ,
1525
1561
margin_char = '+' if is_toplevel else None )
1526
- yield from _ctx .emit (exc .stack .format (colorize = colorize ))
1562
+ yield from _ctx .emit (exc .stack .format (show_lines = show_lines , colorize = colorize ))
1527
1563
1528
1564
yield from _ctx .emit (exc .format_exception_only (colorize = colorize ))
1529
1565
num_excs = len (exc .exceptions )
@@ -1548,7 +1584,7 @@ def format(self, *, chain=True, _ctx=None, **kwargs):
1548
1584
f'+---------------- { title } ----------------\n ' )
1549
1585
_ctx .exception_group_depth += 1
1550
1586
if not truncated :
1551
- yield from exc .exceptions [i ].format (chain = chain , _ctx = _ctx , colorize = colorize )
1587
+ yield from exc .exceptions [i ].format (chain = chain , show_lines = show_lines , _ctx = _ctx , colorize = colorize )
1552
1588
else :
1553
1589
remaining = num_excs - self .max_group_width
1554
1590
plural = 's' if remaining > 1 else ''
@@ -1566,12 +1602,15 @@ def format(self, *, chain=True, _ctx=None, **kwargs):
1566
1602
_ctx .exception_group_depth = 0
1567
1603
1568
1604
1569
- def print (self , * , file = None , chain = True , ** kwargs ):
1570
- """Print the result of self.format(chain=chain) to 'file'."""
1605
+ def print (self , * , file = None , chain = True , show_lines = True , ** kwargs ):
1606
+ """Print the result of self.format(chain=chain) to 'file'.
1607
+
1608
+ If show_lines is False, source code lines are not included in the output.
1609
+ """
1571
1610
colorize = kwargs .get ("colorize" , False )
1572
1611
if file is None :
1573
1612
file = sys .stderr
1574
- for line in self .format (chain = chain , colorize = colorize ):
1613
+ for line in self .format (chain = chain , show_lines = show_lines , colorize = colorize ):
1575
1614
print (line , file = file , end = "" )
1576
1615
1577
1616
0 commit comments