Skip to content

Commit feb7c5e

Browse files
authored
Merge pull request #11999 from pytest-dev/backport-11996-to-8.0.x
[8.0.x] code: fix `IndexError` crash in `getstatementrange_ast`
2 parents 68524d4 + 0909655 commit feb7c5e

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

Diff for: changelog/11953.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix an ``IndexError`` crash raising from ``getstatementrange_ast``.

Diff for: src/_pytest/_code/source.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ def getstatementrange_ast(
196196
# by using the BlockFinder helper used which inspect.getsource() uses itself.
197197
block_finder = inspect.BlockFinder()
198198
# If we start with an indented line, put blockfinder to "started" mode.
199-
block_finder.started = source.lines[start][0].isspace()
199+
block_finder.started = (
200+
bool(source.lines[start]) and source.lines[start][0].isspace()
201+
)
200202
it = ((x + "\n") for x in source.lines[start:end])
201203
try:
202204
for tok in tokenize.generate_tokens(lambda: next(it)):

0 commit comments

Comments
 (0)