o
    iM                     @   sd  d dl Z d dlZd dlmZmZ d dlmZmZmZm	Z	m
Z
mZmZmZmZmZ ddlmZmZmZmZmZmZ ddlmZmZ d dlmZ ded	ee fd
dZded	ee fddZded	efddZedded	e ej! fddZ"e j#e j$e j%e j&fZ'e'e j(e j)e j*e j+f Z,e j-e j.e j/e j0e j1e j2e j3e j4e j5e j6e j7e j8e j9dZ:G dd de;Z<dS )    N)CodeType	FrameType)
AnyCallableIteratorOptionalSequenceSetTupleTypeUnioncast   )EnhancedASTNotOneValueFoundSourceonlyfunction_node_typesassert_)
KnownIssueVerifierFailure)	lru_cachenodereturnc                 c   s"    	 t | dr| j} | V  nd S q)NTparent)hasattrr   r    r   Z/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/executing/_position_node_finder.pyparents   s   
r   c                 c   s    | V  t | E d H  d S N)r   r   r   r   r   node_and_parents   s   r!   c                 C   s  t | tjr
| j}n;t | tjr| j}n1t | tjr&| jp$| j	dd }nt | tj
tjtjfr5| j}nt | tjrA| jp?d}ntd|dr|ds| j| }}t |tjr`||jvsyt|dsh	 |S |j|}}t |tjr`||jvr`|jd}|dkrd| | S |S )	z

    Parameters:
        node: the node which should be mangled
        name: the name of the node

    Returns:
        The mangled name of `node`
    .r   exczno node to mangle__r   _ )
isinstanceast	AttributeattrNameidaliasasnamenamesplitFunctionDefClassDefAsyncFunctionDefExceptHandler	TypeError
startswithendswithr   basesr   lstrip)r   r/   r   child
class_namer   r   r   mangled_name   s.   

	r<      codec                 C   s   t tj| ddS )NT)show_caches)listdisget_instructions)r>   r   r   r   rB   C   s   rB   )z***@z///%+-z<<z>>&^|c                
   @   s   e Zd ZdZdedee dejde	de
f
ddZd	ed
e	ddfddZd	edejddfddZedejd	edefddZd	edejddfddZd
e	dejfddZd
e	defddZdejejejejffd
e	dee deedf defdd ZdS )!PositionNodeFindera%  
    Mapping bytecode to ast-node based on the source positions, which where introduced in pyhon 3.11.
    In general every ast-node can be exactly referenced by its begin/end line/col_offset, which is stored in the bytecode.
    There are only some exceptions for methods and attributes.
    framestmtstreelastisourcec                 C   s   t |j| _|| _d | _| |dkr|d8 }| |dksz| || _W n+ tyQ   | |dv r9t	j
f}n| |dkrEt	jf}n | j|d|d| _Y nw | | j| | | | j| | jd u rt| | j| | d S d S )NCACHE   )LOAD_METHOD	LOAD_ATTR
STORE_ATTRDELETE_ATTRCALL)end_col_offset
end_lineno)match_positionstyp)rB   f_codebc_listrQ   	decoratoropname	find_noderesultr   r(   r)   Callknown_issuesinstructiontest_for_decoratorverify)selfrM   rN   rO   rP   rQ   r\   r   r   r   __init__n   s2   



zPositionNodeFinder.__init__r   indexr   Nc                 C   s   t |jtjtfrW||jjv rY|j}	 | |d dks#| |dkr%d S |d7 }| |dv r;|d7 }| |dv s0| |drR| ||krR|| _	|| _
d S |d7 }qd S d S )NT   PRECALLrX   rS   )rR   EXTENDED_ARGSTORE_)r'   r   r(   r2   r   decorator_listr`   r6   ra   rb   r_   )rh   r   rj   	node_funcr   r   r   rf      s*    z%PositionNodeFinder.test_for_decoratorre   c                 C   s   |j dv r<t|tr<t|tr6dd t|jD }t|d t|dkr0t	t
|d  }| _ntd| td| t|tjrFtd	td
d t|D rUtd|j dkrc|jdkretdd S d S )N
COMPARE_OPIS_OPCONTAINS_OPc                 S   s*   g | ]}t |tjrt|jd kr|qS )r   )r'   r(   Comparelenops.0nr   r   r   
<listcomp>   s    
z3PositionNodeFinder.known_issues.<locals>.<listcomp>z expected at least one comparisonr   r   z4multiple chain comparison inside %s can not be fixedz+chain comparison inside %s can not be fixedassertc                 s   s    | ]	}t |tjV  qd S r    )r'   r(   patternrx   r   r   r   	<genexpr>   s    z2PositionNodeFinder.known_issues.<locals>.<genexpr>z)pattern matching ranges seems to be wrong
STORE_NAME__classcell__zstore __classcell__)r`   r'   types_cmp_issuetypes_cmp_issue_fixr(   walktestr   rv   r   r   rb   r   Assertanyr!   argval)rh   r   re   comparisonsr   r   r   rd      s*   
	

zPositionNodeFinder.known_issuesinstc                    s(    j dvrdS t fddt|D S )N)r   
STORE_FASTSTORE_DEREFSTORE_GLOBALDELETE_NAMEDELETE_FASTDELETE_GLOBALFc                 3   s*    | ]}t |tjot| jkV  qd S r    )r'   r(   r4   r<   r   rx   r   r   r   r~   &  s
    
z7PositionNodeFinder.is_except_cleanup.<locals>.<genexpr>)r`   r   r   )r   r   r   r   r   is_except_cleanup  s
   
	z$PositionNodeFinder.is_except_cleanupc                    s   j }dd }td}dtttt f dtdtf fdd}d	ttttd
f f dtdtffdd}|dkr9dS |drG|t	j
t	jfrGdS |drY|t	jt	jt	jt	jfrYdS |drg|t	jt	jfrgdS |drr|t	jrrdS |dddr|t	jrdS |ddds|dr|t	jt	jt	jdrttt	jtt	jjjtrdS |dr|t	jrdS |  rdS |dr|t	j jt	jdrdS |dr|t	js|t	jt	jdrdS |dr|t	j
rdS |dd dr|t	jrdS |d!r|t	j r jt!krdS |d"r(|t	j"t	j#fr(t$ fd#d$tt	j"j%D r(dS |d%rK|t	j&t	jt	j'fsA|t	jt	j(d&rK jt!krKdS 	 |d,ra|t	j,t- j./d- dradS |t	j0t	jd&ru|d.t!drudS |d/r|t	j1rdS |d0r|t	j2t	jd&rdS |t	jt	j3d&r|d1t!drdS |t	jt	jd&r|d2t!drdS td}td|4d3rt	j2}t	j3}n|4d4rt	j}t- j. fd5d}nn|4d6rt	j5}t6t	j7t	j8t	j9t	j:d7| fd8d}nP|d9v rt	j0}t	j3} fd:d}n>|d;v r&t	j}t	j3} fd<d}n,|dv r3t	j}d=d }n|4d>r@t	j(}t	j2}n|4d?rRt	j(}t	j0} fd@d}t;dAd}	|tdupjt<dA pjt|	|}
t|r{|
r{|r{dS dBtj= j f }t>| )CzE
        checks if this node could gererate this instruction
        c                 S   s   dS )NTr   er   r   r   <lambda>1  s    z+PositionNodeFinder.verify.<locals>.<lambda>Nopnameskwargsr   c                    s0   t | tr| g}  j| v o| fdd|D kS )a)  
            match instruction

            Parameters:
                opnames: (str|Seq[str]): inst.opname has to be equal to or in `opname`
                **kwargs: every arg has to match inst.arg

            Returns:
                True if all conditions match the instruction

            c                    s   i | ]}|t  |qS r   getattr)ry   kre   r   r   
<dictcomp>C  s    zAPositionNodeFinder.verify.<locals>.inst_match.<locals>.<dictcomp>)r'   strr`   )r   r   r   r   r   
inst_match4  s
   
z-PositionNodeFinder.verify.<locals>.inst_match	node_type.c                    s$   t  | ot fdd| D S )a  
            match the ast-node

            Parameters:
                node_type: type of the node
                **kwargs: every `arg` has to be equal `node.arg`
                        or `node.arg` has to be an instance of `arg` if it is a type.
            c                 3   s<    | ]\}}t |trt t ||nt ||kV  qd S r    )r'   typer   )ry   r   vr   r   r   r~   P  s    
z@PositionNodeFinder.verify.<locals>.node_match.<locals>.<genexpr>)r'   allitems)r   r   r   r   r   
node_matchG  s   	z-PositionNodeFinder.verify.<locals>.node_matchrR   rX   )rX   	LOAD_FAST)rX   CALL_FUNCTION_EXrq   	LOAD_NAME__annotations__)r   rT   join)rX   BUILD_STRING)leftopSTORE_SUBSCR)r   r   )r,   ctxr   )r   )BEFORE_WITHWITH_EXCEPT_START)r   r   __doc__)r   r   r   r   )r   r   r   r   c                 3   s$    | ]}t tt| jkV  qd S r    )r<   r   r   r   )ry   r-   r   r   r   r~     s   " z,PositionNodeFinder.verify.<locals>.<genexpr>)r   r   r   r   )r   F)r   r   )r/   rr   z==	BINARY_OP=rW   )JUMP_IF_TRUE_OR_POPJUMP_IF_FALSE_OR_POPDELETE_SUBSCR)r   r   LOAD_GLOBAL)r   r   )BINARY_SUBSCRzSLICE+BINARY_c                       t ttj| j S r    )r'   r   r(   BinOpr   r   op_typer   r   r         UNARY_)UNARY_POSITIVEUNARY_NEGATIVE	UNARY_NOTUNARY_INVERTc                    r   r    )r'   r   r(   UnaryOpr   r   r   r   r   r     r   )rU   rT   LOOKUP_METHODc                       t |  jkS r    r<   r   r   r   r   r   r         )r   r   r   
LOAD_DEREFLOAD_CLASSDEREFc                    s   t tj| j jkS r    )r   r(   r+   r,   r   r   r   r   r   r     r   c                 S   s   t ttj| jdkS )Nr   )rv   r   r(   ru   rw   r   r   r   r   r     s    )STORE_SLICEr   rV   c                    r   r    r   r   r   r   r   r     r   r   zast.%s is not created from %s)?r`   r   r   r   r   r   boolr   r
   r(   With	AsyncWithListCompGeneratorExpSetCompDictCompr2   rc   ru   	AnnAssignr   ConstantModr'   r   r   valuer   r+   r   Del	JoinedStrr4   r<   Import
ImportFromr   namesr1   r3   StoreMatchAsMatchSequence
MatchValue	AugAssignop_type_mapargreprremovesuffixr)   BoolOp	SubscriptLoadr6   r   dictUAddUSubNotInvertr   r   __name__r   )rh   r   re   op_nameextra_filterr   r   r   r\   node_ctx	ctx_matchtitler   )re   r   r   r   rg   +  s0  &*




	
	





zPositionNodeFinder.verifyc                 C   s   | j |d  S )NrS   )r^   rh   rj   r   r   r   re   #  s   zPositionNodeFinder.instructionc                 C   s   |  |jS r    )re   r`   r   r   r   r   r`   &  s   zPositionNodeFinder.opname)linenorZ   
col_offsetrY   r[   r\   .c                    sF   |  |jd urjd usJ t fdd| jjj D S )Nc                 3   sT    | ]% t  rt  tjst  tjst fd dD rtt V  qdS )c                 3   s$    | ]}t |t  |kV  qd S r    r   )ry   r*   )r   positionr   r   r~   9  s
    
z9PositionNodeFinder.find_node.<locals>.<genexpr>.<genexpr>N)r'   r(   Exprr   r   r   r   )ry   r[   r   r\   r   r   r~   2  s    


z/PositionNodeFinder.find_node.<locals>.<genexpr>)re   	positionsr   r   rQ   _nodes_by_line)rh   rj   r[   r\   r   r   r   ra   )  s
   zPositionNodeFinder.find_node)r   
__module____qualname__r   r   r	   r   r(   Moduleintr   ri   rf   rA   Instructionrd   staticmethodr   r   rg   re   r   r`   exprstmtexcepthandlerr}   r   tupler   ra   r   r   r   r   rL   g   s,    $2+;$ y
rL   )=r(   rA   typesr   r   typingr   r   r   r   r   r	   r
   r   r   r   	executingr   r   r   r   r   r   _exceptionsr   r   	functoolsr   r   r!   r   r<   r@   r   rB   IfExpIfr   Whiler   r   r   r   r   r   PowMultMatMultFloorDivDivr   AddSubLShiftRShiftBitAndBitXorBitOrr   objectrL   r   r   r   r   <module>   sL    0 )