Skip to content

Commit 78ca12f

Browse files
authored
Improve exception message raised in GamePlayers.__getitem__ (#702)
1 parent c9c00d5 commit 78ca12f

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

src/pygambit/game.pxi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,10 @@ class GamePlayers:
307307
raise ValueError(f"Game has multiple players with label '{index}'")
308308
return matches[0]
309309
if isinstance(index, int):
310-
return Player.wrap(self.game.deref().GetPlayer(index + 1))
310+
try:
311+
return Player.wrap(self.game.deref().GetPlayer(index + 1))
312+
except IndexError:
313+
raise IndexError("Index out of range") from None
311314
raise TypeError(f"Player index must be int or str, not {index.__class__.__name__}")
312315

313316
@property

tests/test_players.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ def test_player_index_by_string():
4040
def test_player_index_out_of_range():
4141
game = gbt.Game.new_table([2, 2])
4242
assert len(game.players) == 2
43-
with pytest.raises(IndexError):
43+
exp_error_msg = "Index out of range"
44+
with pytest.raises(IndexError, match=exp_error_msg):
4445
_ = game.players[2]
45-
with pytest.raises(IndexError):
46+
with pytest.raises(IndexError, match=exp_error_msg):
4647
_ = game.players[3]
47-
with pytest.raises(IndexError):
48+
with pytest.raises(IndexError, match=exp_error_msg):
4849
_ = game.players[-1]
4950

5051

0 commit comments

Comments
 (0)