added check for Constant, moved check for Str/Num

This commit is contained in:
2025-06-07 20:14:25 +02:00
parent 847fb43708
commit 3c56d93d73
+9 -4
View File
@@ -40,10 +40,6 @@ def arithmetic_eval(s):
def ast_node_eval(node):
if isinstance(node, ast.Expression):
return ast_node_eval(node.body)
elif isinstance(node, ast.Str):
return node.s
elif isinstance(node, ast.Num):
return node.n
elif isinstance(node, ast.BinOp):
op = get_operator(node, BIN_OPS)
left = ast_node_eval(node.left)
@@ -53,6 +49,15 @@ def ast_node_eval(node):
op = get_operator(node, UNARY_OPS)
operand = ast_node_eval(node.operand)
return op(operand)
# from >=3.8, Constant can replace Str/Num
# from >=3.14, Str/Num are deprecated and removed
# check Constant first then fall back to Str/Num for <3.8
elif isinstance(node, ast.Constant):
return node.value
elif isinstance(node, ast.Str):
return node.s
elif isinstance(node, ast.Num):
return node.n
else:
tn = typename(node)
raise ArithmeticEvalError(f"Unsupported node type {tn}")