
    +j                     d   d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	 dZ
d Zd Zi d	ed
edededededej        dej        dej        dej        dej        dej        dej        dej        dej        dedeZ G d de	j                  Z G d de          ZdefdZ dS ) zMath expression node using simpleeval for safe evaluation.

Provides a ComfyMathExpression node that evaluates math expressions
against dynamically-grown numeric inputs.
    N)simple_eval)override)ComfyExtensionioi  c                      t          |           dk    r+t          | d         d          rt          | d                   S t          |           S )z*Support both sum(values) and sum(a, b, c).   r   __iter__)lenhasattrsum)argss    7/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_math.py_variadic_sumr      s?    
4yyA~~'$q':66~47||t99    c                     t          |          t          k    rt          d| dt           d          t          | |          S )zWrap pow() with an exponent cap to prevent DoS via huge exponents.

    The ** operator is already guarded by simpleeval's safe_power, but
    pow() as a callable bypasses that guard.
    z	Exponent z exceeds maximum allowed ())absMAX_EXPONENT
ValueErrorpow)baseexps     r   	_safe_powr      sE     3xx,SSSSLSSSTTTtS>>r   r   minmaxr   roundr   sqrtceilfloorloglog2log10sincostanintfloatc                   z    e Zd ZdZedej        fd            Zededej	        j
        dej        fd            ZdS )MathExpressionNodez=Evaluates a math expression against dynamically-grown inputs.returnc                 f   t           j                            t           j                            dt           j        t           j        t           j        g          t          t          j
                  d          }t          j        dddg dt           j                            dd	d
          t           j                            d|          gt           j                            d          t           j                            d          t           j                            d          g          S )Nvaluer   )inputnamesr   ComfyMathExpressionzMath Expression	utilities)
expressionformula	calculate
calculatorevalmathr1   za + bT)default	multilinevalues)templateFLOAT)display_nameINTBOOL)node_idr<   categorysearch_aliasesinputsoutputs)r   AutogrowTemplateNames	MultiTypeInputFloatIntBooleanliststringascii_lowercaseSchemaStringOutput)clsautogrows     r   define_schemaz MathExpressionNode.define_schema>   s    ;,,,$$Wrx.LMMv-.. - 
 

 y)*   
 	gNN!!(X!>>
 W55511
!!v!66
 
 
 	
r   r1   r9   c                    |                                 st          d          t          |          }t          |                                          |d<   t          ||t                    }t          |t          t          f          s*t          d| dt          |          j         d|          	 t          |          }n$# t          $ r t          d| d|           d w xY wt          j        |          st          d| d|           t          j        |t          |          t#          |                    S )	NzExpression cannot be empty.r9   )r.   	functionszMath Expression 'z)' must evaluate to a numeric result, got z: z7' produced a result too large to represent as a float: z ' produced a non-finite result: )stripr   dictrK   r9   r   MATH_FUNCTIONS
isinstancer&   r'   type__name__OverflowErrorr6   isfiniter   
NodeOutputbool)rQ   r1   r9   contextresultfloat_results         r   executezMathExpressionNode.executeX   sz    !! 	<:;;;V 11Zw.QQQ&3,// 	;J ; ;F||,; ;06; ;  	 ==LL 	 	 	2J 2 2)/2 2  	
 }\** 	XJXXPVXX   }\3v;;VEEEs   5C !C&N)r[   
__module____qualname____doc__classmethodr   rN   rS   strrD   Typer^   rc    r   r   r)   r)   ;   s        GG
bi 
 
 
 [
2 FF&(k&6F	F F F [F F Fr   r)   c                   L    e Zd Zedeeej                          fd            ZdS )MathExtensionr*   c                    K   t           gS N)r)   )selfs    r   get_node_listzMathExtension.get_node_listx   s      "##r   N)	r[   rd   re   r   rK   rZ   r   	ComfyNoderp   rj   r   r   rl   rl   w   sE        $T$r|*<%= $ $ $ X$ $ $r   rl   r*   c                  "   K   t                      S rn   )rl   rj   r   r   comfy_entrypointrs   }   s      ??r   )!rf   r6   rL   
simpleevalr   typing_extensionsr   comfy_api.latestr   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   rX   rq   r)   rl   rs   rj   r   r   <module>rw      s      " " " " " " & & & & & & / / / / / / / /     	=	3 
3 
3	
 U 
9 DI DI TZ 
48 DI TZ 
48 
48 
48  
3!" U#*9F 9F 9F 9F 9F 9F 9F 9Fx$ $ $ $ $N $ $ $      r   