Skip to content

Commit 9f13d41

Browse files
committed
code: fix IndexError crash in getstatementrange_ast
Fix #11953.
1 parent 9844781 commit 9f13d41

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

changelog/11953.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix an ``IndexError`` crash raising from ``getstatementrange_ast``.

src/_pytest/_code/source.py

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

0 commit comments

Comments
 (0)