Skip to content

Commit a13acfa

Browse files
committed
Document that multiline display also needs an end line number
Column position information alone is not enough: end_lineno defaults to lineno, so a frame with colno/end_colno but no end_lineno still shows only its first physical line. Say so, and test it.
1 parent 67bab37 commit a13acfa

3 files changed

Lines changed: 27 additions & 11 deletions

File tree

Doc/library/traceback.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,10 @@ Module-Level Functions
173173
for printing. Each string in the resulting list corresponds to the item with
174174
the same index in the argument list. Each string ends in a newline and may
175175
contain internal newlines. When an item's source text spans multiple
176-
physical lines, only the first physical line is displayed if column position
177-
information is unavailable. Old-style tuples never have column position
178-
information.
176+
physical lines, every line is displayed only if the item has both column
177+
position information and an end line number greater than its line number;
178+
otherwise just the first physical line is displayed. Old-style tuples never
179+
have column position information.
179180

180181

181182
.. function:: format_exception_only(exc, /[, value], *, show_group=False)
@@ -484,8 +485,10 @@ the module-level functions described above.
484485
resulting list corresponds to a single :ref:`frame <frame-objects>` from
485486
the stack.
486487
Each string ends in a newline and may contain internal newlines. When a
487-
frame's source text spans multiple physical lines, only the first physical
488-
line is displayed if column position information is unavailable.
488+
frame's source text spans multiple physical lines, every line is displayed
489+
only if the frame has both column position information and an end line
490+
number greater than its line number; otherwise just the first physical
491+
line is displayed.
489492

490493
For long sequences of the same frame and line, the first few
491494
repetitions are shown, followed by a summary line stating the exact

Lib/test/test_traceback.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3438,6 +3438,17 @@ def test_from_list_multiline_with_columns(self):
34383438
' line 2\n'],
34393439
s.format())
34403440

3441+
def test_from_list_multiline_with_columns_without_end_lineno(self):
3442+
# end_lineno defaults to lineno, so the frame spans a single line
3443+
# and the trailing physical lines are not displayed.
3444+
frame = traceback.FrameSummary(
3445+
'foo.py', 1, 'fred', line='line 1\nline 2',
3446+
colno=0, end_colno=6)
3447+
s = traceback.StackSummary.from_list([frame])
3448+
self.assertEqual(
3449+
[' File "foo.py", line 1, in fred\n line 1\n'],
3450+
s.format())
3451+
34413452
def test_format_list_multiline_old_style_tuple(self):
34423453
frames = [
34433454
('test.py', 10, 'func',

Lib/traceback.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ def format_list(extracted_list):
8585
Each string in the resulting list corresponds to the item with the
8686
same index in the argument list. Each string ends in a newline and may
8787
contain internal newlines. When an item's source text spans multiple
88-
physical lines, only the first physical line is displayed if column
89-
position information is unavailable. Old-style tuples never have column
90-
position information.
88+
physical lines, every line is displayed only if the item has both column
89+
position information and an end line number greater than its line number;
90+
otherwise just the first physical line is displayed. Old-style tuples
91+
never have column position information.
9192
"""
9293
return StackSummary.from_list(extracted_list).format()
9394

@@ -802,9 +803,10 @@ def format(self, **kwargs):
802803
Returns a list of strings ready for printing. Each string in the
803804
resulting list corresponds to a single frame from the stack.
804805
Each string ends in a newline and may contain internal newlines. When
805-
a frame's source text spans multiple physical lines, only the first
806-
physical line is displayed if column position information is
807-
unavailable.
806+
a frame's source text spans multiple physical lines, every line is
807+
displayed only if the frame has both column position information and an
808+
end line number greater than its line number; otherwise just the first
809+
physical line is displayed.
808810
809811
For long sequences of the same frame and line, the first few
810812
repetitions are shown, followed by a summary line stating the exact

0 commit comments

Comments
 (0)