26
26
#
27
27
28
28
29
- def print_list (extracted_list , file = None , * , show_lines = True ):
29
+ def print_list (extracted_list , file = None , * , show_source_lines = True ):
30
30
"""Print the list of tuples as returned by extract_tb() or
31
31
extract_stack() as a formatted stack trace to the given file.
32
32
33
33
To print in "recent call first" order, call "extracted_list.reverse()"
34
34
before passing it to this function.
35
35
36
- If 'show_lines ' is true, source code lines are included in the output.
36
+ If 'show_source_lines ' is true, source code lines are included in the output.
37
37
"""
38
38
if file is None :
39
39
file = sys .stderr
40
40
for item in StackSummary .from_list (extracted_list ).format (
41
- show_lines = show_lines ):
41
+ show_source_lines = show_source_lines ):
42
42
print (item , file = file , end = "" )
43
43
44
- def format_list (extracted_list , * , show_lines = True ):
44
+ def format_list (extracted_list , * , show_source_lines = True ):
45
45
"""Format a list of tuples or FrameSummary objects for printing.
46
46
47
47
Given a list of tuples or FrameSummary objects as returned by
@@ -51,38 +51,40 @@ def format_list(extracted_list, *, show_lines=True):
51
51
Each string ends in a newline; the strings may contain internal newlines as
52
52
well, for those items whose source text line is not None.
53
53
54
- If 'show_lines ' is true, source code lines are included in the output.
54
+ If 'show_source_lines ' is true, source code lines are included in the output.
55
55
"""
56
- return StackSummary .from_list (extracted_list ).format (show_lines = show_lines )
56
+ return StackSummary .from_list (extracted_list ).format (
57
+ show_source_lines = show_source_lines )
57
58
58
59
#
59
60
# Printing and Extracting Tracebacks.
60
61
#
61
62
62
- def print_tb (tb , limit = None , file = None , * , show_lines = True , recent_first = False ):
63
+ def print_tb (tb , limit = None , file = None , * ,
64
+ show_source_lines = True , recent_first = False ):
63
65
"""Print up to 'limit' stack trace entries from the traceback 'tb'.
64
66
65
67
If 'limit' is omitted or None, all entries are printed. If 'file'
66
68
is omitted or None, the output goes to sys.stderr; otherwise
67
69
'file' should be an open file or file-like object with a write()
68
70
method.
69
71
70
- If 'show_lines ' is true, source code lines are included in the output.
72
+ If 'show_source_lines ' is true, source code lines are included in the output.
71
73
If 'recent_first' is true, the stack trace is printed in "most recent call
72
74
first" order.
73
75
"""
74
76
tblist = extract_tb (tb , limit = limit )
75
77
if recent_first :
76
78
tblist .reverse ()
77
- print_list (tblist , file = file , show_lines = show_lines )
79
+ print_list (tblist , file = file , show_source_lines = show_source_lines )
78
80
79
- def format_tb (tb , limit = None , * , show_lines = True , recent_first = False ):
81
+ def format_tb (tb , limit = None , * , show_source_lines = True , recent_first = False ):
80
82
"""A shorthand for 'format_list(extract_tb(tb, limit))'."""
81
83
82
84
tblist = extract_tb (tb , limit = limit )
83
85
if recent_first :
84
86
tblist .reverse ()
85
- return tblist .format (show_lines = show_lines )
87
+ return tblist .format (show_source_lines = show_source_lines )
86
88
87
89
def extract_tb (tb , limit = None ):
88
90
"""
@@ -135,7 +137,7 @@ def _parse_value_tb(exc, value, tb):
135
137
136
138
137
139
def print_exception (exc , / , value = _sentinel , tb = _sentinel , limit = None ,
138
- file = None , chain = True , * , show_lines = True ,
140
+ file = None , chain = True , * , show_source_lines = True ,
139
141
recent_first = False , ** kwargs ):
140
142
"""Print exception up to 'limit' stack trace entries from 'tb' to 'file'.
141
143
@@ -147,15 +149,15 @@ def print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None,
147
149
occurred with a caret on the next line indicating the approximate
148
150
position of the error.
149
151
150
- If 'show_lines ' is true, source code lines are included in the output.
152
+ If 'show_source_lines ' is true, source code lines are included in the output.
151
153
If 'recent_first' is true, exception is printed first and traceback is shown
152
154
by "most recent call first" order.
153
155
"""
154
156
colorize = kwargs .get ("colorize" , False )
155
157
value , tb = _parse_value_tb (exc , value , tb )
156
158
te = TracebackException (type (value ), value , tb , limit = limit , compact = True )
157
159
te .print (file = file , chain = chain , colorize = colorize ,
158
- show_lines = show_lines , recent_first = recent_first )
160
+ show_source_lines = show_source_lines , recent_first = recent_first )
159
161
160
162
161
163
BUILTIN_EXCEPTION_LIMIT = object ()
@@ -167,8 +169,8 @@ def _print_exception_bltin(exc, /):
167
169
return print_exception (exc , limit = BUILTIN_EXCEPTION_LIMIT , file = file , colorize = colorize )
168
170
169
171
170
- def format_exception (exc , / , value = _sentinel , tb = _sentinel , limit = None ,
171
- chain = True , * , show_lines = True , recent_first = False , ** kwargs ):
172
+ def format_exception (exc , / , value = _sentinel , tb = _sentinel , limit = None , chain = True ,
173
+ * , show_source_lines = True , recent_first = False , ** kwargs ):
172
174
"""Format a stack trace and the exception information.
173
175
174
176
The arguments have the same meaning as the corresponding arguments
@@ -181,7 +183,8 @@ def format_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None,
181
183
value , tb = _parse_value_tb (exc , value , tb )
182
184
te = TracebackException (type (value ), value , tb , limit = limit , compact = True )
183
185
return list (te .format (chain = chain , colorize = colorize ,
184
- show_lines = show_lines , recent_first = recent_first ))
186
+ show_source_lines = show_source_lines ,
187
+ recent_first = recent_first ))
185
188
186
189
187
190
def format_exception_only (exc , / , value = _sentinel , * , show_group = False , ** kwargs ):
@@ -230,63 +233,64 @@ def _safe_string(value, what, func=str):
230
233
231
234
# --
232
235
233
- def print_exc (limit = None , file = None , chain = True , * , show_lines = True ,
236
+ def print_exc (limit = None , file = None , chain = True , * , show_source_lines = True ,
234
237
recent_first = False ):
235
238
"""Shorthand for 'print_exception(sys.exception(), limit=limit, file=file, chain=chain, ...)'."""
236
239
print_exception (sys .exception (), limit = limit , file = file , chain = chain ,
237
- show_lines = show_lines , recent_first = recent_first )
240
+ show_source_lines = show_source_lines , recent_first = recent_first )
238
241
239
- def format_exc (limit = None , chain = True , * , show_lines = True , recent_first = False ):
242
+ def format_exc (limit = None , chain = True , * , show_source_lines = True , recent_first = False ):
240
243
"""Like print_exc() but return a string."""
241
244
return "" .join (format_exception (
242
245
sys .exception (), limit = limit , chain = chain ,
243
- show_lines = show_lines , recent_first = recent_first ))
246
+ show_source_lines = show_source_lines , recent_first = recent_first ))
244
247
245
- def print_last (limit = None , file = None , chain = True , * , show_lines = True ,
248
+ def print_last (limit = None , file = None , chain = True , * , show_source_lines = True ,
246
249
recent_first = False ):
247
250
"""This is a shorthand for 'print_exception(sys.last_exc, limit=limit, file=file, chain=chain, ...)'."""
248
251
if not hasattr (sys , "last_exc" ) and not hasattr (sys , "last_type" ):
249
252
raise ValueError ("no last exception" )
250
253
251
254
if hasattr (sys , "last_exc" ):
252
255
print_exception (sys .last_exc , limit = limit , file = file , chain = chain ,
253
- show_lines = show_lines , recent_first = recent_first )
256
+ show_source_lines = show_source_lines , recent_first = recent_first )
254
257
else :
255
258
print_exception (sys .last_type , sys .last_value , sys .last_traceback ,
256
259
limit = limit , file = file , chain = chain ,
257
- show_lines = show_lines , recent_first = recent_first )
260
+ show_source_lines = show_source_lines , recent_first = recent_first )
258
261
259
262
260
263
#
261
264
# Printing and Extracting Stacks.
262
265
#
263
266
264
- def print_stack (f = None , limit = None , file = None , * , show_lines = True , recent_first = False ):
267
+ def print_stack (f = None , limit = None , file = None , * , show_source_lines = True ,
268
+ recent_first = False ):
265
269
"""Print a stack trace from its invocation point.
266
270
267
271
The optional 'f' argument can be used to specify an alternate
268
272
stack frame at which to start. The optional 'limit' and 'file'
269
273
arguments have the same meaning as for print_exception().
270
274
271
- If 'show_lines ' is true, source code lines are included in the output.
275
+ If 'show_source_lines ' is true, source code lines are included in the output.
272
276
If 'recent_first' is true, stack is printed by "most recent call first" order.
273
277
"""
274
278
if f is None :
275
279
f = sys ._getframe ().f_back
276
280
stack = extract_stack (f , limit = limit )
277
281
if recent_first :
278
282
stack .reverse ()
279
- print_list (stack , file = file , show_lines = show_lines )
283
+ print_list (stack , file = file , show_source_lines = show_source_lines )
280
284
281
285
282
- def format_stack (f = None , limit = None , * , show_lines = True , recent_first = False ):
283
- """Shorthand for 'format_list(extract_stack(f, limit), show_lines )'."""
286
+ def format_stack (f = None , limit = None , * , show_source_lines = True , recent_first = False ):
287
+ """Shorthand for 'format_list(extract_stack(f, limit), show_source_lines )'."""
284
288
if f is None :
285
289
f = sys ._getframe ().f_back
286
290
stack = extract_stack (f , limit = limit )
287
291
if recent_first :
288
292
stack .reverse ()
289
- return format_list (stack , show_lines = show_lines )
293
+ return format_list (stack , show_source_lines = show_source_lines )
290
294
291
295
292
296
def extract_stack (f = None , limit = None ):
@@ -573,7 +577,7 @@ def from_list(klass, a_list):
573
577
result .append (FrameSummary (filename , lineno , name , line = line ))
574
578
return result
575
579
576
- def format_frame_summary (self , frame_summary , * , show_lines = True , ** kwargs ):
580
+ def format_frame_summary (self , frame_summary , * , show_source_lines = True , ** kwargs ):
577
581
"""Format the lines for a single FrameSummary.
578
582
579
583
Returns a string representing one frame involved in the stack. This
@@ -601,7 +605,8 @@ def format_frame_summary(self, frame_summary, *, show_lines=True, **kwargs):
601
605
theme .reset ,
602
606
)
603
607
)
604
- if show_lines and frame_summary ._dedented_lines and frame_summary ._dedented_lines .strip ():
608
+ if (show_source_lines
609
+ and frame_summary ._dedented_lines and frame_summary ._dedented_lines .strip ()):
605
610
if (
606
611
frame_summary .colno is None or
607
612
frame_summary .end_colno is None
@@ -790,7 +795,7 @@ def _spawns_full_line(value):
790
795
return True
791
796
return False
792
797
793
- def format (self , * , show_lines = True , ** kwargs ):
798
+ def format (self , * , show_source_lines = True , ** kwargs ):
794
799
"""Format the stack ready for printing.
795
800
796
801
Returns a list of strings ready for printing. Each string in the
@@ -809,7 +814,8 @@ def format(self, *, show_lines=True, **kwargs):
809
814
last_name = None
810
815
count = 0
811
816
for frame_summary in self :
812
- formatted_frame = self .format_frame_summary (frame_summary , show_lines = show_lines , colorize = colorize )
817
+ formatted_frame = self .format_frame_summary (
818
+ frame_summary , show_source_lines = show_source_lines , colorize = colorize )
813
819
if formatted_frame is None :
814
820
continue
815
821
if (last_file is None or last_file != frame_summary .filename or
@@ -1513,7 +1519,8 @@ def _format_syntax_error(self, stype, **kwargs):
1513
1519
filename_suffix ,
1514
1520
)
1515
1521
1516
- def format (self , * , chain = True , show_lines = True , _ctx = None , recent_first = False , ** kwargs ):
1522
+ def format (self , * , chain = True , show_source_lines = True , _ctx = None ,
1523
+ recent_first = False , ** kwargs ):
1517
1524
"""Format the exception.
1518
1525
1519
1526
If chain is not *True*, *__cause__* and *__context__* will not be formatted.
@@ -1556,13 +1563,13 @@ def format(self, *, chain=True, show_lines=True, _ctx=None, recent_first=False,
1556
1563
if not recent_first and exc .stack :
1557
1564
yield from _ctx .emit ('Traceback (most recent call last):\n ' )
1558
1565
yield from _ctx .emit (exc .stack .format (
1559
- show_lines = show_lines , colorize = colorize ))
1566
+ show_source_lines = show_source_lines , colorize = colorize ))
1560
1567
yield from _ctx .emit (exc .format_exception_only (colorize = colorize ))
1561
1568
if recent_first and exc .stack :
1562
1569
yield from _ctx .emit ('Traceback (most recent call first):\n ' )
1563
1570
reversed_stack = StackSummary (reversed (self .stack ))
1564
1571
yield from _ctx .emit (reversed_stack .format (
1565
- show_lines = show_lines , colorize = colorize ))
1572
+ show_source_lines = show_source_lines , colorize = colorize ))
1566
1573
elif _ctx .exception_group_depth > self .max_group_depth :
1567
1574
# exception group, but depth exceeds limit
1568
1575
yield from _ctx .emit (
@@ -1578,7 +1585,7 @@ def format(self, *, chain=True, show_lines=True, _ctx=None, recent_first=False,
1578
1585
'Exception Group Traceback (most recent call last):\n ' ,
1579
1586
margin_char = '+' if is_toplevel else None )
1580
1587
yield from _ctx .emit (exc .stack .format (
1581
- show_lines = show_lines , colorize = colorize ))
1588
+ show_source_lines = show_source_lines , colorize = colorize ))
1582
1589
1583
1590
yield from _ctx .emit (exc .format_exception_only (colorize = colorize ))
1584
1591
if recent_first and exc .stack :
@@ -1587,7 +1594,7 @@ def format(self, *, chain=True, show_lines=True, _ctx=None, recent_first=False,
1587
1594
margin_char = '+' if is_toplevel else None )
1588
1595
reversed_stack = StackSummary (reversed (self .stack ))
1589
1596
yield from _ctx .emit (reversed_stack .format (
1590
- show_lines = show_lines , colorize = colorize ))
1597
+ show_source_lines = show_source_lines , colorize = colorize ))
1591
1598
1592
1599
num_excs = len (exc .exceptions )
1593
1600
if num_excs <= self .max_group_width :
@@ -1611,7 +1618,9 @@ def format(self, *, chain=True, show_lines=True, _ctx=None, recent_first=False,
1611
1618
f'+---------------- { title } ----------------\n ' )
1612
1619
_ctx .exception_group_depth += 1
1613
1620
if not truncated :
1614
- yield from exc .exceptions [i ].format (chain = chain , show_lines = show_lines , _ctx = _ctx , colorize = colorize )
1621
+ yield from exc .exceptions [i ].format (
1622
+ chain = chain , show_source_lines = show_source_lines ,
1623
+ _ctx = _ctx , colorize = colorize )
1615
1624
else :
1616
1625
remaining = num_excs - self .max_group_width
1617
1626
plural = 's' if remaining > 1 else ''
@@ -1629,12 +1638,13 @@ def format(self, *, chain=True, show_lines=True, _ctx=None, recent_first=False,
1629
1638
_ctx .exception_group_depth = 0
1630
1639
1631
1640
1632
- def print (self , * , file = None , chain = True , show_lines = True , recent_first = False , ** kwargs ):
1641
+ def print (self , * , file = None , chain = True , show_source_lines = True ,
1642
+ recent_first = False , ** kwargs ):
1633
1643
"""Print the result of self.format(chain=chain) to 'file'."""
1634
1644
colorize = kwargs .get ("colorize" , False )
1635
1645
if file is None :
1636
1646
file = sys .stderr
1637
- for line in self .format (chain = chain , show_lines = show_lines ,
1647
+ for line in self .format (chain = chain , show_source_lines = show_source_lines ,
1638
1648
recent_first = recent_first , colorize = colorize ):
1639
1649
print (line , file = file , end = "" )
1640
1650
0 commit comments