a
    
d-v                  0   @   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 )z0
Test bytecode fixes provided in interpreter.py
    N)jitnjitobjmodetypeof	literally)overload)types)UnsupportedError)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arg15Zarg16Zarg17Zarg18Zarg19Zarg20Zarg21Zarg22Zarg23Zarg24Zarg25Zarg26Zarg27Zarg28Zarg29Zarg30Zarg31Zarg32Zarg33Zarg34Zarg35Zarg36Zarg37Zarg38Zarg39Zarg40arg41Zarg42Zarg43Zarg44Zarg45Zarg46r   r   e/var/www/html/stable-diffusion-webui/venv/lib/python3.9/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 ]2}|d dkrDd| d}n
d| }|| q&d|}d	| d
}|d7 }t |D ]}|d||  d7 }qt ||| D ]"}|d||  d||  d7 }q|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>       z7TestCallFunctionExPeepHole.gen_func.<locals>.<listcomp>   r   zpow(argz, 2)r%   z, z	def impl(z):
z    return sum_jit_func(
z        z,
=z    )
r!   impl)rangeappendjoinexecr!   )
selfZn_argsZn_kwsZ
param_listZ	args_listr(   arg_valZtotal_paramsZ	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   s   g | ]}|qS r   r   r&   r   r   r    r)      r*   z<TestCallFunctionExPeepHole.test_all_args.<locals>.<listcomp>r   N)r.   THRESHOLD_ARGSr5   r   assertEqualr2   
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   s   g | ]}|qS r   r   r&   r   r   r    r)      r*   z;TestCallFunctionExPeepHole.test_all_kws.<locals>.<listcomp>r   N)r.   THRESHOLD_KWSr5   r   r7   r8   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   s   g | ]}|qS r   r   r&   r   r   r    r)      r*   zHTestCallFunctionExPeepHole.test_small_args_small_kws.<locals>.<listcomp>Nr6   r@   r.   r5   r   r7   	r2   	used_argsZused_kwsr9   r:   r;   r<   r=   r>   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
        rB   c                 S   s   g | ]}|qS r   r   r&   r   r   r    r)      r*   zHTestCallFunctionExPeepHole.test_small_args_large_kws.<locals>.<listcomp>NrC   rD   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
        rB   c                 S   s   g | ]}|qS r   r   r&   r   r   r    r)      r*   zHTestCallFunctionExPeepHole.test_large_args_small_kws.<locals>.<listcomp>NrC   rD   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   s   g | ]}|qS r   r   r&   r   r   r    r)      r*   zHTestCallFunctionExPeepHole.test_large_args_large_kws.<locals>.<listcomp>NrC   rD   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   rB            r+            	   
                  r   r   r   r   r    r;     s    zBTestCallFunctionExPeepHole.test_large_kws_objmode.<locals>.py_funcc                     s   t ddddddddddddddddd6   \} }}}}}}}}}	}
}}}}}W d   n1 sd0    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.
            int64r=   r>   cder:   ghr(   jklmnopN)r   rX   r;   r   r    objmode_func  s    #	
zGTestCallFunctionExPeepHole.test_large_kws_objmode.<locals>.objmode_funcN)r   sumlistr7   )r2   rf   r=   r>   r   re   r    test_large_kws_objmode   s    =z1TestCallFunctionExPeepHole.test_large_kws_objmodec                 C   sV   dd }|  t}t |d W d   n1 s60    Y  | dt|j dS )ze
        Tests generating large args when one of the inputs
        has inlined controlflow.
        c              '   S   sZ   t ddddddddddddddddddddd| r4dnddddddddddddddddd%S NrB   rJ   )r   r!   flagr   r   r    inline_func^  sN    
zRTestCallFunctionExPeepHole.test_large_args_inline_controlflow.<locals>.inline_funcFN9You can resolve this issue by moving the control flow outassertRaisesr	   r   ZassertInstr	exceptionr2   rn   Zraisesr   r   r    "test_large_args_inline_controlflowX  s    ),z=TestCallFunctionExPeepHole.test_large_args_inline_controlflowc                 C   s6   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 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 rj   rk   rm   Za_valr   r   r    rn     sP    zVTestCallFunctionExPeepHole.test_large_args_noninlined_controlflow.<locals>.inline_funcFNr   r7   r2   rn   r;   r<   r=   r>   r   r   r    &test_large_args_noninlined_controlflow  s    *
zATestCallFunctionExPeepHole.test_large_args_noninlined_controlflowc                 C   sV   dd }|  t}t |d W d   n1 s60    Y  | dt|j dS )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   sV   t ddddddddddddddddddddd| r4dnddddddddddddddd$S NrB   rJ   rk   rl   r   r   r    rn     sL    
zPTestCallFunctionExPeepHole.test_all_args_inline_controlflow.<locals>.inline_funcFNro   rp   rt   r   r   r     test_all_args_inline_controlflow  s    (,z;TestCallFunctionExPeepHole.test_all_args_inline_controlflowc                 C   s6   dd }|}t  |}|d}|d}| || dS )rv   c              %   S   sZ   | rdnd}t 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 r{   rk   rw   r   r   r    rn     sN    zTTestCallFunctionExPeepHole.test_all_args_noninlined_controlflow.<locals>.inline_funcFNrx   ry   r   r   r    $test_all_args_noninlined_controlflow  s    )
z?TestCallFunctionExPeepHole.test_all_args_noninlined_controlflowc                 C   sV   dd }|  t}t |d W d   n1 s60    Y  | dt|j dS )zd
        Tests generating large kws when one of the inputs
        has inlined controlflow.
        c                 S   s0   t ddddddddddddddd| r(dnddS NrB   rJ   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rk   rl   r   r   r    rn   8  s$    
zQTestCallFunctionExPeepHole.test_large_kws_inline_controlflow.<locals>.inline_funcFNro   rp   rt   r   r   r    !test_large_kws_inline_controlflow2  s    ,z<TestCallFunctionExPeepHole.test_large_kws_inline_controlflowc                 C   s6   dd }|}t  |}|d}|d}| || dS )z
        Tests generating large kws when one of the inputs
        has the change suggested in the error message
        for inlined control flow.
        c                 S   s4   | rdnd}t ddddddddddddddd|dS r~   rk   rw   r   r   r    rn   Z  s&    zUTestCallFunctionExPeepHole.test_large_kws_noninlined_controlflow.<locals>.inline_funcFNrx   ry   r   r   r    %test_large_kws_noninlined_controlflowS  s    
z@TestCallFunctionExPeepHole.test_large_kws_noninlined_controlflowN)__name__
__module____qualname____doc__r6   r@   r5   r   r?   rA   rF   rG   rH   rI   ri   ru   rz   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   s4   dddddddddddddddddddd} | d S )NrB   r=   ABCDEFGHIJKLMNOPQRSr   r   rZ   r   r   r    
const_func  s*    zJTestLargeConstDict.test_large_heterogeneous_const_dict.<locals>.const_funcNrx   )r2   r   r;   r<   r=   r>   r   r   r    #test_large_heterogeneous_const_dict  s    
z6TestLargeConstDict.test_large_heterogeneous_const_dictc                    s6   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   s   d S r   r   r   r   r   r    bar  s    zYTestLargeConstDict.test_large_heterogeneous_LiteralStrKeyDict_literal_values.<locals>.barc                    s   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 )NrB   r=   r   c                 S   s   t | rt | S t| S r   )r   Z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   )r'   xyr   r   r    
<dictcomp>  r*   zpTestLargeConstDict.test_large_heterogeneous_LiteralStrKeyDict_literal_values.<locals>.ol_bar.<locals>.<dictcomp>initial_valueFc                 S   s   | S r   r   r   r   r   r    <lambda>  r*   znTestLargeConstDict.test_large_heterogeneous_LiteralStrKeyDict_literal_values.<locals>.ol_bar.<locals>.<lambda>)items
assertTrue
isinstancer   ZLiteralStrKeyDictr7   Zliteral_valuehasattr)rZ   r=   expectedr2   r   r    ol_bar  s4    z\TestLargeConstDict.test_large_heterogeneous_LiteralStrKeyDict_literal_values.<locals>.ol_barc                     s8   dddddddddddddddddddd}  |  d S )NrB   r=   r   r   r   r   r   r    foo  s*    zYTestLargeConstDict.test_large_heterogeneous_LiteralStrKeyDict_literal_values.<locals>.fooNr   r   r2   r   r   r   r   r2   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   s4   dddddddddddddddddd| d}|d S )NrB   r   r   r   )r=   rZ   r   r   r    const_keys_func  s*    zTTestLargeConstDict.test_large_heterogeneous_const_keys_dict.<locals>.const_keys_funca_stringNrx   )r2   r   r;   r<   valuer=   r>   r   r   r    (test_large_heterogeneous_const_keys_dict  s    
z;TestLargeConstDict.test_large_heterogeneous_const_keys_dictc                    s6   dd  t  fdd}t fdd}|  dS )zChecks that the optimization for large dictionaries
        do not incorrectly update initial values due to other
        mutations.
        c                 S   s   d S r   r   r   r   r   r    r   #  s    zDTestLargeConstDict.test_large_dict_mutation_not_carried.<locals>.barc                    sf   dddddddddddddddddddd}| j d u r>dd S  t| tj  | j | dd S )NrB   rN   r   c                 S   s   t | S r   r   r   r   r   r    r   >  r*   zYTestLargeConstDict.test_large_dict_mutation_not_carried.<locals>.ol_bar.<locals>.<lambda>c                 S   s   | S r   r   r   r   r   r    r   A  r*   r   r   r   r   DictTyper7   rZ   r=   r   r   r    r   &  s2    
zGTestLargeConstDict.test_large_dict_mutation_not_carried.<locals>.ol_barc                     s@   dddddddddddddddddddd} d| d<  |  d S )NrB   rN   r   rL   Xr   r   r   r   r    r   C  s,    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   s   d S r   r   r   r   r   r    check_beforeh  s    zDTestLargeConstDict.test_usercode_update_use_d2.<locals>.check_beforec                 S   s   d S r   r   r   r   r   r    check_afterk  s    zCTestLargeConstDict.test_usercode_update_use_d2.<locals>.check_afterFT)Zprefer_literalc                    s<    s4d dddd} t| tj | j| dd S )NTrB   rJ   rK   r=   r>   rY   c                 S   s   d S r   r   r   r   r   r    r     r*   zYTestLargeConstDict.test_usercode_update_use_d2.<locals>.ol_check_before.<locals>.<lambda>)r   r   r   r   r7   r   r   )checked_beforer2   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   s   d S 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_afterr2   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.
            rB   rJ   rK   r   rL   )rZ   r[   )updatelen)d1Zd2)r   r   r   r    const_dict_func  s    
zGTestLargeConstDict.test_usercode_update_use_d2.<locals>.const_dict_funcN)r   r   r7   )r2   r   r   r   r;   r<   r=   r>   r   )r   r   r   r   r2   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 s80    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<   ddddddd|rdnddddddddddd| d}|d S NrB   rJ   r   r   r   )r=   rm   rZ   r   r   r    rn     s*    
zPTestLargeConstDict.test_large_const_dict_inline_controlflow.<locals>.inline_funcr   FNro   rp   rt   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}ddddddd|dddddddddd| d}|d S r   r   )r=   rm   valrZ   r   r   r    non_inline_func  s,    zWTestLargeConstDict.test_large_const_dict_noninline_controlflow.<locals>.non_inline_funcr   FNrx   )r2   r   r;   r<   r   r=   r>   r   r   r    +test_large_const_dict_noninline_controlflow  s    


z>TestLargeConstDict.test_large_const_dict_noninline_controlflowc                    s6   dd  t  fdd}t fdd}|  dS )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   s   d S r   r   r   r   r   r    r     s    z>TestLargeConstDict.test_fuse_twice_literal_values.<locals>.barc              $      s   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 r^d%d& S  t| tj  | j | d'd& S )(NrB   rJ   rK   rL   r+   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   r$                                             r#       !   "   #   #Za1Za2a3Za4Za5Za6Za7Za8Za9Za10Za11Za12Za13Za14Za15Za16Za17Za18Za19Za20Za21Za22Za23Za24Za25Za26Za27Za28Za29Za30Za31Za32Za33Za34Za35c                 S   s   t | S r   r   r   r   r   r    r   4  r*   zSTestLargeConstDict.test_fuse_twice_literal_values.<locals>.ol_bar.<locals>.<lambda>c                 S   s   | S r   r   r   r   r   r    r   7  r*   r   r   r   r   r    r     sR    &
zATestLargeConstDict.test_fuse_twice_literal_values.<locals>.ol_barc               $      sX   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 )%NrB   rJ   rK   rL   r+   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   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   9  sJ    &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   s   | S r   r   r   r   r   r    consumert  s    zSTestListExtendInStarArgNonTupleIterable.test_list_extend_forceobj.<locals>.consumerT)Zforceobjc                    s    ddg| R  S r{   r   r   r   r   r    r   w  s    zNTestListExtendInStarArgNonTupleIterable.test_list_extend_forceobj.<locals>.fooZijo)rB   rJ   r(   r^   rc   )r   r;   r7   )r2   r   gotexpectr   r   r    test_list_extend_forceobjs  s    
zATestListExtendInStarArgNonTupleIterable.test_list_extend_forceobjN)r   r   r   r   r   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   ZunittestZnumbar   r   r   r   r   Znumba.extendingr   Z
numba.corer   Znumba.core.errorsr	   Znumba.tests.supportr
   r   r   r!   r"   r   r   r   mainr   r   r   r    <module>   s                                                  d       u
