
    xKg?                     d   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j#                         Zde_        d Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Z G d dee
      Z  G d de
      Z! G d dee
      Z"e#dk(  r ejH                          yy)    )StringION)types)compile_extraFlags)TestCasetagMemoryLeakMixinTc                     ddl m} |j                  dd        |j                  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   s       `/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/numba/tests/test_looplifting.pycompile_isolatedr      sT    .
mT*
h#!!!!	
      c                 n    t        j                  d      }t        |j                        D ]  }| ||<   	 |S )N   )npemptyrangesizexais      r   lift1r#       s1    
A166]!  Hr   c                     t        j                  d      }t        |j                  d         D ]&  }t        |j                  d         D ]	  }| |||f<    ( |S )N)r      r      )r   r   r   shape)r    r!   r"   js       r   lift2r)   )   sR    
A1771:qwwqz"AAadG #  Hr   c                     t               }t        j                  dt        j                        }d}t	        |j
                  d         D ]  }|||   | z  z  } |S N   dtyper   objectr   arangeint64r   r'   )r    _r!   cr"   s        r   lift3r5   3   sO    A
		!288$A	A1771:	QqTAX Hr   c                     t               }t        j                  dt        j                        }d}d}t	        |j
                  d         D ]  }|||   | z  z  }||z  } ||z   S r+   r/   )r    r3   r!   r4   dr"   s         r   lift4r8   <   sc    A
		!288$A	A	A1771:	QqTAX	Q  q5Lr   c                     t               }t        j                  d      }t        |j                  d         D ]
  }|dkD  s	 |S  |S )Nr%   r      r0   r   r1   r   r'   r    r3   r!   r"   s       r   lift5r=   G   sC    A
		!A1771:q5H	  Hr   c              #      K   t        j                  d      }d t        |j                        D ]  }| ||<   	 t        j                  |       y w)Nr   r   )r   r   r   r   sumr   s      r   	lift_gen1r@   P   sB     
A
G166]!  &&)Os   AAc                  r    t        j                  d       t        d      D ]  } t        d      D ]  }  y  y)Nr&   
   r:   )r   r   r   )r"   r(   s     r   lift_issue2561rC   Y   s1    HHQK2YrA   r   c                 p    t        j                  d      }t        |j                  d         D ]  }|c S  |S Nr%   r   )r   r1   r   r'   r   s      r   reject1rF   `   s2    
		!A1771:  Hr   c              #      K   t               }t        j                  d      }t        |j                  d         D ]	  }||     y wrE   r;   r<   s       r   reject_gen1rH   h   s:     A
		!A1771:d
 s   AAc              #      K   t               }t        j                  d      }t        |j                        D ]#  }||   | z   }t        |      D ]  }|dz  }	 | % y w)Nr   r:   )r0   r   r1   r   r   )r    r3   r!   r"   resr(   s         r   reject_gen2rK   o   sY     A
		!A166]dQhqA (C  	 s   AAc                     t        j                  dt         j                        }t        |j                        D ]*  }t               }t        j                  |dz         |   ||<   , |S )Nr   r-   r&   )r   r   int32r   r   r0   r1   )r    r!   r"   r3   s       r   reject_npm1rN   {   sQ    
"((#A166]HyyQ"! 
 Hr   c                   l    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zy)TestLoopLiftingc           	          ddl m} t        |j                  |j                  ||d t
        i       }| j                  t        |j                        d       |S )Nr   r   )r   flagsr   r&   )	r   r   r   r   r   looplift_flagsassertEquallenlifted)selfr   r   r   cress        r   try_liftzTestLoopLifting.try_lift   sM    2%%%%HN2	
 	T[[)1-r   c                     |j                   d   }|j                  j                         \  }| j                  |j                  j
                         y Nr   )rV   	overloadsvalues
assertTruefndescnative)rW   rX   jitlooploopcress       r   assert_lifted_nativez$TestLoopLifting.assert_lifted_native   s;    ++a.&&--/
../r   c                     | j                  ||      } || } |j                  | }| j                  |       | j                  ||       y)zH
        Check that pyfunc can loop-lift even in nopython mode.
        N)rY   entry_pointrc   assertPreciseEqualrW   r   r   argsrX   expectedgots          r   check_lift_okzTestLoopLifting.check_lift_ok   sM     }}VX.4=d%!!$'#.r   c                     | j                  ||      }t         ||       }t         |j                  |       }| j                  |       | j	                  ||       y)zg
        Check that pyfunc (a generator function) can loop-lift even in
        nopython mode.
        N)rY   listre   rc   rf   rg   s          r   check_lift_generator_okz'TestLoopLifting.check_lift_generator_ok   sX    
 }}VX.&#4##T*+!!$'#.r   c                     t        ||t              }| j                  |j                          || } |j                  | }| j                  ||       y)z4
        Check that pyfunc can't loop-lift.
        rR   N)r   rS   assertFalserV   re   rf   rg   s          r   check_no_liftzTestLoopLifting.check_no_lift   sR      &46%4=d%#.r   c                     t        ||t              }| j                  |j                         t	         ||       }t	         |j
                  |       }| j                  ||       y)zK
        Check that pyfunc (a generator function) can't loop-lift.
        rp   N)r   rS   rq   rV   rm   re   rf   rg   s          r   check_no_lift_generatorz'TestLoopLifting.check_no_lift_generator   s]      &46%&#4##T*+#.r   c                 P    | j                  t        t        j                  fd       y N){   )rk   r#   r   intprW   s    r   
test_lift1zTestLoopLifting.test_lift1       55::-8r   c                 P    | j                  t        t        j                  fd       y rv   )rk   r)   r   rx   ry   s    r   
test_lift2zTestLoopLifting.test_lift2   r{   r   c                 P    | j                  t        t        j                  fd       y rv   )rk   r5   r   rx   ry   s    r   
test_lift3zTestLoopLifting.test_lift3   r{   r   c                 P    | j                  t        t        j                  fd       y rv   )rk   r8   r   rx   ry   s    r   
test_lift4zTestLoopLifting.test_lift4   r{   r   c                 P    | j                  t        t        j                  fd       y rv   )rk   r=   r   rx   ry   s    r   
test_lift5zTestLoopLifting.test_lift5   r{   r   c                 2    | j                  t        dd       y )N )rk   rC   ry   s    r   test_lift_issue2561z#TestLoopLifting.test_lift_issue2561   s    >2r2r   c                 P    | j                  t        t        j                  fd       y rv   )rn   r@   r   rx   ry   s    r   test_lift_gen1zTestLoopLifting.test_lift_gen1   s    $$YvFr   c                 P    | j                  t        t        j                  fd       y rv   )rr   rF   r   rx   ry   s    r   test_reject1zTestLoopLifting.test_reject1   s    7UZZM6:r   c                 P    | j                  t        t        j                  fd       y rv   )rt   rH   r   rx   ry   s    r   test_reject_gen1z TestLoopLifting.test_reject_gen1       $$[5::-Hr   c                 P    | j                  t        t        j                  fd       y rv   )rt   rK   r   rx   ry   s    r   test_reject_gen2z TestLoopLifting.test_reject_gen2   r   r   N)__name__
__module____qualname__rY   rc   rk   rn   rr   rt   rz   r}   r   r   r   r   r   r   r   r   r   r   r   rP   rP      sU    0	/
/
/	/999993G;IIr   rP   c                       e Zd Zd Zd Zy)TestLoopLiftingAnnotatec                   
 ddl m} d 

fd}  |d      |      }t        j                  d      }|j	                         } ||      }t        j
                  j                  ||dz          t               }|j                  |	       |j                         }|j                          | j                  d
|       |j                  j                  dz   }	| j                  dj                  |	      |       | j                  d|       y)zO
        Verify that annotation works as expected with one lifted loop
        r   jitc                       y Nr   r   r   r   barz4TestLoopLiftingAnnotate.test_annotate_1.<locals>.bar       r   c                 d             t        | j                        D ]  }| |xx   dz  cc<    | S )Nr&   r   r   )r    r"   r   s     r   fooz4TestLoopLiftingAnnotate.test_annotate_1.<locals>.foo   s.    E166]!	 # Hr   TforceobjrB   r&   file"The function contains lifted loopsr:   Loop at line {line}linezHas 1 overloadsNnumbar   r   r1   copytestingassert_equalr   inspect_typesgetvaluecloseassertIn__code__co_firstlinenoformat)rW   r   r   cfoor    xcopyrbuf
annotationr   r   s             @r   test_annotate_1z'TestLoopLiftingAnnotate.test_annotate_1   s     			 "sD!#&IIbMG


519-j$\\^
		:JG||**Q.+222=zJ'4r   c                 n   ddl m} d fd}  |d      |      }t        j                  d      }|j	                         } ||      }t        j
                  j                  ||dz   d	z         t               }|j                  |
       |j                         }|j                          | j                  d|       |j                  j                  dz   }	|j                  j                  dz   }
| j                  dj                  |	      |       | j                  dj                  |
      |       y)zP
        Verify that annotation works as expected with two lifted loops
        r   r   c                       y r   r   r   r   r   r   z4TestLoopLiftingAnnotate.test_annotate_2.<locals>.bar  r   r   c                              t        | j                        D ]  }| |xx   dz  cc<    t        | j                        D ]  }| |xx   dz  cc<    | S )Nr&   r:   r   )r    r"   r(   r   s      r   r   z4TestLoopLiftingAnnotate.test_annotate_2.<locals>.foo  sM    E166]!	 # 166]!	 #Hr   Tr   rB   r&   r:   r   r   r      r   r   Nr   )rW   r   r   r   r    r   r   r   r   line1line2r   s              @r   test_annotate_2z'TestLoopLiftingAnnotate.test_annotate_2
  s     			 "sD!#&IIbMG


EAI?3j$\\^
		:JG++a/++a/+222>
K+222>
Kr   N)r   r   r   r   r   r   r   r   r   r      s     5D$Lr   r   c                   Z    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zy)TestLoopLiftingInActionc                     |j                   |j                  d      j                  }| j                  t	        |      |       y r[   )r\   
signaturesrV   rT   rU   )rW   jitted	loopcountrV   s       r   assert_has_liftedz)TestLoopLiftingInAction.assert_has_lifted2  s7    !!&"3"3A"67>>Vi0r   c                    ddl m}m}m}m}  | |||d d        d      d        }dD ]]  }t        j                  dd	      }t        j                  dd	      } |||       |j                  ||       | j                  ||       _ y )
Nr   )r   voidrM   doubleTr   c                     | dk(  r)t        |j                  d         D ]  }||   dz  ||<    y t        |j                  d         D ]  }||   dz   ||<    y )Nr   g       @      ?)r   r'   )ur!   r"   s      r   forloop_with_ifz?TestLoopLiftingInAction.test_issue_734.<locals>.forloop_with_if9  s[    Avqwwqz*AQ4#:AaD + qwwqz*AQ4#:AaD +r   )r   r&   rB   rM   r-   )	r   r   r   rM   r   r   r1   py_funcrf   )	rW   r   r   rM   r   r   r   nb_anp_as	            r   test_issue_734z&TestLoopLiftingInAction.test_issue_7346  s    22	T%#d	3	& 
4	& A99Rw/D99Rw/DAt$##At,##D$/ r   c                     ddl m}  |dd      d        }t        j                  g d      }t        j                  j                  |j                  |       ||             y )Nr   r   zf8[:](f8[:])Tr   c                     t        j                  t        |             }d}t        t        |             D ]  }|dz  }| |   ||<   | |   dk\  s n t        |dz   t        |             D ]  }d||<   	 |S )Nr   r&   rB   )r   zerosrU   r   )r    rJ   indiis       r   testz4TestLoopLiftingInAction.test_issue_812.<locals>.testL  sx    ((3q6"CCCFmqS6CS6R<	 $ C!GSV,B -Jr   )
r   r%   r:   r,   r:   rB   r,   r:   r   )r   r   r   arrayr   r   r   )rW   r   r   r    s       r   test_issue_812z&TestLoopLiftingInAction.test_issue_812I  sO    	^d	+	 
,	 HH67


Qa9r   c                    ddl m} d }t        j                  d      }t	               }  |d      |      } |||      } |||      }| j                  |d   |d          | j                  |d   |d          |j                  |j                  d      j                  d   }|j                  j                         \  }	| j                  |	j                  j                         y )Nr   r   c                 F    d}| D ]  }||z  }	 |j                         }||fS r[   )__hash__)r!   bsehs        r   lift_issue2368z?TestLoopLiftingInAction.test_issue_2368.<locals>.lift_issue2368b  s.    AQ 

Aa4Kr   rB   Tr   r&   )r   r   r   onesr0   rT   r\   r   rV   r]   r^   r_   r`   )
rW   r   r   r!   r   r   ri   rj   ra   rb   s
             r   test_issue_2368z'TestLoopLiftingInAction.test_issue_2368_  s    	 GGBKH#d#N3!!Q'Ql!c!f-!c!f-""6#4#4Q#78??B&&--/
../r   c                    ddl m}  |d      d        }| j                  |j                  d       |d             | j	                  |d       | j                  |j                  d       |d             | j	                  |d       y )	Nr   r   Tr   c                 .    d}t        |       D ]  }|} |S r[   r   nrJ   r"   s      r   r   z?TestLoopLiftingInAction.test_no_iteration_w_redef.<locals>.test}  s    C1X Jr   r&   r   r   r   rT   r   r   rW   r   r   s      r   test_no_iteration_w_redefz1TestLoopLiftingInAction.test_no_iteration_w_redefx  s     		d		 
	 	b)484tq1a$q'2tq1r   c                    ddl m}  |d      d        }| j                  |j                  d       |d             | j	                  |d       | j                  |j                  d       |d             | j	                  |d       y )	Nr   r   Tr   c                 4    d}t        |       D ]  }||z  }	 |S r[   r   r   s      r   r   z7TestLoopLiftingInAction.test_no_iteration.<locals>.test  s#    C1Xq Jr   r   r&   r   r   r   s      r   test_no_iterationz)TestLoopLiftingInAction.test_no_iteration  s}    	d		 
	 	b)484tq1a$q'2tq1r   c                     ddl m}  |d      d        }| j                  |j                  d       |d             | j	                  |d       y )Nr   r   Tr   c                 *    t        |       D ]  }|} S r   r   )r   r"   rJ   s      r   r   z>TestLoopLiftingInAction.test_define_in_loop_body.<locals>.test  s    1X Jr   r&   r   r   r   s      r   test_define_in_loop_bodyz0TestLoopLiftingInAction.test_define_in_loop_body  sM    	d		 
	 	a$q'2tq1r   c                     ddl m}  |d      d        }t        j                  d      }| j	                  |j                  |       ||             y)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   r   Tr   c                     t        |       t        j                  k(  r7| j                  dk(  r"d}d}t	        | j
                  d         D ]  } y t        d}d\  }}|S )Nr&           r   )r   r   )typer   ndarrayndimr   r'   	Exception)argresultr(   r"   s       r   r   z;TestLoopLiftingInAction.test_invalid_argument.<locals>.test  s[    CyBJJ&88q= FA"399Q<0 1 $O1r   rB   N)r   r   r   r1   rT   r   )rW   r   r   r   s       r   test_invalid_argumentz-TestLoopLiftingInAction.test_invalid_argument  sJ     		d		 
	 iimc*DI6r   c                     ddl m}  |d      d        }| j                  |j                          |              | j	                  |d       y )Nr   r   Tr   c                  J    d} d}t        d      D ]  }|dkD  rd} || z  } || fS )Nr,   r   r:   r   r   )r    yr"   s      r   r   zHTestLoopLiftingInAction.test_conditionally_defined_in_loop.<locals>.test  s:    AA1Xq51Q  a4Kr   r&   r   r   r   s      r   "test_conditionally_defined_in_loopz:TestLoopLiftingInAction.test_conditionally_defined_in_loop  sI    	d		 
	 	0tq1r   c                     ddl m} dd l}d }|j                         5  |j	                  d         |d      |      }| j                   |d       |d             d d d        y # 1 sw Y   y xY w)Nr   r   c                 *    | rt        d      D ]  } y y )NrB   r   )r!   r"   s     r   r   zRTestLoopLiftingInAction.test_stack_offset_error_when_has_no_return.<locals>.pyfunc  s    rA # r   errorTr   )r   r   warningscatch_warningssimplefilterrT   )rW   r   r  r   cfuncs        r   *test_stack_offset_error_when_has_no_returnzBTestLoopLiftingInAction.test_stack_offset_error_when_has_no_return  s^    	
 $$&!!'*&C&v.EVD\5;7	 '&&s   ?A&&A/c                 |    ddl m} d fd}  |d      |      }| j                   |d       |d             y)	z
        https://github.com/numba/numba/issues/2179

        Looplifting transformation is using the wrong version of variable `h`.
        r   r   c                     | S r   r   )r    s    r   r   z<TestLoopLiftingInAction.test_variable_scope_bug.<locals>.bar  s    Hr   c                 L    d}t        |       D ]  }||z   }	 | |       z
  }|S )Nr   r   )r    r   kr   s      r   r   z<TestLoopLiftingInAction.test_variable_scope_bug.<locals>.foo  s1    A1XE CF
AHr   Tr   rB   N)r   r   rT   )rW   r   r   r   r   s       @r   test_variable_scope_bugz/TestLoopLiftingInAction.test_variable_scope_bug  s=     			 "sD!#&R$r(+r   c                 T   ddl m} d }  |d      |      }t        j                  d      df}| j	                   ||  ||        | j	                  t        |j                  |j                  d      j                        d       |j                  |j                  d      j                  d   }| j	                  t        |j                        d       t        j                  d      df}| j	                   ||  ||        | j	                  t        |j                        d	       y
)z<
        https://github.com/numba/numba/issues/2481
        r   r   c                 ~    | d d |   }d}t        |j                        D ]  }t                ||d d d   |   z  } |S )Nr&   r   )r   r   r0   )r    r  Ar4   r  s        r   r   z<TestLoopLiftingInAction.test_recompilation_loop.<locals>.foo  sI     #A#AA166]$B$
N # Hr   Tr   rB   r&   r   r:   N)	r   r   r   r1   rT   rU   r\   r   rV   )rW   r   r   r   rh   rV   s         r   test_recompilation_loopz/TestLoopLiftingInAction.test_recompilation_loop  s     		 "sD!#&yy}adT4[1T^^DOOA,>?FFGK 23::1=V../3yy}b dT4[1V../3r   c           	         ddl m}  |d      d        }t        ddddddd	
      } |di |} |j                  di |}| j	                  |d   |d          | j	                  |d   |d          |j
                  |j                  d      j                  \  }| j                  t        |j                        d       y )Nr   r   Tr   c                    t        j                  |      t        j                  |      }}||c|d<   |d<   t        j                  |dz
        D ]  }	t        j                  | ||	   z        |t        j                  | ||	   z        z  z   ||	dz   <   t        j                  |||	   z        |t        j                  |||	   z        z  z   ||	dz   <    t                ||fS )Nr   r&   )r   r   r1   sincosr0   )
r!   r   r4   r7   x0y0r   xsysr"   s
             r   r   zDTestLoopLiftingInAction.test_lift_objectmode_issue_4223.<locals>.foo  s    XXa["((1+BrLBqE2a5YYqs^&&RU+a"&&RU2C.CC1Q3&&RU+a"&&RU2C.CC1Q3 $ Hr6Mr   g333333?g333333?g333333?   )r!   r   r4   r7   r  r  r   r&   r   )r   r   dictr   rf   r\   r   rV   rT   rU   nopython_signatures)rW   r   r   r   rj   ri   rV   s          r   test_lift_objectmode_issue_4223z7TestLoopLiftingInAction.test_lift_objectmode_issue_4223  s    	d		 
	 scSQ1DmFm3;;((A4  Q!5==!23::V778!<r   N)r   r   r   r   r   r   r   r   r   r   r   r  r
  r  r  r  r   r   r   r   r   1  sD    10&:,022&2"27828,*4@=r   r   __main__)%ior   numpyr   
numba.corer   numba.core.compilerr   r   numba.tests.supportr   r   r	   unittestrS   force_pyobjectenable_loopliftr   pyobject_looplift_flagsenable_pyobject_loopliftr   r#   r)   r5   r8   r=   r@   rC   rF   rH   rK   rN   rP   r   r   r   mainr   r   r   <module>r,     s       4 > >   $ !% (--/ 37  0	
_Iox _IDGLh GLT{=ox {=| zHMMO r   