
    sh?                     f   S SK Jr  S SKrS SKJr  S SKJrJr  S SK	J
r
JrJr  S SKr\" 5       rS\l        S\l        \R#                  5       rS\l        S rS rS	 rS
 rS rS rS rS rS rS rS rS r " S S\\
5      r  " S S\
5      r! " S S\\
5      r"\#S:X  a  \RH                  " 5         gg)    )StringION)types)compile_extraFlags)TestCasetagMemoryLeakMixinTc                     SSK Jn  UR                  SS 5        UR                  S0 5        [        UR                  UR
                  U U40 UD6$ )Nr   
cpu_targetreturn_typelocals)numba.core.registryr   
setdefaultr   typing_contexttarget_context)pyfuncargtypeskwargsr   s       p/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/numba/tests/test_looplifting.pycompile_isolatedr      sT    .
mT*
h#!!!!	
      c                 r    [         R                  " S5      n[        UR                  5       H  nXU'   M	     U$ )N   )npemptyrangesizexais      r   lift1r#       s/    
A166]!  Hr   c                     [         R                  " S5      n[        UR                  S   5       H'  n[        UR                  S   5       H  nXX#4'   M
     M)     U$ )N)r      r      )r   r   r   shape)r    r!   r"   js       r   lift2r)   )   sN    
A1771:qwwqz"AadG #  Hr   c                     [        5       n[        R                  " S[        R                  S9nSn[	        UR
                  S   5       H  nX2U   U -  -  nM     U$ N   dtyper   objectr   arangeint64r   r'   )r    _r!   cr"   s        r   lift3r5   3   sM    A
		!288$A	A1771:	qTAX Hr   c                     [        5       n[        R                  " S[        R                  S9nSnSn[	        UR
                  S   5       H  nX2U   U -  -  nXC-  nM     X4-   $ r+   r/   )r    r3   r!   r4   dr"   s         r   lift4r8   <   s]    A
		!288$A	A	A1771:	qTAX	  5Lr   c                     [        5       n[        R                  " S5      n[        UR                  S   5       H  nUS:  d  M    U$    U$ )Nr%   r      r0   r   r1   r   r'   r    r3   r!   r"   s       r   lift5r=   G   sC    A
		!A1771:q5H	  Hr   c              #      #    [         R                  " S5      nSv   [        UR                  5       H  nXU'   M	     [         R                  " U5      v   g 7f)Nr   r   )r   r   r   r   sumr   s      r   	lift_gen1r@   P   s?     
A
G166]!  &&)Os   AAc                  z    [         R                  " S5        [        S5       H  n [        S5       H  n    g   M     g)Nr&   
   r:   )r   r   r   )r"   r(   s     r   lift_issue2561rC   Y   s1    HHQK2YrA   r   c                 t    [         R                  " S5      n[        UR                  S   5       H  nUs  $    U$ Nr%   r   )r   r1   r   r'   r   s      r   reject1rF   `   s2    
		!A1771:  Hr   c              #      #    [        5       n[        R                  " S5      n[        UR                  S   5       H	  nX#   v   M     g 7frE   r;   r<   s       r   reject_gen1rH   h   s7     A
		!A1771:d
 s   AA	c              #      #    [        5       n[        R                  " S5      n[        UR                  5       H%  nX#   U -   n[        U5       H  nUS-  nM
     Uv   M'     g 7f)Nr   r:   )r0   r   r1   r   r   )r    r3   r!   r"   resr(   s         r   reject_gen2rK   o   sV     A
		!A166]dQhqA (C  	 s   A A"c                     [         R                  " S[         R                  S9n[        UR                  5       H+  n[        5       n[         R                  " US-   5      U   X'   M-     U$ )Nr   r-   r&   )r   r   int32r   r   r0   r1   )r    r!   r"   r3   s       r   reject_npm1rN   {   sO    
"((#A166]HyyQ" 
 Hr   c                   t    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rSrg)TestLoopLifting   c           
          SSK Jn  [        UR                  UR                  XS [
        0 S9nU R                  [        UR                  5      S5        U$ )Nr   r   )r   flagsr   r&   )	r   r   r   r   r   looplift_flagsassertEquallenlifted)selfr   r   r   cress        r   try_liftTestLoopLifting.try_lift   sK    2%%%%N2	
 	T[[)1-r   c                     UR                   S   nUR                  R                  5       u  nU R                  UR                  R
                  5        g Nr   )rW   	overloadsvalues
assertTruefndescnative)rX   rY   jitlooploopcress       r   assert_lifted_native$TestLoopLifting.assert_lifted_native   s;    ++a.&&--/
../r   c                     U R                  X5      nU" U6 nUR                  " U6 nU R                  U5        U R                  XV5        g)z8
Check that pyfunc can loop-lift even in nopython mode.
N)rZ   entry_pointre   assertPreciseEqualrX   r   r   argsrY   expectedgots          r   check_lift_okTestLoopLifting.check_lift_ok   sG     }}V.4=%!!$'.r   c                     U R                  X5      n[        U" U6 5      n[        UR                  " U6 5      nU R                  U5        U R	                  XV5        g)zO
Check that pyfunc (a generator function) can loop-lift even in
nopython mode.
N)rZ   listrh   re   ri   rj   s          r   check_lift_generator_ok'TestLoopLifting.check_lift_generator_ok   sQ    
 }}V.&4##T*+!!$'.r   c                     [        X[        S9nU R                  UR                  5        U" U6 nUR                  " U6 nU R                  XV5        g)z$
Check that pyfunc can't loop-lift.
rS   N)r   rT   assertFalserW   rh   ri   rj   s          r   check_no_liftTestLoopLifting.check_no_lift   sL      &46%4=%.r   c                     [        X[        S9nU R                  UR                  5        [	        U" U6 5      n[	        UR
                  " U6 5      nU R                  XV5        g)z;
Check that pyfunc (a generator function) can't loop-lift.
ru   N)r   rT   rv   rW   rq   rh   ri   rj   s          r   check_no_lift_generator'TestLoopLifting.check_no_lift_generator   sV      &46%&4##T*+.r   c                 P    U R                  [        [        R                  4S5        g N){   )rn   r#   r   intprX   s    r   
test_lift1TestLoopLifting.test_lift1       55::-8r   c                 P    U R                  [        [        R                  4S5        g r}   )rn   r)   r   r   r   s    r   
test_lift2TestLoopLifting.test_lift2   r   r   c                 P    U R                  [        [        R                  4S5        g r}   )rn   r5   r   r   r   s    r   
test_lift3TestLoopLifting.test_lift3   r   r   c                 P    U R                  [        [        R                  4S5        g r}   )rn   r8   r   r   r   s    r   
test_lift4TestLoopLifting.test_lift4   r   r   c                 P    U R                  [        [        R                  4S5        g r}   )rn   r=   r   r   r   s    r   
test_lift5TestLoopLifting.test_lift5   r   r   c                 2    U R                  [        SS5        g )N )rn   rC   r   s    r   test_lift_issue2561#TestLoopLifting.test_lift_issue2561   s    >2r2r   c                 P    U R                  [        [        R                  4S5        g r}   )rr   r@   r   r   r   s    r   test_lift_gen1TestLoopLifting.test_lift_gen1   s    $$YvFr   c                 P    U R                  [        [        R                  4S5        g r}   )rw   rF   r   r   r   s    r   test_reject1TestLoopLifting.test_reject1   s    7UZZM6:r   c                 P    U R                  [        [        R                  4S5        g r}   )rz   rH   r   r   r   s    r   test_reject_gen1 TestLoopLifting.test_reject_gen1       $$[5::-Hr   c                 P    U R                  [        [        R                  4S5        g r}   )rz   rK   r   r   r   s    r   test_reject_gen2 TestLoopLifting.test_reject_gen2   r   r   r   N)__name__
__module____qualname____firstlineno__rZ   re   rn   rr   rw   rz   r   r   r   r   r   r   r   r   r   r   __static_attributes__r   r   r   rP   rP      sU    0	/
/
/	/999993G;IIr   rP   c                        \ rS rSrS rS rSrg)TestLoopLiftingAnnotate   c                   ^
 SSK Jn  S m
U
4S jnU" SS9" U5      n[        R                  " S5      nUR	                  5       nU" U5      n[        R
                  R                  XeS-   5        [        5       nUR                  US	9  UR                  5       nUR                  5         U R                  S
U5        UR                  R                  S-   n	U R                  SR                  U	S9U5        U R                  SU5        g)z?
Verify that annotation works as expected with one lifted loop
r   jitc                      g Nr   r   r   r   bar4TestLoopLiftingAnnotate.test_annotate_1.<locals>.bar       r   c                 f   > T" 5         [        U R                  5       H  nX==   S-  ss'   M     U $ )Nr&   r   r   )r    r"   r   s     r   foo4TestLoopLiftingAnnotate.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)rX   r   r   cfoor    xcopyrbuf
annotationr   r   s             @r   test_annotate_1'TestLoopLiftingAnnotate.test_annotate_1   s     			 D!#&IIbMG


19-j$\\^
		:JG||**Q.+222=zJ'4r   c                 X  ^ SSK Jn  S mU4S jnU" SS9" U5      n[        R                  " S5      nUR	                  5       nU" U5      n[        R
                  R                  XeS-   S	-  5        [        5       nUR                  US
9  UR                  5       nUR                  5         U R                  SU5        UR                  R                  S-   n	UR                  R                  S-   n
U R                  SR                  U	S9U5        U R                  SR                  U
S9U5        g)z@
Verify that annotation works as expected with two lifted loops
r   r   c                      g r   r   r   r   r   r   4TestLoopLiftingAnnotate.test_annotate_2.<locals>.bar  r   r   c                    > T" 5         [        U R                  5       H  nX==   S-  ss'   M     [        U R                  5       H  nX==   S-  ss'   M     U $ )Nr&   r:   r   )r    r"   r(   r   s      r   r   4TestLoopLiftingAnnotate.test_annotate_2.<locals>.foo  sI    E166]	 # 166]	 #Hr   Tr   rB   r&   r:   r   r   r      r   r   Nr   )rX   r   r   r   r    r   r   r   r   line1line2r   s              @r   test_annotate_2'TestLoopLiftingAnnotate.test_annotate_2
  s     			 D!#&IIbMG


AI?3j$\\^
		:JG++a/++a/+222>
K+222>
Kr   r   N)r   r   r   r   r   r   r   r   r   r   r   r      s     5D$Lr   r   c                   b    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rSrg)TestLoopLiftingInActioni1  c                     UR                   UR                  S      R                  nU R                  [	        U5      U5        g r]   )r^   
signaturesrW   rU   rV   )rX   jitted	loopcountrW   s       r   assert_has_lifted)TestLoopLiftingInAction.assert_has_lifted2  s7    !!&"3"3A"67>>Vi0r   c                    SSK JnJnJnJn  U" U" X4S S  5      SS9S 5       nS HW  n[
        R                  " SSS	9n[
        R                  " SSS	9nU" Xg5        UR                  Xh5        U R                  Xx5        MY     g )
Nr   )r   voidrM   doubleTr   c                     U S:X  a)  [        UR                  S   5       H  nX   S-  X'   M     g [        UR                  S   5       H  nX   S-   X'   M     g )Nr   g       @      ?)r   r'   )ur!   r"   s      r   forloop_with_if?TestLoopLiftingInAction.test_issue_734.<locals>.forloop_with_if9  sS    Avqwwqz*A4#:AD + qwwqz*A4#:AD +r   )r   r&   rB   rM   r-   )	r   r   r   rM   r   r   r1   py_funcri   )	rX   r   r   rM   r   r   r   nb_anp_as	            r   test_issue_734&TestLoopLiftingInAction.test_issue_7346  sz    22	T%#d	3	& 
4	& A99Rw/D99Rw/DA$##A,##D/ r   c                     SSK Jn  U" SSS9S 5       n[        R                  " / SQ5      n[        R                  R                  UR                  U5      U" U5      5        g )Nr   r   zf8[:](f8[:])Tr   c                     [         R                  " [        U 5      5      nSn[        [        U 5      5       H  nUS-  nX   X'   X   S:  d  M    O   [        US-   [        U 5      5       H  nSX'   M	     U$ )Nr   r&   rB   )r   zerosrV   r   )r    rJ   indiis       r   test4TestLoopLiftingInAction.test_issue_812.<locals>.testL  sp    ((3q6"CCCFmq66R<	 $ C!GSV, -Jr   )
r   r%   r:   r,   r:   rB   r,   r:   r   )r   r   r   arrayr   r   r   )rX   r   r   r    s       r   test_issue_812&TestLoopLiftingInAction.test_issue_812I  sO    	^d	+	 
,	 HH67


Qa9r   c                    SSK Jn  S n[        R                  " S5      n[	        5       nU" SS9" U5      nU" X45      nU" X45      nU R                  US   US   5        U R                  US   US   5        UR                  UR                  S      R                  S   nUR                  R                  5       u  n	U R                  U	R                  R                  5        g )Nr   r   c                 F    SnU  H  nX#-  nM	     UR                  5       nX$4$ r]   )__hash__)r!   bsehs        r   lift_issue2368?TestLoopLiftingInAction.test_issue_2368.<locals>.lift_issue2368b  s*    A 

A4Kr   rB   Tr   r&   )r   r   r   onesr0   rU   r^   r   rW   r_   r`   ra   rb   )
rX   r   r  r!   r   r   rl   rm   rc   rd   s
             r   test_issue_2368'TestLoopLiftingInAction.test_issue_2368_  s    	 GGBKHd#N3!!'Ql!c!f-!c!f-""6#4#4Q#78??B&&--/
../r   c                    SSK Jn  U" SS9S 5       nU R                  UR                  S5      U" S5      5        U R	                  USS9  U R                  UR                  S5      U" S5      5        U R	                  USS9  g )	Nr   r   Tr   c                 2    Sn[        U 5       H  nUnM     U$ r]   r   nrJ   r"   s      r   r   ?TestLoopLiftingInAction.test_no_iteration_w_redef.<locals>.test}  s    C1X Jr   r&   r   r   r   rU   r   r   rX   r   r   s      r   test_no_iteration_w_redef1TestLoopLiftingInAction.test_no_iteration_w_redefx  s     		d		 
	 	b)484tq1a$q'2tq1r   c                    SSK Jn  U" SS9S 5       nU R                  UR                  S5      U" S5      5        U R	                  USS9  U R                  UR                  S5      U" S5      5        U R	                  USS9  g )	Nr   r   Tr   c                 6    Sn[        U 5       H  nX-  nM	     U$ r]   r  r	  s      r   r   7TestLoopLiftingInAction.test_no_iteration.<locals>.test  s!    C1X Jr   r  r&   r  r  r  s      r   test_no_iteration)TestLoopLiftingInAction.test_no_iteration  s}    	d		 
	 	b)484tq1a$q'2tq1r   c                     SSK Jn  U" SS9S 5       nU R                  UR                  S5      U" S5      5        U R	                  USS9  g )Nr   r   Tr   c                 .    [        U 5       H  nUnM     W$ r   r  )r
  r"   rJ   s      r   r   >TestLoopLiftingInAction.test_define_in_loop_body.<locals>.test  s    1X Jr   r&   r  r  r  s      r   test_define_in_loop_body0TestLoopLiftingInAction.test_define_in_loop_body  sM    	d		 
	 	a$q'2tq1r   c                     SSK Jn  U" SS9S 5       n[        R                  " S5      nU R	                  UR                  U5      U" U5      5        g)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                     [        U 5      [        R                  :X  a:  U R                  S:X  a$  SnSn[	        U R
                  S   5       H  nM     g [        eSnSu  p2U$ )Nr&           r   )r   r   )typer   ndarrayndimr   r'   	Exception)argresultr(   r"   s       r   r   ;TestLoopLiftingInAction.test_invalid_argument.<locals>.test  sY    CyBJJ&88q= FA"399Q<0 1 $Or   rB   N)r   r   r   r1   rU   r   )rX   r   r   r#  s       r   test_invalid_argument-TestLoopLiftingInAction.test_invalid_argument  sJ     		d		 
	 iimc*DI6r   c                     SSK Jn  U" SS9S 5       nU R                  UR                  5       U" 5       5        U R	                  USS9  g )Nr   r   Tr   c                  L    Sn Sn[        S5       H  nUS:  a  Sn X-  nM     X4$ )Nr,   r   r:   r   r  )r    yr"   s      r   r   HTestLoopLiftingInAction.test_conditionally_defined_in_loop.<locals>.test  s6    AA1Xq51  4Kr   r&   r  r  r  s      r   "test_conditionally_defined_in_loop:TestLoopLiftingInAction.test_conditionally_defined_in_loop  sI    	d		 
	 	0tq1r   c                     SSK Jn  SS KnS nUR                  5          UR	                  S5        U" SS9" U5      nU R                  U" S5      U" S5      5        S S S 5        g ! , (       d  f       g = f)Nr   r   c                 8    U (       a  [        S5       H  nM     g g )NrB   r  )r!   r"   s     r   r   RTestLoopLiftingInAction.test_stack_offset_error_when_has_no_return.<locals>.pyfunc  s    rA # r   errorTr   )r   r   warningscatch_warningssimplefilterrU   )rX   r   r2  r   cfuncs        r   *test_stack_offset_error_when_has_no_returnBTestLoopLiftingInAction.test_stack_offset_error_when_has_no_return  s\    	
 $$&!!'*&v.EVD\5;7	 '&&s   <A##
A1c                 x   ^ SSK Jn  S mU4S jnU" SS9" U5      nU R                  U" S5      U" S5      5        g)	zt
https://github.com/numba/numba/issues/2179

Looplifting transformation is using the wrong version of variable `h`.
r   r   c                     U $ r   r   )r    s    r   r   <TestLoopLiftingInAction.test_variable_scope_bug.<locals>.bar  s    Hr   c                 N   > Sn[        U 5       H  nX-   nM	     UT" U 5      -
  nU$ )Nr  r  )r    r   kr   s      r   r   <TestLoopLiftingInAction.test_variable_scope_bug.<locals>.foo  s/    A1XE CF
AHr   Tr   rB   N)r   r   rU   )rX   r   r   r   r   s       @r   test_variable_scope_bug/TestLoopLiftingInAction.test_variable_scope_bug  s;     			 D!#&R$r(+r   c                 R   SSK Jn  S nU" SS9" U5      n[        R                  " S5      S4nU R	                  U" U6 U" U6 5        U R	                  [        UR                  UR                  S      R                  5      S5        UR                  UR                  S      R                  S   nU R	                  [        UR                  5      S5        [        R                  " S5      S4nU R	                  U" U6 U" U6 5        U R	                  [        UR                  5      S	5        g
)z,
https://github.com/numba/numba/issues/2481
r   r   c                     U S S U2   nSn[        UR                  5       H  n[        5         X2S S S2   U   -  nM     U$ )Nr&   r  )r   r   r0   )r    r*  Ar4   r<  s        r   r   <TestLoopLiftingInAction.test_recompilation_loop.<locals>.foo  sG     #A#AA166]$B$
N # Hr   Tr   rB   r&   r  r:   N)	r   r   r   r1   rU   rV   r^   r   rW   )rX   r   r   r   rk   rW   s         r   test_recompilation_loop/TestLoopLiftingInAction.test_recompilation_loop  s     		 D!#&yy}adT4[1T^^DOOA,>?FFGK 23::1=V../3yy}b dT4[1V../3r   c           
      t   SSK Jn  U" SS9S 5       n[        SSSSSSS	S
9nU" S0 UD6nUR                  " S0 UD6nU R	                  US   US   5        U R	                  US   US   5        UR
                  UR                  S      R                  u  nU R                  [        UR                  5      S5        g )Nr   r   Tr   c                    [         R                  " U5      [         R                  " U5      pXEsUS'   US'   [         R                  " US-
  5       H  n	[         R                  " XU	   -  5      U[         R                  " XU	   -  5      -  -   XyS-   '   [         R                  " XU	   -  5      U[         R                  " XU	   -  5      -  -   XS-   '   M     [        5         Xx4$ )Nr   r&   )r   r   r1   sincosr0   )
r!   r   r4   r7   x0y0r
  xsysr"   s
             r   r   DTestLoopLiftingInAction.test_lift_objectmode_issue_4223.<locals>.foo  s    XXa["((1+LBqE2a5YYqs^&&U+a"&&U2C.CCQ3&&U+a"&&U2C.CCQ3 $ H6Mr   g333333?g333333?g333333?   )r!   r   r4   r7   rJ  rK  r
  r&   r   )r   r   dictr   ri   r^   r   rW   rU   rV   nopython_signatures)rX   r   r   r   rm   rl   rW   s          r   test_lift_objectmode_issue_42237TestLoopLiftingInAction.test_lift_objectmode_issue_4223  s    	d		 
	 scSQ1DmFm;;((A4  Q!5==!23::V778!<r   r   N)r   r   r   r   r   r   r   r  r  r  r  r&  r,  r6  r>  rD  rR  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	   unittestrT   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MMO r   