
    `i                     V   d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ ddlZddlZddlZddlmZ ddlZddlmZmZmZmZmZmZmZmZmZ ddlmZ  G d	 d
e          Z ej!         G d d                      Z"ej!         G d d                      Z#de
eef         de$fdZ%dS )zFA simple program to transform bytecode into more readable source code.    N)CodeType)ListTupleDictUnionCallableOptional)defaultdict   )	nop_unreachable_bytecodenop_instructionadd_indentationremove_indentationremove_some_temppropagate_line_numsconvert_instructionsimplify_finally_statementInstruction)get_function_signaturec                   *     e Zd ZdZd fd	Zd Z xZS )DecompilationErrorz)Custom exception class for decompilation. c                 b    || _         t                                          | j                    d S N)messagesuper__init__)selfr   	__class__s     d/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/depyf/decompiler.pyr   zDecompilationError.__init__!   s*    &&&&&    c                     d| j          S )NzDecompilationError: )r   r   s    r    __str__zDecompilationError.__str__%   s    4dl444r!   )r   )__name__
__module____qualname____doc__r   r$   __classcell__)r   s   @r    r   r      sR        33' ' ' ' ' '5 5 5 5 5 5 5r!   r   c                   R    e Zd ZU dZeed<   eed<   dZeed<   dZ	e
ed<   dZe
ed<   d	S )
DecompilerStatez_State of decompiler, keep track of the evaluation stack, as well as the decompiled source code.source_codestackFinside_looploop_start_indexloop_end_indexN)r%   r&   r'   r(   str__annotations__listr.   boolr0   intr1    r!   r    r+   r+   )   s^         iiKKKKcNCr!   r+   c                   @   e Zd ZU dZeed<   dZeed<   dZe	ed<    e
j        d           Zeed	<   d
Zeed<   ej        drd            ZdefdZeZexZxZxZxZxZxZxZxZxZxZxZxZ Z!exZ"xZ#xZ$xZ%xZ&xZ'xZ(Z)eZ*exZ+Z,eZ-exZ.xZ/xZ0xZ1xZ2xZ3Z4eZ5defdZ6e6Z7e6xZ8xZ9xZ:xZ;xZ<xZ=Z>e6Z?e6Z@e6ZAdefdZBdefdZCeCxZDxZExZFxZGxZHxZIxZJZKdefdZLdefdZMeMZNdefdZOdefdZPdefdZQdefdZRdefdZSdefdZTdefdZUdefdZVdefdZWeWxZXxZYxZZZ[defdZ\defd Z]defd!Z^defd"Z_defd#Z`e`xZaxZbZce6Zddefd$Zedefd%Zfdefd&Zgdefd'ZhehxZixZjxZkZldefd(Zmdefd)ZnenxZoxZpxZqxZrxZsxZtxZuxZvxZwxZxxZyxZzZ{defd*Z|defd+Z}defd,Z~e~xZxZxZxZxZxZxZxZxZxZxZxZZdefd-Zdefd.Zdefd/Zdefd0Zdefd1Zdefd2ZexZxZxZZdefd3Zdefd4Zdefd5Zdefd6Zdefd7Zdefd8ZexZZexZZexZZexZZexZZexZZeZeZdefd9Zdefd:ZeZdefd;Zdefd<ZexZxZxZZdefd=Zdefd>Zdefd?Zdefd@ZdefdAZdefdBZdefdCZdefdDZexZZdefdEZdefdFZdefdGZdefdHZdefdIZdefdJZdefdKZexZxZZdefdLZexZZdefdMZexZZdefdNZexZZdefdOZdefdPZdefdQZdefdRZdefdSZdefdTZdefdUZexZxZZdefdVZdefdWZdefdXZdefdYZdZed[efd\Zd]efd^Zed_ee         fd`            Zdeeef         fdaZdb ZdsddefdeZedf             Z ej        dgh          	 	 	 dtde	diee	         dje	fdk            Ze	 	 	 dtdledmede	dnee	         djef
do            Zdp Zdq ZdgS )u
DecompilerzA decompiler for a code object.coder   
temp_count__temp_temp_prefixc                  $    t          dg           S )Nr   r,   r-   )r+   r7   r!   r    <lambda>zDecompiler.<lambda>:   s    "!& !& !& r!   )default_factorystate   indentationFr/   c              #      K   t          d||||          }| j        }|j        r+|j        s$|j        |_        |j        |_        |j        |_        || _        dV  || _        dS )z"Create a new state for decompiler.r   )r,   r-   r.   r0   r1   N)r+   rB   r.   r0   r1   )r   r-   r.   r0   r1   rB   	old_states          r    	new_statezDecompiler.new_state>   s        Begw  IW  X  X  XJ	  	<): 	< ) 5E%.%?E"#,#;E 



r!   instc                 0    t          d|j                   )NzUnsupported instruction: )NotImplementedErroropnamer   rH   s     r    unimplemented_instructionz$Decompiler.unimplemented_instructionN   s    !"Kdk"K"KLLLr!   c                     d S r   r7   rL   s     r    generic_nopzDecompiler.generic_nopi   s    r!   c                    d}	 t          t          |j                            |j        k    }n# t          $ r Y nw xY w|r3| j        j                            t          |j                             dS t          |j        t                    r|j        j	        }|j        j
        }| j        xj        dz  c_        |                                 }| j        xj        | d| d| dz  c_        | j        j                            |           dS |j                            d          rj| j        xj        dz  c_        |                                 }| j        xj        | d|j         dz  c_        | j        j                            |           dS t          |j        t                    r&| j        j                            |j                   dS | j        j                            d	|j         d
           dS )zPush a constant onto the stack.
        `inst.argval` is the constant value, we have to use `repr` to get the source code
        Fzimport importlib
z = importlib.import_module("z").
ztorch.zimport torch
 = z'__co_consts[z]'N)evalreprargvalBaseExceptionrB   r-   append
isinstancetyper&   r%   r,   get_temp_nameargrepr
startswithr   arg)r   rH   can_reprmodulename	temp_names         r    
LOAD_CONSTzDecompiler.LOAD_CONST   s    	D--..$+=HH 	 	 	D	 	FJ##D$5$566666$+t,, F /{+
&&*>>&& ..00	
&&Y*g*gTZ*g*g_c*g*g*gg&&
 ''	22222((22 F 
&&*::&& ..00	
&&Y*J*J4;*J*J*JJ&&
 ''	22222DK22 F
 ''44444
 ''(D(D(D(DEEEEEs   */ 
<<c                 F   d|j         v r| j        j                            d           |j                             d          r:| j        j                            |j                            dd                     dS | j        j                            |j                   dS )z-`inst.argval` is the variable name, in stringzNULL + N.	comp_arg_)r[   rB   r-   rW   r\   rU   replacerL   s     r    generic_loadzDecompiler.generic_load   s    $$J##D)))<""3'' 	1J##DK$7$7[$I$IJJJJJJ##DK00000r!   c                 l    | j         j                            d           |                                  d S )Nzlocals())rB   r-   rW   replace_mutable_tos_with_temprL   s     r    LOAD_LOCALSzDecompiler.LOAD_LOCALS   s3    

+++**,,,,,r!   c                     | j         j                                        }| j         j                            | d|j         d|j         d| d|j         	           |                                  d S )N[z] if 'z' in z else rB   r-   poprW   rU   ri   r   rH   toss      r    LOAD_FROM_DICT_OR_GLOBALSz$Decompiler.LOAD_FROM_DICT_OR_GLOBALS   s    j""$$
RRT[RRRR#RRT[RR	T 	T 	T**,,,,,r!   c                    t           j        dk     r| j        j                                        }	 t          |          }n# t          $ r Y nw xY w|                    d          d         }d|v r-| j        xj        d| dz  c_        | 	                                }n| 	                                }| j        j                                        }|j
        dz  r| j        j                                         |j
        dz  r| j        j                                         |j
        d	z  r| j        j                                        ni }|j
        d
z  r| j        j                                        nd}t          |          st          |          rt          d           |                     |j                  }d}| j        |d
z            j        dk    r| j        |d
z            j
        }d}t#          |                              |          }	| j        xj        |	z  c_        |s!| j        j                            |           d S |d	z   S )N      rd   r/   <z"original function name z is illegal, use a temp name."
   rC      r   r7   zNFunction with default arguments is not supported, ignore the default argumentsF
STORE_FASTT)overwite_fn_name)sysversion_inforB   r-   rn   rS   	Exceptionsplitr,   rZ   rU   lenprintindex_ofoffsetinstructionsrK   r9   	decompilerW   )
r   rH   	qual_name	func_namer:   kw_defaultsdefaults
this_indeximmediately_used
inner_funcs
             r    MAKE_FUNCTIONzDecompiler.MAKE_FUNCTION   s_   g%%
(,,..I OO		    ",,R0Ii
&&*pY*p*p*pp&& ..00	 **,,Iz##%%; 	#J  """; 	#J  """04d0BJdj&**,,,-1[4-?G4:#'')))R{ 	bs8}} 	b`b b b]]4;//
 Z!^,3|CC )*q.9@I#%%///KK

*, 	"J##I..... >!s   A   
AAc                     d S r   r7   rL   s     r    COPY_FREE_VARSzDecompiler.COPY_FREE_VARS   s	     	r!   c                 D   t          | j        j                                                  }|j        }|                                r&| j        j                            | d|            d S | j        j                            d| dt          |           d           d S )Nrd   zgetattr(, ))r2   rB   r-   rn   rU   isidentifierrW   rT   )r   rH   lhsrhss       r    	LOAD_ATTRzDecompiler.LOAD_ATTR   s    $*"&&(())k 	DJ##sNNSNN33333J##$Bs$B$Bd3ii$B$B$BCCCCCr!   c           	      @   | j         j                                        }| j         j                                        }| j         j                                        }| j         j                            | d| d| d|j                    |                                  d S )N(r   z).rm   )r   rH   self_objcls_obj	super_objs        r    LOAD_SUPER_ATTRzDecompiler.LOAD_SUPER_ATTR   s    :#''))*"&&((J$((**	
??7??h??$+??	A 	A 	A**,,,,,r!   c                     | j         j                            | j         j                                         d|j                    d S )Nrd   )rB   r-   rW   rn   rU   rL   s     r    LOAD_METHODzDecompiler.LOAD_METHOD   sB    
4:#3#7#7#9#9 I IDK I IJJJJJr!   c                 D    | j         j                            d           d S )NAssertionErrorrB   r-   rW   rL   s     r    LOAD_ASSERTION_ERRORzDecompiler.LOAD_ASSERTION_ERROR   s"    
 011111r!   c                 D    | j         j                            d            d S r   r   rL   s     r    	PUSH_NULLzDecompiler.PUSH_NULL  !    
%%%%%r!   c                     | j         j                                        }| j         j                            d| d           d S )Niter(r   rB   r-   rn   rW   ro   s      r    GET_ITERzDecompiler.GET_ITER  s?    j""$$
/////r!   c                     |j         }| j        j                                        }||k    r| j        xj        | d| dz  c_        d S d S NrR   rQ   rU   rB   r-   rn   r,   )r   rH   leftrights       r    generic_storezDecompiler.generic_store  s^    {
 $$&&5== J""&;&;%&;&;&;;"""" =r!   c                     | j         j                                        }| j         j                                        }| j         j                                        }| j         xj        | d| d| dz  c_        d S )Nrl   ] = rQ   rB   r-   rn   r,   )r   rH   indexxvalues        r    STORE_SUBSCRzDecompiler.STORE_SUBSCR  sy    
 $$&&J  ""
 $$&&
Q"="="="=E"="="==r!   c           
      8   | j         j                                        }| j         j                                        }| j         j                                        }| j         j                                        }| j         xj        | d| d| d| dz  c_        d S )Nrl   :r   rQ   r   )r   rH   endstart	containerr   s         r    STORE_SLICEzDecompiler.STORE_SLICE  s     j""$$
 $$&&J$((**	
 $$&&
Y"K"K"K"K"K"K%"K"K"KKr!   c                     | j         j                                        }| j         j                                        }| j         xj        | d|j         d| dz  c_        d S )Nrd   rR   rQ   rB   r-   rn   r,   rU   )r   rH   r   r   s       r    
STORE_ATTRzDecompiler.STORE_ATTR%  se    J  ""
 $$&&
Q"B"B"B"B"B"B"BBr!   c                     | j         j                                        }| j         j                                        }| d| d| j         j        vr| j         xj        d| d| dz  c_        d S d S )Nrl   ]del z]
r   )r   rH   r   r   s       r    DELETE_SUBSCRzDecompiler.DELETE_SUBSCR,  s    
 $$&&J  ""e$*"222 J""&;Q&;&;&;&;&;;""""	 32r!   c                 B    | j         xj        d|j         dz  c_        d S )Nr   rQ   rB   r,   rU   rL   s     r    generic_deletezDecompiler.generic_delete5  s,    
"8"8"8"88r!   c                     | j         j                                        }| j         xj        d| d|j         dz  c_        d S )Nr   rd   rQ   r   )r   rH   r   s      r    DELETE_ATTRzDecompiler.DELETE_ATTR=  sJ    J  ""
"<"<"<T["<"<"<<r!   c           
      b   |j                             d          d         }| j        j                                        }| j        j                                        }| j        xj        | dt          |j                    d| d| dz  c_        | j        j                            |           d S )Nrd   r   z = __import__(z, fromlist=z, level=)
)rU   r~   rB   r-   rn   r,   rT   rW   )r   rH   r`   fromlistlevels        r    IMPORT_NAMEzDecompiler.IMPORT_NAMEB  s    {  %%a(:#''))
 $$&&
T"s"sdk9J9J"s"sW_"s"sin"s"s"ss
%%%%%r!   c                     |j         }| j        j        d         }| j        xj        | d| d| dz  c_        | j        j                            |           d S )Nr/   rR   rd   rQ   )rU   rB   r-   r,   rW   )r   rH   r`   r_   s       r    IMPORT_FROMzDecompiler.IMPORT_FROMJ  sf    {!"%
T"?"?f"?"?t"?"?"??
%%%%%r!   c                     ddddd|j                  }| j        j                            d| d| j        j                                         d           d S )	N-+~not)UNARY_NEGATIVEUNARY_POSITIVEUNARY_INVERT	UNARY_NOTr    r   )rK   rB   r-   rW   rn   )r   rH   ops      r    generic_unaryzDecompiler.generic_unaryR  sl    !!	
 

 + 	
 BB B B)9)=)=)?)? B B BCCCCCr!   c                 l    | j         j                            d| j         j        d          d           d S )Nzlen(r/   r   r   rL   s     r    GET_LENzDecompiler.GET_LEN]  s7    
 >tz'7'; > > >?????r!   c                    | j         j                                        }| j         j                                        }ddddddddd	d
dddd|j                 }| j         j                            d| d| d| d           d S )N*r   r   ///%**&|^<<>>@)BINARY_MULTIPLY
BINARY_ADDBINARY_SUBTRACTBINARY_TRUE_DIVIDEBINARY_FLOOR_DIVIDEBINARY_MODULOBINARY_POWER
BINARY_AND	BINARY_OR
BINARY_XORBINARY_LSHIFTBINARY_RSHIFTBINARY_MATRIX_MULTIPLYr   r   r   )rB   r-   rn   rK   rW   r   rH   r   r   r   s        r    generic_binaryzDecompiler.generic_binarya  s    j""$$j""$$"""%#'  !!&)
 
 + 	
 5C 5 5" 5 5s 5 5 566666r!   c                     | j         j                                        }| j         j                                        }| j         j                            | d| d           d S )Nrl   r   r   r   rH   r   r   s       r    BINARY_SUBSCRzDecompiler.BINARY_SUBSCRw  sY    j""$$j""$$
300000r!   c                 
   | j         j                                        }| j         j                                        }| j         j                                        }| j         j                            | d| d| d           d S )Nrl   r   r   r   )r   rH   r   r   r   s        r    BINARY_SLICEzDecompiler.BINARY_SLICE|  s|    j""$$
 $$&&J$((**	
9 = =u = =s = = =>>>>>r!   c                 .   | j         j                                        }| j         j                                        }ddddddddd	d
dddd|j                 }| j         xj        | d| d| dz  c_        | j         j                            |           d S )Nr   r   r   r   r   r   r   r   r   r   r   r   r   )INPLACE_MULTIPLYINPLACE_ADDINPLACE_SUBTRACTINPLACE_TRUE_DIVIDEINPLACE_FLOOR_DIVIDEINPLACE_MODULOINPLACE_POWERINPLACE_AND
INPLACE_ORINPLACE_XORINPLACE_LSHIFTINPLACE_RSHIFTINPLACE_MATRIX_MULTIPLYr   = rQ   )rB   r-   rn   rK   r,   rW   r   s        r    generic_inplace_binaryz!Decompiler.generic_inplace_binary  s    j""$$j""$$ # ##&$(!!""'*
 
 + 	
S"8"82"8"8"8"8"88
$$$$$r!   c           	      t   | j         j                                        }| j         j                                        }d|j        v rD| j         xj        | d|j         d| dz  c_        | j         j                            |           d S | j         j                            d| d|j         d| d           d S )N=r   rQ   r   r   )rB   r-   rn   r[   r,   rW   r   s       r    	BINARY_OPzDecompiler.BINARY_OP  s    j""$$j""$$$,J""&E&Et|&E&Ec&E&E&EE""J##C(((((J##$C$C$Cdl$C$CS$C$C$CDDDDDr!   c           	          | j         j                                        }| j         j                                        }| j         j                            d| d|j         d| d           d S )Nr   r   r   )rB   r-   rn   rW   rU   r   s       r    
COMPARE_OPzDecompiler.COMPARE_OP  sk    j""$$j""$$
 >C > >$+ > > > > >?????r!   c           	          | j         j                                        }| j         j                                        }|j        dk    rdnd}| j         j                            d| d| d| d           d S )Nr   iszis notr   r   r   rB   r-   rn   rU   rW   r   s        r    IS_OPzDecompiler.IS_OP  ~    j""$$j""$$[A%%TT8
 5C 5 5" 5 5s 5 5 566666r!   c           	          | j         j                                        }| j         j                                        }|j        dk    rdnd}| j         j                            d| d| d| d           d S )Nr   inznot inr   r   r   r  r   s        r    CONTAINS_OPzDecompiler.CONTAINS_OP  r  r!   c                 0    | j         xj        dz  c_        d S )Nbreak
)rB   r,   rL   s     r    
BREAK_LOOPzDecompiler.BREAK_LOOP  s    
)+r!   c                    |                                 }|                     |          }| j        j        rP|| j        j        k    r| j        xj        dz  c_        d S || j        j        k    r| j        xj        dz  c_        d S |S |S )Nr  	continue
)get_jump_targetr   rB   r.   r1   r,   r0   )r   rH   jump_offset
jump_indexs       r    generic_abs_jumpzDecompiler.generic_abs_jump  s    **,,]];//
:! 	TZ666
&&)3&&&&tz:::
&&,6&&&&!!r!   c                     | j         xj        d| j         j        d          dz  c_        | j         j                                         d S )Nreturn r/   rQ   )rB   r,   r-   rn   rL   s     r    RETURN_VALUEzDecompiler.RETURN_VALUE  sK    
"DDJ,<R,@"D"D"DD
r!   c                 B    | j         xj        d|j         dz  c_        d S )Nr$  rQ   r   rL   s     r    RETURN_CONSTzDecompiler.RETURN_CONST  s,    
";DK";";";;r!   c                     t           j        dk    rt          d          | j        xj        d| j        j        d          dz  c_        d S )N)rt      z+YIELD_VALUE is not supported in Python 3.12zyield r/   rQ   )r{   r|   rJ   rB   r,   r-   rL   s     r    YIELD_VALUEzDecompiler.YIELD_VALUE  sW    w&&%=? ? ?
"C4:+;B+?"C"C"CCr!   c                 D    | j         j                            d            d S r   r   rL   s     r    RETURN_GENERATORzDecompiler.RETURN_GENERATOR  r   r!   c                 0    |j         dk    s
J d            d S )Nr   z&Only generator expression is supported)rU   rL   s     r    	GEN_STARTzDecompiler.GEN_START  s$    {a!Ir!   c                    |                                 |                               }|                     |j                  }| j        j        d         }| j        j                                        }| j        j                                        }d|j        v rd| d}n,d|j        v rd| d}nd|j        v rd| d	}nd
|j        v r| }d|j        v r)|                                 |                                 nd|j        v r|                                 t          | j	                  g}| j        j
        r|                    | j        j                   dt          ffdfd| j	        ||         D             }|s| j        j        d         }d|j        v rd| d}n,d|j        v rd| d}nd|j        v r| }nd
|j        v rd| d	}d| dt          d| j                  z   }	| j        xj        |	z  c_        dS t#          |          }
|                     |
          }fd| j	        ||         D             }|                     t#          |                    }| j	        |dz
           }d|j        v sd|j        v s	d|j        v rnf|}|t          | j	                  k     rL| j	        |         j        }d|v sd|v r|dz  }n,d|v r||k    sd|v rn|dz  }|t          | j	                  k     L|                    |           t%          |          }|                     |          5  |                     |dz   |           | j        j        }t          || j                  }| j        j                                        }ddd           n# 1 swxY w Y   d| d| }	| j        xj        |	z  c_        |                     |          5  |                     ||           | j        j        }ddd           n# 1 swxY w Y   |r/t          || j                  }d| }| j        xj        |z  c_        || j        _        |S )u  How we support if-else:

        Failed idea: try to paritition the block of instructions into if and else.
        This is not possible, as the if-else block might have overlapping instructions.
        Take this function as an example:

        def f(a):
            b = 1 if a else 2
            print(b)
        
        The bytecode is:
  2           0 LOAD_FAST                0 (a)
              2 POP_JUMP_IF_FALSE        4 (to 8)
              4 LOAD_CONST               1 (1)
              6 JUMP_FORWARD             1 (to 10)
        >>    8 LOAD_CONST               2 (2)
        >>   10 STORE_FAST               1 (b)

  3          12 LOAD_GLOBAL              0 (print)
             14 LOAD_FAST                1 (b)
             16 CALL_FUNCTION            1
             18 POP_TOP
             20 LOAD_CONST               0 (None)
             22 RETURN_VALUE
        
        The instructions for if branch: 2, 4, 6, 10
        The instructions for else branch: 8, 10
        They share the same instruction 10, so we cannot partition the block into if and else.

        Another example:

        def f():
            g(arg1=a if a is not None else b, arg2=2)
            print(1)

        The bytecode is:

  2           0 LOAD_GLOBAL              0 (g)
              2 LOAD_GLOBAL              1 (a)
              4 LOAD_CONST               0 (None)
              6 IS_OP                    1
              8 POP_JUMP_IF_FALSE        7 (to 14)
             10 LOAD_GLOBAL              1 (a)
             12 JUMP_FORWARD             1 (to 16)
        >>   14 LOAD_GLOBAL              2 (b)
        >>   16 LOAD_CONST               1 (2)
             18 LOAD_CONST               2 (('arg1', 'arg2'))
             20 CALL_FUNCTION_KW         2
             22 POP_TOP

  3          24 LOAD_GLOBAL              3 (print)
             26 LOAD_CONST               3 (1)
             28 CALL_FUNCTION            1
             30 POP_TOP
             32 LOAD_CONST               0 (None)
             34 RETURN_VALUE

        The instructions for if branch: 8, 14, 16, 18, 20, 22
        The instructions for else branch: 10, 12, 16, 18, 20, 22
        They share the same instructions 16, 18, 20, 22, so we cannot partition the block into if and else.

        Current idea:

        We take advantage of the following fact:

        This code snippet:

        if cond:
            if-body
        else:
            else-body
        rest-body

        is equivalent to:

        if cond：
            if-body
            rest-body
        else:
            else-body
            rest-body
        
        By duplicating the rest-body, we can decompile the if-else block separately. And they will have some duplicated code.

        Of course, we don't want to duplicate too long code, so we need to find the end of if-else block.
        The current heuristic is to find the first store/return/jump/for-iter instruction after the if-else block (because they are indicators that we will generate meaningful source code).
        r/   IF_NOT_NONEr   z	 is None)IF_NONEz is not None)IF_TRUEz(not r   IF_FALSEPOP_JUMPOR_POPic                 \    |                                  o|                                 k    S r   )is_jumpr  )r6  r   s    r    qualified_jumpz2Decompiler.generic_jump_if.<locals>.qualified_jumpQ  s'    99;;E1#4#4#6#6+#EEr!   c                 N    g | ]!} |          |                                 "S r7   r  .0r6  r9  s     r    
<listcomp>z.Decompiler.generic_jump_if.<locals>.<listcomp>T  s7    tttbpbpqrbsbst))++tttr!   zif :
r  Nc                 N    g | ]!} |          |                                 "S r7   r;  r<  s     r    r>  z.Decompiler.generic_jump_if.<locals>.<listcomp>k  s7    |||Ajxjxyzj{j{|A--//|||r!   r   RAISERETURNSTOREJUMPFOR_ITERzelse:
)r  r   r   rB   r-   copyrK   rn   r   r   r.   rW   r1   r   r   rD   r,   maxminrG   decompile_range)r   rH   r!  r   condfallthrough_stack
jump_stackend_index_candidatesjump_targetsif_codemax_jumpmax_jump_indexall_jump_targets	last_instold_map_jump_indexrK   	end_indexif_bodyif_end_stack	else_body	else_coder   r9  s                        @@r    generic_jump_ifzDecompiler.generic_jump_if  s   p **,,]];//
]]4;//
z# J,1133Z%**,,
DK''&t&&&DD$+%%*t***DD$+%%"4???DD4;&&9D $$NN!!####$$!!### #D$5 6 67:! 	C ''
(ABBB	Fk 	F 	F 	F 	F 	F 	F utttT5FzS]G]5^ttt 	
 :#B'D++.4...dk))*4***dk))yt{**&t%DooodFV(W(WWGJ""g-""F|$$x00||||9J:WeKe9f|||s+;'<'<==%nq&89	i&&&(i6F*F*F'U^UeJeJe!/ 3t'8#9#999*>:Af$$F(:(:"a'N&&><N+N+NS]agSgSg!# !3t'8#9#999 	##N333,--	^^-.. 	3 	3  a;;;j,G%gt/?@@G:+0022L		3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3
 +****
')^^J'' 	/ 	/  Y777
.I	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/  	0'	43CDDI-)--IJ""i/""'
s%   6ANN"N#PPPc                    |                      |j                  |                      |                                          fdt          | j                  D             d         }d}|                     | j        j                  5  |                     dz   |           | j        j	        }t          || j                  }d|z   }d d d            n# 1 swxY w Y   d}|                     | j        j                  5  fdt          | j                  D             }|r|d         }| j        |dz
                                           r|dz  }|                     |dz   |           | j        j	        }t          || j                  }d|z   }d d d            n# 1 swxY w Y   | j        xj	        ||z   z  c_	        S )	Nc                 P    g | ]"\  }}|j         d k    |cxk    rk     n n|#S )	POP_BLOCKrK   )r=  r6  r   rU  start_indexs      r    r>  z,Decompiler.SETUP_FINALLY.<locals>.<listcomp>  sX     \ \ \A"#(k"9"9kQ>Z>Z>Z>ZQZ>Z>Z>Z>Z>Z >Z>Z>Zr!   r/   r   r   ztry:
c                 <    g | ]\  }}|j         d k    |k    |S )END_FINALLYr^  r=  r6  r   r_  s      r    r>  z,Decompiler.SETUP_FINALLY.<locals>.<listcomp>  sB     !Z !Z !Za*+(m*C*CWXHXHX HXHXHXr!   r   z	finally:
)r   r   r  	enumerater   rG   rB   r-   rI  r,   r   rD   r8  )	r   rH   pop_block_indextry_codefinally_codeend_finally_indexfinally_end_indexrU  r_  s	          @@r    SETUP_FINALLYzDecompiler.SETUP_FINALLY  s   mmDK00MM$"6"6"8"899	\ \ \ \ \* * \ \ \\^` ^^DJ,-- 	+ 	+  q/BBBz-H&x1ABBH(*H		+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ ^^DJ,-- 	7 	7!Z !Z !Z !Z'%' '!Z !Z !Z ! 1-a0	 ) !2Q!67??AA '!Q&!  1!46GHHH:1L*<9IJJL',6L	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	
(\"99s&   A CCCBF##F'*F'c                 4   |                      |j                  fdt          | j                  D             d         }|t	          | j                            dz
  }|z
  dk    rO| j        |         }|j                            d          s|j        dk    rt	          |           |dz  }|z
  dk    Ofdt          | j                  D             }t          j        dk    r	|d         }n|d	         }t          |dz             D ]}t	          | j        |                    | j
        j        d         }|                                 }| j
        j                            | d
           | j
        j                            |           d| d| d}	d}
|                     | j
        j                  5  |                     dz              | j
        j        }
t#          |
| j                  }
|
                                }g }|D ]5}| d
|v sd|                                v r |                    |           6d                    d |D                       }
ddd           n# 1 swxY w Y   | j
        xj        |	|
z   z  c_        |dz   S )a  
        with expression as var:
            body

        is equivalent to:

        var = expression
        var.__enter__()
        try:
            body
        finally:
            var.__exit__()

        We find the start of `finally` by `WITH_EXCEPT_START`, and the end of `finally` by `POP_EXCEPT`.
        In early python version, the start is `WITH_CLEANUP_START` and the end is `WITH_CLEANUP_FINISH`.
        c                 8    g | ]\  }}|j         d v |k    |S ))WITH_EXCEPT_STARTWITH_CLEANUP_STARTr^  rb  s      r    r>  z)Decompiler.SETUP_WITH.<locals>.<listcomp>  sC     o o o41a"#(.Y"Y"Y^_bm^m^m ^m^m^mr!   r/   r   rx   rD  PUSH_EXC_INFOc                 8    g | ]\  }}|j         d v |k    |S ))
POP_EXCEPTWITH_CLEANUP_FINISHr^  )r=  r6  r   rU  s      r    r>  z)Decompiler.SETUP_WITH.<locals>.<listcomp>  sC     g g gDAq"#(.S"S"SXY\eXeXe  XeXeXer!   rs   r   z	.__exit__zwith z as r?  r   zNone(None, None)c                     g | ]}|d z   S )rQ   r7   r=  r   s     r    r>  z)Decompiler.SETUP_WITH.<locals>.<listcomp>  s     7 7 7aT 7 7 7r!   N)r   r   rc  r   r   rK   r\   r{   r|   rangerB   r-   rZ   rW   rG   rI  r,   r   rD   
splitlinesstripjoin)r   rH   with_except_indexr6  _instpop_except_indicespop_except_indexrp   tempwith_clause	with_bodylinesanslinerU  r_  s                 @@r    
SETUP_WITHzDecompiler.SETUP_WITH  sU   " mmDK00o o o o9, , o o ooqs%	))4555 M!mq  %a(E|&&v.. '%,/2Q2Q&&&FA	 !mq  g g g gI- - g g gw&&1!41!4y"2Q"677 	2 	2AD-a01111jr"!!##
4 2 2 2333
%%%0c00t000	^^DJ,-- 	9 	9  q)<<<
.I'	43CDDI((**EC ! !%%%--1Ctzz||1S1S 

4     7 73 7 7 788I	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9" 	
+	"99!##s   >B(I22I69I6c                 ,   |                      |j                  }|                      |                                          }|                                 }d| d| j        j                                         d}| j        j                            |           | j        |         }|	                                r"|                                |j        k    r|dz  }| 
                    | j        j        d||          5  |                     |dz   |           | j        j        }|t          || j                  z   }| j        j                                        }d d d            n# 1 swxY w Y   | j        xj        |z  c_        || j        _        |S )Nzfor  in r?  r   T)r.   r0   r1   )r   r   r  rZ   rB   r-   rn   rW   r   r8  rG   rI  r,   r   rD   rF  )	r   rH   r_  rU  ra   for_coderS  r:   for_end_stacks	            r    rE  zDecompiler.FOR_ITER  s   mmDK00MM$"6"6"8"899	&&((	D)DD)9)=)=)?)?DDD
	***%i0	 	9#<#<#>#>$+#M#M NI^^DJ,$Q\mv^ww 	4 	4  q)<<<:)D/$8H"I"IIH J,1133M		4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4
 	
(*(
s    AE((E,/E,c                     |j         dk    r|j        }ndddd|j                  }| j        j        | d          }|d         g|d d         z   }|| j        j        | d <   d S )NROT_Nrx   rt   rC   )ROT_TWO	ROT_THREEROT_FOURr/   )rK   rU   rB   r-   )r   rH   nvaluess       r    rot_nzDecompiler.rot_n  s    ;'!!AA   k	A
 !1"##&*ss+ &
!r!   c                     |j         }| j        j        d         }| j        j        |          }||}}|| j        j        d<   || j        j        | <   d S Nr/   )rU   rB   r-   )r   rH   r  rp   r   s        r    SWAPzDecompiler.SWAP&  sX    Kjr"
 1%CU"
 %
1r!   c                     |j         }|dk    rd S | j        j        d|dz
  z
           }| j        j                            |           d S )Nr   r/   r   )rU   rB   r-   rW   )r   rH   r  r   s       r    COPYzDecompiler.COPY.  sO    K 66F
 q1u.
&&&&&r!   c                 B    | j         j                                         d S r   rB   r-   rn   rL   s     r    POP_TOPzDecompiler.POP_TOP8  s    
r!   c                 d    | j         j                            | j         j        d                    d S r  r   rL   s     r    DUP_TOPzDecompiler.DUP_TOP;  s+    

 0 455555r!   c                     | j         j        d         }| j         j        d         }| j         j                            |           | j         j                            |           d S )Nr/   r   r   rH   rp   tos1s       r    DUP_TOP_TWOzDecompiler.DUP_TOP_TWO?  sX    jr"z#
%%%
$$$$$r!   c                     | j         j        |j                 }| j        j                            t          |                     d S r   )r:   	co_constsr]   rB   r-   rW   rT   )r   rH   namess      r    KW_NAMESzDecompiler.KW_NAMESG  s8    	#DH-
U,,,,,r!   c                 0    fd j         D             }d}|rH|d         j        dk    s5t          |          dk    r$|d         j        dk    r|d         j        dk    rd}t                      }|r+t	           j        j                                                  } fd	t          j	                  D             }|d d d         }|d t          |          t          |          z
           }|t          |          t          |          z
  d          }g }t          ||          D ]\  }	}
|                    |	 d
|
              j        j                                        } j        j        r0 j        j        d          j        j                                         d|v r!|g} j        j                                        } j        j                            | dd                    ||z              d                                             d S )Nc                 4    g | ]}|j         j         k     |S r7   )r   )r=  r   rH   s     r    r>  z#Decompiler.CALL.<locals>.<listcomp>L  s'    LLL1QX5K5KQ5K5K5Kr!   Fr/   r  r   r  PRECALLTc                 L    g | ] }j         j                                        !S r7   r  r=  _r   s     r    r>  z#Decompiler.CALL.<locals>.<listcomp>U  *    EEEQ!%%''EEEr!   r  r   r   r   r   )r   rK   r   tuplerS   rB   r-   rn   rt  rU   ziprW   rw  ri   )r   rH   rS  has_kw_nameskw_namesargspos_argskwargskwcallsr`   r   funcs   ``          r    CALLzDecompiler.CALLK  s1   LLLL 1LLL	 	$}#z11c7 7!"7# 7#'0}';z'I'IiXZmNbfoNoNo#77 	4DJ,002233HEEEE%2D2DEEEDDbDz2TS]]223c$ii#h--/001x00 	. 	.KD%NNd,,U,,----z##%%: 	#
 0 4 <J  """d?? vH:#''))D
4 J J$))Hw4F*G*G J J JKKK**,,,,,r!   c                 2     fdt          |j                  D             }|d d d         } j        j                                        } j        j                            | dd                    |           d                                             d S )Nc                 L    g | ] }j         j                                        !S r7   r  r  s     r    r>  z+Decompiler.generic_call.<locals>.<listcomp>i  r  r!   r/   r   r   r   )rt  rU   rB   r-   rn   rW   rw  ri   )r   rH   r  r  s   `   r    generic_callzDecompiler.generic_callh  s    EEEE%2D2DEEEDDbDzz##%%
4 < <$))D// < < <===**,,,,,r!   c                     t           j        j                                                  } fdt	          t          |                    D             }|                                 g }t          ||          D ]\  }}|                    | d|              fdt	          |j	        t          |          z
            D             }|d d d         } j        j                                        } j        j                            | dd
                    ||z              d                                             d S )Nc                 L    g | ] }j         j                                        !S r7   r  r  s     r    r>  z/Decompiler.CALL_FUNCTION_KW.<locals>.<listcomp>s  s*    IIIDJ$((**IIIr!   r  c                 L    g | ] }j         j                                        !S r7   r  r  s     r    r>  z/Decompiler.CALL_FUNCTION_KW.<locals>.<listcomp>x  s@     @ @ @ Z%))++ @ @ @r!   r/   r   r   r   )rS   rB   r-   rn   rt  r   reverser  rW   rU   rw  ri   )	r   rH   kw_argskw_valsr  r`   valr  r  s	   `        r    CALL_FUNCTION_KWzDecompiler.CALL_FUNCTION_KWq  sU   tz'++--..IIIIU3w<<5H5HIIIWg.. 	, 	,ID#NNd??S??++++@ @ @ @"4;W#=>>@ @ @DDbD>z##%%
4 J J$))Hw4F*G*G J J JKKK**,,,,,r!   c                 "   |j         dk    rb| j        j                                        }| j        j                                        }| j        j                            | d| d           n|j         dk    r| j        j                                        }| j        j                                        }| j        j                                        }| j        j                            | d| d| d           |                                  d S )Nr   z(*r   r   z, **)rU   rB   r-   rn   rW   ri   )r   rH   r  r  r  s        r    CALL_FUNCTION_EXzDecompiler.CALL_FUNCTION_EX  s   ;!:#''))D:#''))DJ##t$6$6t$6$6$67777[Aj&**,,G:#''))D:#''))DJ##t$C$Ct$C$C$C$C$CDDD**,,,,,r!   c                    |j         dv rd S |j         dv rd S |j         dk    rV| j        xj        d| j        j                                         dz  c_        | j        j                            d           d S |j         dk    r$d| j        j        d	          | j        j        d	<   d S |j         d
k    r|                     |          S d S )N)INTRINSIC_1_INVALIDINTRINSIC_IMPORT_STARINTRINSIC_STOPITERATION_ERRORINTRINSIC_ASYNC_GEN_WRAP)INTRINSIC_TYPEVARINTRINSIC_PARAMSPECINTRINSIC_TYPEVARTUPLEINTRINSIC_SUBSCRIPT_GENERICINTRINSIC_TYPEALIASINTRINSIC_PRINTzprint(r   NoneINTRINSIC_UNARY_POSITIVEr   r/   INTRINSIC_LIST_TO_TUPLE)r[   rB   r,   r-   rn   rW   LIST_TO_TUPLErL   s     r    CALL_INTRINSIC_1zDecompiler.CALL_INTRINSIC_1  s    < , , , D\  Z  Z  ZD\...J""&Jtz/?/C/C/E/E&J&J&JJ""J##F+++++\777#=tz'7';#=#=DJR   \666%%d+++ 76r!   c                    | j         j                                        }g }t          |j                  D ])}|                    |                                            *d                    d |D                       }| j         xj        |d| dz   z  c_        |d d d         D ]!}| j         j                            |           "d S )Nr   c                     g | ]}| d S )r   r7   rs  s     r    r>  z.Decompiler.UNPACK_SEQUENCE.<locals>.<listcomp>  s    333A!xxx333r!   r  rQ   r/   )	rB   r-   rn   rt  rU   rW   rZ   rw  r,   )r   rH   varname	tmp_namesr6  r   r`   s          r    UNPACK_SEQUENCEzDecompiler.UNPACK_SEQUENCE  s     *"&&((	t{## 	3 	3AT//112222 gg3333344
#(8W(8(8(8"88dddO 	* 	*DJ##D))))	* 	*r!   c                    | j         j                                        }g }t          |j                  D ])}|                    |                                            *|                                 }| j         xj        d                    |          d| z   d| dz   z  c_        | j         j                            |           |d d d         D ]!}| j         j                            |           "d S )Nr   z, *rR   rQ   r/   )	rB   r-   rn   rt  rU   rW   rZ   r,   rw  )r   rH   r  r  r6  	star_namer`   s          r    	UNPACK_EXzDecompiler.UNPACK_EX  s   *"&&((	t{## 	3 	3AT//112222&&((	
$))I"6"69Jy9J9J"JM^SZM^M^M^"^^
	***dddO 	* 	*DJ##D))))	* 	*r!   c           	         | j         j                                        }| j         j                                        }|j        dk    r(| j         j                            d| d| d           d S |j        dk    rI| j         j                                        }| j         j                            d| d| d| d           d S d S )Nrx   zslice(r   r   rt   r  )r   rH   rp   r  tos2s        r    BUILD_SLICEzDecompiler.BUILD_SLICE  s    j""$$z##%%;!J##$;T$;$;S$;$;$;<<<<<[A:#''))DJ##$CT$C$CT$C$CS$C$C$CDDDDD r!   c                 `     fdt          |j                  D             }|d d d         }d|j        v rd |D             }|j        dk    r+ j        j                            d|d          d           d S  j        j                            dd	                    |           d
           d S )Nc                 L    g | ] }j         j                                        !S r7   r  r  s     r    r>  z*Decompiler.build_tuple.<locals>.<listcomp>  *    CCC1
 $$&&CCCr!   r/   UNPACKc                     g | ]}d | S r   r7   r=  r]   s     r    r>  z*Decompiler.build_tuple.<locals>.<listcomp>      ...#III...r!   r   r   r   z,)r   r   )rt  rU   rK   rB   r-   rW   rw  r   rH   r  s   `  r    build_tuplezDecompiler.build_tuple  s    CCCCdk0B0BCCCDDbDzt{"".....D;!J##OQOOO44444J##$:		$$:$:$:;;;;;r!   c                      fdt          |j                  D             }|d d d         }d|j        v rd |D             } j        j                            dd                    |           d                                             d S )Nc                 L    g | ] }j         j                                        !S r7   r  r  s     r    r>  z)Decompiler.build_list.<locals>.<listcomp>  r  r!   r/   r  c                     g | ]}d | S r  r7   r  s     r    r>  z)Decompiler.build_list.<locals>.<listcomp>  r  r!   rl   r   r   )rt  rU   rK   rB   r-   rW   rw  ri   r  s   `  r    
build_listzDecompiler.build_list  s    CCCCdk0B0BCCCDDbDzt{"".....D
 6DIIdOO 6 6 6777**,,,,,r!   c                 @    d}|j         dk    rd}nY fdt          |j                   D             }|d d d         }d|j        v rd |D             }dd	                    |           d
} j        j                            |                                             d S )Nr   r   zset()c                 L    g | ] }j         j                                        !S r7   r  r  s     r    r>  z(Decompiler.build_set.<locals>.<listcomp>  *    GGGqDJ$((**GGGr!   r/   r  c                     g | ]}d | S r  r7   r  s     r    r>  z(Decompiler.build_set.<locals>.<listcomp>  s    222c	C		222r!   {r   })rU   rt  rK   rw  rB   r-   rW   ri   )r   rH   r  r  s   `   r    	build_setzDecompiler.build_set  s    ;!CCGGGGE$+4F4FGGGD":D4;&&22T222*tyy***C
$$$**,,,,,r!   c                 `    |j         dk    r  j        j                            d           nm fdt	          |j                   D             }|d d d         }d |D             } j        j                            dd                    |           d                                             d S )	Nr   zdict()c                 L    g | ] }j         j                                        !S r7   r  r  s     r    r>  z/Decompiler.build_map_unpack.<locals>.<listcomp>  r  r!   r/   c                     g | ]}d | S )r   r7   r  s     r    r>  z/Decompiler.build_map_unpack.<locals>.<listcomp>  s    ///3JJJ///r!   r  r   r  )rU   rB   r-   rW   rt  rw  ri   r  s   `  r    build_map_unpackzDecompiler.build_map_unpack  s    ;!J##H----GGGGE$+4F4FGGGD":D//$///DJ##$<4$<$<$<===**,,,,,r!   c           
      T     fdt          |j        dz            D             }|d d d         }|d d d         }|dd d         } j        j                            dd                    d t          ||          D                        d                                             d S )	Nc                 L    g | ] }j         j                                        !S r7   r  r  s     r    r>  z(Decompiler.BUILD_MAP.<locals>.<listcomp>  s*    GGG1
 $$&&GGGr!   rx   r/   r   r  r   c                 "    g | ]\  }}| d | S z: r7   r=  kvs      r    r>  z(Decompiler.BUILD_MAP.<locals>.<listcomp>  &    FFFDAqqKKAKKFFFr!   r  )rt  rU   rB   r-   rW   rw  r  ri   )r   rH   r  keysr  s   `    r    	BUILD_MAPzDecompiler.BUILD_MAP  s    GGGGdkAo0F0FGGGDDbDzCCaCyadd
KFFCf4E4EFFFGGKKK	M 	M 	M**,,,,,r!   c           
      x    t           j        j                                                  } fdt	          |j                  D             }|d d d         } j        j                            dd                    d t          ||          D                        d            	                                 d S )Nc                 L    g | ] }j         j                                        !S r7   r  r  s     r    r>  z2Decompiler.BUILD_CONST_KEY_MAP.<locals>.<listcomp>  r  r!   r/   r  r   c                 "    g | ]\  }}| d | S r  r7   r  s      r    r>  z2Decompiler.BUILD_CONST_KEY_MAP.<locals>.<listcomp>  r  r!   r  )
rS   rB   r-   rn   rt  rU   rW   rw  r  ri   )r   rH   r  r  r  s   `    r    BUILD_CONST_KEY_MAPzDecompiler.BUILD_CONST_KEY_MAP  s    DJ$((**++CCCCdk0B0BCCCddd
KFFCf4E4EFFFGGKKK	M 	M 	M**,,,,,r!   c                       fdt          |j                  D             }|d d d         }d                    |          } j        j                            |           d S )Nc                 L    g | ] }j         j                                        !S r7   r  r  s     r    r>  z+Decompiler.BUILD_STRING.<locals>.<listcomp>  r  r!   r/   z + )rt  rU   rw  rB   r-   rW   )r   rH   r  r  s   `   r    BUILD_STRINGzDecompiler.BUILD_STRING  se    CCCCdk0B0BCCCDDbDzD!!
'''''r!   c                     | j         j                                        }| j         j                            d| d           d S )Nztuple(r   r   )r   rH   items      r    r  zDecompiler.LIST_TO_TUPLE  sC    z##%%
 0 0 0 011111r!   c                     | j         j                                        }|                     |j                  }| j         xj        | d| dz  c_        d S )N)posz.extend(r   )rB   r-   rn   ri   rU   r,   r   rH   r  r|  s       r    LIST_EXTENDzDecompiler.LIST_EXTEND  s]    !%%''11dk1BB
T">">6">">">>r!   c                     |j         dk    r|xj         dz  c_         | j        j        |j                   }| j        j                                        }| j        xj        | d| dz  c_        d S )Nr   z.append(r   r   r   rH   r   r   s       r    LIST_APPENDzDecompiler.LIST_APPEND  su    ;!KK1KKJ$dk\2	
 $$&&
Y"B"B"B"B"BBr!   c                     |j         dk    s
J d            | j        j                                        }|                                 }| j        xj        | d| dz  c_        d S )Nr   zOnly tested for argval==1z.update(r   )rU   rB   r-   rn   ri   r,   r	  s       r    generic_updatezDecompiler.generic_update%  sr    {a!<!%%''1133
T">">6">">">>r!   c                     |j         dk    r|xj         dz  c_         | j        j        |j                   }| j        j                                        }| j        xj        | d| dz  c_        d S )Nr   z.add(r   r   r  s       r    SET_ADDzDecompiler.SET_ADD-  su    ;!KK1KKJ$dk\2	
 $$&&
Y"?"?U"?"?"??r!   c                    | j         j        |j         dz
           }t          j        dk    r=| j         j                                        }| j         j                                        }n<| j         j                                        }| j         j                                        }| j         xj        | d| d| dz  c_        d S )Nr   )rt   rw   z.__setitem__(r   r   )rB   r-   rU   r{   r|   rn   r,   )r   rH   r   r   keys        r    MAP_ADDzDecompiler.MAP_ADD5  s    J$dk\A%56	v%%J$((**E*"&&((CC*"&&((CJ$((**E
Y"N"NS"N"NE"N"N"NNr!   c                    |j         dk    r| j        xj        dz  c_        d S |j         dk    r7| j        xj        d| j        j                                         dz  c_        d S |j         dk    rZ| j        j                                        }| j        j                                        }| j        xj        d| d| dz  c_        d S d S )Nr   zraise
r   zraise rQ   rx   z from )rU   rB   r,   r-   rn   r  s       r    RAISE_VARARGSzDecompiler.RAISE_VARARGSA  s    ;!J""i/""""[AJ""&Itz/?/C/C/E/E&I&I&II""""[A*"&&((C:#''))DJ""&Bt&B&B3&B&B&BB"""" r!   c                    |j         \  }}|rd| j        j                                        }| j        j                                        }| j        j                            d| d| d           d S | j        j                                        }|t
          n|}| j        j                            |j         d| d           d S )Nzformat(r   r   r   )rU   rB   r-   rn   rW   r2   r%   )r   rH   r  spec	form_specr   s         r    FORMAT_VALUEzDecompiler.FORMAT_VALUEK  s    [
d 	A
(,,..IJ$((**EJ##$Ce$C$Cy$C$C$CDDDDDJ$((**E,33DDJ##t}$?$?u$?$?$?@@@@@r!   r   r   c                    	 |}||k     rP| j         |         }t          t          |j        t          j                  } || |          }|r|}n|dz  }||k     Nd S d S # t
          $ r%}t          d| d| j        j                   |d }~ww xY w)Nr   z Failed to decompile instruction r  )	r   getattrr9   rK   rM   r}   r   r:   co_name)r   r   r   running_indexrH   methodoutputes           r    rI  zDecompiler.decompile_rangeW  s    	Y!M#%%(7 K8: :  d++ '$*MM!Q&M  #%%%%%%  	Y 	Y 	Y$P4PPTY=NPPR RWXY	Ys   AA 
B& BBr   c                 x    t          | j                  D ]\  }}|j        |k    r|c S t          d|           )Nz$Cannot find instruction with offset )rc  r   r   
ValueError)r   r   idxrH   s       r    r   zDecompiler.index_ofi  sR    "4#455 	 	IC{f$$


 %HHHIIIr!   r   c                 b    t          |           t          |           t          | |           d S r   )r   r   r   )r:   r   s     r    cleanup_instructionszDecompiler.cleanup_instructionso  s4    L)))"<000 |44444r!   c                 $   t          |          rddlm}  ||          j        }|| _        t          d t          j        |          D                       }t          	                    ||           || _
        t          dg           | _        d S )Nr   )get_code_ownerc              3   4   K   | ]}t          |          V  d S r   )r   )r=  r  s     r    	<genexpr>z&Decompiler.__init__.<locals>.<genexpr>z  sF       A A ! 022 A A A A A Ar!   r   r?   )callabledepyf.utilsr(  __code__r:   r4   disget_instructionsr9   r&  r   r+   rB   )r   r:   r(  r   s       r    r   zDecompiler.__init__u  s    D>> 	1222222!>$''0D	 A A%(%9$%?%?A A A A A''l;;;($2>>>


r!   c                 V    t           xj        dz  c_        | j         t           j         S )Nr   )r9   r;   r=   r#   s    r    rZ   zDecompiler.get_temp_name  s-    "";J$9;;;r!   r   r  c                     | j         j        |          }|                                 }| j         xj        | d| dz  c_        || j         j        | <   |S r   )rB   r-   rZ   r,   )r   r  r  ra   s       r    ri   z(Decompiler.replace_mutable_tos_with_temp  sb    jt$&&((	
Y":":3":":"::!*
#r!   c                      g } t           j        D ]C}t          t          |t          j                  t          j        ur|                     |           D| S r   )r.  rK   r  r9   rM   rW   )opnamesr   s     r    supported_opnameszDecompiler.supported_opnames  sa     	" 	"A8: : BLAef f q!!!r!   N)maxsizerz   returnc                    	 || _         || _        |                     dt          | j                             | j        j        }t          j        	                    dd          dk    rt          || j        |          }t          | j        |          }t          d t          j        | j                  D                       }|rdd                    |          z   dz   nd}| j        j        r%d	d                    | j        j                  z   dz   nd}||z   |z   }|t%          ||          z   }|S # t&          $ r  t(          $ r"}	t'          d
| j        j                   |	d }	~	ww xY w)Nr   DEPYF_REMOVE_TEMP1c              3   :   K   | ]}|j         d k    |j        V  dS )STORE_GLOBALN)rK   rU   )r=  rH   s     r    r*  z'Decompiler.decompile.<locals>.<genexpr>  s3      xxtZ^ZeiwZwZwt{ZwZwZwZwxxr!   zglobal r   rQ   r   z	nonlocal zFailed to decompile )rD   r=   rI  r   r   rB   r,   osenvirongetr   r   r:   setr.  r/  rw  co_freevarsr   r   r}   r  )
r   rD   r=   rz   r,   headerglobal_namesglobal_statementsnonlocal_statementr!  s
             r    r   zDecompiler.decompile  s   	C*D*D  C(9$:$:;;;*0K z~~13773>>
 /!1;@ @+DI7GHHFxxs7KDI7V7VxxxxxL(4!=	DII- - ! $!% !%:<  261F"Otyy	%0' 0' "')-". ".LN +.@@;NK ?;#L#LLK! 	 	 	 	C 	C 	C$:ty'8::< <ABC	Cs   D/D2 2E(E##E(code_to_decompilereference_codefilepath_templatec                 4   t          |                               ||d          }ddlm}  |||          }|!|j        |                    d          }d}n||                    d          d          }|j        r||                    d          d          }d}		 ||	z  }t          j	        
                    |          rrt          |d                                          }
|
|
                    d          d          }|j        r||                    d          d          }||k    r|
}n|	d	z  }	n|                    t          j	        j                  d
                             d          d         |                    d          }t          |d          5 }|                    |           d d d            n# 1 swxY w Y   n/|                    t          j	        j                  d
                             d          d         ddlm} t%          ||d          } ||          }fd|D             d         }g }t'          |j                  D ]\  }}t+          |t,                    rf|                    d          rQt1          |                    d          d
         d d
                   }|                    | j        |                    |                    |           |                    t5          |                    }|S )N__place_holder__)rD   r=   rz   r   )fix_irregular_codenonamer   Trr   r/   rd   w)collect_all_code_objectsexec)filenamemodec                 *    g | ]}|j         k    |S r7   )r  )r=  r   r   s     r    r>  z9Decompiler.decompile_and_compile_like.<locals>.<listcomp>  s&    ,d,d,d1QYZcMcMcQMcMcMcr!   __co_constsrl   )r  )r9   r   depyf.code_transformrJ  r  rf   findr@  r<  pathexistsopenreadr~   sepwriter,  rN  compilerc  r  rX   r2   r\   r6   rW   r  )rE  rF  rD   r=   rG  srcrJ  rP  src_bodycountexisting_codeexisting_code_bodyfrN  transformed_codetransformed_codes!decompiled_and_compiled_back_codeupdated_constsr6  r   r   r   s                        @r    decompile_and_compile_likez%Decompiler.decompile_and_compile_like  s    *++55+[f  zL5  M  M 	<;;;;;  55$&.I++0)<<CHH388C==>>*H) 9#HMM#$6$6$7$78E,u47>>(++ $(3$7$7$<$<$>$>M)6}7I7I#7N7N7O7O)P&%1 _-?@R@W@WX[@\@\@]@]-^*#555+
 (rw{ ; ;B ? E Ec J J1 MI++&8)DDCh,, %% % % % % % % % % % % % % % %%( !rw{33B7==cBB1EI888888"3GGG445EFF,d,d,d,d8I,d,d,def,g) ?IJJ 	) 	)DAq!S!! )all=&A&A )AGGCLL,SbS122%%&7&A%&HIIII%%a((((,M,U,U`eft`u`u,U,v,v)00s   GGGc                 *    t          | j                  S r   )idr:   r#   s    r    __hash__zDecompiler.__hash__  s    $)}}r!   c                 B    t          |           t          |          k    S r   )hash)r   others     r    __eq__zDecompiler.__eq__  s    DzzT%[[((r!   )Fr/   r/   )r   )rC   r<   N)r%   r&   r'   r(   r   r3   r;   r6   r=   r2   dataclassesfieldrB   r+   rD   
contextlibcontextmanagerrG   r   rM   GET_YIELD_FROM_ITERrp  rl  JUMP_IF_NOT_EXC_MATCHCHECK_EG_MATCHrn  PREP_RERAISE_STARrq  CALL_FINALLYPOP_FINALLYrm  SETUP_EXCEPTCHECK_EXC_MATCHCLEANUP_THROWGET_AWAITABLE	GET_AITER	GET_ANEXTEND_ASYNC_FORBEFORE_ASYNC_WITHSETUP_ASYNC_WITHSENDASYNC_GEN_WRAPCACHE
PRINT_EXPRCOPY_DICT_WITHOUT_KEYSIMPORT_STAR
YIELD_FROMSETUP_ANNOTATIONSLOAD_BUILD_CLASSMATCH_MAPPINGMATCH_SEQUENCE
MATCH_KEYSMATCH_CLASSCALL_INTRINSIC_2rO   EXTENDED_ARGNOPRESUME
SETUP_LOOPr]  r  BEGIN_FINALLYra  	MAKE_CELLRERAISEEND_FORrb   rg   	LOAD_FASTLOAD_FAST_AND_CLEARLOAD_FAST_CHECKLOAD_GLOBAL
LOAD_DEREF	LOAD_NAMELOAD_CLASSDEREFLOAD_CLOSURErj   rq   LOAD_FROM_DICT_OR_DEREFr   r   r   r   r   r   r   r   r   ry   r;  STORE_DEREF
STORE_NAMEr   r   r   r   r   DELETE_NAMEDELETE_GLOBALDELETE_DEREFDELETE_FASTr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   r   r  r  r  r  r  r  r  r  r	  r
  r  r  r  r  r  r"  JUMP_ABSOLUTEJUMP_FORWARDJUMP_BACKWARDJUMP_BACKWARD_NO_INTERRUPTr%  r'  r*  r,  r.  rZ  POP_JUMP_IF_TRUEPOP_JUMP_IF_FALSEPOP_JUMP_FORWARD_IF_TRUEPOP_JUMP_FORWARD_IF_FALSEPOP_JUMP_BACKWARD_IF_TRUEPOP_JUMP_BACKWARD_IF_FALSEPOP_JUMP_FORWARD_IF_NONEPOP_JUMP_FORWARD_IF_NOT_NONEPOP_JUMP_BACKWARD_IF_NONEPOP_JUMP_BACKWARD_IF_NOT_NONEJUMP_IF_TRUE_OR_POPJUMP_IF_FALSE_OR_POPPOP_JUMP_IF_NOT_NONEPOP_JUMP_IF_NONEri  r  BEFORE_WITHrE  r  r  r  r  r  r  r  r  r  r  r  r  r  CALL_FUNCTIONCALL_METHODr  r  r  r  r  r  r  BUILD_TUPLEBUILD_TUPLE_UNPACKBUILD_TUPLE_UNPACK_WITH_CALLr  
BUILD_LISTBUILD_LIST_UNPACKr  	BUILD_SETBUILD_SET_UNPACKr  BUILD_MAP_UNPACKBUILD_MAP_UNPACK_WITH_CALLr  r  r  r  r
  r  r  
SET_UPDATEDICT_UPDATE
DICT_MERGEr  r  r  r  rI  r   staticmethodr   r&  r   r   r   rZ   ri   r4  	functools	lru_cacher	   r   rg  rj  rn  r7   r!   r    r9   r9   3   s        ))
NNNJ K   .[.& &' ' 'E? ' ' ' K   Mk M M M M 4 lE  EJ  E"  E%:  E^  Em  E^o  E  sF  E  IU  E  Xc  E  fx  E  {G  E  JY  \i |U  UM  UI  U	  UM  U<M  UP`  Ucgjx%E +DCJ' ,K uN  NJ  N"  N%5  N  N  NYcfq 1
     LT__C_&_:_	_G_mkIG
 G
 F{  F  F  F  FD	1 	1 	1 	1 	1 AM  MI  M#  Mo  M  Mj  MS\  M_nq}- - - - --k - - - - 8*"+ *" *" *" *"X;    
Dk D D D D-K - - - -K K K K K2 2 2 2 2&k & & & &0[ 0 0 0 0<+ < < < < <IHJHHj> > > > >L L L L LC{ C C C C<+ < < < <9; 9 9 9 9 2@?K?-, K= = = = =
& & & & && & & & &D+ D D D D BONNN^NlY@K @ @ @ @7; 7 7 7 7( _m  mO  mj  m?  m5G  mJ]  m`m  mp|  m  @J  m  MV  m  Yc  m  fs  m  vC  F\1+ 1 1 1 1
? ? ? ? ?%; % % % %* lB  B  B{  B%5  B8K  BNb  Bes  B  wD  B  GR  B  U_  B  bm  B  p~  B  AO  RiEk E E E E@{ @ @ @ @
7+ 7 7 7 77 7 7 7 7,{ , , , ,[     Qa`M`L`=3M    < < < < <D D D D D&[ & & & &Jk J J J JtK t t t tn ,;:(;JJ8=LL :>MM;@OO =1@@.80+    >@${ @$ @$ @$ @$D K[    ,'+ ' ' ' ' .32E2G2i(& & & & &' ' ' ' 'K    6K 6 6 6 6% % % % %-[ - - - -- - - - -:- - - - - #/.MK-[ - - - -
-[ 
- 
- 
- 
-,[ , , , ,,*K * * * *.	*k 	* 	* 	* 	*E E E E E< < < < < GRQKQ$'C-{ - - - - &0/J"-k - - - - $-,I -[ - - - - 5ED1-k - - - -- - - - -( ( ( ( (2+ 2 2 2 2? ? ? ? ?
C C C C C?; ? ? ? ? -;:J:z@K @ @ @ @	OK 	O 	O 	O 	OC+ C C C C	A 	A 	A 	A 	AYS Ys Y Y Y Y$Js J J J J 5k1B 5 5 5 \5
	?U8X#56 	? 	? 	? 	?< < <        \ Y&&& (.2	!C !C !C 'sm	!C 8;	!C !C !C '&!CF  (/3C1 C1'C1$C1 	C1
  (}C1
 9AC1 C1 C1 \C1J  ) ) ) ) )r!   r9   r:   r6  c                 D    t          |                                           S )aS  Decompile any callable or code object into Python source code.
    It is especially useful for some dynamically generated code, like ``torch.compile``,
    or ``dataclasses``.

    Example usage:

    .. code-block:: python

        from dataclasses import dataclass
        @dataclass
        class Data:
            x: int
            y: float

        import depyf
        print(depyf.decompile(Data.__init__))
        print(depyf.decompile(Data.__eq__))
    
    Output:

    .. code-block:: python

        def __init__(self, x, y):
            self.x = x
            self.y = y
            return None

        def __eq__(self, other):
            if other.__class__ is self.__class__:
                return (self.x, self.y) == (other.x, other.y)
            return NotImplemented
    
    The output source code is semantically equivalent to the function, but not syntactically the same. It verbosely adds many details that are hidden in the Python code. For example, the above output code of ``__init__`` explicitly returns ``None``, which is typically ignored.

    Another detail is that the output code of ``__eq__`` returns ``NotImplemented`` instead of raising ``NotImplemented`` exception when the types are different. At the first glance, it seems to be a bug. However, it is actually the correct behavior. The ``__eq__`` method should return ``NotImplemented`` when the types are different, so that the other object can try to compare with the current object. See `the Python documentation <https://docs.python.org/3/library/numbers.html#implementing-the-arithmetic-operations>`_ for more details.
    )r9   r   )r:   s    r    r   r     s    J d%%'''r!   )&r(   r{   r<  r.  typesr   typingr   r   r   r   r   r	   ro  inspectr  collectionsr
   rq  code_transformr   r   r   r   r   r   r   r   r   utilsr   r}   r   	dataclassr+   r9   r2   r   r7   r!   r    <module>r     s   L L 



 				 



       ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?          # # # # # #    
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
     
5 5 5 5 5 5 5 5         R) R) R) R) R) R) R) R)h&%(E(H,- %(# %( %( %( %( %( %(r!   