Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion bink/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ def __getitem__(self, idx: int) -> str:
if not isinstance(idx, int):
raise TypeError

if idx < 0 or idx > self._len:
# The index is 0-based; valid values range from 0 to ``self._len - 1``.
# The previous check allowed ``idx == self._len`` which would defer the
# bounds validation to the underlying C library, resulting in a
# ``RuntimeError`` instead of the expected ``IndexError``. Ensure the
# upper bound is exclusive so out-of-range access raises ``IndexError``
# consistently.
if idx < 0 or idx >= self._len:
raise IndexError

return self.get_text(idx)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_choices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest
from bink.story import story_from_file


def test_choices_getitem_out_of_bounds():
story = story_from_file("inkfiles/TheIntercept.ink.json")
# Advance the story until at least one choice is available
while story.can_continue() and len(story.choices) == 0:
story.cont()
choices = story.choices
with pytest.raises(IndexError):
_ = choices[len(choices)]
Loading