Skip to content

Commit

Permalink
Merge pull request #2464 from sopel-irc/rm-ast.Num
Browse files Browse the repository at this point in the history
tools.calculation: replace deprecated `ast.Num` with `ast.Constant`
  • Loading branch information
dgw authored Aug 13, 2023
2 parents d58d6a7 + 2829487 commit 7b74964
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions sopel/tools/calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,50 @@ def _eval_node(self, node, timeout):
A subclass could overwrite this to handle more nodes, calling it only
for nodes it does not implement itself.
"""
if isinstance(node, ast.Num):
return node.n
if isinstance(node, ast.Constant):
if not isinstance(node.value, (int, float)):
raise ExpressionEvaluator.Error(
"'{}' values are not supported".format(
type(node.value).__name__,
)
)

return node.value

elif isinstance(node, ast.BinOp):
if type(node.op) not in self.binary_ops:
raise ExpressionEvaluator.Error(
"Unsupported binary operator '{}'".format(
type(node.op).__name__,
)
)

elif (isinstance(node, ast.BinOp) and
type(node.op) in self.binary_ops):
left = self._eval_node(node.left, timeout)
right = self._eval_node(node.right, timeout)
if time.time() > timeout:
raise ExpressionEvaluator.Error(
"Time for evaluating expression ran out.")
return self.binary_ops[type(node.op)](left, right)

elif (isinstance(node, ast.UnaryOp) and
type(node.op) in self.unary_ops):
elif isinstance(node, ast.UnaryOp):
if type(node.op) not in self.unary_ops:
raise ExpressionEvaluator.Error(
"Unsupported unary operator '{}'".format(
type(node.op).__name__,
)
)

operand = self._eval_node(node.operand, timeout)
if time.time() > timeout:
raise ExpressionEvaluator.Error(
"Time for evaluating expression ran out.")
return self.unary_ops[type(node.op)](operand)

raise ExpressionEvaluator.Error(
"Ast.Node '%s' not implemented." % (type(node).__name__,))
"Node type '{}' is not supported.".format(
type(node).__name__,
)
)


def guarded_mul(left, right):
Expand Down

0 comments on commit 7b74964

Please sign in to comment.