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
commit 67af623d9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View File

@ -734,11 +734,11 @@ 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.lines)
if source is None or line_index >= len(source.lines) or line_index < 0:
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())

View File

@ -1397,6 +1397,29 @@ def test_cwd_deleted(pytester: Pytester) -> None:
result.stderr.no_fnmatch_line("*INTERNALERROR*") result.stderr.no_fnmatch_line("*INTERNALERROR*")
def test_regression_nagative_line_index(pytester: Pytester) -> None:
"""
With Python 3.10 alphas, there was an INTERNALERROR reported in
https://github.com/pytest-dev/pytest/pull/8227
This test ensures it does not regress.
"""
pytester.makepyfile(
"""
import ast
import pytest
def test_literal_eval():
with pytest.raises(ValueError, match="^$"):
ast.literal_eval("pytest")
"""
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(["* 1 failed in *"])
result.stdout.no_fnmatch_line("*INTERNALERROR*")
result.stderr.no_fnmatch_line("*INTERNALERROR*")
@pytest.mark.usefixtures("limited_recursion_depth") @pytest.mark.usefixtures("limited_recursion_depth")
def test_exception_repr_extraction_error_on_recursion(): def test_exception_repr_extraction_error_on_recursion():
""" """