From 997d2fb13af6aadf36873ea2ea3317e56f405aef Mon Sep 17 00:00:00 2001 From: Mel Massadian Date: Thu, 8 Aug 2024 22:34:03 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20don't=20fallback=20to=20e?= =?UTF-8?q?val?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit addresses legitimate concerns raised in #190 This limits the use a bit, SimpleMath from: https://github.com/cubiq/ComfyUI_essentials Is a better alternative --- nodes/graph_utils.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/nodes/graph_utils.py b/nodes/graph_utils.py index b201ecc..9cdb0cb 100644 --- a/nodes/graph_utils.py +++ b/nodes/graph_utils.py @@ -491,14 +491,14 @@ class MTB_MathExpression: RETURN_NAMES = ("result (float)", "result (int)") CATEGORY = "mtb/math" DESCRIPTION = ( - "evaluate a simple math expression string (!! Fallsback to eval)" + "evaluate a simple math expression string, only supports literal_eval" ) - def eval_expression(self, expression, **kwargs): + def eval_expression(self, expression: str, **kwargs): from ast import literal_eval for key, value in kwargs.items(): - print(f"Replacing placeholder <{key}> with value {value}") + log.debug(f"Replacing placeholder <{key}> with value {value}") expression = expression.replace(f"<{key}>", str(value)) result = -1 @@ -509,15 +509,10 @@ class MTB_MathExpression: f"The expression syntax is wrong '{expression}': {e}" ) from e - except ValueError: - try: - expression = expression.replace("^", "**") - result = eval(expression) - except Exception as e: - # Handle any other exceptions and provide a meaningful error message - raise ValueError( - f"Error evaluating expression '{expression}': {e}" - ) from e + except Exception as e: + raise ValueError( + f"Math expression only support literal_eval now: {e}" + ) return (result, int(result))