Skip to content

Commit

Permalink
dice: edge case cleanup
Browse files Browse the repository at this point in the history
Python 3.5 changed `compile()` (which `eval_equation()` uses, far down
the call stack) to raise `ValueError` in the case that used to raise a
`TypeError`, so that block isn't necessary any more.

See https://docs.python.org/3.8/library/functions.html#compile and
#1386 (comment)

Input that doesn't contain any dice expressions could still be processed
and wind up at the wrong error handler; added an early-return for that.
(An expression without dice rolls should be evaluated using `.calc`. And
yes, I'm aware that this could be someone's spacebar heater. Tough!)

The tests also now specifically cover an expression with no number of
dice (to verify that the default of 1 die works), and cases where the
input contains one or more valid dice expression(s) but has invalid
syntax somewhere else in the string.
  • Loading branch information
dgw committed Oct 28, 2023
1 parent 0226bf6 commit f59886b
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions sopel/modules/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,10 @@ def _roll_dice(dice_expression: str) -> DicePouch:
@plugin.command('roll', 'dice', 'd')
@plugin.priority("medium")
@plugin.example(".roll", "No dice to roll.")
@plugin.example(".roll 65(2)",
@plugin.example(".roll 2d6+4^2&",
"I don't know how to process that. "
"Are the dice as well as the algorithms correct?")
@plugin.example(".roll 65(2)", "I couldn't find any valid dice expressions.")
@plugin.example(".roll 1d0", "I don't have any dice with 0 sides.")
@plugin.example(".roll -1d6", "I can't roll -1 dice.")
@plugin.example(".roll 3d6v-1", "I can't drop the lowest -1 of 3 dice.")
Expand All @@ -264,7 +265,7 @@ def _roll_dice(dice_expression: str) -> DicePouch:
".roll {}d1".format(MAX_DICE + 1), 'I only have {}/{} dice.'.format(MAX_DICE, MAX_DICE + 1))
@plugin.example(".roll 1d1 + 1d1", '1d1 + 1d1: (1) + (1) = 2')
@plugin.example(".roll 1d1+1d1", '1d1+1d1: (1)+(1) = 2')
@plugin.example(".roll 1d6 # initiative", r'1d6: \(\d\) = \d', re=True)
@plugin.example(".roll d6 # initiative", r'd6: \(\d\) = \d', re=True)
@plugin.example(".roll 2d20v1+2 # roll with advantage", user_help=True)
@plugin.example(".roll 2d10+3", user_help=True)
@plugin.example(".roll 1d6", user_help=True)
Expand Down Expand Up @@ -294,6 +295,10 @@ def roll(bot: SopelWrapper, trigger: Trigger):
arg_str = arg_str_raw.replace("%", "%%")
arg_str = re.sub(dice_regexp, "%s", arg_str)

if not dice_expressions:
bot.reply("I couldn't find any valid dice expressions.")
return

try:
dice = [_roll_dice(dice_expr) for dice_expr in dice_expressions]
except DiceError as err:
Expand All @@ -317,12 +322,6 @@ def _get_pretty_str(dice: DicePouch) -> str:

try:
result = eval_equation(eval_str)
except TypeError:
bot.reply(
"The type of this equation is, apparently, not a string. "
"How did you do that, anyway?"
)
return
except ValueError:
# As it seems that ValueError is raised if the resulting equation would
# be too big, give a semi-serious answer to reflect on this.
Expand Down

0 comments on commit f59886b

Please sign in to comment.