Skip to content

Commit

Permalink
[8.0.x] code: fix IndexError crash in getstatementrange_ast
Browse files Browse the repository at this point in the history
  • Loading branch information
bluetech authored and pytestbot committed Feb 17, 2024
1 parent 68524d4 commit 0909655
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/11953.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix an ``IndexError`` crash raising from ``getstatementrange_ast``.
4 changes: 3 additions & 1 deletion src/_pytest/_code/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down

0 comments on commit 0909655

Please sign in to comment.