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:
@@ -461,6 +461,24 @@ class TestFormattedExcinfo:
|
||||
assert lines[0] == "| def f(x):"
|
||||
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:
|
||||
"""Check if indentation is right."""
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user