
    sg֕                        d dl 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	m
Z
mZmZ d dlZddlmZ  e       rd dlZ G d de      Z ee      D  ci c],  }  e ee|       e      r e ee|       e      r
|  ee|       . c} Zg d	Zd
\  aZd\  aZ  G d de!      Z" G d de!      Z# G d de!      Z$d Z%d Z&d Z'd Z(d Z)d Z*d Z+d Z,d Z-d Z.d Z/d Z0d Z1d Z2d  Z3d! Z4d" Z5d# Z6d$ Z7d% Z8d& Z9d' Z:d( Z;d) Z<d* Z=d+ Z>efd,e j~                  d-e
e@ef   d.e
e@e	f   d/e
e@e	f   d0ee@   f
d1ZAefd2e@d3eBd4e@fd5ZCdddefd6e@d.ee
e@e	f      d/ee
e@e	f      d-ee
e@ef      d0ee@   f
d7ZDyc c} w )8    N)Mapping)import_module)AnyCallableDictListOptional   )is_pandas_availablec                       e Zd ZdZy)InterpreterErrorz
    An error raised when the interpretor cannot evaluate a Python expression, due to syntax error or unsupported
    operations.
    N)__name__
__module____qualname____doc__     Y/var/www/html/venv/lib/python3.12/site-packages/transformers/agents/python_interpreter.pyr   r   !   s    
 	r   r   )
randomcollectionsmathtimequeue	itertoolsrestat
statisticsunicodedata) iP  )r   i c                       e Zd Zy)BreakExceptionNr   r   r   r   r   r   r!   r!   B       r   r!   c                       e Zd Zy)ContinueExceptionNr"   r   r   r   r%   r%   F   r#   r   r%   c                       e Zd Zd Zy)ReturnExceptionc                     || _         y N)value)selfr*   s     r   __init__zReturnException.__init__K   s	    
r   N)r   r   r   r,   r   r   r   r'   r'   J   s    r   r'   c                 j    t        | t              r| S t        | d      rt        |       S t        d      )N__iter__zObject is not iterable)
isinstancelisthasattrr   )objs    r   get_iterabler3   O   s1    #t
	j	!Cy788r   c                    t        | j                  |||      }t        | j                  t        j
                        r| S t        | j                  t        j                        r|S t        | j                  t        j                        r| S t        | j                  t        j                        r| S t        d| j                  j                  j                   d      )NzUnary operation  is not supported.)evaluate_astoperandr/   opastUSubUAddNotInvertr   	__class__r   )
expressionstatestatic_toolscustom_toolsr7   s        r   evaluate_unaryoprC   X   s    :--ulLQG*--*x	JMM388	,	JMM377	+{	JMM3::	.x!1*--2I2I2R2R1SSefggr   c                       j                   j                   D cg c]  }|j                   c} fd}|S c c}w )Nc                      j                         }t        |       D ]
  \  }}|||<    t        j                  |      S r)   )copyzipr6   body)	values	new_stateargr*   argsrB   lambda_expressionr@   rA   s	       r   lambda_funcz$evaluate_lambda.<locals>.lambda_funci   sI    JJL	dF+ 	#JC"IcN	#-22I|\ZZr   )rL   rK   )rM   r@   rA   rB   rK   rN   rL   s   ````  @r   evaluate_lambdarO   f   s<    055::;CGG;D[ [  <s   ?c                    d}d}t        | j                  |||      rR| j                  D ]  }	 t        ||||        |dz  }||kD  rt        d| d      t        | j                  |||      rRy # t        $ r Y  y t        $ r Y  Jw xY w)Ni  r      zMaximum number of z" iterations in While loop exceeded)r6   testrH   r!   r%   r   )
while_loopr@   rA   rB   max_iterations
iterationsnodes          r   evaluate_whilerW   r   s    NJ
z|\
JOO 	DT5,E	 	a
&"%77GGi#jkk z|\
J  " $ s   A00	B<BBc                       fd}|S )Nc            	         j                         }j                  j                  D cg c]  }|j                   }}j                  j                  D cg c]  }t	        |       }}t        t        |t        |       d  |            }t        ||       D ]
  \  }}	|	||<    |j                         D ]
  \  }}	|	||<    j                  j                  r%j                  j                  j                  }
| ||
<   j                  j                  r%j                  j                  j                  }|||<   |j                         D ]  \  }}	||vs|	||<    j                  j                  rBj                  j                  d   j                  dk(  r| r| d   |d<   | d   j                  |d<   d }	 j                  D ]  }t	        ||      } 	 |S c c}w c c}w # t        $ r}|j                  }Y d }~|S d }~ww xY w)Nr   r+   r>   )rF   rL   rK   defaultsr6   dictrG   lenitemsvarargkwargr>   rH   r'   r*   )rL   kwargs
func_staterK   	arg_namesddefault_valuesrZ   namer*   vararg_name
kwarg_nameresultstmterB   func_defr@   rA   s                  r   new_funcz!create_function.<locals>.new_func   s    ZZ\
(0(:(:;SWW;	;V^VcVcVlVlmQR,q%|Lmm Is>':&:&<=~NO y$/ 	%KD%$Jt	% "<<> 	%KD%$Jt	% =="--..22K&*J{#==!,,00J%+Jz" $>>+ 	)KD%:%#(
4 	)
 ==(--"4"4Q"7";";v"E%)!W
6"*.q'*;*;
;'	  T%dJlST Q <mJ  	WWF	s#   GG 9G% %	H.H  Hr   )rk   r@   rA   rB   rl   s   ```` r   create_functionrm      s    *X Or   c                 l    i }|j                         D ]
  \  }}|||<    t        | t        |      |      S r)   )r]   typetuple)
class_nameclass_bases
class_body
class_dictkeyr*   s         r   create_classrv      s@    J &&(  
U
3 
E+.
;;r   c                 V    t        | |||      || j                  <   || j                     S r)   )rm   re   )rk   r@   rA   rB   s       r   evaluate_function_defrx      s+    "1(E<Q]"^L&&r   c           	         | j                   }| j                  D cg c]  }t        ||||       }}i }| j                  D ]  }t	        |t
        j                        rt        ||||      ||j                   <   :t	        |t
        j                        r|j                  D ]  }	t	        |	t
        j                        r&t        |j                  |||      ||	j                  <   Ct	        |	t
        j                        s^t        |j                  |||      ||	j                  <    t        d|j                   j"                          t%        |t'        |      |      }
|
||<   |
S c c}w )Nz%Unsupported statement in class body: )re   basesr6   rH   r/   r9   FunctionDefrx   AssigntargetsNamer*   id	Attributeattrr   r>   r   ro   rp   )	class_defr@   rA   rB   rq   baserz   rt   ri   target	new_classs              r   evaluate_class_defr      s4   JOX_t\$|\B_E_J 
fdCOO,$9$|Ua$bJtyy!cjj),, jfchh/,8ULZf,gJvyy)6.:4::ul\h.iJv{{+	j #%J4>>KbKbJc#dee
f Zuz:I!E*# `s   E&c                 d   fd | j                         }t        | j                        }t        | j                  t
        j                        r=t        |t              r&t        |t              st        d| d      ||z   }n||z   }nt        | j                  t
        j                        r||z
  }nt        | j                  t
        j                        r||z  }nt        | j                  t
        j                        r||z  }n~t        | j                  t
        j                        r||z  }nSt        | j                  t
        j                        r||z  }n(t        | j                  t
        j                        r||z  }nt        | j                  t
        j                        r||z  }nt        | j                  t
        j                         r||z  }nt        | j                  t
        j"                        r||z  }nt        | j                  t
        j$                        r||z  }nVt        | j                  t
        j&                        r||z	  }n,t        dt)        | j                        j*                   d      t-        | j                   |       |S )Nc                    t        | t        j                        rj                  | j                  d      S t        | t        j
                        r5t        | j                        }t        | j                        }||   S t        | t        j                        r.t        | j                        }t        || j                        S t        | t        j                        rt        fd| j                  D              S t        | t        j                        r | j                  D cg c]
  } |       c}S t!        d      c c}w )Nr   c              3   .   K   | ]  } |        y wr)   r   ).0eltget_current_values     r   	<genexpr>z@evaluate_augassign.<locals>.get_current_value.<locals>.<genexpr>   s     GC*3/Gs   z3AugAssign not supported for {type(target)} targets.)r/   r9   r~   getr   	Subscriptr6   r*   slicer   getattrr   Tuplerp   eltsr   r   )r   r2   ru   r   rB   r   r@   rA   s       r   r   z-evaluate_augassign.<locals>.get_current_value   s    fchh'99VYY**.v||UL,OCv||UL,OCs8O.v||UL,OC3,,		*G6;;GGG)6<kkBs%c*BB"#XYY Cs   /EzCannot add non-list value z to a list.z
Operation r5   )r   r6   r*   r/   r8   r9   Addr0   r   SubMultDivModPowFloorDivBitAndBitOrBitXorLShiftRShiftro   r   	set_value)r?   r@   rA   rB   current_valuevalue_to_addupdated_valuer   s    ```   @r   evaluate_augassignr      s   Z" &j&7&78M
 0 0%|TL *--)mT*lD1&)CL>Q\']^^)L8M)L8M	JMM377	+%4	JMM388	,%4	JMM377	+%4	JMM377	+%4	JMM377	+%|3	JMM3<<	0%5	JMM3::	.%4	JMM399	-%4	JMM3::	.%4	JMM3::	.%5	JMM3::	.%5D,?,H,H+II[\]] j|\Rr   c                    t        | j                  t        j                        r"| j                  D ]  }t        ||||      r y yt        | j                  t        j                        r"| j                  D ]  }t        ||||      s y yy NFT)r/   r8   r9   AndrI   r6   Or)rV   r@   rA   rB   r*   s        r   evaluate_boolopr     s}    $''377#[[ 	EulLI	 	DGGSVV	$[[ 	EE5,E	 	 
%r   c                    t        | j                  |||      }t        | j                  |||      }t        | j                  t
        j                        r||z   S t        | j                  t
        j                        r||z
  S t        | j                  t
        j                        r||z  S t        | j                  t
        j                        r||z  S t        | j                  t
        j                        r||z  S t        | j                  t
        j                        r||z  S t        | j                  t
        j                        r||z  S t        | j                  t
        j                        r||z  S t        | j                  t
        j                        r||z  S t        | j                  t
        j                        r||z  S t        | j                  t
        j                         r||z  S t        | j                  t
        j"                        r||z	  S t%        dt'        | j                        j(                   d      )NzBinary operation z is not implemented.)r6   leftrightr/   r8   r9   r   r   r   r   r   r   r   r   r   r   r   r   NotImplementedErrorro   r   )binopr@   rA   rB   left_val	right_vals         r   evaluate_binopr     s   EJJ|\JHU[[%|LI %((CGG$)##	EHHcgg	&)##	EHHchh	')##	EHHcgg	&)##	EHHcgg	&)##	EHHcgg	&""	EHHcll	+9$$	EHHcjj	))##	EHHcii	()##	EHHcjj	))##	EHHcjj	)9$$	EHHcjj	)9$$!$5d588n6M6M5NNb"cddr   c                 >   t        | j                  |||      }t        | j                        dk(  r | j                  d   }t	        |||||       |S t        | j                        t        |      k7  r.t        dt        |       dt        | j                         d      g }| j                  D ]?  }t        |t        j                        r|j                  |       /|j                  |       A t        | j                  |      D ]  \  }}t	        |||||        |S )NrQ   r   zAssign failed: expected z values but got .)r6   r*   r\   r}   r   r   r/   r9   StarredextendappendrG   )	assignr@   rA   rB   rh   r   expanded_valuestgtvals	            r   evaluate_assignr   A  s   &,,|\JF
6>>a"&&%|D M v~~#f+-"%=c&k]JZ[^_e_m_m[nZoop#qrr>> 	/C#s{{+&&v.&&v.		/
 FNNO< 	CHCc3|\B	CMr   c                 X   t        | t        j                        r7| j                  |v rt	        d| j                   d      ||| j                  <   y t        | t        j
                        rt        |t              s9t        |d      r"t        |t        t        f      st        |      }nt	        d      t        | j                        t        |      k7  rt	        d      t        | j                        D ]  \  }}t        |||   |||        y t        | t        j                        r6t        | j                   |||      }t        | j"                  |||      }|||<   y t        | t        j$                        r0t        | j                   |||      }t'        || j(                  |       y y )NzCannot assign to name 'z,': doing this would erase the existing tool!r.   zCannot unpack non-tuple valuez!Cannot unpack tuple of wrong size)r/   r9   r~   r   r   r   rp   r1   strbytesr\   r   	enumerater   r   r6   r*   r   r   setattrr   )	r   r*   r@   rA   rB   ielemr2   ru   s	            r   r   r   T  sQ   &#((#99$"%<VYYKGs#tuu fii	FCII	&%'uj)*US%L2Qe&'FGGv{{s5z)"#FGG - 	IGAtdE!He\<H	I	FCMM	*6<<lK6<<lKC	FCMM	*6<<lKV[[%( 
+r   c           
         t        | j                  t        j                        s=t        | j                  t        j                        st        d| j                   d      t        | j                  t        j                        rbt        | j                  j                  |||      }| j                  j                  }t        ||      st        d| d|       t        ||      }nt        | j                  t        j                        ri| j                  j                  }||v r||   }nI||v r||   }n?||v r||   }n5|t        v r
t        |   }n#t        d| j                  j                   d      g }| j                  D ]a  }t        |t        j                        r(|j                  t        |j                  |||             E|j!                  t        ||||             c g }| j                  D ]  }t        |t        j                        rZt        |j                  |||      }	t        |	d      rt        |	t"        t$        f      rt        d|	       |j                  |	       w|j!                  t        ||||              | j&                  D 
ci c]%  }
|
j(                  t        |
j                  |||      ' }}
t        t*              rXt-        |j.                  j1                  d            d	kD  r1|j3                  |      }t        |d
      r |j4                  |i | |S dk(  r|s%d|v rd|v rt7        |d   |d         S t        d      |d   }t        |t*              st        d      t-        |      d	k(  rt7        |      S t-        |      dk(  r|d	   }t7        ||      S t        d      |dk(  r,dj9                  t;        t"        |            }t<        |dz   z  ay  ||i |}|S c c}
w )Nz This is not a correct function: z).zObject z has no attribute zIt is not permitted to evaluate other functions than the provided tools or functions defined in previous code (tried to execute r.   z!Cannot unpack non-iterable value r   rQ   r,   superr>   r+   z#super() needs at least one argumentr   zsuper() argument 1 must be typer
   z!super() takes at most 2 argumentsprint 
)r/   funcr9   r   r~   r   r6   r*   r   r1   r   r   ERRORSrL   r   r   r   r   r   keywordsrK   ro   r\   r   split__new__r,   r   joinmapPRINT_OUTPUTS)callr@   rA   rB   r2   	func_namer   rL   rK   unpackedkeywordr`   clsinstanceoutputs                  r   evaluate_callr   l  s   tyy#--0Jtyy#((4S!A$))BOPP$))S]]+499??E<NIINN	sI&"WSE1CI;#OPPsI&	DIIsxx	(IILL	#D,&	*D,&	*D& )$D" S  TX  T]  T]  T`  T`  Sa  ac  d  Dyy Nc3;;'KKSYY|\RSKKS%|LM	N Dyy Nc3;;'#CIIulLQH8Z0Jx#u4V&)J8*'UVVKK!KKS%|LMN jnivivw^egkk<ulLYYwFw$#doo&;&;C&@"AA"Ell4 3
#CLL$)&)
%'FeO {!3U6]CC*+PQQq'Cc4(&'HII4yA~Sz!Ta7S(++&'JKKG##c4.1$.t.v.E xs   *O7c                    t        | j                  |||      }t        | j                  |||      }t        |t              rt        |t              rt        d      t        |t        j                  j                  j                        r|j                  }|j                  |   S t        |t        j                  t        j                  t        j                  f      r||   S t        |t        j                  j                   j"                  j$                        r||   S t        |t              r||   S t        |t&        t(        f      rHt+        |       |cxk  rt+        |      k  sn t        d| dt+        |             |t-        |         S t        |t              r?t+        |       |cxk  rt+        |      k  sn t        d| dt+        |             ||   S ||v r||   S t        |t              rSt        |t.              rCt1        j2                  |t'        |j5                                     }t+        |      dkD  r||d      S t        d| d| d      )	NzLYou're trying to subscript a string with a string index, which is impossiblezIndex z" out of bounds for list of length z$ out of bounds for string of length r   zCould not index z with 'z'.)r6   r   r*   r/   r   r   pdcoreindexing_LocIndexerr2   loc	DataFrameSeriesnpndarraygroupbygenericDataFrameGroupByr0   rp   r\   intr   difflibget_close_matcheskeys)	subscriptr@   rA   rB   indexr*   parent_objectclose_matchess           r   evaluate_subscriptr     s   %|LE%|LE%*UC"8mnn%))556		  ''%",,		2::>?U|	E277??22CC	DU|	E5	!U|	ED%=	)Uu1s5z1"VE72TUXY^U_T`#abbSZ  	E3	Uu1s5z1"VE72VWZ[`WaVb#cddU|	%U|	E3	Jug$>11%ejjl9KL}!q)**
-eWGE7"E
FFr   c                    | j                   |v r|| j                      S | j                   |v r|| j                      S | j                   t        v rt        | j                      S t        j                  | j                   t	        |j                                     }t        |      dkD  r||d      S t        d| j                    d      )Nr   zThe variable `z` is not defined.)r   r   r   r   r0   r   r\   r   )re   r@   rA   rB   r   s        r   evaluate_namer     s    ww%TWW~	L	 DGG$$	F	dgg--dggtEJJL7IJM
=A]1%&&
^DGG94EF
GGr   c           	         t        | j                  |||      }| j                  D cg c]  }t        ||||       }}| j                  D cg c]  }t	        |       }}d}	|}
t        ||      D ]'  \  }}|t        j                  k(  r|
|k(  }n|t        j                  k(  r|
|k7  }n|t        j                  k(  r|
|k  }n|t        j                  k(  r|
|k  }n|t        j                  k(  r|
|kD  }n|t        j                  k(  r|
|k\  }nn|t        j                  k(  r|
|u }nV|t        j                  k(  r|
|u}n>|t        j                  k(  r|
|v }n&|t        j                   k(  r|
|v}nt#        d|       |	|z  }	|}
t%        |	t&              s$|	r( n t%        |	t&        t(        j*                  f      r|	S |	j-                         S c c}w c c}w )NTzOperator not supported: )r6   r   comparatorsopsro   rG   r9   EqNotEqLtLtEGtGtEIsIsNotInNotInr   r/   boolr   r   all)	conditionr@   rA   rB   r   cr   r8   r   rh   current_left
comparatorcurrent_results                r   evaluate_conditionr    s   	|\JDOXOdOde!<5,EeKe'mm
,48
,C
,FLc;/ J<)Z7N399_)Z7N366\)J6N377])Z7N366\)J6N377])Z7N366\)Z7N399_);N366\)Z7N399_);N"%=bT#BCC.(!fd#F9<  ryy(9:6L

LI f
,s   GGc                     d }t        | j                  |||      }|r&| j                  D ]  }t        ||||      }||} |S | j                  D ]  }t        ||||      }||} |S r)   )r6   rR   rH   orelse)if_statementr@   rA   rB   rh   test_resultlineline_results           r   evaluate_ifr    s    F|00%|TK %% 	%D&tUL,OK&$	% M	 !'' 	%D&tUL,OK&$	% Mr   c                     d }t        | j                  |||      }|D ]?  }t        | j                  ||||       | j                  D ]  }	 t        ||||      }||} A |S # t
        $ r Y   |S t        $ r Y 4w xY wr)   )r6   iterr   r   rH   r!   r%   )	for_loopr@   rA   rB   rh   iteratorcounterrV   r
  s	            r   evaluate_forr  !  s    FHMM5,MH (//7E<NMM 
	D*4lS*(F	
	  M " 
 	M % s   	A!!	A:/A:9A:c                 B      fd  j                   d|      S )Nc                 ,  	 |t        |       k\  rt        j                  |
      gS | |   }t        |j                  |
      }g }|D ]  }|j	                         	t        |j                  t        j                        r:t        |j                  j                        D ]  \  }}||   	|j                  <    n|	|j                  j                  <   t        
	fd|j                  D              s|j                   | |dz   	              |S )Nc              3   :   K   | ]  }t        |        y wr)   r6   r   	if_clauserB   rJ   rA   s     r   r   z<evaluate_listcomp.<locals>.inner_evaluate.<locals>.<genexpr>C  s     qV_<	9lLQq   rQ   )r\   r6   r   r  rF   r/   r   r9   r   r   r   r   r   ifsr   )
generatorsr   current_state	generator
iter_valuerh   r*   idxr   rJ   rB   inner_evaluatelistcomprA   s            @r   r  z)evaluate_listcomp.<locals>.inner_evaluate6  s    C
O# }lLYZZu%	!)..-|\
 	PE%**,I)**CII6!*9+;+;+@+@!A 4IC).sIdgg&4 27	)**--.qclcpcpqqnZINO	P r   r   )r  )r   r@   rA   rB   r  s   ` ``@r   evaluate_listcompr!  5  s    " (--q%88r   c                    	 | j                   D ]  }t        ||||        	 | j                  r | j                  D ]  }t        ||||        	 | j                  r | j                  D ]  }t        ||||        y y # t        $ r}d}| j                  D ]n  }|j
                  #t        |t        |j
                  |||            s2d}|j                  r|||j                  <   |j                   D ]  }t        ||||         n |s|Y d }~d }~ww xY w# | j                  r | j                  D ]  }t        ||||        w w xY wr   )	rH   r6   r  	Exceptionhandlersro   r/   re   	finalbody)try_noder@   rA   rB   ri   rj   matchedhandlers           r   evaluate_tryr)  J  sb   FMM 	BDulLA	B ??  FT5,EF  ** FT5,EF #  (( 	G||#z!\',,PUWceq5r's<<*+E',,'#LL JD ulLIJ	 G "  ** FT5,EF s5   A; ,D ;	D?DADD DD .Ec                     | j                   t        | j                   |||      }nd }| j                  t        | j                  |||      }nd }|||||t        d      )Nz5Re-raise is not supported without an active exception)excr6   causer   )
raise_noder@   rA   rB   r+  r,  s         r   evaluate_raiser.  d  sp    ~~!:>>5,M#Z--ulLQ
5 IVWWr   c                     t        | j                  |||      }|s\| j                  r#t        | j                  |||      }t        |      t	        j
                  | j                        }t        d|       y )NzAssertion failed: )r6   rR   msgAssertionErrorr9   unparse)assert_noder@   rA   rB   r  r0  	test_codes          r   evaluate_assertr5  v  so    {//lSK??{|\RC %% K$4$45I #5i[!ABB r   c                 l   g }| j                   D ]  }t        |j                  |||      }|j                  rP|j	                         ||j                  j
                  <   |j                  ||j                  j
                            w|j	                         }|j                  |        	 | j                  D ]  }t        ||||        	 t        |      D ]  }	|	j                  d d d         y # t        $ r<}
t        |      D ](  }	|	j                  t        |
      |
|
j                         *  d }
~
ww xY wr)   )r]   r6   context_exproptional_vars	__enter__r   r   rH   reversed__exit__r#  ro   __traceback__)	with_noder@   rA   rB   contextsitemr7  context_varri   contextrj   s              r   evaluate_withrB    s"   H )#D$5$5ulLY+7+A+A+CE$$$''(OOE$"4"4"7"789&002KOOK()	/NN 	BDulLA	B  ) 	/GT4.	/  ) 	:GT!Wa9	:s   *C. .	D377D..D3c           	         fd}t        | t        j                        rz| j                  D ]j  } ||j                        r3t        |j                        }|||j                  xs |j                  <   Ht        d|j                   dt                      y t        | t        j                        r || j                        r|t        | j                  | j                  D cg c]  }|j                   c}      }| j                  D ]3  }t        ||j                        ||j                  xs |j                  <   5 y t        d| j                   d      y c c}w )Nc                     | j                  d      }t        dt        |      dz         D cg c]  }dj                  |d |        }}t	        fd|D              S c c}w )Nr   rQ   c              3   &   K   | ]  }|v  
 y wr)   r   )r   subpathauthorized_importss     r   r   zBimport_modules.<locals>.check_module_authorized.<locals>.<genexpr>  s     PW700Ps   )r   ranger\   r   any)module_namemodule_pathr   module_subpathsrG  s       r   check_module_authorizedz/import_modules.<locals>.check_module_authorized  s^    !'',>CAs;GWZ[G[>\]388KO4]]PPPP ^s   Az
Import of z) is not allowed. Authorized imports are: )fromlistzImport from z is not allowed.)r/   r9   Importnamesre   r   asnamer   r   
ImportFrommodule
__import__r   )r?   r@   rG  rM  aliasrS  s     `   r   import_modulesrV    s6   Q
 *cjj)%% 	E&uzz2&uzz24:ell0ejj1& ,UVYZlVmUno 	 	J	/":#4#45
 1 1U_UeUe<fEUZZ<fgF#)) P4;FEJJ4Oell0ejj1P  #\*2C2C1DDT#UVV 
0<fs   %E
c           	      n  
 i }| j                   D ]  }t        |j                  |      }|D ]  }|j                         
t	        |j
                  |
       t        
fd|j                  D              sMt        | j                  
      }t        | j                  
      }	|	||<     |S )Nc              3   :   K   | ]  }t        |        y wr)   r  r  s     r   r   z$evaluate_dictcomp.<locals>.<genexpr>  s     kV_<	9lLQkr  )
r  r6   r  rF   r   r   r   r  ru   r*   )dictcompr@   rA   rB   rh   genr  r*   ru   r   rJ   s     ``      @r   evaluate_dictcompr[    s    F"" "!#((E<N
 	"E

Icjj%L,Okcfcjcjkk"8<<L,W"8>>9lLY!s	"" Mr   r?   r@   rA   rB   rG  c                    t         t        k\  rt        dt         d      t         dz  a t        | t        j
                        rt        |       S t        | t        j                        rt        |       S t        | t        j                        rt        |       S t        | t        j                        r| j                  S t        | t        j                        r t        fd| j                  D              S t        | t        j                   t        j"                  f      rt%        |       S t        | t        j&                        rt)        |       S t        | t        j*                        rt-        | j                        S t        | t        j.                        rt1        |       S t        | t        j2                        r
t5               t        | t        j6                        r
t9               t        | t        j:                        rt=        |       S t        | t        j>                        rtA        |       S t        | t        jB                        rtE        |       S t        | t        jF                        rtI        |       S t        | t        jJ                        r_| jL                  D cg c]  }t-        |       }}| jN                  D cg c]  }t-        |       }}tQ        tS        ||            S t        | t        jT                        rt-        | j                        S t        | t        jV                        rtY        |       S t        | t        jZ                        rt-        | j                        S t        | t        j\                        rt_        |       S ta        t        d      r2t        | t        jb                        rt-        | j                        S t        | t        jd                        r>djg                  | jN                  D cg c]  }ti        t-        |             c}      S t        | t        jj                        r&| j                  D 	cg c]  }	t-        |	       c}	S t        | t        jl                        rto        |       S t        | t        jp                        rts        |       S t        | t        jt                        rJt-        | jv                        }
|
rt-        | jx                        S t-        | jz                        S t        | t        j|                        r.t-        | j                        }t        || j                        S t        | t        j                        r}t        | j                  t-        | j                        nd| j                  t-        | j                        nd| j                  t-        | j                              S d      S t        | t        j                        rt        |       S t        | t        j                        rt        |       S t        | t        j                  t        j                  f      rt        | |      S t        | t        j                        rt        |       S t        | t        j                        rt        |       S t        | t        j                        rt        |       S t        | t        j                        rt        |       S t        | t        j                        rt        |       S t        | t        j                        r&| j                  D 	ch c]  }	t-        |	       c}	S t        | t        j                        r3t        | j                  rt-        | j                              d      t        | j                  j                   d      c c}w c c}w c c}w c c}	w c c}	w )	a   
    Evaluate an abstract syntax tree using the content of the variables stored in a state and only evaluating a given
    set of functions.

    This function will recurse trough the nodes of the tree provided.

    Args:
        expression (`ast.AST`):
            The code to evaluate, as an abstract syntax tree.
        state (`Dict[str, Any]`):
            A dictionary mapping variable names to values. The `state` is updated if need be when the evaluation
            encounters assignements.
        static_tools (`Dict[str, Callable]`):
            Functions that may be called during the evaluation. Trying to change one of these static_tools will raise an error.
        custom_tools (`Dict[str, Callable]`):
            Functions that may be called during the evaluation. These static_tools can be overwritten.
        authorized_imports (`List[str]`):
            The list of modules that can be imported by the code. By default, only a few safe modules are allowed.
            Add more at your own risk!
    z(Reached the max number of operations of ze. Maybe there is an infinite loop somewhere in the code, or you're just asking too many calculations.rQ   c              3   :   K   | ]  }t        |        y wr)   r  )r   r   rB   r@   rA   s     r   r   zevaluate_ast.<locals>.<genexpr>  s     ec\#ulLIer  Indexr   Nr5   )\OPERATIONS_COUNTMAX_OPERATIONSr   r/   r9   r|   r   	AugAssignr   Callr   Constantr*   r   rp   r   ListCompGeneratorExpr!  UnaryOprC   r   r6   BoolOpr   Breakr!   Continuer%   BinOpr   Comparer  LambdarO   r{   rx   r   r   rI   r[   rG   ExprForr  FormattedValueIfr  r1   r^  	JoinedStrr   r   r   r~   r   r   r   IfExprR   rH   r  r   r   r   Slicer   lowerupperstepDictCompr[  WhilerW   rO  rR  rV  ClassDefr   Tryr)  Raiser.  Assertr5  WithrB  SetReturnr'   r>   r   )r?   r@   rA   rB   rG  kr   vrI   r   test_valr*   s    ```        r   r6   r6     s   8 >)6~6F  Gl  m
 	
 *cjj) z5,MM	J	.!*e\<PP	J	)ZlKK	J	-	J			*eU_UdUdeee	Js/?/? @	A UL,OO	J	,
E<NN	J	,J,,e\<PP	J

	+z5,MM	J			*	J	-!!	J			*j%|LL	J	,!*e\<PP	J

	+z5,MM	J	0$ZlSS	J	)LVOO\qQ|\B\\NXN_N_`,q%|D``Cf%&&	J	)J,,e\<PP	J	(J|\JJ	J 2 2	3J,,e\<PP	J	':ulLII	g	:j#))#DJ,,e\<PP	J	.wwYcYjYjkTULE<NOkll	J	)PZP_P_`S%|D``	J	)ZlKK	J	.!*e\<PP	J			*
lS
lSS
 1 15,UU	J	.Z--ulLQujoo..	J			*+ ))5,M+ ))5,MPZP_P_PkL%|L
 	
 rv
 	
 
J	- UL,OO	J			*j%|LL	JS^^ <	=j%1CDD	J	-!*e\<PP	J	(J|\JJ	J			*j%|LL	J

	+z5,MM	J	)ZlKK	J	(PZP_P_`S%|D``	J

	+Q[QaQaL))5,M
 	
gk
 	

 *"6"6"?"?!@@RSTTO ]`" l aV as   `?4a	a	a8aprint_outputsmax_len_outputsreturnc                 :    t        |       |k  r| S d| d |  d| dS )NzPrint outputs:
z6
_Print outputs have been truncated over the limit of z characters._
)r\   )r  r  s     r   truncate_print_outputsr  S  sI    
=O+!-0@"A!BBy  {J  zK  KZ  [  	[r   codec                    	 t        j                  |       }|i }|i }|i }d}dada	 |j
                  D ]  }t        |||||      } t        t        t              |d<   |S # t        $ r}t        d|       d}~ww xY w# t        $ rD}t        t        t              }	|	dt        j                  |        d| z  }	t        |	      d}~ww xY w)	a  
    Evaluate a python expression using the content of the variables stored in a state and only evaluating a given set
    of functions.

    This function will recurse through the nodes of the tree provided.

    Args:
        code (`str`):
            The code to evaluate.
        static_tools (`Dict[str, Callable]`):
            The functions that may be called during the evaluation.
            These tools cannot be overwritten in the code: any assignment to their name will raise an error.
        custom_tools (`Dict[str, Callable]`):
            The functions that may be called during the evaluation.
            These tools can be overwritten in the code: any assignment to their name will overwrite them.
        state (`Dict[str, Any]`):
            A dictionary mapping variable names to values. The `state` should contain the initial inputs but will be
            updated by this function to contain all variables as they are evaluated.
            The print outputs will be stored in the state under the key 'print_outputs'.
    z.The code generated by the agent is not valid.
Nr   r   )r  r  z.EXECUTION FAILED:
Evaluation stopped at line 'z"' because of the following error:
)r9   parseSyntaxErrorr   r_  rH   r6   r  MAX_LEN_OUTPUTr   get_source_segment)
r  rA   rB   r@   rG  r?   rj   rh   rV   r0  s
             r   evaluate_python_coder  Z  s   6QYYt_
 }FM$OO 	_D!$|\K]^F	_!7We!fo#  QKA3OPPQ$  $$]NS@AWAWX\^bAc@d  eH  IJ  HK  L  	Ls##$s.   A$ 9B $	B -A;;B 	C?CC)Er9   builtinsr   collections.abcr   	importlibr   typingr   r   r   r   r	   numpyr   utilsr   pandasr   
ValueErrorr   dirr/   r   ro   
issubclassBaseExceptionr   LIST_SAFE_MODULESr   r  r_  r`  r#  r!   r%   r'   r3   rC   rO   rW   rm   rv   rx   r   r   r   r   r   r   r   r   r   r  r  r  r!  r)  r.  r5  rB  rV  r[  ASTr   r6   r   r  r  )re   s   0r   <module>r     s[  "    # # 6 6  ' 	z 	 H
'(D)40ZRV@WYf5g 	'(D
!!
  !* ~#.   .	Y 			 	i 
9h	"-`<'
,:z
eD&)0L^G@
H&MR (9*F4X$	C/.4& %6OUOUS>OU sH}%OU sH}%	OU
 S	OUd GU [# [ [Y\ [ 3726&*$52$
2$4X./2$ 4X./2$ DcN#	2$
 S	2$a
s   1E)