Skip to content

Commit

Permalink
Merge pull request #11999 from pytest-dev/backport-11996-to-8.0.x
Browse files Browse the repository at this point in the history
[8.0.x] code: fix `IndexError` crash in `getstatementrange_ast`
  • Loading branch information
bluetech authored Feb 17, 2024
2 parents 68524d4 + 0909655 commit feb7c5e
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 feb7c5e

Please sign in to comment.