Skip to content

Commit

Permalink
code: fix IndexError crash in getstatementrange_ast
Browse files Browse the repository at this point in the history
  • Loading branch information
bluetech authored and flying-sheep committed Apr 9, 2024
1 parent 90c73d1 commit a6a176b
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 @@ -197,7 +197,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 a6a176b

Please sign in to comment.