Fix uncaught OverflowError in Math Expression node for large int results (#14214)

This commit is contained in:
vidigoat 2026-06-02 06:45:04 +05:30 committed by GitHub
parent 4b48535a7d
commit 33799c4a2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 2 deletions

View File

@ -102,11 +102,18 @@ class MathExpressionNode(io.ComfyNode):
f"Math Expression '{expression}' must evaluate to a numeric result, " f"Math Expression '{expression}' must evaluate to a numeric result, "
f"got {type(result).__name__}: {result!r}" f"got {type(result).__name__}: {result!r}"
) )
if not math.isfinite(result): try:
float_result = float(result)
except OverflowError:
raise ValueError(
f"Math Expression '{expression}' produced a result too large to "
f"represent as a float: {result}"
) from None
if not math.isfinite(float_result):
raise ValueError( raise ValueError(
f"Math Expression '{expression}' produced a non-finite result: {result}" f"Math Expression '{expression}' produced a non-finite result: {result}"
) )
return io.NodeOutput(float(result), int(result), bool(result)) return io.NodeOutput(float_result, int(result), bool(result))
class MathExtension(ComfyExtension): class MathExtension(ComfyExtension):

View File

@ -197,3 +197,10 @@ class TestMathExpressionExecute:
def test_pow_huge_exponent_raises(self): def test_pow_huge_exponent_raises(self):
with pytest.raises(ValueError, match="Exponent .* exceeds maximum"): with pytest.raises(ValueError, match="Exponent .* exceeds maximum"):
self._exec("pow(a, b)", a=10, b=10000000) self._exec("pow(a, b)", a=10, b=10000000)
def test_huge_int_result_raises_value_error(self):
# Exponent is within the allowed MAX_EXPONENT range, so the result is a
# finite Python int that is nonetheless too large to convert to float.
# This must raise a clean ValueError, not an uncaught OverflowError.
with pytest.raises(ValueError, match="too large to represent as a float"):
self._exec("2 ** 3999")