[8.0.x] code: fix `IndexError` crash in `getstatementrange_ast`

This commit is contained in:
Ran Benita 2024-02-17 18:59:10 +02:00 committed by pytest bot
parent 68524d4858
commit 090965574e
2 changed files with 4 additions and 1 deletions

View File

@ -0,0 +1 @@
Fix an ``IndexError`` crash raising from ``getstatementrange_ast``.

View File

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