o
    iUv                     @   s  d Z ddlZddlmZmZmZmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZmZmZ e																																															ddd	ZG d
d deeZG dd deeZG dd deeZedkre  dS dS )z0
Test bytecode fixes provided in interpreter.py
    N)jitnjitobjmodetypeof	literally)overload)types)UnsupportedBytecodeError)TestCaseMemoryLeakMixinskip_unless_py10_or_laterc/           /      C   s   | | | | | | | | | |	 |
 | | | | | | | | | | | | | | | | | | | | | |  |! |" |# |$ |% |& |' |( |) |* |+ |, |- |. S N )/arg0arg1arg2arg3arg4arg5arg6arg7arg8arg9arg10arg11arg12arg13arg14arg15arg16arg17arg18arg19arg20arg21arg22arg23arg24arg25arg26arg27arg28arg29arg30arg31arg32arg33arg34arg35arg36arg37arg38arg39arg40arg41arg42arg43arg44arg45arg46r   r   Y/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/numba/tests/test_interpreter.pysum_jit_func   s   3	
 !"#$%&'()*+,-.r?   c                   @   s   e Zd ZdZdZdZdd Zedd Zedd	 Z	ed
d Z
edd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zd S )!TestCallFunctionExPeepHolea  
    gh #7812

    Tests that check a peephole optimization for Function calls
    in Python 3.10. The bytecode changes when
    (n_args / 2) + n_kws > 15, which moves the arguments from
    the stack into a tuple and dictionary.

    This peephole optimization updates the IR to use the original format.
    There are different paths when n_args > 30 and n_args <= 30 and when
    n_kws > 15 and n_kws <= 15.
          c           
      C   s   dd t || D }g }t || D ]}|d dkr"d| d}nd| }|| qd|}d	| d
}|d7 }t |D ]}|d||  d7 }q@t ||| D ]}|d||  d||  d7 }qT|d7 }i }	t|dti|	 |	d S )zw
            Generates a function that calls sum_jit_func
            with the desired number of args and kws.
        c                 S   s   g | ]}d | qS )argr   .0ir   r   r>   
<listcomp>   s    z7TestCallFunctionExPeepHole.gen_func.<locals>.<listcomp>   r   zpow(argz, 2)rC   z, z	def impl(z):
z    return sum_jit_func(
z        z,
=z    )
r?   impl)rangeappendjoinexecr?   )
selfn_argsn_kws
param_list	args_listrF   arg_valtotal_params	func_text
local_varsr   r   r>   gen_func   s$   

 z#TestCallFunctionExPeepHole.gen_funcc                 C   sP   dd t | jD }| | jd}|}t |}|| }|| }| || dS )z
        Tests calling a function when n_args > 30 and
        n_kws = 0. This shouldn't use the peephole, but
        it should still succeed.
        c                 S      g | ]}|qS r   r   rD   r   r   r>   rG          z<TestCallFunctionExPeepHole.test_all_args.<locals>.<listcomp>r   N)rK   THRESHOLD_ARGSrX   r   assertEqualrO   
total_argsfpy_funccfuncabr   r   r>   test_all_args   s   
z(TestCallFunctionExPeepHole.test_all_argsc                 C   sP   dd t | jD }| d| j}|}t |}|| }|| }| || dS )zR
        Tests calling a function when n_kws > 15 and
        n_args = 0.
        c                 S   rY   r   r   rD   r   r   r>   rG      rZ   z;TestCallFunctionExPeepHole.test_all_kws.<locals>.<listcomp>r   N)rK   THRESHOLD_KWSrX   r   r\   r]   r   r   r>   test_all_kws   s   
z'TestCallFunctionExPeepHole.test_all_kwsc           	      C   sd   | j d }| jd }dd t|| D }| ||}|}t |}|| }|| }| || dS )zs
        Tests calling a function when (n_args / 2) + n_kws > 15,
        but n_args <= 30 and n_kws <= 15
           c                 S   rY   r   r   rD   r   r   r>   rG      rZ   zHTestCallFunctionExPeepHole.test_small_args_small_kws.<locals>.<listcomp>Nr[   re   rK   rX   r   r\   	rO   	used_argsused_kwsr^   r_   r`   ra   rb   rc   r   r   r>   test_small_args_small_kws   s   


z4TestCallFunctionExPeepHole.test_small_args_small_kwsc           	      C   s`   | j d }| j}dd t|| D }| ||}|}t |}|| }|| }| || dS )zr
        Tests calling a function when (n_args / 2) + n_kws > 15,
        but n_args <= 30 and n_kws > 15
        rg   c                 S   rY   r   r   rD   r   r   r>   rG      rZ   zHTestCallFunctionExPeepHole.test_small_args_large_kws.<locals>.<listcomp>Nrh   ri   r   r   r>   test_small_args_large_kws   s   

z4TestCallFunctionExPeepHole.test_small_args_large_kwsc           	      C   s`   | j }| jd }dd t|| D }| ||}|}t |}|| }|| }| || dS )zr
        Tests calling a function when (n_args / 2) + n_kws > 15,
        but n_args > 30 and n_kws <= 15
        rg   c                 S   rY   r   r   rD   r   r   r>   rG      rZ   zHTestCallFunctionExPeepHole.test_large_args_small_kws.<locals>.<listcomp>Nrh   ri   r   r   r>   test_large_args_small_kws   s   

z4TestCallFunctionExPeepHole.test_large_args_small_kwsc           	      C   s\   | j }| j}dd t|| D }| ||}|}t |}|| }|| }| || dS )zq
        Tests calling a function when (n_args / 2) + n_kws > 15,
        but n_args > 30 and n_kws > 15
        c                 S   rY   r   r   rD   r   r   r>   rG      rZ   zHTestCallFunctionExPeepHole.test_large_args_large_kws.<locals>.<listcomp>Nrh   ri   r   r   r>   test_large_args_large_kws   s   
z4TestCallFunctionExPeepHole.test_large_args_large_kwsc                    s<   dd  t  fdd}tt  }| }| || dS )zO
        Tests calling an objectmode function with > 15 return values.
        c                   S   s   dS )N)r   rg            rH            	   
                  r   r   r   r   r>   r`        zBTestCallFunctionExPeepHole.test_large_kws_objmode.<locals>.py_funcc                     s   t di ddddddddddddddd	dd
ddddddddddddddd   \} }}}}}}}}}	}
}}}}}W d   n1 sSw   Y  | | | | | | | | | |	 |
 | | | | | S )z
            Wrapper to call py_func from objmode. This tests
            large kws with objmode. If the definition for the
            call is not properly updated this test will fail.
            rb   int64rc   cder_   ghrF   jklmnopNr   )r   )rb   rc   r   r   r   r_   r   r   rF   r   r   r   r   r   r   r   r`   r   r>   objmode_func  s   	
#%	
zGTestCallFunctionExPeepHole.test_large_kws_objmode.<locals>.objmode_funcN)r   sumlistr\   )rO   r   rb   rc   r   r   r>   test_large_kws_objmode   s   =z1TestCallFunctionExPeepHole.test_large_kws_objmodec                 C   V   dd }|  t}t |d W d   n1 sw   Y  | dt|j dS )ze
        Tests generating large args when one of the inputs
        has inlined controlflow.
        c                 S   s   t g ddddddddddddddddddddd| r0dndddddddddddddddR ddiS Nrg   rp   r8   r?   flagr   r   r>   inline_func^  s   	

 !"#$%zRTestCallFunctionExPeepHole.test_large_args_inline_controlflow.<locals>.inline_funcFN9You can resolve this issue by moving the control flow outassertRaisesr	   r   assertInstr	exceptionrO   r   raisesr   r   r>   "test_large_args_inline_controlflowX  s   )z=TestCallFunctionExPeepHole.test_large_args_inline_controlflowc                 C   6   dd }|}t  |}|d}|d}| || dS )
        Tests generating large args when one of the inputs
        has the change suggested in the error message
        for inlined control flow.
        c                 S   s   | rdnd}t g ddddddddddddddddddddd|ddddddddddddddR ddiS r   r   r   a_valr   r   r>   r     s   	
 !"#$%zVTestCallFunctionExPeepHole.test_large_args_noninlined_controlflow.<locals>.inline_funcFNr   r\   rO   r   r`   ra   rb   rc   r   r   r>   &test_large_args_noninlined_controlflow  s   *
zATestCallFunctionExPeepHole.test_large_args_noninlined_controlflowc                 C   r   )z
        Tests generating only large args when one of the inputs
        has inlined controlflow. This requires a special check
        inside peep_hole_call_function_ex_to_call_function_kw
        because it usually only handles varkwargs.
        c                 S   s   t g ddddddddddddddddddddd| r0dndddddddddddddddR  S Nrg   rp   r   r   r   r   r>   r     s   	

 !"#$zPTestCallFunctionExPeepHole.test_all_args_inline_controlflow.<locals>.inline_funcFNr   r   r   r   r   r>    test_all_args_inline_controlflow  s   (z;TestCallFunctionExPeepHole.test_all_args_inline_controlflowc                 C   r   )r   c                 S   s   | rdnd}t g ddddddddddddddddddddd|ddddddddddddddR  S r   r   r   r   r   r>   r     s   	
 !"#$zTTestCallFunctionExPeepHole.test_all_args_noninlined_controlflow.<locals>.inline_funcFNr   r   r   r   r>   $test_all_args_noninlined_controlflow  s   )
z?TestCallFunctionExPeepHole.test_all_args_noninlined_controlflowc                 C   r   )zd
        Tests generating large kws when one of the inputs
        has inlined controlflow.
        c                 S   sv   t di ddddddddddddddd	dd
dddddddddddddd| r7dS dS )Nr   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rp   r   r   r   r   r   r>   r   8  sF   	
zQTestCallFunctionExPeepHole.test_large_kws_inline_controlflow.<locals>.inline_funcFNr   r   r   r   r   r>   !test_large_kws_inline_controlflow2  s   z<TestCallFunctionExPeepHole.test_large_kws_inline_controlflowc                 C   r   )z
        Tests generating large kws when one of the inputs
        has the change suggested in the error message
        for inlined control flow.
        c                 S   sv   | rdnd}t di ddddddddddddd	dd
dddddddddddddddd|S )Nrg   rp   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r>   r   Z  sD   	
zUTestCallFunctionExPeepHole.test_large_kws_noninlined_controlflow.<locals>.inline_funcFNr   r   r   r   r>   %test_large_kws_noninlined_controlflowS  s   
z@TestCallFunctionExPeepHole.test_large_kws_noninlined_controlflowN)__name__
__module____qualname____doc__r[   re   rX   r   rd   rf   rl   rm   rn   ro   r   r   r   r   r   r   r   r   r   r   r>   r@   u   s>    






Z
5
6
6
5
 r@   c                   @   sp   e Zd ZdZedd Zedd Zedd Zedd	 Zed
d Z	edd Z
edd Zedd ZdS )TestLargeConstDicta  
    gh #7894

    Tests that check a peephole optimization for constant
    dictionaries in Python 3.10. The bytecode changes when
    number of elements > 15, which splits the constant dictionary
    into multiple dictionaries that are joined by a DICT_UPDATE
    bytecode instruction.

    This optimization modifies the IR to rejoin dictionaries
    and remove the DICT_UPDATE generated code. This then allows
    code that depends on literal dictionaries or literal keys
    to succeed.
    c                 C   s2   dd }|}t  |}| }| }| || dS )r
        Tests that a function with a large heterogeneous constant
        dictionary remains a constant.
        c                  S   s|   i ddddddddddddddd	dd
dddddddddddddddddddd} | d S )NArg   BCDEFGHIJKLMNOPQrb   RSr   r   r   r   r   r>   
const_func  N   	
zJTestLargeConstDict.test_large_heterogeneous_const_dict.<locals>.const_funcNr   )rO   r   r`   ra   rb   rc   r   r   r>   #test_large_heterogeneous_const_dict  s   
z6TestLargeConstDict.test_large_heterogeneous_const_dictc                    6   dd  t  fdd}t fdd}|  dS )zvCheck the literal values for a LiteralStrKeyDict requiring
        optimizations because it is heterogeneous.
        c                 S      d S r   r   r   r   r   r>   bar  r}   zYTestLargeConstDict.test_large_heterogeneous_LiteralStrKeyDict_literal_values.<locals>.barc                    s   i ddddddddddddddd	dd
dddddddddddddddddddd}dd   fdd|  D }t| tj | j| t| dd dd S )Nr   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rb   r   c                 S   s   t | r
t | S t| S r   )r   maybe_literalliteralr   )zr   r   r>   specific_ty  s   zqTestLargeConstDict.test_large_heterogeneous_LiteralStrKeyDict_literal_values.<locals>.ol_bar.<locals>.specific_tyc                    s    i | ]\}}t | |qS r   )r   r   )rE   xyr   r   r>   
<dictcomp>  s     zpTestLargeConstDict.test_large_heterogeneous_LiteralStrKeyDict_literal_values.<locals>.ol_bar.<locals>.<dictcomp>initial_valueFc                 S      | S r   r   r   r   r   r>   <lambda>      znTestLargeConstDict.test_large_heterogeneous_LiteralStrKeyDict_literal_values.<locals>.ol_bar.<locals>.<lambda>)items
assertTrue
isinstancer   LiteralStrKeyDictr\   literal_valuehasattr)r   rb   expectedrO   r   r>   ol_bar  sX   	
z\TestLargeConstDict.test_large_heterogeneous_LiteralStrKeyDict_literal_values.<locals>.ol_barc                     s   i ddddddddddddddd	dd
dddddddddddddddddddd}  |  d S )Nr   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rb   r   r   r   r   r   r>   foo  sN   	
zYTestLargeConstDict.test_large_heterogeneous_LiteralStrKeyDict_literal_values.<locals>.fooNr   r   rO   r   r   r   r   rO   r>   9test_large_heterogeneous_LiteralStrKeyDict_literal_values  s   
zLTestLargeConstDict.test_large_heterogeneous_LiteralStrKeyDict_literal_valuesc                 C   s:   dd }|}t  |}d}||}||}| || dS )r   c                 S   s|   i ddddddddddddddd	dd
dddddddddddddddddd| d}|d S )Nr   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )rb   r   r   r   r>   const_keys_func  r   zTTestLargeConstDict.test_large_heterogeneous_const_keys_dict.<locals>.const_keys_funca_stringNr   )rO   r   r`   ra   valuerb   rc   r   r   r>   (test_large_heterogeneous_const_keys_dict  s   
z;TestLargeConstDict.test_large_heterogeneous_const_keys_dictc                    r   )zChecks that the optimization for large dictionaries
        do not incorrectly update initial values due to other
        mutations.
        c                 S   r   r   r   r   r   r   r>   r   #  r}   zDTestLargeConstDict.test_large_dict_mutation_not_carried.<locals>.barc                    s   i ddddddddddddddd	dd
dddddddddddddddddddd}| j d u rCdd S  t| tj  | j | dd S )Nr   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rt   r   c                 S      t | S r   r   r   r   r   r>   r   >      zYTestLargeConstDict.test_large_dict_mutation_not_carried.<locals>.ol_bar.<locals>.<lambda>c                 S   r   r   r   r   r   r   r>   r   A  r   r   r   r   r   DictTyper\   r   rb   r   r   r>   r   &  sV   	

zGTestLargeConstDict.test_large_dict_mutation_not_carried.<locals>.ol_barc                     s   i ddddddddddddddd	dd
dddddddddddddddddddd} d| d<  |  d S )Nr   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rt   r   rr   Xr   r   r   r   r>   r   C  sP   	
zDTestLargeConstDict.test_large_dict_mutation_not_carried.<locals>.fooNr   r   r   r   r>   $test_large_dict_mutation_not_carried  s   
z7TestLargeConstDict.test_large_dict_mutation_not_carriedc                    s   dd dd  ddt ddfdd	}t  ddfd
d} fdd}|}t |}| }| }|| dS )zf
        Tests an example using a regular update is
        not modified by the optimization.
        c                 S   r   r   r   r   r   r   r>   check_beforeh  r}   zDTestLargeConstDict.test_usercode_update_use_d2.<locals>.check_beforec                 S   r   r   r   r   r   r   r>   check_afterk  r}   zCTestLargeConstDict.test_usercode_update_use_d2.<locals>.check_afterFT)prefer_literalc                    s<    sd dddd} t| tj | j| dd S )NTrg   rp   rq   rb   rc   r   c                 S   r   r   r   r   r   r   r>   r     r   zYTestLargeConstDict.test_usercode_update_use_d2.<locals>.ol_check_before.<locals>.<lambda>)r   r   r   r   r\   r   r   )checked_beforerO   r   r>   ol_check_beforeq  s   zGTestLargeConstDict.test_usercode_update_use_d2.<locals>.ol_check_beforec                    s2    sd  t| tj  | jd u  dd S )NTc                 S   r   r   r   r   r   r   r>   r     r   zXTestLargeConstDict.test_usercode_update_use_d2.<locals>.ol_check_after.<locals>.<lambda>)r   r   r   r   r   r   )checked_afterrO   r   r>   ol_check_after  s
   zFTestLargeConstDict.test_usercode_update_use_d2.<locals>.ol_check_afterc                     sD   dddd} ddd}|  |  |  |  t| dkr |S | S )z
            Dictionary update between two constant
            dictionaries. This verifies d2 doesn't
            get incorrectly removed.
            rg   rp   rq   r   rr   )r   r   )updatelen)d1d2)r   r   r   r>   const_dict_func  s   
zGTestLargeConstDict.test_usercode_update_use_d2.<locals>.const_dict_funcN)r   r   r\   )rO   r   r   r   r`   ra   rb   rc   r   )r   r   r   r   rO   r>   test_usercode_update_use_d2a  s   


z.TestLargeConstDict.test_usercode_update_use_d2c                 C   sX   dd }|  t}t |dd W d   n1 sw   Y  | dt|j dS )z
        Tests generating a large dictionary when one of
        the inputs requires inline control flow
        has the change suggested in the error message
        for inlined control flow.
        c                 S   s   i ddddddddddddddd	|rdnd
ddddddddddddddddddd| d}|d S )Nr   rg   r   r   r   r   r   r   r   rp   r   r   r   r   r   r   r   r   r   r   r   r   )rb   r   r   r   r   r>   r     sN   	
zPTestLargeConstDict.test_large_const_dict_inline_controlflow.<locals>.inline_funcr   FNr   r   r   r   r   r>   (test_large_const_dict_inline_controlflow  s   z;TestLargeConstDict.test_large_const_dict_inline_controlflowc                 C   s>   dd }|}t  |}d}||d}||d}| || dS )z
        Tests generating large constant dict when one of the
        inputs has the change suggested in the error message
        for inlined control flow.
        c                 S   s   |rdnd}i ddddddddddddd	dd
|ddddddddddddddddddd| d}|d S )Nrg   rp   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )rb   r   valr   r   r   r>   non_inline_func  sP   	
zWTestLargeConstDict.test_large_const_dict_noninline_controlflow.<locals>.non_inline_funcr   FNr   )rO   r  r`   ra   r   rb   rc   r   r   r>   +test_large_const_dict_noninline_controlflow  s   


z>TestLargeConstDict.test_large_const_dict_noninline_controlflowc                    r   )z
        Tests that the correct literal values are generated
        for a dictionary that produces two DICT_UPDATE
        bytecode entries for the same dictionary.
        c                 S   r   r   r   r   r   r   r>   r     r}   z>TestLargeConstDict.test_fuse_twice_literal_values.<locals>.barc                    s  i ddddddddd	d
dddddddddddddddddddddd d!d"i d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFi}| j d u rwdGdH S  t| tj  | j | dIdH S )JNa1rg   a2rp   a3rq   a4rr   a5rH   a6rs   a7rt   a8ru   a9rv   a10rw   a11rx   a12ry   a13rz   a14r{   a15r|   a16rB   a17   a18   a19   a20   a21   a22   a23   a24   a25   a26   a27   a28   a29   a30   a31rA   a32    a33!   a34"   a35#   c                 S   r   r   r   r   r   r   r>   r   4  r   zSTestLargeConstDict.test_fuse_twice_literal_values.<locals>.ol_bar.<locals>.<lambda>c                 S   r   r   r   r   r   r   r>   r   7  r   r   r   r   r   r>   r     s   	
 !"$
&zATestLargeConstDict.test_fuse_twice_literal_values.<locals>.ol_barc                     s   i ddddddddd	d
dddddddddddddddddddddd d!d"i d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFi}  |  d S )GNr  rg   r  rp   r  rq   r	  rr   r
  rH   r  rs   r  rt   r  ru   r  rv   r  rw   r  rx   r  ry   r  rz   r  r{   r  r|   r  rB   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/  r0  r1  r2  rA   r3  r4  r5  r6  r7  r8  r9  r:  r   r   r   r   r>   r   9  s   	
 !"$&z>TestLargeConstDict.test_fuse_twice_literal_values.<locals>.fooNr   r   r   r   r>   test_fuse_twice_literal_values  s   -
+z1TestLargeConstDict.test_fuse_twice_literal_valuesN)r   r   r   r   r   r   r   r   r   r  r  r  r;  r   r   r   r>   r   v  s$    
&
F
(
C
K
)
)r   c                   @   s   e Zd ZdZdd ZdS )'TestListExtendInStarArgNonTupleIterablea^  Test `fn(pos_arg0, pos_arg1, *args)` where args is a non-tuple iterable.

    Python 3.9+ will generate LIST_EXTEND bytecode to combine the positional
    arguments with the `*args`.

    See #8059

    NOTE: At the moment, there are no meaningful tests for NoPython because the
    lack of support for `tuple(iterable)` for most iterable types.
    c                    sL   dd  t dd fdd}|d}|d}| |d | || d S )	Nc                  W   r   r   r   r   r   r   r>   consumert  r}   zSTestListExtendInStarArgNonTupleIterable.test_list_extend_forceobj.<locals>.consumerT)forceobjc                    s    ddg| R  S r   r   r   r=  r   r>   r   w  s   zNTestListExtendInStarArgNonTupleIterable.test_list_extend_forceobj.<locals>.fooijo)rg   rp   rF   r   r   )r   r`   r\   )rO   r   gotexpectr   r?  r>   test_list_extend_forceobjs  s   
zATestListExtendInStarArgNonTupleIterable.test_list_extend_forceobjN)r   r   r   r   rC  r   r   r   r>   r<  h  s    
r<  __main__)/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   r   r   r   )r   unittestnumbar   r   r   r   r   numba.extendingr   
numba.corer   numba.core.errorsr	   numba.tests.supportr
   r   r   r?   r@   r   r<  r   mainr   r   r   r>   <module>   s    d       u