Skip to content

Commit 78fb971

Browse files
committed
Make code.FormattedExcinfo.get_source more defensive
When line_index was a large negative number, get_source failed on `source.lines[line_index]`. Use the same dummy Source as with a large positive line_index.
1 parent 31ae4e1 commit 78fb971

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/_pytest/_code/code.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -721,11 +721,11 @@ def get_source(
721721
) -> List[str]:
722722
"""Return formatted and marked up source lines."""
723723
lines = []
724-
if source is None or line_index >= len(source.lines):
724+
if source is not None and line_index < 0:
725+
line_index += len(source.lines)
726+
if source is None or line_index >= len(source.lines) or line_index < 0:
725727
source = Source("???")
726728
line_index = 0
727-
if line_index < 0:
728-
line_index += len(source)
729729
space_prefix = " "
730730
if short:
731731
lines.append(space_prefix + source.lines[line_index].strip())

0 commit comments

Comments
 (0)