FormattedExcinfo.get_source: Avoid a crash when source.lines == 0
pytest could crash given pathological AST position attributes, which shouldn't happen when testing real Python code, but could happen when testing AST produced by e.g. Hylang.
This commit is contained in:
parent
31d0b51039
commit
aaa53d22b2
|
@ -0,0 +1 @@
|
||||||
|
pytest should no longer crash on AST with pathological position attributes.
|
|
@ -743,11 +743,13 @@ class FormattedExcinfo:
|
||||||
) -> List[str]:
|
) -> List[str]:
|
||||||
"""Return formatted and marked up source lines."""
|
"""Return formatted and marked up source lines."""
|
||||||
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:
|
||||||
|
# `line_index` could still be outside `range(len(source.lines))` if
|
||||||
|
# we're processing AST with pathological position attributes.
|
||||||
source = Source("???")
|
source = Source("???")
|
||||||
line_index = 0
|
line_index = 0
|
||||||
if line_index < 0:
|
|
||||||
line_index += len(source)
|
|
||||||
space_prefix = " "
|
space_prefix = " "
|
||||||
if short:
|
if short:
|
||||||
lines.append(space_prefix + source.lines[line_index].strip())
|
lines.append(space_prefix + source.lines[line_index].strip())
|
||||||
|
|
Loading…
Reference in New Issue