Skip to content

Commit

Permalink
Backport of fix in indexing mixed profiles
Browse files Browse the repository at this point in the history
There was a regression in the indexing of mixed profiles in Python -
when an exception was raised an undefined name was used to format
the exception.  This corrects the issue, which has already been fixed
in development of 16.2.
  • Loading branch information
tturocy committed Jan 12, 2024
1 parent a7f0754 commit 50d4067
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
24 changes: 12 additions & 12 deletions src/pygambit/behav.pxi
Expand Up @@ -52,27 +52,27 @@ class MixedAgentStrategy:
def __len__(self) -> len:
return len(self.infoset.actions)

def __getitem__(self, action: typing.Union[Action, str]):
if isinstance(action, Action):
if action.infoset != self.infoset:
def __getitem__(self, index: typing.Union[Action, str]):
if isinstance(index, Action):
if index.infoset != self.infoset:
raise MismatchError("action must belong to this infoset")
return self.profile._getprob_action(action)
if isinstance(action, str):
return self.profile._getprob_action(index)
if isinstance(index, str):
try:
return self.profile._getprob_action(self.infoset.actions[action])
return self.profile._getprob_action(self.infoset.actions[index])
except KeyError:
raise KeyError(f"no action with label '{index}' at infoset") from None
raise TypeError(f"strategy index must be Action or str, not {index.__class__.__name__}")

def __setitem__(self, action: typing.Union[Action, str], value: typing.Any) -> None:
if isinstance(action, Action):
if action.infoset != self.infoset:
def __setitem__(self, index: typing.Union[Action, str], value: typing.Any) -> None:
if isinstance(index, Action):
if index.infoset != self.infoset:
raise MismatchError("action must belong to this infoset")
self.profile._setprob_action(action, value)
self.profile._setprob_action(index, value)
return
if isinstance(action, str):
if isinstance(index, str):
try:
self.profile._setprob_action(self.infoset.actions[action], value)
self.profile._setprob_action(self.infoset.actions[index], value)
return
except KeyError:
raise KeyError(f"no action with label '{index}' at infoset") from None
Expand Down
24 changes: 12 additions & 12 deletions src/pygambit/mixed.pxi
Expand Up @@ -51,27 +51,27 @@ class MixedStrategy:
def __len__(self) -> int:
return len(self.player.strategies)

def __getitem__(self, strategy: typing.Union[Strategy, str]):
if isinstance(strategy, Strategy):
if strategy.player != self.player:
def __getitem__(self, index: typing.Union[Strategy, str]):
if isinstance(index, Strategy):
if index.player != self.player:
raise MismatchError("strategy must belong to this player")
return self.profile._getprob_strategy(strategy)
if isinstance(strategy, str):
return self.profile._getprob_strategy(index)
if isinstance(index, str):
try:
return self.profile._getprob_strategy(self.player.strategies[strategy])
return self.profile._getprob_strategy(self.player.strategies[index])
except KeyError:
raise KeyError(f"no strategy with label '{index}' for player") from None
raise TypeError(f"strategy index must be Strategy or str, not {index.__class__.__name__}")

def __setitem__(self, strategy: typing.Union[Strategy, str], value: typing.Any) -> None:
if isinstance(strategy, Strategy):
if strategy.player != self.player:
def __setitem__(self, index: typing.Union[Strategy, str], value: typing.Any) -> None:
if isinstance(index, Strategy):
if index.player != self.player:
raise MismatchError("strategy must belong to this player")
self.profile._setprob_strategy(strategy, value)
self.profile._setprob_strategy(index, value)
return
if isinstance(strategy, str):
if isinstance(index, str):
try:
self.profile._setprob_strategy(self.player.strategies[strategy], value)
self.profile._setprob_strategy(self.player.strategies[index], value)
return
except KeyError:
raise KeyError(f"no strategy with label '{index}' for player") from None
Expand Down

0 comments on commit 50d4067

Please sign in to comment.