a
    lcL                     @   s  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZmZ ddlmZ ddlmZ ddlmZ ddl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 m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& erdd	l'm(Z(m)Z) dd
l*m+Z+ ej,fZ-e	j.d dkr e-ej/f7 Z-e	j.d dkrpddlm0Z0 ddl1m2Z2 ddlm3Z3 ddl4m5Z5 e0ddZ6e7Z8n8ddl9m2Z2m:Z; ddlm<Z3 G dd de=Z5dd Z6e>Z8G dd dej?Z@e	j.dkrejAZBddlmCZD G dd deDZCn>G dd deddZCddlmEZEmFZFmGZGmHZHmIZImJZJ d d! ZBG d"d# d#eCZKddd%d&ZLd'd( ZAdZMG d)d* d*eNZOe$d+ZPd,d- ZQG d.d/ d/e=ZRG d0d1 d1e=ZSG d2d3 d3ejTZUeVd4d5 ejWD ZXd6d7 ZYd8ZZd9d: Z[G d;d< d<e=Z\d=d> Z]d?d@ Z^dAdB Z_dCdD Z`dEdF ZadGdH ZbdIdJ ZcdKdL ZddMdN Zee Zfe6dOdP ZgdQdR ZhdSdT ZidUdV ZjdWdX ZkdYdZ Zld[d\ Zmd]d^ Znd_d` Zoe	j.dakrdbdclpmqZr ne\ZrdS )ea+  
MIT License

Copyright (c) 2021 Alex Hall

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
    N)defaultdict
namedtuple)deepcopy)islice)
attrgetter)RLock)TYPE_CHECKINGAnyCallableDictIterableIteratorListOptionalSequenceSetSizedTupleTypeTypeVarUnioncast)	ASTTokensASTText)ASTTextBase   )	lru_cache)detect_encoding)zip_longest)Path)maxsize)r   	cookie_re)izip_longestc                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r&   r&   O/var/www/html/django/DPS/env/lib/python3.9/site-packages/executing/executing.pyr   B   s   r   c                    s    i  t  fdd}|S )Nc                     s$   |  v r |  S |   } | < |S Nr&   )argsresultdfuncr&   r'   wrapperJ   s    zcache.<locals>.wrapper)	functoolswraps)r-   r.   r&   r+   r'   cacheF   s    r1   c                   @   s   e Zd ZdZdS )EnhancedASTN)r#   r$   r%   parentr&   r&   r&   r'   r2   Z   s   r2   )r      )Instructionc                   @   s   e Zd ZdZdS r5   Nr#   r$   r%   linenor&   r&   r&   r'   r5   b   s   r5   c                   @   s   e Zd ZdZdS r6   r7   r&   r&   r&   r'   r5   e   s   z offset argval opname starts_line)HAVE_ARGUMENTEXTENDED_ARGhasconstopnamefindlinestartshasnamec                 c   s   | j }tt| }t|}d}d}||k r|}|| }t|}||}	d }
|d }|tkrt|| t||d  d  | }d}|d }|tkr|d }|tv r| j	| }
n*|t
v r| j| }
nt| dkr| j| }
t||
t| |	V  q"d S )Nr            i   	LOAD_FAST)co_codedictr=   lenordgetr9   r:   r;   	co_constsr>   co_namesr<   co_varnamesr5   )cocode
linestartsniextended_argoffsetcopr8   argvalZopargr&   r&   r'   _get_instructionsm   s2    
$
rU   c                   @   s   e Zd ZdZdS )EnhancedInstructionN)r#   r$   r%   _copiedr&   r&   r&   r'   rV      s   rV    c                 C   s   | st t|dS )z
    Like an assert statement, but unaffected by -O
    :param condition: value that is expected to be truthy
    :type message: Any
    N)AssertionErrorstr)	conditionmessager&   r&   r'   assert_   s    r]   c                 c   s@   | j }t| D ],}tt|}|jp$|}t| ||_|V  qd S r(   )co_firstlinenorU   r   rV   starts_liner]   r8   )rK   r8   instr&   r&   r'   get_instructions   s    

ra   c                       s    e Zd Zg f fdd	Z  ZS )NotOneValueFoundc                    s   || _ tt| | d S r(   )valuessuperrb   __init__)selfmsgrc   	__class__r&   r'   re      s    zNotOneValueFound.__init__)r#   r$   r%   re   __classcell__r&   r&   rh   r'   rb      s   rb   Tc                 C   sr   t | tr2t| dkr&tdt|  t| d S tt| d}t|dkrTtdt|dkrjtd||d S )Nr?   zExpected one value, found %sr   rA   zExpected one value, found 0z!Expected one value, found several)
isinstancer   rE   rb   listtupler   )itlstr&   r&   r'   only   s    

rq   c                   @   s   e Zd ZdZdd Zed ddZed!dd	Zed
d Zedd Z	edd Z
edd Zedd Zdd Zdd Zdd Zedd Zedd Zdd ZdS )"Sourcea  
    The source code of a single file and associated metadata.

    The main method of interest is the classmethod `executing(frame)`.

    If you want an instance of this class, don't construct it.
    Ideally use the classmethod `for_frame(frame)`.
    If you don't have a frame, use `for_filename(filename [, module_globals])`.
    These methods cache instances by filename, so at most one instance exists per filename.

    Attributes:
        - filename
        - text
        - lines
        - tree: AST parsed from text, or None if text is not valid Python
            All nodes in the tree have an extra `parent` attribute

    Other methods of interest:
        - statements_at_line
        - asttokens
        - code_qualname
    c           	   	      sB  || _ d|}t|ts@| | | } fdd|D }|| _dd |D | _tj	d dkrj|}nddd t
|D }tt| _d| _i | _d| _d| _ztj||d	| _W n ttfy   Y np0 t| jD ]F}t|D ]}tt|tt|_qt|D ]}| j| | q
qt }|| j |j| _dS )
zG
        Don't call this constructor, see the class docstring.
        rX   c                    s   g | ]}|  qS r&   )decode.0lineencodingr&   r'   
<listcomp>       z#Source.__init__.<locals>.<listcomp>c                 S   s   g | ]}| d qS )z
)rstriprt   r&   r&   r'   ry      rz   r   r   c                 S   s*   g | ]"\}}|d k r"t |r"dn|qS )rA   
)encoding_patternmatch)ru   rO   rv   r&   r&   r'   ry      s   Nfilename) r   joinrl   	text_typer   rs   textlinessysversion_info	enumerater   rm   _nodes_by_linetree
_qualnames
_asttokens_asttextastparseSyntaxError
ValueErrorwalkiter_child_nodesr   r2   r3   node_linenosappendQualnameVisitorvisit	qualnames)	rf   r   r   r   Zast_textnodechildr8   visitorr&   rw   r'   re      s<    





zSource.__init__Tc                 C   s   |  |jj|jpi |S )zb
        Returns the `Source` object corresponding to the file the frame is executing in.
        )for_filenamef_codeco_filename	f_globals)clsframe	use_cacher&   r&   r'   	for_frame  s    zSource.for_frameNc                    sh   t  trt   fdd}tj }t  | }|d urX|sX|tj < | }|  t|S )Nc                      s   t tt S r(   )	linecachegetlinesr   r   r&   r   module_globalsr&   r'   	get_lines"  s    z&Source.for_filename.<locals>.get_lines)	rl   r   rZ   r   r1   rG   
checkcache_for_filename_and_linesrn   )r   r   r   r   r   entryr   r&   r   r'   r     s    


zSource.for_filenamec                 C   sH   |  di }z|||f W S  ty,   Y n0 | || }|||f< |S )NZ__source_cache_with_lines)_class_localKeyError)r   r   r   Zsource_cacher*   r&   r&   r'   r   4  s    zSource._for_filename_and_linesc                 C   s    t jdkrt|jj|j d S )N)r      )r   r   r   	lazycacher   r   r   )r   r   r&   r&   r'   r   @  s    
zSource.lazycachec                 C   s:  t |tjr$|}|j}|j}|j}n|}|j}|j}|j}|t	||f}| 
di }||}	|	s*d }
 }}| |}|j}|rzJ||}|rt|rt||||\}}
nt|||||}|j}
|j}W n ty   tr Y n0 |dusJ |
rt|
h}t||k |}||
||f ||< }	t|g|	R  S )z
        Returns an `Executing` object representing the operation
        currently executing in the given frame or traceback object.
        Z__executing_cacheN)rl   typesTracebackTypetb_frame	tb_linenotb_lastif_linenof_lastir   idr   rG   r   r   statements_at_lineis_ipython_cell_codefind_node_ipython
NodeFinderr*   	decorator	ExceptionTESTINGstatement_containing_noder]   	Executing)r   Zframe_or_tbtbr   r8   lastirL   keyZexecuting_cacher)   r   stmtsr   sourcer   node_finderZ	new_stmtsr&   r&   r'   	executingF  sF    




zSource.executingc                 C   s   | j ||}t| || |S )z
        Returns an attribute directly associated with this class
        (as opposed to subclasses), setting default if necessary
        )__dict__rG   setattr)r   namedefaultr*   r&   r&   r'   r     s    zSource._class_localc                 C   s   dd | j | D S )az  
        Returns the statement nodes overlapping the given line.

        Returns at most one statement unless semicolons are present.

        If the `text` attribute is not valid python, meaning
        `tree` is None, returns an empty set.

        Otherwise, `Source.for_frame(frame).statements_at_line(frame.f_lineno)`
        should return at least one statement.
        c                 S   s   h | ]}t |qS r&   )r   )ru   r   r&   r&   r'   	<setcomp>  s   z,Source.statements_at_line.<locals>.<setcomp>)r   )rf   r8   r&   r&   r'   r     s    zSource.statements_at_linec                 C   s2   ddl m} | jdu r,|| j| j| jd| _| jS )z
        Returns an ASTText object for getting the source of specific AST nodes.

        See http://asttokens.readthedocs.io/en/latest/api-index.html
        r   )r   Nr   r   )	asttokensr   r   r   r   r   )rf   r   r&   r&   r'   asttext  s    
zSource.asttextc                 C   sH   ddl }| jdu rBt|dr*|  j | _n|j| j| j| jd| _| jS )z
        Returns an ASTTokens object for getting the source of specific AST nodes.

        See http://asttokens.readthedocs.io/en/latest/api-index.html
        r   Nr   r   )r   r   hasattrr   r   r   r   r   rf   r   r&   r&   r'   r     s    

zSource.asttokensc                 C   s&   dd l }t|dr|  S |   S d S )Nr   r   )r   r   r   r   r&   r&   r'   _asttext_base  s    
zSource._asttext_basec                 C   s&   t | trt| }| |S | S d S r(   )rl   bytesrr   r   rs   )r   rx   r&   r&   r'   decode_source  s    


zSource.decode_sourcec                 C   s   t t| jd S )Nr   )r   ioBytesIOreadline)r   r&   r&   r'   r     s    zSource.detect_encodingc                 C   s(   t |j| jk | j|j|jf|jS )a  
        Imitates the __qualname__ attribute of functions for code objects.
        Given:

            - A function `func`
            - A frame `frame` for an execution of `func`, meaning:
                `frame.f_code is func.__code__`

        `Source.for_frame(frame).code_qualname(frame.f_code)`
        will be equal to `func.__qualname__`*. Works for Python 2 as well,
        where of course no `__qualname__` attribute exists.

        Falls back to `code.co_name` if there is no appropriate qualname.

        Based on https://github.com/wbolster/qualname

        (* unless `func` is a lambda
        nested inside another lambda on the same line, in which case
        the outer lambda's qualname will be returned for the codes
        of both lambdas)
        )r]   r   r   r   rG   co_namer^   rf   rL   r&   r&   r'   code_qualname  s    zSource.code_qualname)T)NT)r#   r$   r%   __doc__re   classmethodr   r   r   r   r   r   r1   r   r   r   r   staticmethodr   r   r   r&   r&   r&   r'   rr      s4   3  


:

	

rr   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )r   a  
    Information about the operation a frame is currently executing.

    Generally you will just want `node`, which is the AST node being executed,
    or None if it's unknown.

    If a decorator is currently being called, then:
        - `node` is a function or class definition
        - `decorator` is the expression in `node.decorator_list` being called
        - `statements == {node}`
    c                 C   s"   || _ || _|| _|| _|| _d S r(   )r   r   r   Z
statementsr   )rf   r   r   r   r   r   r&   r&   r'   re     s
    zExecuting.__init__c                 C   s   | j | jjS r(   )r   r   r   r   rf   r&   r&   r'   r     s    zExecuting.code_qualnamec                 C   s   | j  | jS r(   )r   r   Zget_textr   r   r&   r&   r'   r     s    zExecuting.textc                 C   s   | j  | jS r(   )r   r   Zget_text_ranger   r   r&   r&   r'   
text_range  s    zExecuting.text_rangeN)r#   r$   r%   r   re   r   r   r   r&   r&   r&   r'   r     s
   r   c                       sD   e Zd Z fddZdddZdddZeZdd	 Zd
d Z  Z	S )r   c                    s   t t|   g | _i | _d S r(   )rd   r   re   stackr   r   rh   r&   r'   re     s    zQualnameVisitor.__init__Nc                 C   sT   |p|j }| j| t|ddr0|jd j}n|j}| j||fd| j d S )Ndecorator_listr&   r   .)	r   r   r   getattrr   r8   r   
setdefaultr   )rf   r   r   r8   r&   r&   r'   add_qualname  s    
zQualnameVisitor.add_qualnamec                 C   s  t jd dkr.t|tjtjtjfsHJ |nt|tjtjfsHJ || || | j	d g }t|tjrz|j
g}n|j
}|D ]}| | q| j  | j  t|D ]T\}}|dkrqt|tjr| | qt|tr|D ]}t|tjr| | qqd S )Nr   r   z<locals>body)r   r   rl   r   FunctionDefAsyncFunctionDefLambdar   r   r   r   r   popiter_fieldsASTrm   )rf   r   r   childrenr   fieldZ
grandchildr&   r&   r'   visit_FunctionDef%  s,     



z!QualnameVisitor.visit_FunctionDefc                 C   s    t |tjsJ | |d d S )Nz<lambda>)rl   r   r   r   rf   r   r&   r&   r'   visit_LambdaF  s    zQualnameVisitor.visit_Lambdac                 C   s2   t |tjsJ | | | | | j  d S r(   )rl   r   ClassDefr   generic_visitr   r   r   r&   r&   r'   visit_ClassDefK  s    

zQualnameVisitor.visit_ClassDef)N)N)
r#   r$   r%   re   r   r   visit_AsyncFunctionDefr   r   rj   r&   r&   rh   r'   r     s   


r   c                 c   s   | ]}t t|jV  qd S r(   )r   
__future__compiler_flag)ru   fnamer&   r&   r'   	<genexpr>V  s   r   c                 C   s   t | |jdt|j@ ddS )NexecT)flagsdont_inherit)compiler   future_flagsco_flags)r   Zmatching_coder&   r&   r'   compile_similar_to[  s    r   Z8io8urthglkjdghvljusketgIYRFYUVGHFRTBGVHKGF78678957647698c                 C   s   t dd t| D S )Nc                 s   s0   | ](}|j d ko&t|jto&|jdV  qdS )
LOAD_CONSTz@pyN)r<   rl   rT   rZ   
startswith)ru   bcr&   r&   r'   r   j  s   z)is_rewritten_by_pytest.<locals>.<genexpr>)anyra   rL   r&   r&   r'   is_rewritten_by_pytesth  s    r  c                   @   sP   e Zd Z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S )SentinelNodeFinderNc           
         s  t | || _|| _|j | _}t|| _| jr@tt|| _	nt | _	d | _
| | | _j}dd td  td |drtjn~|drtjtj nd|drtjttjtjttddtjtjtjtjtjtjtjtj tj!tj"d| fd	dn|d
rLtj#ttj$tj%tj&tj'd| fddn|dv rptj(tj fddn|dv rtj)tj t*j+d dksj,rfddnb|dv rtj-dd nH|drtj. tjn.|drtj. tj(fddnt/|t0  fdd|D } tj.krPt1|| _2W d    d S t3| 4|}	|	s|tjkr|| 5| n
t1|	| _2W d    n1 s0    Y  d S )Nc                 S   s   dS )NTr&   er&   r&   r'   <lambda>  rz   z-SentinelNodeFinder.__init__.<locals>.<lambda>ZCALL_)BINARY_SUBSCRzSLICE+ZBINARY_MatMultr&   )BINARY_POWERBINARY_MULTIPLYBINARY_MATRIX_MULTIPLYBINARY_FLOOR_DIVIDEBINARY_TRUE_DIVIDEBINARY_MODULO
BINARY_ADDBINARY_SUBTRACTBINARY_LSHIFTBINARY_RSHIFT
BINARY_AND
BINARY_XOR	BINARY_ORc                    s   t | j S r(   rl   rS   r  op_typer&   r'   r	    rz   ZUNARY_)UNARY_POSITIVEUNARY_NEGATIVE	UNARY_NOTUNARY_INVERTc                    s   t | j S r(   r  r  r  r&   r'   r	    rz   )	LOAD_ATTRLOAD_METHODLOOKUP_METHODc                    s   t | j jS r(   attr_names_matchattrrT   r  instructionr&   r'   r	    rz   )	LOAD_NAMELOAD_GLOBALrB   
LOAD_DEREFLOAD_CLASSDEREFr   r   c                    s   | j  jkS r(   )r   rT   r  r&  r&   r'   r	    rz   )
COMPARE_OPIS_OPCONTAINS_OPc                 S   s   t | jdkS )Nr?   )rE   opsr  r&   r&   r'   r	    rz   )ZSTORE_SLICESTORE_SUBSCR
STORE_ATTRc                    s   t | j jS r(   r#  r  r&  r&   r'   r	    rz   c                    sV   h | ]N}t |D ]>}t|rtt|d d r|rt||krtt|qqS )ctxN)r   r   rl   r   r   r   r2   )ru   stmtr   )r2  extra_filtertypr&   r'   r     s   
z.SentinelNodeFinder.__init__.<locals>.<setcomp>)6r]   r   r   r   rL   r  	is_pytest	frozensetassert_linenosignore_linenosr   get_actual_current_instructionr'  r<   typer  r   Call	SubscriptLoadBinOprD   PowMultr   FloorDivDivModAddSubLShiftRShiftBitAndBitXorBitOrUnaryOpUAddUSubNotInvert	AttributeNamer   r   rT   CompareStoreRuntimeErrorlockrq   r*   rm   matching_nodesfind_decorator)
rf   r   r   r   r   r   rL   Zop_nameexprsZmatchingr&   )r2  r4  r'  r  r5  r'   re   s  s    











zSentinelNodeFinder.__init__c           	         s   t |}tt|tjtf |j}t|  fdd  jD }dd t	|D d }t||d  j
d ||t| d |d  }tdd |D d	hk | j}|d d d | }| _| _d S )
Nc                    s   g | ]}|j  jjkr|qS r&   )r8   r   r   ru   r`   r   r&   r'   ry     s   z5SentinelNodeFinder.find_decorator.<locals>.<listcomp>c                 S   s   g | ]\}}|j d kr|qS )CALL_FUNCTIONr<   ru   rO   r`   r&   r&   r'   ry     s   
r?   ZSTORE_c                 S   s   h | ]
}|j qS r&   r\  rZ  r&   r&   r'   r     rz   z4SentinelNodeFinder.find_decorator.<locals>.<setcomp>r[  )rq   r]   rl   r   r   function_node_typesr   clean_instructionsrL   r   r<   r  rE   indexr'  r   r*   )	rf   r   r3  Z
decoratorsZline_instructionsZ last_decorator_instruction_indexZdecorator_instructionsZdecorator_indexr   r&   r   r'   rX    sB    

z!SentinelNodeFinder.find_decoratorc                    s    fddt |D S )Nc                    s&   g | ]}|j d vr|j jvr|qS ))r:   NOP)r<   r8   r9  rZ  r   r&   r'   ry     s   
z9SentinelNodeFinder.clean_instructions.<locals>.<listcomp>)ra   r   r&   r   r'   r`    s    
z%SentinelNodeFinder.clean_instructionsc                 C   s4   |  | j}tdd |  D s0dd |D }|S )Nc                 s   s   | ]}|j d kV  qdS )JUMP_IF_NOT_DEBUGNr\  rZ  r&   r&   r'   r     s   zESentinelNodeFinder.get_original_clean_instructions.<locals>.<genexpr>c                 S   s   g | ]}|j d kr|qS )rc  r\  rZ  r&   r&   r'   ry     s   
zFSentinelNodeFinder.get_original_clean_instructions.<locals>.<listcomp>)r`  rL   r  compile_instructions)rf   r*   r&   r&   r'   get_original_clean_instructions   s    z2SentinelNodeFinder.get_original_clean_instructionsc              	   #   s.     }t fddt|D }t|D ]\}}t|}|d usHJ tj|t tjtdd}t	| || z 
 }W || n
|| 0 tjdkrzt|| W n, ty   ts|t|d k rY q* Y n0 dd t|D }	t|	D ]<\}
}||
d	 8 }t||jd
k t||jdk qt|	D ]\}
}||
d	 8 }|d }||krfq<|| }|| }|j|j  krdv rn nL|j|jkr||d  j||d  j  krdkrn n||d  tjdk rt||D ]$\}}t|o|ot|| q|V  q<q*d S )Nc                 3   s    | ]\}}| j kr|V  qd S r(   r&  r]  r   r&   r'   r     s   
z4SentinelNodeFinder.matching_nodes.<locals>.<genexpr>)s)leftrS   right)r   
   r?   c                 S   s   g | ]\}}|j tkr|qS r&   )rT   sentinel)ru   rO   r'  r&   r&   r'   ry   4  s   
z5SentinelNodeFinder.matching_nodes.<locals>.<listcomp>rA   r   r  r.  r-  r  )re  rq   r   
get_setterr   r?  r@  Strrj  fix_missing_locationsrd  r   r   handle_jumpsr   r   rE   r]   r   r<   argr   opnames_match)rf   rY  original_instructionsZoriginal_indexZ
expr_indexexprsetterreplacementinstructionsindicesZ	index_numZsentinel_indexZ	new_indexoriginal_instnew_instinst1inst2r&   r   r'   rW    sp    



	



z!SentinelNodeFinder.matching_nodesc                 C   s&   t | j| j}t| |}| |S r(   )r   r   rL   rq   
find_codesr`  )rf   Zmodule_coderL   r&   r&   r'   rd  f  s    z'SentinelNodeFinder.compile_instructionsc                    sv   t dt dt ddd g js6 t dt dg7   fdd	g |rZ| fd
d| S )Nr^   co_freevarsco_cellvarsc                 S   s   t | jp| jS r(   )is_ipython_cell_code_namer   rR   r&   r&   r'   r	  r  rz   z/SentinelNodeFinder.find_codes.<locals>.<lambda>rI   rJ   c                    s   t  fddD S )Nc                 3   s    | ]}| |j kV  qd S r(   r  )ru   f)rR   rf   r&   r'   r   |  s   zASentinelNodeFinder.find_codes.<locals>.matches.<locals>.<genexpr>)allr  )checksrf   r  r'   matchesz  s    z.SentinelNodeFinder.find_codes.<locals>.matchesc                    s6   | j D ]*}t|sq|r( | | qd S r(   )rH   inspectiscoder   )rL   const)code_optionsfinderr  r&   r'   r    s    


z-SentinelNodeFinder.find_codes.<locals>.finder)r   r6  r   )rf   Z	root_coder&   )r  r  r  r  rf   r'   r|  l  s"    

zSentinelNodeFinder.find_codesc                    sL   t t| j}t fddt|D }|| }|jdkr>|S |d7 }q(dS )z{
        Get the instruction corresponding to the current
        frame offset, skipping EXTENDED_ARG instructions
        c                 3   s    | ]\}}|j  kr|V  qd S r(   )rQ   r]  r   r&   r'   r     s   
zDSentinelNodeFinder.get_actual_current_instruction.<locals>.<genexpr>r:   r?   N)rm   ra   rL   rq   r   r<   )rf   r   rv  ra  r'  r&   r  r'   r:    s    	
z1SentinelNodeFinder.get_actual_current_instruction)r#   r$   r%   r*   re   rX  r`  re  rW  rd  r|  r:  r&   r&   r&   r'   r  p  s   c!	S&r  c                 c   sd   d}t t| |dD ]J\}}|jtkr<t|jdk d}qn|rTt|jdk d}q||fV  qdS )zz
    Yields (index, instruction) pairs excluding the basic
    instructions introduced by the sentinel transformation
    FNr   Tr  )r   r   rT   rj  r]   r<   )rv  startZ
skip_powerrO   r`   r&   r&   r'   non_sentinel_instructions  s    
r  c                 c   s   t t| |d}t||}d}zt|\}}t|\}	}
W n tyN   Y dS 0 |r||j|
j  krldkr|n nt|\}	}
|j|
j  kodv n  o|j|
jk}|||	|
fV  qdS )z
    Yields matching indices and instructions from the new and original instructions,
    leaving out changes made by the sentinel transformation.
    NFr  rk  )r   r   r  nextStopIterationr<   rp  )rr  original_startrv  r  Zoriginal_iterZnew_iterZinverted_comparison
original_irx  new_iry  r&   r&   r'   walk_both_instructions  s&    


r  c           	         s   t |d| dD ]\}}} t| r&qd jv rd|jvrt fddt| D }t||| |}|dusnJ || ||d < nHg }||d D ]}|| |jdv r qqttt|| | ||<  q qdS q dS )a  
    Transforms instructions in place until it looks more like original_instructions.
    This is only needed in 3.10+ where optimisations lead to more drastic changes
    after the sentinel transformation.
    Replaces JUMP instructions that aren't also present in original_instructions
    with the sections that they jump to until a raise or return.
    In some other cases duplication found in `original_instructions`
    is replicated in `instructions`.
    r   JUMPc                 3   s.   | ]&\}}|j  jkrt|d ds|V  qdS )rW   FN)rQ   rT   r   r]  ry  r&   r'   r     s   zhandle_jumps.<locals>.<genexpr>Nr?   RETURN_VALUERAISE_VARARGS)	r  rq  r<   rq   r   handle_jumpr   rY   find_new_matching)	rv  rr  r  rx  r  r  Znew_instructionsorig_sectionZsection_instr&   r  r'   ro    s.    
	

ro  c                 c   sp   t t|t|  D ]V}ttt||t|  \}}t|t| k rJ dS t| |r|||d d  V  qdS )z
    Yields sections of `instructions` which match `orig_section`.
    The yielded sections include sentinel instructions, but these
    are ignored when checking for matches.
    Nr^  r?   )rangerE   zipr   r  sections_match)r  rv  r  rw  dup_sectionr&   r&   r'   r    s    
r  c                 C   s   t | |||D ]|\}}}}tt|| |jdv rt|||d  }|D ]
}	d|	_qJ| ||d  }
t||
| sg |||d < |  S qdS )a`  
    Returns the section of instructions starting at `start` and ending
    with a RETURN_VALUE or RAISE_VARARGS instruction.
    There should be a matching section in original_instructions starting at original_start.
    If that section doesn't appear elsewhere in original_instructions,
    then also delete the returned section of instructions.
    r  r?   TN)r  r]   rq  r<   r   rW   check_duplicates)rr  r  rv  r  Z
original_jrx  Znew_jry  ZinlinedZinlr  r&   r&   r'   r  $  s    	

r  c                 C   sZ   t t|D ]H}|| krq|||t|  }t|t|k rD dS t||r dS qdS )z
    Returns True if a section of original_instructions starting somewhere other
    than original_i and matching orig_section is found, i.e. orig_section is duplicated.
    FT)r  rE   r  )r  r  rr  Z	dup_startr  r&   r&   r'   r  ?  s    
r  c                 C   s   t dd t| |D S )z\
    Returns True if the given lists of instructions have matching linenos and opnames.
    c                 s   sD   | ]<\}}|j |j ks2d |j  ko,|jkn  o:t||V  qdS )	POP_BLOCKN)r8   r<   rq  )ru   Z	orig_instZdup_instr&   r&   r'   r   U  s
   z!sections_match.<locals>.<genexpr>)r  r  )r  r  r&   r&   r'   r  P  s    r  c                 C   s\   | j |j kpZd| j v r d|j v pZ| j dkr4|j dkpZ| j dv rH|j dkpZ| j dkoZ|j dkS )Nr  
PRINT_EXPRPOP_TOP)r!  r"  r   CALL_METHODr[  r\  )rz  r{  r&   r&   r'   rq  `  s    

rq  c                    sz   | j tD ]d\  | u r6fdd}|  S t trt D ]*\}|| u rH fdd}|    S qHqd S )Nc                    s   t  | S r(   )r   new_node)r   r3   r&   r'   rt  t  s    zget_setter.<locals>.setterc                    s   |  < d S r(   r&   r  )r   rO   r&   r'   rt  {  s    )r3   r   r   rl   rm   r   )r   rt  itemr&   )r   rO   r   r3   r'   rl  o  s    
rl  c                 C   s$   t | tjstt| j} q tt| S r(   )rl   r   r3  r   r2   r3   )r   r&   r&   r'   r     s    r   c                 c   sB   t | D ]2}t|dr
tt|t jr
t|D ]
}|V  q0q
d S )Nr3   )r   r   r   rl   r   Assertr   )r   r   r8   r&   r&   r'   r8    s    r8  c                 C   s@   t | jtjs| j} q td}ttj| g|_t||  |S )NrX   )	rl   r3   r   Moduler   r   r3  r   copy_location)r3  r   r&   r&   r'   _extract_ipython_statement  s    
r  c                 C   s   t td| S )Nz(<module>|<cell line: \d+>)$)boolrer~   )Z	code_namer&   r&   r'   r    s    r  c                 C   s   t td| S )Nz'<ipython-input-|[/\\]ipykernel_\d+[/\\])r  r  searchr   r&   r&   r'   is_ipython_cell_filename  s    r  c                 C   s   t | jot| jS r(   )r  r   r  r   )Zcode_objr&   r&   r'   r     s    
r   c           	   	   C   sr   d  }}|D ]\}t |}z<t| ||||}|s2|rF|js>|jrFW  dS |j}|j}W q tyf   Y q0 q||fS )N)NN)r  r   r*   r   r   )	r   r   r   r   r   r   r3  r   r   r&   r&   r'   r     s    
r   c                 C   s.   | |krdS |  dsdS ttd|  |S )z
    Checks that the user-visible attr (from ast) can correspond to
    the argval in the bytecode, i.e. the real attribute fetched internally,
    which may be mangled for private attributes.
    T__Fz^_\w+%s$)r  r  r  r~   )r%  rT   r&   r&   r'   r$    s
    
r$  c                 c   sb   t | dr^g }t | drFt| tjrF| jd us2J t| j| jd }n| jg}|D ]
}|V  qRd S )Nr8   
end_linenor?   )r   rl   r   rs  r  r  r8   )r   Zlinenosr8   r&   r&   r'   r     s    
r   )r      r?   )PositionNodeFinder)rX   )sr   r   r   disr/   r  r   r   r  r   r   collectionsr   r   copyr   	itertoolsr   operatorr   	threadingr   typingr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zasttokens.asttokensr   r   r_  r   r   r   tokenizer   r   pathlibr   r1   rZ   r   Zlib2to3.pgen2.tokenizer!   r}   r"   objectunicoder   r2   ra   rU   r5   _Instructionr9   r:   r;   r<   r=   r>   rV   r]   r   r   rb   rk   rq   rr   r   NodeVisitorr   sumall_feature_namesr   r   rj  r  r  r  r  ro  r  r  r  r  rq  rl  rV  r   r8  r  r  r  r   r   r$  r   Z_position_node_finderr  r   r&   r&   r&   r'   <module>   s   H
  
  0"B  <8
