File tree Expand file tree Collapse file tree 2 files changed +26
-3
lines changed Expand file tree Collapse file tree 2 files changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -717,11 +717,11 @@ def get_source(
717
717
) -> List [str ]:
718
718
"""Return formatted and marked up source lines."""
719
719
lines = []
720
- if source is None or line_index >= len (source .lines ):
720
+ if source is not None and line_index < 0 :
721
+ line_index += len (source .lines )
722
+ if source is None or line_index >= len (source .lines ) or line_index < 0 :
721
723
source = Source ("???" )
722
724
line_index = 0
723
- if line_index < 0 :
724
- line_index += len (source )
725
725
space_prefix = " "
726
726
if short :
727
727
lines .append (space_prefix + source .lines [line_index ].strip ())
Original file line number Diff line number Diff line change @@ -1389,6 +1389,29 @@ def test(tmpdir):
1389
1389
result .stderr .no_fnmatch_line ("*INTERNALERROR*" )
1390
1390
1391
1391
1392
+ def test_regression_nagative_line_index (pytester ):
1393
+ """
1394
+ With Python 3.10 alphas, there was an INTERNALERROR reported in
1395
+ https://github.com/pytest-dev/pytest/pull/8227
1396
+ This test ensures it does not regress.
1397
+ """
1398
+ pytester .makepyfile (
1399
+ """
1400
+ import ast
1401
+ import pytest
1402
+
1403
+
1404
+ def test_literal_eval():
1405
+ with pytest.raises(ValueError, match="^$"):
1406
+ ast.literal_eval("pytest")
1407
+ """
1408
+ )
1409
+ result = pytester .runpytest ()
1410
+ result .stdout .fnmatch_lines (["* 1 failed in *" ])
1411
+ result .stdout .no_fnmatch_line ("*INTERNALERROR*" )
1412
+ result .stderr .no_fnmatch_line ("*INTERNALERROR*" )
1413
+
1414
+
1392
1415
@pytest .mark .usefixtures ("limited_recursion_depth" )
1393
1416
def test_exception_repr_extraction_error_on_recursion ():
1394
1417
"""
You can’t perform that action at this time.
0 commit comments