U
    9%e-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   [/var/www/html/Darija-Ai-API/env/lib/python3.8/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>   s     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)      s     z<TestCallFunctionExPeepHole.test_all_args.<locals>.<listcomp>r   N)r-   THRESHOLD_ARGSr4   r   assertEqualr1   
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)      s     z;TestCallFunctionExPeepHole.test_all_kws.<locals>.<listcomp>r   N)r-   THRESHOLD_KWSr4   r   r6   r7   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)      s     zHTestCallFunctionExPeepHole.test_small_args_small_kws.<locals>.<listcomp>Nr5   r?   r-   r4   r   r6   	r1   	used_argsZused_kwsr8   r9   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
        rA   c                 S   s   g | ]}|qS r   r   r&   r   r   r    r)      s     zHTestCallFunctionExPeepHole.test_small_args_large_kws.<locals>.<listcomp>NrB   rC   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
        rA   c                 S   s   g | ]}|qS r   r   r&   r   r   r    r)      s     zHTestCallFunctionExPeepHole.test_large_args_small_kws.<locals>.<listcomp>NrB   rC   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)      s     zHTestCallFunctionExPeepHole.test_large_args_large_kws.<locals>.<listcomp>NrB   rC   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   rA            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,   \} }}}}}}}}}	}
}}}}}W 5 Q R X | | | | | | | | | |	 |
 | | | | | 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.
            Zint64r<   r=   cder9   ghr(   jklmnop)r   rV   r:   r   r    objmode_func  s    #
	
zGTestCallFunctionExPeepHole.test_large_kws_objmode.<locals>.objmode_funcN)r   sumlistr6   )r1   rd   r<   r=   r   rc   r    test_large_kws_objmode   s    =z1TestCallFunctionExPeepHole.test_large_kws_objmodec              	   C   sB   dd }|  t}t |d W 5 Q R X | 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 NrA   rI   )r   r!   flagr   r   r    inline_func^  sN    
zRTestCallFunctionExPeepHole.test_large_args_inline_controlflow.<locals>.inline_funcF9You can resolve this issue by moving the control flow outNassertRaisesr	   r   ZassertInstr	exceptionr1   rl   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 rh   ri   rk   Za_valr   r   r    rl     sP    zVTestCallFunctionExPeepHole.test_large_args_noninlined_controlflow.<locals>.inline_funcFNr   r6   r1   rl   r:   r;   r<   r=   r   r   r    &test_large_args_noninlined_controlflow  s    *
zATestCallFunctionExPeepHole.test_large_args_noninlined_controlflowc              	   C   sB   dd }|  t}t |d W 5 Q R X | 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 NrA   rI   ri   rj   r   r   r    rl     sL    
zPTestCallFunctionExPeepHole.test_all_args_inline_controlflow.<locals>.inline_funcFrm   Nrn   rr   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 )rt   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 ry   ri   ru   r   r   r    rl     sN    zTTestCallFunctionExPeepHole.test_all_args_noninlined_controlflow.<locals>.inline_funcFNrv   rw   r   r   r    $test_all_args_noninlined_controlflow  s    )
z?TestCallFunctionExPeepHole.test_all_args_noninlined_controlflowc              	   C   sB   dd }|  t}t |d W 5 Q R X | 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 NrA   rI   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ri   rj   r   r   r    rl   8  s$    
zQTestCallFunctionExPeepHole.test_large_kws_inline_controlflow.<locals>.inline_funcFrm   Nrn   rr   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|   ri   ru   r   r   r    rl   Z  s&    zUTestCallFunctionExPeepHole.test_large_kws_noninlined_controlflow.<locals>.inline_funcFNrv   rw   r   r   r    %test_large_kws_noninlined_controlflowS  s    
z@TestCallFunctionExPeepHole.test_large_kws_noninlined_controlflowN)__name__
__module____qualname____doc__r5   r?   r4   r   r>   r@   rE   rF   rG   rH   rg   rs   rx   rz   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 )NrA   r<   ABCDEFGHIJKLMNOPQRSr   r   rX   r   r   r    
const_func  s*    zJTestLargeConstDict.test_large_heterogeneous_const_dict.<locals>.const_funcNrv   )r1   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 )NrA   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>  s      zpTestLargeConstDict.test_large_heterogeneous_LiteralStrKeyDict_literal_values.<locals>.ol_bar.<locals>.<dictcomp>initial_valueFc                 S   s   | S r   r   r   r   r   r    <lambda>      znTestLargeConstDict.test_large_heterogeneous_LiteralStrKeyDict_literal_values.<locals>.ol_bar.<locals>.<lambda>)items
assertTrue
isinstancer   ZLiteralStrKeyDictr6   Zliteral_valuehasattr)rX   r<   expectedr1   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 )NrA   r<   r   r   r   r   r   r    foo  s*    zYTestLargeConstDict.test_large_heterogeneous_LiteralStrKeyDict_literal_values.<locals>.fooNr   r   r1   r   r   r   r   r1   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 )NrA   r   r   r   )r<   rX   r   r   r    const_keys_func  s*    zTTestLargeConstDict.test_large_heterogeneous_const_keys_dict.<locals>.const_keys_funca_stringNrv   )r1   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 kr>dd S  t| tj  | j | dd S )NrA   rM   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   DictTyper6   rX   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 )NrA   rM   r   rK   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 )NTrA   rI   rJ   r<   r=   rW   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   r6   r   r   )checked_beforer1   r   r    ol_check_beforeq  s    zGTestLargeConstDict.test_usercode_update_use_d2.<locals>.ol_check_beforec                    s2    s*d  t| tj  | jd k 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_afterr1   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.
            rA   rI   rJ   r   rK   )rX   rY   )updatelen)Zd1Zd2)r   r   r   r    const_dict_func  s    
zGTestLargeConstDict.test_usercode_update_use_d2.<locals>.const_dict_funcN)r   r   r6   )r1   r   r   r   r:   r;   r<   r=   r   )r   r   r   r   r1   r    test_usercode_update_use_d2a  s    


z.TestLargeConstDict.test_usercode_update_use_d2c              	   C   sD   dd }|  t}t |dd W 5 Q R X | 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 NrA   rI   r   r   r   )r<   rk   rX   r   r   r    rl     s*    
zPTestLargeConstDict.test_large_const_dict_inline_controlflow.<locals>.inline_funcr   Frm   Nrn   rr   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<   rk   valrX   r   r   r    non_inline_func  s,    zWTestLargeConstDict.test_large_const_dict_noninline_controlflow.<locals>.non_inline_funcr   FNrv   )r1   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 kr^d%d& S  t| tj  | j | d'd& S )(NrA   rI   rJ   rK   r*   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   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 )%NrA   rI   rJ   rK   r*   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   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|  S )NrA   rI   )rA   rI   r   r   r   r   r    r   w  s    zNTestListExtendInStarArgNonTupleIterable.test_list_extend_forceobj.<locals>.fooZijo)rA   rI   r(   r\   ra   )r   r:   r6   )r1   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
