Skip to content

Commit 0651ed2

Browse files
committed
show_source_lines
1 parent 63113e7 commit 0651ed2

File tree

1 file changed

+52
-42
lines changed

1 file changed

+52
-42
lines changed

Lib/traceback.py

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@
2626
#
2727

2828

29-
def print_list(extracted_list, file=None, *, show_lines=True):
29+
def print_list(extracted_list, file=None, *, show_source_lines=True):
3030
"""Print the list of tuples as returned by extract_tb() or
3131
extract_stack() as a formatted stack trace to the given file.
3232
3333
To print in "recent call first" order, call "extracted_list.reverse()"
3434
before passing it to this function.
3535
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.
3737
"""
3838
if file is None:
3939
file = sys.stderr
4040
for item in StackSummary.from_list(extracted_list).format(
41-
show_lines=show_lines):
41+
show_source_lines=show_source_lines):
4242
print(item, file=file, end="")
4343

44-
def format_list(extracted_list, *, show_lines=True):
44+
def format_list(extracted_list, *, show_source_lines=True):
4545
"""Format a list of tuples or FrameSummary objects for printing.
4646
4747
Given a list of tuples or FrameSummary objects as returned by
@@ -51,38 +51,40 @@ def format_list(extracted_list, *, show_lines=True):
5151
Each string ends in a newline; the strings may contain internal newlines as
5252
well, for those items whose source text line is not None.
5353
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.
5555
"""
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)
5758

5859
#
5960
# Printing and Extracting Tracebacks.
6061
#
6162

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):
6365
"""Print up to 'limit' stack trace entries from the traceback 'tb'.
6466
6567
If 'limit' is omitted or None, all entries are printed. If 'file'
6668
is omitted or None, the output goes to sys.stderr; otherwise
6769
'file' should be an open file or file-like object with a write()
6870
method.
6971
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.
7173
If 'recent_first' is true, the stack trace is printed in "most recent call
7274
first" order.
7375
"""
7476
tblist = extract_tb(tb, limit=limit)
7577
if recent_first:
7678
tblist.reverse()
77-
print_list(tblist, file=file, show_lines=show_lines)
79+
print_list(tblist, file=file, show_source_lines=show_source_lines)
7880

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):
8082
"""A shorthand for 'format_list(extract_tb(tb, limit))'."""
8183

8284
tblist = extract_tb(tb, limit=limit)
8385
if recent_first:
8486
tblist.reverse()
85-
return tblist.format(show_lines=show_lines)
87+
return tblist.format(show_source_lines=show_source_lines)
8688

8789
def extract_tb(tb, limit=None):
8890
"""
@@ -135,7 +137,7 @@ def _parse_value_tb(exc, value, tb):
135137

136138

137139
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,
139141
recent_first=False, **kwargs):
140142
"""Print exception up to 'limit' stack trace entries from 'tb' to 'file'.
141143
@@ -147,15 +149,15 @@ def print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None,
147149
occurred with a caret on the next line indicating the approximate
148150
position of the error.
149151
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.
151153
If 'recent_first' is true, exception is printed first and traceback is shown
152154
by "most recent call first" order.
153155
"""
154156
colorize = kwargs.get("colorize", False)
155157
value, tb = _parse_value_tb(exc, value, tb)
156158
te = TracebackException(type(value), value, tb, limit=limit, compact=True)
157159
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)
159161

160162

161163
BUILTIN_EXCEPTION_LIMIT = object()
@@ -167,8 +169,8 @@ def _print_exception_bltin(exc, /):
167169
return print_exception(exc, limit=BUILTIN_EXCEPTION_LIMIT, file=file, colorize=colorize)
168170

169171

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):
172174
"""Format a stack trace and the exception information.
173175
174176
The arguments have the same meaning as the corresponding arguments
@@ -181,7 +183,8 @@ def format_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None,
181183
value, tb = _parse_value_tb(exc, value, tb)
182184
te = TracebackException(type(value), value, tb, limit=limit, compact=True)
183185
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))
185188

186189

187190
def format_exception_only(exc, /, value=_sentinel, *, show_group=False, **kwargs):
@@ -230,63 +233,64 @@ def _safe_string(value, what, func=str):
230233

231234
# --
232235

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,
234237
recent_first=False):
235238
"""Shorthand for 'print_exception(sys.exception(), limit=limit, file=file, chain=chain, ...)'."""
236239
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)
238241

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):
240243
"""Like print_exc() but return a string."""
241244
return "".join(format_exception(
242245
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))
244247

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,
246249
recent_first=False):
247250
"""This is a shorthand for 'print_exception(sys.last_exc, limit=limit, file=file, chain=chain, ...)'."""
248251
if not hasattr(sys, "last_exc") and not hasattr(sys, "last_type"):
249252
raise ValueError("no last exception")
250253

251254
if hasattr(sys, "last_exc"):
252255
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)
254257
else:
255258
print_exception(sys.last_type, sys.last_value, sys.last_traceback,
256259
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)
258261

259262

260263
#
261264
# Printing and Extracting Stacks.
262265
#
263266

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):
265269
"""Print a stack trace from its invocation point.
266270
267271
The optional 'f' argument can be used to specify an alternate
268272
stack frame at which to start. The optional 'limit' and 'file'
269273
arguments have the same meaning as for print_exception().
270274
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.
272276
If 'recent_first' is true, stack is printed by "most recent call first" order.
273277
"""
274278
if f is None:
275279
f = sys._getframe().f_back
276280
stack = extract_stack(f, limit=limit)
277281
if recent_first:
278282
stack.reverse()
279-
print_list(stack, file=file, show_lines=show_lines)
283+
print_list(stack, file=file, show_source_lines=show_source_lines)
280284

281285

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)'."""
284288
if f is None:
285289
f = sys._getframe().f_back
286290
stack = extract_stack(f, limit=limit)
287291
if recent_first:
288292
stack.reverse()
289-
return format_list(stack, show_lines=show_lines)
293+
return format_list(stack, show_source_lines=show_source_lines)
290294

291295

292296
def extract_stack(f=None, limit=None):
@@ -573,7 +577,7 @@ def from_list(klass, a_list):
573577
result.append(FrameSummary(filename, lineno, name, line=line))
574578
return result
575579

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):
577581
"""Format the lines for a single FrameSummary.
578582
579583
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):
601605
theme.reset,
602606
)
603607
)
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()):
605610
if (
606611
frame_summary.colno is None or
607612
frame_summary.end_colno is None
@@ -790,7 +795,7 @@ def _spawns_full_line(value):
790795
return True
791796
return False
792797

793-
def format(self, *, show_lines=True, **kwargs):
798+
def format(self, *, show_source_lines=True, **kwargs):
794799
"""Format the stack ready for printing.
795800
796801
Returns a list of strings ready for printing. Each string in the
@@ -809,7 +814,8 @@ def format(self, *, show_lines=True, **kwargs):
809814
last_name = None
810815
count = 0
811816
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)
813819
if formatted_frame is None:
814820
continue
815821
if (last_file is None or last_file != frame_summary.filename or
@@ -1513,7 +1519,8 @@ def _format_syntax_error(self, stype, **kwargs):
15131519
filename_suffix,
15141520
)
15151521

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):
15171524
"""Format the exception.
15181525
15191526
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,
15561563
if not recent_first and exc.stack:
15571564
yield from _ctx.emit('Traceback (most recent call last):\n')
15581565
yield from _ctx.emit(exc.stack.format(
1559-
show_lines=show_lines, colorize=colorize))
1566+
show_source_lines=show_source_lines, colorize=colorize))
15601567
yield from _ctx.emit(exc.format_exception_only(colorize=colorize))
15611568
if recent_first and exc.stack:
15621569
yield from _ctx.emit('Traceback (most recent call first):\n')
15631570
reversed_stack = StackSummary(reversed(self.stack))
15641571
yield from _ctx.emit(reversed_stack.format(
1565-
show_lines=show_lines, colorize=colorize))
1572+
show_source_lines=show_source_lines, colorize=colorize))
15661573
elif _ctx.exception_group_depth > self.max_group_depth:
15671574
# exception group, but depth exceeds limit
15681575
yield from _ctx.emit(
@@ -1578,7 +1585,7 @@ def format(self, *, chain=True, show_lines=True, _ctx=None, recent_first=False,
15781585
'Exception Group Traceback (most recent call last):\n',
15791586
margin_char = '+' if is_toplevel else None)
15801587
yield from _ctx.emit(exc.stack.format(
1581-
show_lines=show_lines, colorize=colorize))
1588+
show_source_lines=show_source_lines, colorize=colorize))
15821589

15831590
yield from _ctx.emit(exc.format_exception_only(colorize=colorize))
15841591
if recent_first and exc.stack:
@@ -1587,7 +1594,7 @@ def format(self, *, chain=True, show_lines=True, _ctx=None, recent_first=False,
15871594
margin_char = '+' if is_toplevel else None)
15881595
reversed_stack = StackSummary(reversed(self.stack))
15891596
yield from _ctx.emit(reversed_stack.format(
1590-
show_lines=show_lines, colorize=colorize))
1597+
show_source_lines=show_source_lines, colorize=colorize))
15911598

15921599
num_excs = len(exc.exceptions)
15931600
if num_excs <= self.max_group_width:
@@ -1611,7 +1618,9 @@ def format(self, *, chain=True, show_lines=True, _ctx=None, recent_first=False,
16111618
f'+---------------- {title} ----------------\n')
16121619
_ctx.exception_group_depth += 1
16131620
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)
16151624
else:
16161625
remaining = num_excs - self.max_group_width
16171626
plural = 's' if remaining > 1 else ''
@@ -1629,12 +1638,13 @@ def format(self, *, chain=True, show_lines=True, _ctx=None, recent_first=False,
16291638
_ctx.exception_group_depth = 0
16301639

16311640

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):
16331643
"""Print the result of self.format(chain=chain) to 'file'."""
16341644
colorize = kwargs.get("colorize", False)
16351645
if file is None:
16361646
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,
16381648
recent_first=recent_first, colorize=colorize):
16391649
print(line, file=file, end="")
16401650

0 commit comments

Comments
 (0)