Skip to content

Commit

Permalink
fix: 🐛 don't fallback to eval
Browse files Browse the repository at this point in the history
addresses legitimate concerns raised in #190
This limits the use a bit, SimpleMath from:
https://github.com/cubiq/ComfyUI_essentials
Is a better alternative
melMass committed Aug 8, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent f8829fc commit 997d2fb
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions nodes/graph_utils.py
Original file line number Diff line number Diff line change
@@ -491,14 +491,14 @@ def INPUT_TYPES(cls):
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 @@ def eval_expression(self, expression, **kwargs):
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))

0 comments on commit 997d2fb

Please sign in to comment.