File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -40,7 +40,13 @@ def __getitem__(self, idx: int) -> str:
4040 if not isinstance (idx , int ):
4141 raise TypeError
4242
43- if idx < 0 or idx > self ._len :
43+ # The index is 0-based; valid values range from 0 to ``self._len - 1``.
44+ # The previous check allowed ``idx == self._len`` which would defer the
45+ # bounds validation to the underlying C library, resulting in a
46+ # ``RuntimeError`` instead of the expected ``IndexError``. Ensure the
47+ # upper bound is exclusive so out-of-range access raises ``IndexError``
48+ # consistently.
49+ if idx < 0 or idx >= self ._len :
4450 raise IndexError
4551
4652 return self .get_text (idx )
Original file line number Diff line number Diff line change 1+ import pytest
2+ from bink .story import story_from_file
3+
4+
5+ def test_choices_getitem_out_of_bounds ():
6+ story = story_from_file ("inkfiles/TheIntercept.ink.json" )
7+ # Advance the story until at least one choice is available
8+ while story .can_continue () and len (story .choices ) == 0 :
9+ story .cont ()
10+ choices = story .choices
11+ with pytest .raises (IndexError ):
12+ _ = choices [len (choices )]
You can’t perform that action at this time.
0 commit comments