o
    iF                     @   s  d dl mZ d dlZd dlmZ d dlmZmZ d dl	m
Z
mZmZ d dlZe Zde_de_e Zde_dd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZG dd  d ee
Z G d!d" d"e
Z!G d#d$ d$ee
Z"e#d%kre$  dS dS )&    )StringION)types)compile_extraFlags)TestCasetagMemoryLeakMixinTc                 K   s>   ddl m} |dd  |di  t|j|j| |fi |S )Nr   
cpu_targetreturn_typelocals)numba.core.registryr
   
setdefaultr   typing_contexttarget_context)pyfuncargtypeskwargsr
    r   k/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/numba/tests/test_looplifting.pycompile_isolated   s   r   c                 C   s&   t d}t|jD ]}| ||< q
|S )N   )npemptyrangesizexair   r   r   lift1    s   

r    c                 C   sB   t d}t|jd D ]}t|jd D ]}| |||f< qq|S )N)r      r      )r   r   r   shape)r   r   r   jr   r   r   lift2)   s   
r%   c                 C   sB   t  }tjdtjd}d}t|jd D ]
}||| |  7 }q|S N   Zdtyper   objectr   arangeZint64r   r#   )r   _r   cr   r   r   r   lift33   s   r.   c                 C   sR   t  }tjdtjd}d}d}t|jd D ]}||| |  7 }||7 }q|| S r&   r)   )r   r,   r   r-   dr   r   r   r   lift4<   s   
r0   c                 C   s6   t  }td}t|jd D ]	}|dkr |S q|S )Nr!   r      r*   r   r+   r   r#   r   r,   r   r   r   r   r   lift5G   s   
r4   c                 c   s:    t d}dV  t|jD ]}| ||< qt |V  d S )Nr   r   )r   r   r   r   sumr   r   r   r   	lift_gen1P   s   

r6   c                  C   s0   t d tdD ]} tdD ]}  dS q	dS )Nr"   
   r1   )r   r   r   )r   r$   r   r   r   lift_issue2561Y   s   
r8   c                 C   s(   t d}t|jd D ]}|  S |S Nr!   r   )r   r+   r   r#   r   r   r   r   reject1`   s   
r:   c                 c   s4    t  }td}t|jd D ]}|| V  qd S r9   r2   r3   r   r   r   reject_gen1h   s   
r;   c                 c   sN    t  }td}t|jD ]}|| |  }t|D ]}|d }q|V  qd S )Nr   r1   )r*   r   r+   r   r   )r   r,   r   r   resr$   r   r   r   reject_gen2o   s   

r=   c                 C   s@   t jdt jd}t|jD ]}t }t |d | ||< q|S )Nr   r(   r"   )r   r   int32r   r   r*   r+   )r   r   r   r,   r   r   r   reject_npm1{   s
   r?   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%S )&TestLoopLiftingc              	   C   s<   ddl m} t|j|j||d ti d}| t|jd |S )Nr   r	   )r   flagsr   r"   )	r   r
   r   r   r   looplift_flagsassertEquallenlifted)selfr   r   r
   cresr   r   r   try_lift   s   zTestLoopLifting.try_liftc                 C   s(   |j d }|j \}| |jj d S Nr   )rE   	overloadsvalues
assertTruefndescnative)rF   rG   jitlooploopcresr   r   r   assert_lifted_native   s   
z$TestLoopLifting.assert_lifted_nativec                 C   s8   |  ||}|| }|j| }| | | || dS )zH
        Check that pyfunc can loop-lift even in nopython mode.
        N)rH   entry_pointrQ   assertPreciseEqualrF   r   r   argsrG   expectedgotr   r   r   check_lift_ok   s
   

zTestLoopLifting.check_lift_okc                 C   s@   |  ||}t|| }t|j| }| | | || dS )zg
        Check that pyfunc (a generator function) can loop-lift even in
        nopython mode.
        N)rH   listrR   rQ   rS   rT   r   r   r   check_lift_generator_ok   s
   
z'TestLoopLifting.check_lift_generator_okc                 C   s<   t ||td}| |j || }|j| }| || dS )z4
        Check that pyfunc can't loop-lift.
        rA   N)r   rB   assertFalserE   rR   rS   rT   r   r   r   check_no_lift   s   
zTestLoopLifting.check_no_liftc                 C   sD   t ||td}| |j t|| }t|j| }| || dS )zK
        Check that pyfunc (a generator function) can't loop-lift.
        r[   N)r   rB   r\   rE   rY   rR   rS   rT   r   r   r   check_no_lift_generator   s   z'TestLoopLifting.check_no_lift_generatorc                 C   s   t ||td}| |j |   |j|  W d   n1 s!w   Y  t ||td}| |j || }|j| }| || dS )z
        Check that pyfunc will fail loop-lifting if pyobject mode
        is disabled inside the loop, succeed otherwise.
        r[   N)r   rB   rL   rE   ZassertTypingErrorrR   pyobject_looplift_flagsrS   rT   r   r   r   check_no_lift_nopython   s   

z&TestLoopLifting.check_no_lift_nopythonc                 C      |  ttjfd d S N){   )rX   r    r   intprF   r   r   r   
test_lift1      zTestLoopLifting.test_lift1c                 C   ra   rb   )rX   r%   r   rd   re   r   r   r   
test_lift2   rg   zTestLoopLifting.test_lift2c                 C   ra   rb   )rX   r.   r   rd   re   r   r   r   
test_lift3   rg   zTestLoopLifting.test_lift3c                 C   ra   rb   )rX   r0   r   rd   re   r   r   r   
test_lift4   rg   zTestLoopLifting.test_lift4c                 C   ra   rb   )rX   r4   r   rd   re   r   r   r   
test_lift5   rg   zTestLoopLifting.test_lift5c                 C   s   |  tdd d S )Nr   )rX   r8   re   r   r   r   test_lift_issue2561   s   z#TestLoopLifting.test_lift_issue2561c                 C   ra   rb   )rZ   r6   r   rd   re   r   r   r   test_lift_gen1   rg   zTestLoopLifting.test_lift_gen1c                 C   ra   rb   )r]   r:   r   rd   re   r   r   r   test_reject1   rg   zTestLoopLifting.test_reject1c                 C   ra   rb   )r^   r;   r   rd   re   r   r   r   test_reject_gen1   rg   z TestLoopLifting.test_reject_gen1c                 C   ra   rb   )r^   r=   r   rd   re   r   r   r   test_reject_gen2   rg   z TestLoopLifting.test_reject_gen2c                 C   ra   rb   )r`   r?   r   rd   re   r   r   r   test_reject_npm1   rg   z TestLoopLifting.test_reject_npm1N)__name__
__module____qualname__rH   rQ   rX   rZ   r]   r^   r`   rf   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   r   r   r   r   r@      s&    r@   c                   @   s   e Zd Zdd Zdd ZdS )TestLoopLiftingAnnotatec           
         s   ddl m} dd   fdd}||}td}| }||}tj||d  t }|j|d	 |	 }|
  | d
| |jjd }	| dj|	d| | d| dS )zO
        Verify that annotation works as expected with one lifted loop
        r   jitc                   S      d S Nr   r   r   r   r   bar     z4TestLoopLiftingAnnotate.test_annotate_1.<locals>.barc                    s*      t | jD ]
}| |  d7  < q| S )Nr"   r   r   )r   r   rz   r   r   foo  s   z4TestLoopLiftingAnnotate.test_annotate_1.<locals>.foor7   r"   file"The function contains lifted loopsr1   Loop at line {line}linezHas 1 overloadsNnumbarw   r   r+   copytestingassert_equalr   Zinspect_typesgetvaluecloseZassertIn__code__co_firstlinenoformat)
rF   rw   r~   cfoor   xcopyrbuf
annotationr   r   r}   r   test_annotate_1   s    
z'TestLoopLiftingAnnotate.test_annotate_1c                    s   ddl m} dd   fdd}||}td}| }||}tj||d d	  t }|j|d
 |	 }|
  | d| |jjd }	|jjd }
| dj|	d| | dj|
d| dS )zP
        Verify that annotation works as expected with two lifted loops
        r   rv   c                   S   rx   ry   r   r   r   r   r   rz   %  r{   z4TestLoopLiftingAnnotate.test_annotate_2.<locals>.barc                    sJ      t | jD ]
}| |  d7  < qt | jD ]
}| |  d9  < q| S )Nr"   r1   r|   )r   r   r$   r}   r   r   r~   (  s   z4TestLoopLiftingAnnotate.test_annotate_2.<locals>.foor7   r"   r1   r   r   r      r   r   Nr   )rF   rw   r~   r   r   r   r   r   r   line1line2r   r}   r   test_annotate_2  s"   

z'TestLoopLiftingAnnotate.test_annotate_2N)rr   rs   rt   r   r   r   r   r   r   ru      s    "ru   c                   @   s|   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )TestLoopLiftingInActionc                 C   s&   |j |jd  j}| t|| d S rI   )rJ   
signaturesrE   rC   rD   )rF   jitted	loopcountrE   r   r   r   assert_has_liftedF  s   z)TestLoopLiftingInAction.assert_has_liftedc           	      C   s   ddl m}m}m}m} ||||d d  dddd }dD ]!}tjdd	d
}tjdd	d
}||| ||| | || qd S )Nr   )rw   voidr>   doubleTZforceobjc                 S   sX   | dkrt |jd D ]
}|| d ||< qd S t |jd D ]
}|| d ||< qd S )Nr   g       @      ?)r   r#   )ur   r   r   r   r   forloop_with_ifM  s   z?TestLoopLiftingInAction.test_issue_734.<locals>.forloop_with_if)r   r"   r7   r>   r(   )	r   rw   r   r>   r   r   r+   py_funcrS   )	rF   rw   r   r>   r   r   r   Znb_aZnp_ar   r   r   test_issue_734J  s   

z&TestLoopLiftingInAction.test_issue_734c                 C   sJ   ddl m} |ddddd }tg d}tj|||| d S )	Nr   rv   zf8[:](f8[:])Tr   c                 S   sl   t t| }d}tt| D ]}|d7 }| | ||< | | dkr# nqt|d t| D ]}d||< q-|S )Nr   r"   r7   )r   zerosrD   r   )r   r<   indiir   r   r   test`  s   
z4TestLoopLiftingInAction.test_issue_812.<locals>.test)
r   r!   r1   r'   r1   r7   r'   r1   r   )r   rw   r   arrayr   r   r   )rF   rw   r   r   r   r   r   test_issue_812]  s
   

z&TestLoopLiftingInAction.test_issue_812c           
      C   s   ddl m} dd }td}t }||}|||}|||}| |d |d  | |d |d  |j|jd  jd }|j	 \}	| 
|	jj d S )Nr   rv   c                 S   s&   d}| D ]}||7 }q|  }||fS rI   )__hash__)r   bsehr   r   r   lift_issue2368v  s
   
z?TestLoopLiftingInAction.test_issue_2368.<locals>.lift_issue2368r7   r"   )r   rw   r   Zonesr*   rC   rJ   r   rE   rK   rL   rM   rN   )
rF   rw   r   r   r   r   rV   rW   rO   rP   r   r   r   test_issue_2368s  s   


z'TestLoopLiftingInAction.test_issue_2368c                 C   j   ddl m} |dddd }| |d|d | j|dd	 | |d|d | j|dd	 d S )
Nr   rv   Tr   c                 S   s   d}t | D ]}|}q|S rI   r   nr<   r   r   r   r   r     s   z?TestLoopLiftingInAction.test_no_iteration_w_redef.<locals>.testr"   r   r   rw   rC   r   r   rF   rw   r   r   r   r   test_no_iteration_w_redef  s   
z1TestLoopLiftingInAction.test_no_iteration_w_redefc                 C   r   )
Nr   rv   Tr   c                 S   s   d}t | D ]}||7 }q|S rI   r   r   r   r   r   r     s   
z7TestLoopLiftingInAction.test_no_iteration.<locals>.testr   r"   r   r   r   r   r   r   test_no_iteration  s   
z)TestLoopLiftingInAction.test_no_iterationc                 C   sF   ddl m} |dddd }| |d|d | j|dd d S )	Nr   rv   Tr   c                 S   s   t | D ]}|}q|S ry   r   )r   r   r<   r   r   r   r     s   z>TestLoopLiftingInAction.test_define_in_loop_body.<locals>.testr"   r   r   r   r   r   r   test_define_in_loop_body  s
   
z0TestLoopLiftingInAction.test_define_in_loop_bodyc                 C   sB   ddl m} |dddd }td}| |||| dS )	zTest a problem caused by invalid discovery of loop argument
        when a variable is used afterwards but not before.

        Before the fix, this will result in::

        numba.ir.NotDefinedError: 'i' is not defined
        r   rv   Tr   c                 S   sL   t | tjkr| jdkrd}d}t| jd D ]}qd S td}d\}}|S )Nr"           r   )r   r   )typer   Zndarrayndimr   r#   	Exception)argresultr$   r   r   r   r   r     s   
z;TestLoopLiftingInAction.test_invalid_argument.<locals>.testr7   N)r   rw   r   r+   rC   r   )rF   rw   r   r   r   r   r   test_invalid_argument  s
   

z-TestLoopLiftingInAction.test_invalid_argumentc                 C   sB   ddl m} |dddd }| | |  | j|dd d S )	Nr   rv   Tr   c                  S   s2   d} d}t dD ]}|dkrd} || 7 }q|| fS )Nr'   r   r1   r   r   )r   yr   r   r   r   r     s   
zHTestLoopLiftingInAction.test_conditionally_defined_in_loop.<locals>.testr"   r   r   r   r   r   r   "test_conditionally_defined_in_loop  s
   
	z:TestLoopLiftingInAction.test_conditionally_defined_in_loopc                 C   sv   ddl m} dd l}dd }|  |d |dd|}| |d|d W d    d S 1 s4w   Y  d S )Nr   rv   c                 S   s   | r	t dD ]}qd S d S )Nr7   r   )r   r   r   r   r   r     s
   zRTestLoopLiftingInAction.test_stack_offset_error_when_has_no_return.<locals>.pyfuncerrorTr   )r   rw   warningscatch_warningssimplefilterrC   )rF   rw   r   r   Zcfuncr   r   r   *test_stack_offset_error_when_has_no_return  s   

"zBTestLoopLiftingInAction.test_stack_offset_error_when_has_no_returnc                    s@   ddl m} dd   fdd}||}| |d|d dS )	z
        https://github.com/numba/numba/issues/2179

        Looplifting transformation is using the wrong version of variable `h`.
        r   rv   c                 S   s   | S ry   r   )r   r   r   r   rz      r{   z<TestLoopLiftingInAction.test_variable_scope_bug.<locals>.barc                    s*   d}t | D ]}|| }q| |  }|S )Nr   r   )r   r   kr}   r   r   r~     s
   
z<TestLoopLiftingInAction.test_variable_scope_bug.<locals>.foor7   N)r   rw   rC   )rF   rw   r~   r   r   r}   r   test_variable_scope_bug  s
   z/TestLoopLiftingInAction.test_variable_scope_bugc                 C   s   ddl m} dd }||}tddf}| || ||  | t|j|jd  jd |j|jd  jd }| t|jd tddf}| || ||  | t|jd d	S )
z<
        https://github.com/numba/numba/issues/2481
        r   rv   c                 S   sB   | d d | }d}t |jD ]}t  ||d d d |  }q|S )Nr"   r   )r   r   r*   )r   r   Ar-   r   r   r   r   r~     s   z<TestLoopLiftingInAction.test_recompilation_loop.<locals>.foor7   r"   r   r1   N)	r   rw   r   r+   rC   rD   rJ   r   rE   )rF   rw   r~   r   rU   rE   r   r   r   test_recompilation_loop  s   
z/TestLoopLiftingInAction.test_recompilation_loopc                 C   sx   dd }ddl m} | |}|d | |j|jd  jd |dd|}|d | t|j|jd  jd d S )	Nc                 S   s   dd dD  dD ]}q	| S )Nc                 S   s   g | ]}|qS r   r   ).0r   r   r   r   
<listcomp>/  s    zRTestLoopLiftingInAction.test_lift_listcomp_block0.<locals>.foo.<locals>.<listcomp>)r"   r   )Xr   r   r   r   r~   .  s   z>TestLoopLiftingInAction.test_lift_listcomp_block0.<locals>.foor   rv   r"   r   Tr   )r   rw   rC   rJ   r   rE   rD   )rF   r~   rw   fr   r   r   test_lift_listcomp_block0,  s   
"z1TestLoopLiftingInAction.test_lift_listcomp_block0c              	   C   s   ddl m} |dd }tdddddddd	}|di |}|jdi |}| |d |d  | |d
 |d
  |j|jd  j\}| t	|j
d
 d S )Nr   rv   c           
      S   s   t |t |}}|||d< |d< t |d D ]2}	t | ||	  |t | ||	    ||	d < t |||	  |t |||	    ||	d < qt  ||fS )Nr   r"   )r   r   r+   sincosr*   )
r   r   r-   r/   x0y0r   ZxsZysr   r   r   r   r~   H  s   02zDTestLoopLiftingInAction.test_lift_objectmode_issue_4223.<locals>.foog333333?g333333?g333333?   )r   r   r-   r/   r   r   r   r"   r   )r   rw   dictr   rS   rJ   r   rE   rC   rD   Znopython_signatures)rF   rw   r~   r   rW   rV   rE   r   r   r   test_lift_objectmode_issue_4223E  s   
	z7TestLoopLiftingInAction.test_lift_objectmode_issue_4223N)rr   rs   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   E  s    r   __main__)%ior   numpyr   Z
numba.corer   Znumba.core.compilerr   r   Znumba.tests.supportr   r   r   ZunittestrB   Zenable_pyobjectZenable_loopliftr   r_   Zenable_pyobject_loopliftr   r    r%   r.   r0   r4   r6   r8   r:   r;   r=   r?   r@   ru   r   rr   mainr   r   r   r   <module>   s>    	
			
vJ  