Evaluate a math expression safely — Rust backend with Python fallback.
Source code in src/openjarvis/tools/calculator.py
| def safe_eval(expression: str) -> float:
"""Evaluate a math expression safely — Rust backend with Python fallback."""
try:
from openjarvis._rust_bridge import get_rust_module
_rust = get_rust_module()
return float(_rust.CalculatorTool().execute(expression))
except ImportError:
import ast as _ast
# Support ^ as the power operator (common math/calculator notation).
expression = expression.replace("^", "**")
try:
tree = _ast.parse(expression, mode="eval")
except SyntaxError as exc:
raise ValueError(f"Syntax error in expression: {exc}") from exc
try:
return float(_safe_eval_node(tree.body))
except ZeroDivisionError:
return math.inf
|