FormattedExcinfo.get_source: avoid crash when line number is out-of-bounds/negative
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. Another example of the failure is in the nightly CI for the JAX project: https://github.com/google/jax/actions/runs/4607513902/jobs/8142126075 Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com> Co-authored-by: Jake VanderPlas <jakevdp@google.com>
This commit is contained in:
parent
31d0b51039
commit
3683722bcb
1
AUTHORS
1
AUTHORS
|
@ -163,6 +163,7 @@ Ionuț Turturică
|
||||||
Itxaso Aizpurua
|
Itxaso Aizpurua
|
||||||
Iwan Briquemont
|
Iwan Briquemont
|
||||||
Jaap Broekhuizen
|
Jaap Broekhuizen
|
||||||
|
Jake VanderPlas
|
||||||
Jakob van Santen
|
Jakob van Santen
|
||||||
Jakub Mitoraj
|
Jakub Mitoraj
|
||||||
James Bourbeau
|
James Bourbeau
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
pytest should no longer crash on AST with pathological position attributes, for example testing AST produced by `Hylang <https://github.com/hylang/hy>__`.
|
|
@ -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())
|
||||||
|
|
|
@ -461,6 +461,24 @@ class TestFormattedExcinfo:
|
||||||
assert lines[0] == "| def f(x):"
|
assert lines[0] == "| def f(x):"
|
||||||
assert lines[1] == " pass"
|
assert lines[1] == " pass"
|
||||||
|
|
||||||
|
def test_repr_source_out_of_bounds(self):
|
||||||
|
pr = FormattedExcinfo()
|
||||||
|
source = _pytest._code.Source(
|
||||||
|
"""\
|
||||||
|
def f(x):
|
||||||
|
pass
|
||||||
|
"""
|
||||||
|
).strip()
|
||||||
|
pr.flow_marker = "|" # type: ignore[misc]
|
||||||
|
|
||||||
|
lines = pr.get_source(source, 100)
|
||||||
|
assert len(lines) == 1
|
||||||
|
assert lines[0] == "| ???"
|
||||||
|
|
||||||
|
lines = pr.get_source(source, -100)
|
||||||
|
assert len(lines) == 1
|
||||||
|
assert lines[0] == "| ???"
|
||||||
|
|
||||||
def test_repr_source_excinfo(self) -> None:
|
def test_repr_source_excinfo(self) -> None:
|
||||||
"""Check if indentation is right."""
|
"""Check if indentation is right."""
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue