File tree 4 files changed +25
-3
lines changed
4 files changed +25
-3
lines changed Original file line number Diff line number Diff line change @@ -163,6 +163,7 @@ Ionuț Turturică
163
163
Itxaso Aizpurua
164
164
Iwan Briquemont
165
165
Jaap Broekhuizen
166
+ Jake VanderPlas
166
167
Jakob van Santen
167
168
Jakub Mitoraj
168
169
James Bourbeau
Original file line number Diff line number Diff line change
1
+ pytest should no longer crash on AST with pathological position attributes, for example testing AST produced by `Hylang <https://github.com/hylang/hy>__ `.
Original file line number Diff line number Diff line change @@ -743,11 +743,13 @@ def get_source(
743
743
) -> List [str ]:
744
744
"""Return formatted and marked up source lines."""
745
745
lines = []
746
- if source is None or line_index >= len (source .lines ):
746
+ if source is not None and line_index < 0 :
747
+ line_index += len (source )
748
+ if source is None or line_index >= len (source .lines ) or line_index < 0 :
749
+ # `line_index` could still be outside `range(len(source.lines))` if
750
+ # we're processing AST with pathological position attributes.
747
751
source = Source ("???" )
748
752
line_index = 0
749
- if line_index < 0 :
750
- line_index += len (source )
751
753
space_prefix = " "
752
754
if short :
753
755
lines .append (space_prefix + source .lines [line_index ].strip ())
Original file line number Diff line number Diff line change @@ -461,6 +461,24 @@ def f(x):
461
461
assert lines [0 ] == "| def f(x):"
462
462
assert lines [1 ] == " pass"
463
463
464
+ def test_repr_source_out_of_bounds (self ):
465
+ pr = FormattedExcinfo ()
466
+ source = _pytest ._code .Source (
467
+ """\
468
+ def f(x):
469
+ pass
470
+ """
471
+ ).strip ()
472
+ pr .flow_marker = "|" # type: ignore[misc]
473
+
474
+ lines = pr .get_source (source , 100 )
475
+ assert len (lines ) == 1
476
+ assert lines [0 ] == "| ???"
477
+
478
+ lines = pr .get_source (source , - 100 )
479
+ assert len (lines ) == 1
480
+ assert lines [0 ] == "| ???"
481
+
464
482
def test_repr_source_excinfo (self ) -> None :
465
483
"""Check if indentation is right."""
466
484
try :
You can’t perform that action at this time.
0 commit comments