o
    =)iE                     @   s&  d dl Z d dlmZmZmZmZmZmZmZm	Z	 d dl
mZ d dlZd dlZd dlmZ d dlZd dlZd dlZejdkZee jee jB ZejG dd dZde jd	efd
dZdefddZdee fddZejG dd dZd	efddZ dee d	dfddZ!d	ee fddZ"dee fddZ#dee j d	ee j fddZ$d<d e%d!ed	e%fd"d#Z&d<d e%d!ed	e%fd$d%Z'G d&d' d'ej(Z)G d(d) d)ej(Z*d*d+ Z+d=d,d-Z,d.d/ Z-	d<d0e%d1e%d!ed	e%fd2d3Z.G d4d5 d5ej(Z/		d>d6ed7e%d8eee%  d9eee%  d	e%f
d:d;Z0dS )?    N)ListTupleUnionOptionalCallableAnyDictSet)CodeType)defaultdict)      c                   @   s   e Zd ZU dZeed< eed< ee ed< eed< eed< dZ	ee ed< dZ
ee ed	< d
Zeed< dd Zdd Zdd Zdd ZdddZdS )Instructionz$A mutable version of dis.InstructionopcodeopnameargargvalargreprNoffsetstarts_lineFis_jump_targetc                 C   s   t | S Nidself r   `/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/depyf/code_transform.py__hash__   s   zInstruction.__hash__c                 C   s   t | t |kS r   r   )r   otherr   r   r   __eq__   s   zInstruction.__eq__c                 C   s   d| j  d| j dS )NzInstruction(opname=z	, offset=))r   r   r   r   r   r   short_inst_repr"   s   zInstruction.short_inst_reprc                 C   s
   | j tv S r   )r   all_jump_opcode_setr   r   r   r   is_jump%   s   
zInstruction.is_jumpr   c                 C   sn   |   rd| jv rt| jdd S | jtjv r| jS | jtj	v r.t
s+| j| j S | jS td| j d)Nzto  zInstruction z does not have jump target)r$   r   intreplacestripr   dishasjabsr   hasjrelpy311r   
ValueErrorr   r   r   r   r   get_jump_target(   s   zInstruction.get_jump_target)r   r   )__name__
__module____qualname____doc__r&   __annotations__strr   r   r   r   r   boolr   r    r"   r$   r.   r   r   r   r   r      s   
 r   ireturnc              	   C   s&   t | j| j| j| j| j| j| j| jS r   )	r   r   r   r   r   r   r   r   r   )r6   r   r   r   convert_instruction6   s   r8   instc                 C   s:   d| _ tjd | _d| _d| _d| _| j | j d| _	| S )z%Inplace modify an instruction as nop.NOPr   r%   F)
r   r)   opmapr   r   r   r   r   r   r   r9   r   r   r   nop_instructionC   s   r=   instructionsc                    s&   d  fdd}| D ]}|| q
dS )zEEnsure every instruction has line number set in case some are removedNc                    s   | j r| j   | _ d S r   )r   r<   Zcur_line_nor   r   populate_line_numT   s   
z.propagate_line_nums.<locals>.populate_line_numr   )r>   r@   r9   r   r?   r   propagate_line_numsP   s
   
rA   c                   @   s6   e Zd ZU eed< eed< eed< eed< eed< dS )ExceptionTableEntrystartendtargetdepthlastiN)r/   r0   r1   r&   r3   r5   r   r   r   r   rB   `   s   
 rB   c                 C   s@   t | }|d@ }|d@ r|dK }t | }||d@ O }|d@ s|S )z5
    Inverse of `encode_exception_table_varint`.
    ?   @      )next)Z
bytes_iterbvalr   r   r   decode_exception_table_varinth   s   rN   tabc                 C   sf   t t| d D ](}| | j| | jkr.| | j| |d  jk r.| |d  j| |d  jks0J qdS )z
    Verifies that a list of ExceptionTableEntries will make a well-formed
    jump table: entries are non-empty, sorted, and do not overlap.
       N)rangelenrC   rD   )rO   r6   r   r   r   check_exception_tablet   s
   "rS   c           
      C   s   t | }g }z3	 t|d }t|d }|| d }t|d }t|}|d? }t|d@ }	|t|||||	 q tyH   t| | Y S w )z
    Parse the exception table according to
    https://github.com/python/cpython/blob/3.11/Objects/exception_handling_notes.txt
    T   rP   )iterrN   r5   appendrB   StopIterationrS   )
ZexntabZexntab_iterrO   rC   lengthrD   rE   dlrF   rG   r   r   r   parse_exception_table   s"   	rZ   c                    sv   t | D ]4\}}|jdkr8|   fddt | D }|r8|d }t | D ]\}}|j kr7||kr7t| q&qdS )zSimplify finally statement.
    3.10 finally statement:
    SETUP_FINALLY
    body
    POP_BLOCK
    finally code
    Exception code
    RERAISE
    SETUP_FINALLYc                    s(   g | ]\}}|j  kr|jd kr|qS )RERAISE)r   r   ).0j_instZfinally_targetr   r   
<listcomp>   s    z.simplify_finally_statement.<locals>.<listcomp>r   N)	enumerater   r.   r   r=   )r>   r6   r9   Zreraise_idxZreraise_indexr^   r_   r   r`   r   simplify_finally_statement   s   

rc   c              
      sj  t tjt tjB }i }trt| j}dd |D }dd |D }t  }d|d< t|D ]\}  js8 j	|v r<d||< |t
|d krF n|| sKq, jdv rQq, j|v r jtjv ro   j	krod	||< d||d < q,d
 jv s~d jv s~d jv rd||d < q, jdv rd||d < q, fddt|D }t
|r|d }	|	|kr|	}
d	}t|||	 D ]\}}|j	|v rd}t|
|}
qt|D ].\}}z!| }||k s||	kr| j	kr|||	 j	k rd}t|
|}
W q ty   Y qw |sd	||< t||
D ]}|| qq,d||d < q,|D ]}d	||< qt||D ]\ }|s1t  q&dS )z!Mark unreachable bytecode as NOP.c                 S   s   i | ]}|j |qS r   )rE   )r]   entryr   r   r   
<dictcomp>       z,nop_unreachable_bytecode.<locals>.<dictcomp>c                 S   s   g | ]}d qS )Fr   r]   xr   r   r   ra      s    z,nop_unreachable_bytecode.<locals>.<listcomp>Tr   rP   )RETURN_VALUEZ
BREAK_LOOPFZIFFOR_ITERZ
SETUP_LOOP)r[   
SETUP_WITHZBEFORE_WITHc                    s"   g | ]\}}|j   kr|qS r   )r   r.   )r]   r^   Zinstructr<   r   r   ra      s    N)setr)   r*   r+   r,   rZ   Zco_exceptiontablerb   r   r   rR   r   r   r.   min	ExceptionrQ   addzipr=   )coder>   ZjumpsZexception_targetsrO   Z	reachableZdeadcode_positionsr6   Zjump_forwardsr^   Zsmallest_jump_inZhas_jump_iniiZinst_iiZjump_location_flagr   r<   r   nop_unreachable_bytecode   s   





(ru      rq   indentationc                       d  fdd|  D S )zAdd indentation to code.r%   c                 3   s     | ]}d   | d V  qdS ) 
Nr   r]   linerw   r   r   	<genexpr>  s    
z"add_indentation.<locals>.<genexpr>join
splitlinesrq   rw   r   r}   r   add_indentation  s   r   c                    rx   )zRemove indentation from code.r%   c                 3   s     | ]}| d  d V  qd S )Nrz   r   r{   r}   r   r   r~     s    z%remove_indentation.<locals>.<genexpr>r   r   r   r}   r   remove_indentation  s   r   c                   @   4   e Zd Zdedeeeej f fddZdd Z	dS )RemoveAssignmentTransformer	temp_nametemp_occurrencesc                 C      || _ || _d S r   r   r   r   r   r   r   r   r   __init__     
z$RemoveAssignmentTransformer.__init__c                 C   s   t |jdkrPt|jd tjrP|jd j}|| jkrPt | j| dkr+tj|j	dS t | j| dkrPt| j| d t
rP| j| |j	 | j| d rPd S |S )NrP   r   )valuer   )rR   targets
isinstanceastNamer   r   r   Exprr   r5   rV   r   nodenamer   r   r   visit_Assign"  s    
&z(RemoveAssignmentTransformer.visit_AssignN)
r/   r0   r1   r4   r   r   r   r   r   r   r   r   r   r   r         
r   c                   @   r   )RemoveAssignment2Transformerr   r   c                 C   r   r   r   r   r   r   r   r   3  r   z%RemoveAssignment2Transformer.__init__c                 C   sV   |j }|| jkr)t| j| dkr)t| j| d tr)| j| d r)| j| d S |S )Nrv   r   r   )r   r   rR   r   r   r5   r   r   r   r   
visit_Name;  s   z'RemoveAssignment2Transformer.visit_NameN)
r/   r0   r1   r4   r   r   r   r   r   r   r   r   r   r   r   2  r   r   c                 C   s&   g }| r| |  t| dd} | s|S )z)Collect all parent nodes of a given node.parentN)rV   getattr)r   parentsr   r   r   get_parentsD  s   
r   c                 C   s$   t | D ]
}||_t|| qdS )z3Recursively set the parent attribute for each node.N)r   iter_child_nodesr   set_parents)r   r   childr   r   r   r   M  s   r   c                 C   sP   t | }t |}|  |  d}t||D ]\}}||u r"|}q |||fS )z+Get the lowest common parent for two nodes.N)r   reverserp   )node1node2Zparents1Zparents2Zlast_commonp1p2r   r   r   lowest_common_parentT  s   
r   source_codetemp_prefixc                 C   s<  t | }t| tt}t |D ]}t|t jr(|j	|r(||j 
| q|D ]g}t|| dkr|| d }|| d }t||\}	}
}t|jt jrR|n|}t|jt jr]|
n|}t jt jt jt jt jt jt jt jt jt jf
}t|| }|| 
| t|||}t|||}q+tj|d| d}|S )NrT   r   rP   ry   )Zindent_with)r   parser   r   listwalkr   r   r   
startswithrV   rR   r   r   AssignFunctionDefAsyncFunctionDefForAsyncForWhileIfTryWith	AsyncWithClassDefr   visitr   astorZ	to_source)r   r   rw   treer   r   keyr   r   r   Zparent1Zparent2Zassignment_nodeZassignment_parentZindentation_nodesZ	can_mergeZreconstructed_coder   r   r   remove_some_tempf  sN   
r   c                   @   s   e Zd Zdd ZdS )IdentifierReplacerc                 C   s   d|_ | |S )NZPLACEHOLDER)r   generic_visit)r   r   r   r   r   visit_FunctionDef  s   
z$IdentifierReplacer.visit_FunctionDefN)r/   r0   r1   r   r   r   r   r   r     s    r   old_bytecodesrc_codeadd_local_variablesadd_cellvarsc                    s  | dd   d  |}|d us|d ur`| }|d }|dd  }|g}|r=ddd |D }	d|	 d	 }	||	 |rTd
ddd |D  }	d|	 }	||	 ddd || D }| j}
|
r}d}|
D ]
}|d| d7 }qi|t|d7 }|}t|dd}ddlm} ||} fdd|D d }t	| j
t	|j
 }t	| jt	|j }|s|rt| |tt|tt|dS |S )N(r   r   rP   z; c                 s   s    | ]}| d V  qdS )z = NoneNr   rg   r   r   r   r~     s    z%fix_irregular_code.<locals>.<genexpr>z    zy # this line helps Python to generate bytecode with at least the same number of local variables as the original function
zreturn z, c                 s   s    | ]}|V  qd S r   r   rg   r   r   r   r~     s    z    def __helper_for_cellvars():
        # this function helps Python to generate bytecode with at least the same number of cellvars as the original function
        r%   c                 S   s   g | ]}|d  qS )rz   r   rg   r   r   r   ra     rf   z&fix_irregular_code.<locals>.<listcomp>a  def __helper_outer_function():
    # this is a helper function to help compilers generate bytecode to read capture variables from closures, rather than reading values from global scope. The value of these variables does not matter, and will be determined in runtime.
z = None
rv   Znonameexec)collect_all_code_objectsc                    s   g | ]	}|j  kr|qS r   )co_namerg   Zfunction_namer   r   ra     s    )r   r   )splitr   r   rV   co_freevarsr   compileutilsr   rl   co_varnamesco_cellvarsfix_irregular_codesortedr   )r   r   r   r   Znew_codelinesheaderbodyheadersZ
added_lineZfreevarsZtmp_codeZfreevarZcompiled_coder   Zcode_objectsZtarget_codeZmissing_local_variablesZmissing_cellvarsr   r   r   r     sN   



r   )rv   r   )NN)1r)   typingr   r   r   r   r   r   r   r	   typesr
   r   r   collectionsr   dataclassessyshashlibversion_infor,   rl   r*   r+   r#   	dataclassr   r8   r=   rA   rB   r&   rN   rS   rZ   rc   ru   r4   r   r   NodeTransformerr   r   r   r   r   r   r   r   r   r   r   r   <module>   st    (
&
_	
	
+

