unsafe_eval()
Evaluate a Python expression string and return the result.
Usage
unsafe_eval(expr)Parses and evaluates the given expression string using Python’s built-in eval() function.
Parameters
expr: str-
A string containing a valid Python expression.
Returns
object-
The result of evaluating the expression.
Raises
SyntaxError-
If the expression string is not valid Python.
NameError-
If the expression references undefined names.
Warnings
Never use with untrusted input. This function uses eval() internally and can execute arbitrary Python code. An attacker could craft an expression that deletes files, exfiltrates data, or compromises the system.
Examples
>>> unsafe_eval("2 + 3")
5>>> unsafe_eval("[i**2 for i in range(5)]")
[0, 1, 4, 9, 16]