Merge pull request #8227 from encukou/defensive-get_source

Make code.FormattedExcinfo.get_source more defensive
This commit is contained in:
Anthony Sottile
2021-04-24 19:42:53 -07:00
committed by GitHub
2 changed files with 26 additions and 3 deletions

View File

@@ -734,11 +734,11 @@ class FormattedExcinfo:
) -> 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.lines)
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())