
    J/Phl_                        d dl mZm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
mZmZ d dlmZ edv r	d dlmZ dZned	v rn ee           ed
dg          Z edd          ZdZd Zd Zd Z eej                  Z eej                  Zeez  Z e eddg                    Zej         Z ej!        Z! G d de"          Z#dZ$dZ%dZ&ej'        (                    d          Z)edv rd Z*nedv rd Z*n ee          d Z+ G d de"          Z, G d de"          Z-d  Z. G d! d"e-          Z/ G d# d$e/          Z0ed%k    re/Z1nedv re0Z1ned%k     re-Z1n ee           G d& d'ej2                  Z3dS )(    )
namedtupleOrderedDictN)CodeType
ModuleType)errorsutils	serialize)	PYVERSION)      r      )_inline_cache_entries   r   
   r      opcode_infoargsize_ExceptionTableEntryzstart end target depth lastic                 L    t          | dd          }|rt          | |          S | S )z
    Objects that wraps function should provide a "__numba__" magic attribute
    that contains a name of an attribute that contains the actual python
    function object.
    	__numba__Ngetattr)objattrs     S/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/numba/core/bytecode.pyget_function_objectr!       s2     3T**D "sD!!!J    c           	      B    t          | dt          | dd                    S )z"Shamelessly borrowed from llpython__code__	func_codeNr   )r   s    r    get_code_objectr&   ,   s     3
GCd$C$CDDDr"   c                     g }| D ]8}t           j                            |          }||                    |           9|S N)disopmapgetappend)seqlstscs       r    _as_opcodesr1   1   sC    
C  IMM!=JJqMMMJr"   RETURN_VALUERAISE_VARARGSc                   j    e Zd ZdZdZd Zed             Zed             Zd Z	d Z
ed             Zd	S )
ByteCodeInstz
    Attributes
    ----------
    - offset:
        byte offset of opcode
    - opcode:
        opcode integer value
    - arg:
        instruction arg
    - lineno:
        -1 means unknown
    )offsetnextopcodeopnamearglinenoc                 z    || _         || _        || _        t          j        |         | _        || _        d| _        d S )N)r6   r7   r8   r)   r9   r:   r;   selfr6   r8   r:   
nextoffsets        r    __init__zByteCodeInst.__init__Q   s7    	j(r"   c                     | j         t          v S r(   )r8   JUMP_OPSr?   s    r    is_jumpzByteCodeInst.is_jumpY       {h&&r"   c                     | j         t          v S r(   )r8   TERM_OPSrD   s    r    is_terminatorzByteCodeInst.is_terminator]   rF   r"   c                 *   | j         sJ t          dv r&| j        d dD             v r| j        | j        dz  z
  S nt          dv r)| j        d dD             v r| j        | j        dz
  dz  z
  S nPt          d	v r)| j        d
 dD             v r| j        | j        dz
  dz  z
  S nt          dv rnt          t                    t          dv r=| j        t          v r| j        | j        dz  z   S | j        t          v sJ | j        dz  dz
  S t          t                    )Nr   c              3   :   K   | ]}t           j        |         V  d S r(   r)   r*   .0ks     r    	<genexpr>z/ByteCodeInst.get_jump_target.<locals>.<genexpr>l   sD       H H#$  #y| H H H H H Hr"   )JUMP_BACKWARDJUMP_BACKWARD_NO_INTERRUPTr   r   c              3   :   K   | ]}t           j        |         V  d S r(   rM   rN   s     r    rQ   z/ByteCodeInst.get_jump_target.<locals>.<genexpr>q   s<       ; ;#$  #y| ; ; ; ; ; ;r"   )rR      )r   c              3   :   K   | ]}t           j        |         V  d S r(   rM   rN   s     r    rQ   z/ByteCodeInst.get_jump_target.<locals>.<genexpr>u   sD       L L#$  #y| L L L L L Lr"   )rR   POP_JUMP_BACKWARD_IF_TRUEPOP_JUMP_BACKWARD_IF_FALSEPOP_JUMP_BACKWARD_IF_NONEPOP_JUMP_BACKWARD_IF_NOT_NONEr   )r   r   r   r   )	rE   r
   r8   r7   r:   r6   NotImplementedErrorJREL_OPSJABS_OPSrD   s    r    get_jump_targetzByteCodeInst.get_jump_targeta   s    |
""{ H H)GH H H H H yDHqL11H *$${ ; ;(9; ; ; ; ;{dhla%777; +%%{ L L)KL L L L L {dhla%777L *$$%i000<<<{h&&y48a<//{h....x!|a''%i000r"   c                 0    d| j         | j        | j        fz  S )Nz%s(arg=%s, lineno=%d))r9   r:   r;   rD   s    r    __repr__zByteCodeInst.__repr__   s    &$+tx)MMMr"   c                 X    | j                             d          rdS | j         dk    rdS dS )zREffect of the block stack
        Returns +1 (push), 0 (none) or -1 (pop)
        SETUP_rV   	POP_BLOCKr=   r   )r9   
startswithrD   s    r    block_effectzByteCodeInst.block_effect   s8    
 ;!!(++ 	1[K''21r"   N)__name__
__module____qualname____doc__	__slots__rA   propertyrE   rI   r`   rb   rg    r"   r    r5   r5   B   s          FI   ' ' X' ' ' X''1 '1 '1RN N N 	 	 X	 	 	r"   r5   rV   NOPrK   c              #   &  K   g }t          j        |           D ]\  }}}}|                    |||f            t          |          D ]H\  }\  }}}|dz   t	          |          k     r||dz            d         }nt	          |           }||||fV  Id S )NrV   r   )r)   _unpack_opargsr,   	enumeratelen)codebufistart_offsetopr:   next_offsets          r    rq   rq      s      (+(:4(@(@ 	0 	0$A|RJJb#.////*3C.. 	7 	7&A&b#1us3xx!!a%jm!$iiS+66666	7 	7r"   r   r   r   c              #     K   d}t          |           }dx}}||k     r'| |         }|t          z  }|t          k    r| |         |z  }t          t                    D ]}|| ||z            d|z  z  z  }|t          z  }t
          dv r|t          |         t          z  z  }nt
          dv rnt          t
                    |t          k    r|t          ||fV  |dt          z  z  }|}nLd}|t          z  }t
          dv r|t          |         t          z  z  }nt
          dv rnt          t
                    d}||||fV  |}||k     %dS dS )zp
        Returns a 4-int-tuple of
        (bytecode offset, opcode, argument, offset of next bytecode).
        r      rT   r   N)rs   CODE_LENHAVE_ARGUMENTrangeARG_LENr
   r   	INSTR_LENr]   EXTENDED_ARG
OPCODE_NOP
NO_ARG_LEN)rt   extended_argnr6   rv   rx   r:   js           r    rq   rq      s     
 II!eeaBMA]""1g,w 2 2A4A;1q511CCW
** .r2Y>>AA"444-i888%% ":sA6666#&!g+#5LF & Z
** .r2Y>>AA"444-i888L2sA&&&&Fa !eeeeeer"   c              #      K   dt           dt          fV  | D ]2\  }}}}|t          v r
|t          z  }|t          z   |||t          z   fV  3dS )zpPatch the bytecode stream.

    - Adds a NOP bytecode at the start to avoid jump target being at the entry.
    r   N)r   _FIXED_OFFSETr_   )	bc_streamr6   r8   r:   r@   s        r    _patched_opargsr      sz       j$
....+4 N N'ZX= C}$fc:3MMMMMM	N Nr"   c                   0    e Zd Zd Zd Zd Zd ZeZd ZdS )ByteCodeIterc                     || _         t          t          t          | j         j                                      | _        d S r(   )rt   iterr   rq   co_code)r?   rt   s     r    rA   zByteCodeIter.__init__   s1    		8I)J)JKKLL			r"   c                     | S r(   rn   rD   s    r    __iter__zByteCodeIter.__iter__  s    r"   c                 *    t          | j                  S r(   )r7   r   rD   s    r    _fetch_opcodezByteCodeIter._fetch_opcode  s    DIr"   c                 ^    |                                  \  }}}}|t          ||||          fS )N)r6   r8   r:   r@   )r   r5   r>   s        r    r7   zByteCodeIter.next  sC    *.*<*<*>*>'Z|6&c/9; ; ; ; 	;r"   c                 r    d}t          |          D ]$}t          | j                  \  }}||d|z  z  z  }%|S )Nr   r|   )r   r7   r   )r?   sizeru   rv   _offsetbytes         r    read_argzByteCodeIter.read_arg  sF    t 	# 	#A OOMGT4AE?"CC
r"   N)	rh   ri   rj   rA   r   r   r7   __next__r   rn   r"   r    r   r      si        M M M    ; ; ;
 H    r"   r   c                   f    e Zd ZdZdZd Zed             Zd Zd Z	d Z
d Zed	             Zd
 ZdS )	_ByteCodezF
    The decoded bytecode of a function, and related information.
    )	func_idco_namesco_varnames	co_constsco_cellvarsco_freevarsexception_entriestablelabelsc                    |j         }t          d t          j        |j                  D                       }|                    d           t          t          |                    }|                     ||           || _	        |j
        | _
        |j        | _        |j        | _        |j        | _        |j        | _        || _        t!          |          | _        d S )Nc              3   *   K   | ]}|t           z   V  d S r(   )r   )rO   xs     r    rQ   z%_ByteCode.__init__.<locals>.<genexpr>"  s'      MM1Q&MMMMMMr"   r   )rt   setr)   
findlabelsr   addr   r   _compute_linenor   r   r   r   r   r   r   sortedr   )r?   r   rt   r   r   s        r    rA   z_ByteCode.__init__  s    |MMt|0L0LMMMMM

1 L..//UD)))+++
Vnnr"   c                     t          j        |          D ] \  }}|t          z   }||v r|||         _        !|j        }|                                D ]#}|j        |j        dk    r|j        }||_        $|S )zI
        Compute the line numbers for all bytecode instructions.
        Nr   )r)   findlinestartsr   r;   co_firstlinenovalues)clsr   rt   r6   r;   
adj_offsetknowninsts           r    r   z_ByteCode._compute_lineno3  s    
 "066 	2 	2NFF-/JU""+1j!( #LLNN 	$ 	$D{&4;!+;+;#r"   c                 N    t          | j                                                  S r(   )r   r   r   rD   s    r    r   z_ByteCode.__iter__F  s    DJ%%''(((r"   c                     | j         |         S r(   r   r?   r6   s     r    __getitem__z_ByteCode.__getitem__I  s    z&!!r"   c                     || j         v S r(   r   r   s     r    __contains__z_ByteCode.__contains__L  s    ##r"   c                       fdd                     fd j                                        D                       S )Nc                 4    | d         j         j        v rdS dS )NrV   > )r6   r   )rv   r?   s    r    label_markerz$_ByteCode.dump.<locals>.label_markerP  s     t{dk))ssr"   
c              3   ^   K   | ]'}|d          j         dk    d |          f|z   z  V  (dS )rV   CACHEz
%s %10s	%sN)r9   )rO   rv   r   s     r    rQ   z!_ByteCode.dump.<locals>.<genexpr>V  sU       4 4aDK722 '<<??*<q*@A22224 4r"   )joinr   items)r?   r   s   `@r    dumpz_ByteCode.dumpO  si    	 	 	 	 	 yy 4 4 4 4"&*"2"2"4"44 4 4 4 4 	4r"   c           	      4   i }|j         }|                    dt          j                  }t	          |t
                    r|j        }|                                D ]R}|j        dk    rE|t          |j
                           }	|	|vr'	 ||	         }
n# t          $ r ||	         }
Y nw xY w|
||	<   S|D ]h}t	          |t                    rQt          t          |                    }|                    |                     |||j        |j                             i|S )za
        Compute the globals used by the function with the given
        bytecode table.
        __builtins__LOAD_GLOBAL)__globals__r+   r   builtins
isinstancer   __dict__r   r9   _fix_LOAD_GLOBAL_argr:   KeyErrorr   r   r   update_compute_used_globalsr   r   )r   funcr   r   r   dglobsr   r   namevaluecosubtables                r    r   z_ByteCode._compute_used_globalsZ  s=     99^U^<<h
++ 	)(HLLNN 	$ 	$D{m++ 4TX > >?q==/ %d# / / / (/#AdG 	O 	OB"h'' O&|B'7'78822435<N N O O Os   BB$#B$c                 d    |                      | j        j        | j        | j        | j                  S )zv
        Get a {name: value} map of the globals used by this code
        object and any nested code objects.
        )r   r   r   r   r   r   rD   s    r    get_used_globalsz_ByteCode.get_used_globalsw  s3    
 ))$,*;TZ*..$-I I 	Ir"   N)rh   ri   rj   rk   rl   rA   classmethodr   r   r   r   r   r   r   rn   r"   r    r   r     s         $I% % %(   [$) ) )" " "$ $ $	4 	4 	4   [8I I I I Ir"   r   c                 \    t           dv r| dz	  S t           dv r| S t          t                     )N)r   r   r   rV   r\   )r
   r]   )r:   s    r    r   r     s5    ///ax	j	 	 
!),,,r"   c                   :     e Zd Z fdZed             Zd Z xZS )ByteCodePy311c                     t                                          |           t          j        |j                  j        }t          t          | j        |                    | _        d S r(   )	superrA   r)   Bytecodert   r   tuplemapfixup_eh)r?   r   entries	__class__s      r    rA   zByteCodePy311.__init__  sQ    !!!,w|,,>!&s4=''B'B!C!Cr"   c                     t          j        | j        t          z   | j        t          z   | j        t          z   | j        | j                  }|S )N)startendtargetdepthlasti)r)   r   r   r   r   r   r   r   )entouts     r    r   zByteCodePy311.fixup_eh  sH     &)m+=1H:-)39
 
 

 
r"   c                     g }| j         D ]8}|j        |cxk    r|j        k     rn |                    |j        |f           9|rt          |          d         }|S dS )zN
        Returns the exception entry for the given instruction offset
        rV   N)r   r   r   r,   r   max)r?   r6   
candidatesr   s       r    find_exception_entryz"ByteCodePy311.find_exception_entry  s     
) 	4 	4CyF,,,,SW,,,,,!!39c"2333 	j//!$CJ	 	r"   )rh   ri   rj   rA   staticmethodr   r   __classcell__r   s   @r    r   r     sg        D D D D D
   \
 
 
 
 
 
 
r"   r   c                   :     e Zd Z fdZed             Zd Z xZS )ByteCodePy312c                 ^    t                                          |           d  _         fdt          j        |j                  j        D             }                     |          }|j        r't          d |D                       fd|D             }t          |           _        d S )Nc                 :    g | ]}                     |          S rn   )r   )rO   er?   s     r    
<listcomp>z*ByteCodePy312.__init__.<locals>.<listcomp>  s1       4==##   r"   c                     g | ]	}|j         
S rn   r   )rO   r   s     r    r   z*ByteCodePy312.__init__.<locals>.<listcomp>  s    'B'B'BQ'B'B'Br"   c                 *    g | ]}|j         k    |S rn   r   )rO   r   max_exception_targets     r    r   z*ByteCodePy312.__init__.<locals>.<listcomp>  s&    NNNQQX9M-M-Mq-M-M-Mr"   )r   rA   _ordered_offsetsr)   r   rt   r   remove_build_list_swap_patternis_generatorr   r   )r?   r   r   r  r   s   `  @r    rA   zByteCodePy312.__init__  s    !!! !%   <--?   55g>>  	O#&'B'B''B'B'B#C#C  ONNN'NNNG!&wr"   c                 J    | j         sd | j        D             | _         | j         S )Nc                     g | ]}|S rn   rn   )rO   os     r    r   z1ByteCodePy312.ordered_offsets.<locals>.<listcomp>  s    $;$;$;1Q$;$;$;r"   )r  r   rD   s    r    ordered_offsetszByteCodePy312.ordered_offsets  s.    $ 	<$;$;
$;$;$;D!$$r"   c                 p   dt           dt          fd}t                      }d}|rRd}t                      }|                                D ])}| j                            |j                  }| j        | j        |                  }|j        dvrD| j        | j        |dz                     }	|	j        dk    s|	j	        d	k    rv| j        | j        |d	z                     }	|	j        d
k    r0|
                    |	           | j        | j        |dz                     }	|	j        dk    st          dv r| j                            |j                  }| j        | j        |d	z
                    }|j        dk    s(| j        | j        |dz
                    }	|	j        dk    sP| j        | j        |                  }	|	j        dk    s|	j	        d	k    rnt          dv rx| j                            |j                  }| j        | j        |dz
                    }|j        dk    s| j        | j        |                  }	|	j        dk    s|	j	        d	k    rnt          t                     |||          }d}||z  }+|R|D ];}
t          |
j        t           j        d         d|
j                  | j        |
j        <   <|S )at   Find the following bytecode pattern:

            BUILD_{LIST, MAP, SET}
            SWAP(2)
            FOR_ITER
            ...
            END_FOR
            SWAP(2)

            This pattern indicates that a list/dict/set comprehension has
            been inlined. In this case we can skip the exception blocks
            entirely along with the dead exceptions that it points to.
            A pair of exception that sandwiches these exception will
            also be merged into a single exception.

            Update for Python 3.13, the ending of the pattern has a extra
            POP_TOP:

            ...
            END_FOR
            POP_TOP
            SWAP(2)

            Update for Python 3.13.1, there's now a GET_ITER before FOR_ITER.
            This patch the GET_ITER to NOP to minimize changes downstream
            (e.g. array-comprehension).
        r   entry_to_removec                    |                                dz
  }|                                dz   }|dk    rw|t          |           k     rd| |         }| |         }|j        |j        k    rDt          |j        |j        |j        |j        |j                  | |<   |                     |           |                                fd| D             } | S )NrV   r   c                 4    g | ]}|j         j        k    |S rn   )r   r   )rO   r   r
  s     r    r   zbByteCodePy312.remove_build_list_swap_pattern.<locals>.pop_and_merge_exceptions.<locals>.<listcomp>  s5     A A AQg)??? ???r"   )	indexrs   r   r   r   r   r   r   remove)r   r
  lower_entry_idxupper_entry_idxlower_entryupper_entrys    `    r    pop_and_merge_exceptionszNByteCodePy312.remove_build_list_swap_pattern.<locals>.pop_and_merge_exceptions  s    %mmO<<q@O%mmO<<q@O !###g,,(F(F%o6%o6%);;;/C#)##*#)#)0+ 0+GO, NN;/// NN?+++A A A A' A A AGNr"   TF)
BUILD_LIST	BUILD_MAP	BUILD_SETrV   SWAPr   GET_ITERr   FOR_ITERrK   END_FORPOP_TOPrz   ro   N)listr   r   copyr  r  r   r   r9   r:   r   r
   r   r]   r5   r6   r)   r*   r7   )r?   r   r  change_to_nopwork_remainingcurrent_nop_fixesentryr  	curr_inst	next_instr   s              r    r  z,ByteCodePy312.remove_build_list_swap_pattern  s   8	d 	6J	 	 	 	2  J	3 #N #  E3 E3 ,225;?? !Jt';E'BC	# ,9 9 9  !Jt';EAI'FG	 '611imq6H6H Jt';EAI'FG	
 #z11%)))444 $
4+?	+J KI ':55
** !066uyAAE $
4+?	+J KI$+y88  $
4+?	+J KI$+y88  $
4+?+F GI$+v55)-1:L:L "=== !066uyAAE $
4+?	+J KI$+y88  $
4+?+F GI$+v55)-1:L:L -i888 327EBB!% !22U  J	3Z " 	> 	>D&24;369U3C37379'> '>DJt{## r"   )rh   ri   rj   rA   rm   r  r  r   r   s   @r    r   r     si        0 0 0 0 04 % % X%I I I I I I Ir"   r   r   c                   j    e Zd ZdZ ej        d          Zed             Zd Z	d Z
ed             ZdS )FunctionIdentityz
    A function's identity and metadata.

    Note this typically represents a function whose bytecode is
    being compiled, not necessarily the top-level user function
    (the two might be distinct).
    rV   c                    t          |          }t          |          }t          j        |          }|st	          j        d|z            	 |j        }n# t          $ r
 |j        }Y nw xY w |             }||_	        ||_
        |                    d          d         |_        ||_        t          j        |          |_        |j        t          j        n|j        j        |_        t          j        |          |_        ||_        |j        |_        |j        |_        t5          |j                  |_        t;          |j                  |_        t?          | j                   }d!                    |j
        |          |_"        ||_#        |S )zD
        Create the FunctionIdentity of the given function.
        z %s does not provide its bytecode.r=   Nz{}${})$r!   r&   r   pysignaturer   ByteCodeSupportErrorrj   AttributeErrorrh   r   func_qualnamesplit	func_namert   inspect	getmodulemodule_dynamic_modnamemodnameisgeneratorfunctionr  pysigco_filenamefilenamer   firstlinenors   
parameters	arg_countr  	arg_namesr7   _unique_idsformatunique_name	unique_id)r   pyfuncr   rt   r4  r+  r?   uids           r    from_functionzFunctionIdentity.from_functionk  s   
 #6**t$$!$'' 	;-2T9; ; ;	* -MM 	* 	* 	* MMMM	* suu	*&,,S11"5	'--;. ..![1 	 $7==
(.U-..e.//
 3?##">>$*<cBBs   A A)(A)c                 6    |                      | j                  S )z:Copy the object and increment the unique counter.
        )rA  r   rD   s    r    derivezFunctionIdentity.derive  s     !!$),,,r"   c                 ,    t          | j                  S )4
        NOTE: part of ReduceMixin protocol
        )r?  )dictr   rD   s    r    _reduce_stateszFunctionIdentity._reduce_states  s     49%%%%r"   c                 ,    |                      |          S )rE  )rA  )r   r?  s     r    _rebuildzFunctionIdentity._rebuild  s    
   (((r"   N)rh   ri   rj   rk   	itertoolscountr;  r   rA  rC  rG  rI  rn   r"   r    r%  r%  a  s          ")/!$$K' ' ['R- - -
& & & ) ) [) ) )r"   r%  )4collectionsr   r   r)   r.  rJ  typesr   r   
numba.corer   r   r	   numba.core.utilsr
   r8   r   r   r]   r   r   r   r!   r&   r1   	frozensethasjrelr^   hasjabsr_   rC   rH   r   r~   objectr5   r}   r   r   r9   r  r   rq   r   r   r   r   r   r   ByteCodeReduceMixinr%  rn   r"   r    <module>rV     s   / / / / / / / / 



      & & & & & & & & / / / / / / / / / / & & & & & & """,,,,,,II$$$

i
(
(( j44!z"8"@B B  	 	 	E E E
   9S[!!9S[!!h9[[./!BCCDD!U U U U U6 U U Up 

Ze$$
 
	7 	7 	7 	7 ---8 8 8 8t 
i
(
((N N N    6   4fI fI fI fI fI fI fI fIR- - -    I   >l l l l lM l l l^ HH%%%HHHH

i
(
((D) D) D) D) D)y, D) D) D) D) D)r"   