From b26f8e2a14b4fb468a1dba0bb2136aeae9e656e7 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Sun, 26 Mar 2023 11:27:06 -0400 Subject: [PATCH] FormattedExcinfo.get_source: Avoid a crash when source.lines == 0 --- src/_pytest/_code/code.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index e375fb70c44..6bcec2da399 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -743,11 +743,11 @@ def get_source( ) -> List[str]: """Return formatted and marked up source lines.""" lines = [] - if source is None or line_index >= len(source.lines): + if source is not None and line_index < 0: + line_index += len(source) + if source is None or line_index >= len(source.lines) or line_index < 0: source = Source("???") line_index = 0 - if line_index < 0: - line_index += len(source) space_prefix = " " if short: lines.append(space_prefix + source.lines[line_index].strip())