o
    hՕ                     @   sV  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 r7d dlZG dd deZd	d
 eeD Zg dZd\aZd\aZG dd deZG dd deZG dd deZdd Z dd Z!dd Z"dd Z#dd Z$dd Z%d d! Z&d"d# Z'd$d% Z(d&d' Z)d(d) Z*d*d+ Z+d,d- Z,d.d/ Z-d0d1 Z.d2d3 Z/d4d5 Z0d6d7 Z1d8d9 Z2d:d; Z3d<d= Z4d>d? Z5d@dA Z6dBdC Z7dDdE Z8dFdG Z9efdHe j:dIe
e;ef dJe
e;e	f dKe
e;e	f dLee; f
dMdNZ<efdOe;dPe=dQe;fdRdSZ>dddefdTe;dJee
e;e	f  dKee
e;e	f  dIee
e;ef  dLee; f
dUdVZ?dS )W    N)Mapping)import_module)AnyCallableDictListOptional   )is_pandas_availablec                   @   s   e Zd ZdZdS )InterpreterErrorz
    An error raised when the interpreter cannot evaluate a Python expression, due to syntax error or unsupported
    operations.
    N)__name__
__module____qualname____doc__ r   r   z/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/agents/python_interpreter.pyr   !   s    r   c                 C   s8   i | ]}t tt|trttt|tr|tt|qS r   )
isinstancegetattrbuiltinstype
issubclassBaseException).0namer   r   r   
<dictcomp>*   s    
r   )
randomcollectionsmathtimequeue	itertoolsrestat
statisticsunicodedata) iP  )r   i c                   @      e Zd ZdS )BreakExceptionNr   r   r   r   r   r   r   r'   B       r'   c                   @   r&   )ContinueExceptionNr(   r   r   r   r   r*   F   r)   r*   c                   @   s   e Zd Zdd ZdS )ReturnExceptionc                 C   s
   || _ d S N)value)selfr-   r   r   r   __init__K   s   
zReturnException.__init__N)r   r   r   r/   r   r   r   r   r+   J   s    r+   c                 C   s(   t | tr| S t| drt| S td)N__iter__zObject is not iterable)r   listhasattrr   )objr   r   r   get_iterableO   s
   

r4   c                 C   st   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   r   r   r   evaluate_unaryopX   s   rC   c                    s*   dd j j D   fdd}|S )Nc                 S      g | ]}|j qS r   argr   rF   r   r   r   
<listcomp>g       z#evaluate_lambda.<locals>.<listcomp>c                     s4     }t | D ]\}}|||< q	tj|S r,   )copyzipr6   body)values	new_staterF   r-   argsrB   lambda_expressionr@   rA   r   r   lambda_funci   s   
z$evaluate_lambda.<locals>.lambda_func)rP   )rQ   r@   rA   rB   rR   r   rO   r   evaluate_lambdaf   s   rS   c              	   C   s   d}d}t | j|||rG| jD ]}z	t |||| W q ty%   Y  d S  ty.   Y  nw |d7 }||kr?td| dt | j|||sd S )Ni  r      zMaximum number of z" iterations in While loop exceeded)r6   testrL   r'   r*   r   )
while_loopr@   rA   rB   max_iterations
iterationsnoder   r   r   evaluate_whiler   s    
rZ   c                    s    fdd}|S )Nc               
      sf    }dd jjD } fddjjD }tt|t| d  |}t|| D ]\}}|||< q-| D ]\}}|||< q:jjrPjjj}| ||< jj	r]jj	j}	|||	< | D ]\}}||vrm|||< qajjrjjd jdkr| r| d |d< | d j
|d< d }
zjD ]	}t|| }
qW |
S  ty } z
|j}
W Y d }~|
S d }~ww )Nc                 S   rD   r   rE   rG   r   r   r   rH      rI   z5create_function.<locals>.new_func.<locals>.<listcomp>c                       g | ]	}t | qS r   r6   )r   drB   r@   rA   r   r   rH          r   r.   r>   )rJ   rP   defaultsdictrK   lenitemsvarargrF   kwargr>   rL   r6   r+   r-   )rP   kwargs
func_state	arg_namesdefault_valuesr`   r   r-   vararg_name
kwarg_nameresultstmterB   func_defr@   rA   r   r   new_func   sB   




z!create_function.<locals>.new_funcr   )rp   r@   rA   rB   rq   r   ro   r   create_function   s   ,rr   c                 C   s.   i }|  D ]\}}|||< qt| t||S r,   )rc   r   tuple)
class_nameclass_bases
class_body
class_dictkeyr-   r   r   r   create_class   s   
ry   c                 C   s   t | ||||| j< || j S r,   )rr   r   )rp   r@   rA   rB   r   r   r   evaluate_function_def   s   
rz   c           
         s   | j } fdd| jD }i }| jD ]K}t|tjr't| ||j < qt|tjrW|jD ]%}t|tj	rDt
|j ||j< q0t|tjrUt
|j ||j< q0qtd|jj t|t||}	|	|< |	S )Nc                    r[   r   r\   )r   baser^   r   r   rH      r_   z&evaluate_class_def.<locals>.<listcomp>z%Unsupported statement in class body: )r   basesrL   r   r9   FunctionDefrz   AssigntargetsNamer6   r-   id	Attributeattrr   r>   r   r   rs   )
	class_defr@   rA   rB   rt   r|   rw   rm   target	new_classr   r^   r   evaluate_class_def   s$   

r   c                    s   fdd| j }t| j }t| jtjr9t|tr4t|ts/td| d|| }n|| }nt| jtj	rE|| }nt| jtj
rQ|| }nxt| jtjr]|| }nlt| jtjri|| }n`t| jtjru|| }nTt| jtjr|| }nHt| jtjr||@ }n<t| jtjr||B }n0t| jtjr||A }n$t| jtjr||> }nt| jtjr||? }ntdt| jj dt| j |  |S )Nc                    s   t | tjr| jdS t | tjr't| j }t| j }|| S t | tj	r;t| j }t
|| jS t | tjrMtfdd| jD S t | tjr]fdd| jD S td)Nr   c                 3   s    | ]} |V  qd S r,   r   r   eltget_current_valuer   r   	<genexpr>       z@evaluate_augassign.<locals>.get_current_value.<locals>.<genexpr>c                    s   g | ]} |qS r   r   r   r   r   r   rH          zAevaluate_augassign.<locals>.get_current_value.<locals>.<listcomp>z3AugAssign not supported for {type(target)} targets.)r   r9   r   getr   	Subscriptr6   r-   slicer   r   r   Tuplers   eltsr   r   )r   r3   rx   rB   r   r@   rA   r   r   r      s   z-evaluate_augassign.<locals>.get_current_valuezCannot add non-list value z to a list.z
Operation r5   )r   r6   r-   r   r8   r9   Addr1   r   SubMultDivModPowFloorDivBitAndBitOrBitXorLShiftRShiftr   r   	set_value)r?   r@   rA   rB   current_valuevalue_to_addupdated_valuer   r   r   evaluate_augassign   sD   















r   c                 C   sh   t | jtjr| jD ]}t||||s dS q
dS t | jtjr2| jD ]}t||||r/ dS q#dS d S NFT)r   r8   r9   AndrM   r6   Or)rY   r@   rA   rB   r-   r   r   r   evaluate_boolop  s   

r   c                 C   s@  t | j|||}t | j|||}t| jtjr|| S t| jtjr&|| S t| jtjr1|| S t| jtj	r<|| S t| jtj
rG|| S t| jtjrR|| S t| jtjr]|| S t| jtjrh||@ S t| jtjrs||B S t| jtjr~||A S t| jtjr||> S t| jtjr||? 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   NotImplementedErrorr   r   )binopr@   rA   rB   left_val	right_valr   r   r   evaluate_binop  s6   r   c           	      C   s   t | j|||}t| jdkr| jd }t||||| |S t| jt|kr7tdt| dt| j dg }| jD ]}t|tjrJ|	| q<|
| q<t| j|D ]\}}t||||| qV|S )NrT   r   zAssign failed: expected z values but got .)r6   r-   rb   r   r   r   r   r9   StarredextendappendrK   )	assignr@   rA   rB   rl   r   expanded_valuestgtvalr   r   r   evaluate_assignA  s   
 
r   c           	      C   s&  t | tjr| j|v rtd| j d||| j< d S t | tjr^t |ts;t|dr7t |tt	fs7t|}ntdt
| jt
|krHtdt| jD ]\}}t||| ||| qMd S t | tjrzt| j|||}t| j|||}|||< d S t | tjrt| j|||}t|| j| d S d S )NzCannot assign to name 'z,': doing this would erase the existing tool!r0   zCannot unpack non-tuple valuez!Cannot unpack tuple of wrong size)r   r9   r   r   r   r   rs   r2   strbytesrb   r   	enumerater   r   r6   r-   r   r   setattrr   )	r   r-   r@   rA   rB   ielemr3   rx   r   r   r   r   T  s,   


r   c                    s  t | jtjst | jtjstd| j dt | jtjr@t| jj }| jj}t	||s:td| d| t
||}n9t | jtjry| jj}|v rT| }n%|v r]| }n| v rf | }n|tv rot| }n
td| jj dg }| jD ]}t |tjr|t|j  q~|t|  q~g }| jD ]3}t |tjrt|j }	t	|	drt |	ttfrtd|	 ||	 q|t|  q fdd	| jD }
t |trt|jd
dkr||}t	|dr|j|i |
 |S |dkrT|s'dv r#dv r#td d S td|d }t |ts5tdt|dkr@t|S t|dkrP|d }t||S td|dkridtt|}t|d 7 ad S ||i |
}|S )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 r0   z!Cannot unpack non-iterable value c                    s    i | ]}|j t|j qS r   )rF   r6   r-   )r   keywordr^   r   r   r     s     z!evaluate_call.<locals>.<dictcomp>r   rT   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   r2   r   r   ERRORSrP   r   r   r   r   r   keywordsr   rb   r   split__new__r/   r   joinmapPRINT_OUTPUTS)callr@   rA   rB   r3   	func_namer   rP   rF   unpackedrf   clsinstanceoutputr   r^   r   evaluate_calll  sx   






"



r   c                 C   s  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rJ|| S t|trS|| S t|ttfr|t| |  krit|k svn td| dt| |t| S t|trt| |  krt|k sn td| dt| || S ||v r|| S t|trt|trt|t| }t|dk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_LocIndexerr3   loc	DataFrameSeriesnpndarraygroupbygenericDataFrameGroupByr1   rs   rb   intr   difflibget_close_matcheskeys)	subscriptr@   rA   rB   indexr-   parent_objectclose_matchesr   r   r   evaluate_subscript  s8   

 
 r   c                 C   s|   | j |v r
|| j  S | j |v r|| j  S | j tv rt| j  S t| j t| }t|dkr5||d  S td| j  d)Nr   zThe variable `z` is not defined.)r   r   r   r   r1   r   rb   r   )r   r@   rA   rB   r   r   r   r   evaluate_name  s   





r   c                    sd  t | j } fdd| jD }dd | jD }d}|}t||D ]~\}	}
|	tjkr3||
k}na|	tjkr=||
k}nW|	tjkrG||
k }nM|	tj	krQ||
k}nC|	tj
kr[||
k}n9|	tjkre||
k}n/|	tjkro||
u }n%|	tjkry||
u}n|	tjkr||
v }n|	tjkr||
v}ntd|	 ||@ }|
}t|tr|s nq%t|ttjfr|S | S )Nc                    r[   r   r\   )r   cr^   r   r   rH     r_   z&evaluate_condition.<locals>.<listcomp>c                 S   s   g | ]}t |qS r   )r   )r   r8   r   r   r   rH     r   TzOperator not supported: )r6   r   comparatorsopsrK   r9   EqNotEqLtLtEGtGtEIsIsNotInNotInr   r   boolr   r   all)	conditionr@   rA   rB   r   r   r   rl   current_leftr8   
comparatorcurrent_resultr   r^   r   evaluate_condition  sB   



















r   c                 C   sl   d }t | j|||}|r!| jD ]}t ||||}|d ur|}q|S | jD ]}t ||||}|d ur3|}q$|S r,   )r6   rU   rL   orelse)if_statementr@   rA   rB   rl   test_resultlineline_resultr   r   r   evaluate_if  s   

r  c           	   
   C   s   d }t | j|||}|D ]4}t| j|||| | jD ]%}zt ||||}|d ur*|}W q ty7   Y   |S  ty?   Y qw q|S r,   )r6   iterr   r   rL   r'   r*   )	for_loopr@   rA   rB   rl   iteratorcounterrY   r  r   r   r   evaluate_for!  s&   
r  c                    s     fddj d|S )Nc           	         s   |t | krtj|gS | | }t|j|}g }|D ]?}|  t|jtjr?t	|jj
D ]\}}||  |j< q2n| |jj< t fdd|jD r^|| |d   q|S )Nc                 3       | ]
}t | V  qd S r,   r\   r   	if_clauserB   rN   rA   r   r   r   C      z<evaluate_listcomp.<locals>.inner_evaluate.<locals>.<genexpr>rT   )rb   r6   r   r  rJ   r   r   r9   r   r   r   r   r   ifsr   )	
generatorsr   current_state	generator
iter_valuerl   r-   idxr   rB   inner_evaluatelistcomprA   )rN   r   r  6  s    z)evaluate_listcomp.<locals>.inner_evaluater   )r  )r  r@   rA   rB   r   r  r   evaluate_listcomp5  s   r  c                 C   s  z|z| j D ]	}t|||| qW nG tyW } z;d}| jD ]+}|jd u s/t|t|j|||rHd}|jr9|||j< |j D ]	}t|||| q< nq|sM|W Y d }~nd }~ww | jrh| jD ]	}t|||| q^W | jry| jD ]}t|||| qod S d S | jr| jD ]
}t|||| qw w r   )	rL   r6   	Exceptionhandlersr   r   r   r   	finalbody)try_noder@   rA   rB   rm   rn   matchedhandlerr   r   r   evaluate_tryJ  sD   

 




r  c                 C   sb   | j d urt| j |||}nd }| jd urt| j|||}nd }|d ur-|d ur+|||td)Nz5Re-raise is not supported without an active exception)excr6   causer   )
raise_noder@   rA   rB   r  r   r   r   r   evaluate_raised  s   

r"  c                 C   sP   t | j|||}|s&| jrt | j|||}t|t| j}td| d S )NzAssertion failed: )r6   rU   msgAssertionErrorr9   unparse)assert_noder@   rA   rB   r   r#  	test_coder   r   r   evaluate_assertv  s   r(  c              
   C   s   g }| j D ](}t|j|||}|jr$| ||jj< |||jj  q| }|| qz| jD ]	}t|||| q2W n ty[ }	 zt	|D ]}
|

t|	|	|	j qI d }	~	ww t	|D ]	}
|

d d d  q`d S r,   )rc   r6   context_exproptional_vars	__enter__r   r   rL   r  reversed__exit__r   __traceback__)	with_noder@   rA   rB   contextsitemr)  context_varrm   rn   contextr   r   r   evaluate_with  s*   

r4  c                    s    fdd}t | tjr3| jD ]!}||jr$t|j}|||jp"|j< qtd|j dt  d S t | tj	rh|| j
r_t| j
dd | jD d}| jD ]}t||j||jp[|j< qNd S td| j
 d	d S )
Nc                    s@   |  d  fddtdt d D }tfdd|D S )Nr   c                    s   g | ]}d   d| qS )r   N)r   )r   r   module_pathr   r   rH         zCimport_modules.<locals>.check_module_authorized.<locals>.<listcomp>rT   c                 3   s    | ]}| v V  qd S r,   r   )r   subpathauthorized_importsr   r   r     r   zBimport_modules.<locals>.check_module_authorized.<locals>.<genexpr>)r   rangerb   any)module_namemodule_subpathsr9  r5  r   check_module_authorized  s   
 z/import_modules.<locals>.check_module_authorizedz
Import of z) is not allowed. Authorized imports are: c                 S   rD   r   )r   )r   aliasr   r   r   rH     rI   z"import_modules.<locals>.<listcomp>)fromlistzImport from z is not allowed.)r   r9   Importnamesr   r   asnamer   r   
ImportFrommodule
__import__r   )r?   r@   r:  r?  r@  rF  r   r9  r   import_modules  s$   




rH  c           
         s   i }| j D ]>}t|j| }|D ]1}| t|j|  t fdd|jD rBt| j }t| j	 }	|	||< qq|S )Nc                 3   r	  r,   r\   r
  r  r   r   r     r  z$evaluate_dictcomp.<locals>.<genexpr>)
r  r6   r  rJ   r   r   r   r  rx   r-   )
dictcompr@   rA   rB   rl   genr  r-   rx   r   r   r  r   evaluate_dictcomp  s   
rK  r?   r@   rA   rB   r:  c           	         s@  t tkrtdt dt d7 a t| tjrt|  S t| tjr*t|  S t| tj	r7t
|  S t| tjr@| jS t| tjrTt fdd| jD S t| tjtjfrdt|  S t| tjrqt|  S t| tjrt| j S t| tjrt|  S t| tjrt t| tjrt t| tjrt|  S t| tjrt |  S t| tj!rt"|  S t| tj#rt$|  S t| tj%r fdd| j&D } fdd| j'D }t(t)||S t| tj*rt| j S t| tj+rt,|  S t| tj-r#t| j S t| tj.r1t/|  S t0td	rFt| tj1rFt| j S t| tj2r\d
3 fdd| j'D S t| tj4ro fdd| jD S t| tj5r}t6|  S t| tj7rt8|  S t| tj9rt| j: }|rt| j; S t| j< S t| tj=rt| j }t>|| j?S t| tj@rtA| jBdurt| jB nd| jCdurt| jC nd| jDdurt| jD S dS t| tjErtF|  S t| tjGrtH|  S t| tjItjJfr&tK| |S t| tjLr4tM|  S t| tjNrBtO|  S t| tjPrPtQ|  S t| tjRr^tS|  S t| tjTrltU|  S t| tjVr fdd| jD S t| tjWrtX| jrt| j dt| jYjZ d)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 through 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 assignments.
        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.rT   c                 3   r	  r,   r\   r   r^   r   r   r     r  zevaluate_ast.<locals>.<genexpr>c                    r[   r   r\   )r   kr^   r   r   rH   	  r_   z evaluate_ast.<locals>.<listcomp>c                    r[   r   r\   r   vr^   r   r   rH   
  r_   Indexr%   c                    s   g | ]}t t| qS r   )r   r6   rM  r^   r   r   rH     r7  c                    r[   r   r\   r   r^   r   r   rH     r_   Nc                    s   h | ]	}t | qS r   r\   r   r^   r   r   	<setcomp>I  r_   zevaluate_ast.<locals>.<setcomp>r5   )[OPERATIONS_COUNTMAX_OPERATIONSr   r   r9   r~   r   	AugAssignr   Callr   Constantr-   r   rs   r   ListCompGeneratorExpr  UnaryOprC   r   r6   BoolOpr   Breakr'   Continuer*   BinOpr   Comparer   LambdarS   r}   rz   r   r   rM   ra   rK   ExprForr  FormattedValueIfr  r2   rO  	JoinedStrr   r   r   r   r   r   IfExprU   rL   r   r   r   r   Slicer   lowerupperstepDictComprK  WhilerZ   rB  rE  rH  ClassDefr   Tryr  Raiser"  Assertr(  Withr4  SetReturnr+   r>   r   )	r?   r@   rA   rB   r:  r   rM   test_valr-   r   r^   r   r6     s   
	r6   print_outputsmax_len_outputsreturnc                 C   s*   t | |k r| S d| d |  d| dS )NzPrint outputs:
z6
_Print outputs have been truncated over the limit of z characters._
)rb   )rs  rt  r   r   r   truncate_print_outputsS  s   rv  codec           
   
   C   s   zt | }W n ty } ztd| d}~ww |du r!i }|du r'i }|du r-i }d}dadaz|jD ]
}t|||||}q7tttd|d< |W S  t	yp } ztttd}	|	dt 
| | d| 7 }	t	|	d}~w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   )rt  rs  z.EXECUTION FAILED:
Evaluation stopped at line 'z"' because of the following error:
)r9   parseSyntaxErrorr   rQ  rL   r6   rv  MAX_LEN_OUTPUTr   get_source_segment)
rw  rA   rB   r@   r:  r?   rn   rl   rY   r#  r   r   r   evaluate_python_codeZ  s4   
r|  )@r9   r   r   collections.abcr   	importlibr   typingr   r   r   r   r   numpyr   utilsr
   pandasr   
ValueErrorr   dirr   LIST_SAFE_MODULESr   rz  rQ  rR  r  r'   r*   r+   r4   rC   rS   rZ   rr   ry   rz   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r"  r(  r4  rH  rK  ASTr   r6   r   rv  r|  r   r   r   r   <module>   s   		0="O )



 	