diff --git a/src/pygambit/game.pxi b/src/pygambit/game.pxi index 69ca46587..3ea378e96 100644 --- a/src/pygambit/game.pxi +++ b/src/pygambit/game.pxi @@ -307,7 +307,10 @@ class GamePlayers: raise ValueError(f"Game has multiple players with label '{index}'") return matches[0] if isinstance(index, int): - return Player.wrap(self.game.deref().GetPlayer(index + 1)) + try: + return Player.wrap(self.game.deref().GetPlayer(index + 1)) + except IndexError: + raise IndexError("Index out of range") from None raise TypeError(f"Player index must be int or str, not {index.__class__.__name__}") @property diff --git a/tests/test_players.py b/tests/test_players.py index 9b02d0e8d..9aa01c42e 100644 --- a/tests/test_players.py +++ b/tests/test_players.py @@ -40,11 +40,12 @@ def test_player_index_by_string(): def test_player_index_out_of_range(): game = gbt.Game.new_table([2, 2]) assert len(game.players) == 2 - with pytest.raises(IndexError): + exp_error_msg = "Index out of range" + with pytest.raises(IndexError, match=exp_error_msg): _ = game.players[2] - with pytest.raises(IndexError): + with pytest.raises(IndexError, match=exp_error_msg): _ = game.players[3] - with pytest.raises(IndexError): + with pytest.raises(IndexError, match=exp_error_msg): _ = game.players[-1]