
    J/Ph?                        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 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j$                     dS dS )    )StringION)types)compile_extraFlags)TestCasetagMemoryLeakMixinTc                     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   s       \/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/numba/tests/test_looplifting.pycompile_isolatedr      ss    ......
mT***
h###!!	 
       c                 f    t          j        d          }t          |j                  D ]}| ||<   |S )N   )npemptyrangesizexais      r   lift1r#       s6    
A16]]  !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)   )   sf    
A171:  qwqz"" 	 	AAadGG	 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   s[    A
	!28$$$A	A171:  	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   <   sm    A
	!28$$$A	A	A171:  	QqTAX	Qq5Lr   c                     t                      }t          j        d          }t          |j        d                   D ]
}|dk    r n|S )Nr%   r      r0   r   r1   r   r'   r    r3   r!   r"   s       r   lift5r=   G   sM    A
	!A171:  q55E Hr   c              #      K   t          j        d          }dV  t          |j                  D ]}| ||<   t          j        |          V  d S )Nr   r   )r   r   r   r   sumr   s      r   	lift_gen1r@   P   sS      
A
GGG16]]  !
&))OOOOOr   c                  |    t          j        d           t          d          D ]} t          d          D ]}  dS dS )Nr&   
   r:   )r   r   r   )r"   r(   s     r   lift_issue2561rC   Y   sL    HQKKK2YY  r 	 	A111	1r   c                 n    t          j        d          }t          |j        d                   D ]}|c S |S Nr%   r   )r   r1   r   r'   r   s      r   reject1rF   `   s:    
	!A171:  Hr   c              #      K   t                      }t          j        d          }t          |j        d                   D ]}||         V  d S rE   r;   r<   s       r   reject_gen1rH   h   sR      A
	!A171:  d



 r   c              #      K   t                      }t          j        d          }t          |j                  D ](}||         | z   }t          |          D ]}|dz  }|V  )d S )Nr   r:   )r0   r   r1   r   r   )r    r3   r!   r"   resr(   s         r   reject_gen2rK   o   sw      A
	!A16]]  dQhq 	 	A (CC				 r   c                     t          j        dt           j                  }t          |j                  D ]0}t                      }t          j        |dz             |         ||<   1|S )Nr   r-   r&   )r   r   int32r   r   r0   r1   )r    r!   r"   r3   s       r   reject_npm1rN   {   s[    
"(###A16]] # #HHyQ"!Hr   c                   n    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dS )TestLoopLiftingc           	          ddl m} t          |j        |j        ||d t
          i           }|                     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   sg    222222%%HN2	
 
 
 	T[))1---r   c                     |j         d         }|j                                        \  }|                     |j        j                   d S Nr   )rV   	overloadsvalues
assertTruefndescnative)rW   rX   jitlooploopcress       r   assert_lifted_nativez$TestLoopLifting.assert_lifted_native   s@    +a.&--//
./////r   c                     |                      ||          } || } |j        | }|                     |           |                     ||           dS )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   sa     }}VX..64=d%!!$'''#.....r   c                     |                      ||          }t           ||           }t           |j        |           }|                     |           |                     ||           dS )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   sr    
 }}VX..&&#4#T*++!!$'''#.....r   c                     t          ||t                    }|                     |j                    || } |j        | }|                     ||           dS )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   sk      &46 6 6%%%64=d%#.....r   c                     t          ||t                    }|                     |j                   t	           ||           }t	           |j        |           }|                     ||           dS )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 6 6%%%&&#4#T*++#.....r   c                 T    |                      t          t          j        fd           d S N){   )rk   r#   r   intprW   s    r   
test_lift1zTestLoopLifting.test_lift1   $    55:-88888r   c                 T    |                      t          t          j        fd           d S rv   )rk   r)   r   rx   ry   s    r   
test_lift2zTestLoopLifting.test_lift2   r{   r   c                 T    |                      t          t          j        fd           d S rv   )rk   r5   r   rx   ry   s    r   
test_lift3zTestLoopLifting.test_lift3   r{   r   c                 T    |                      t          t          j        fd           d S rv   )rk   r8   r   rx   ry   s    r   
test_lift4zTestLoopLifting.test_lift4   r{   r   c                 T    |                      t          t          j        fd           d S rv   )rk   r=   r   rx   ry   s    r   
test_lift5zTestLoopLifting.test_lift5   r{   r   c                 >    |                      t          dd           d S )N )rk   rC   ry   s    r   test_lift_issue2561z#TestLoopLifting.test_lift_issue2561   s     >2r22222r   c                 T    |                      t          t          j        fd           d S rv   )rn   r@   r   rx   ry   s    r   test_lift_gen1zTestLoopLifting.test_lift_gen1   s$    $$YvFFFFFr   c                 T    |                      t          t          j        fd           d S rv   )rr   rF   r   rx   ry   s    r   test_reject1zTestLoopLifting.test_reject1   s$    7UZM6:::::r   c                 T    |                      t          t          j        fd           d S rv   )rt   rH   r   rx   ry   s    r   test_reject_gen1z TestLoopLifting.test_reject_gen1   $    $$[5:-HHHHHr   c                 T    |                      t          t          j        fd           d S 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      s         0 0 0	/ 	/ 	/
/ 
/ 
/
/ 
/ 
/	/ 	/ 	/9 9 99 9 99 9 99 9 99 9 93 3 3G G G; ; ;I I II I I I Ir   rP   c                       e Zd Zd Zd ZdS )TestLoopLiftingAnnotatec                 \  
 ddl m} d 

fd}  |d          |          }t          j        d          }|                                } ||          }t          j                            ||dz              t                      }|                    |	           |	                                }|
                                 |                     d
|           |j        j        dz   }	|                     d                    |	          |           |                     d|           dS )zO
        Verify that annotation works as expected with one lifted loop
        r   jitc                      d S Nr   r   r   r   barz4TestLoopLiftingAnnotate.test_annotate_1.<locals>.bar       Dr   c                 j                  t          | j                  D ]}| |xx         dz  cc<   | S )Nr&   r   r   )r    r"   r   s     r   fooz4TestLoopLiftingAnnotate.test_annotate_1.<locals>.foo   sA    CEEE16]]  !	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?    		 	 		 	 	 	 	 "ssD!!!#&&IbMMDGG

519---jj$$$\\^^
		:JGGG|*Q.+222==zJJJ'44444r   c                    ddl m} d fd}  |d          |          }t          j        d          }|                                } ||          }t          j                            ||dz   d	z             t                      }|                    |
           |	                                }|
                                 |                     d|           |j        j        dz   }	|j        j        dz   }
|                     d                    |	          |           |                     d                    |
          |           dS )zP
        Verify that annotation works as expected with two lifted loops
        r   r   c                      d S 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  so    CEEE16]]  !	16]]  !	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
  sc    		 	 		 	 	 	 	 "ssD!!!#&&IbMMDGG

EAI?333jj$$$\\^^
		:JGGG+a/+a/+222>>
KKK+222>>
KKKKKr   N)r   r   r   r   r   r   r   r   r   r      s8         5  5  5D$L $L $L $L $Lr   r   c                   \    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S )TestLoopLiftingInActionc                     |j         |j        d                  j        }|                     t	          |          |           d S r[   )r\   
signaturesrV   rT   rU   )rW   jitted	loopcountrV   s       r   assert_has_liftedz)TestLoopLiftingInAction.assert_has_lifted2  s<    !&"3A"67>Vi00000r   c                 @   ddl m}m}m}m}  | |||d d                    d          d             }dD ]f}t          j        dd	          }t          j        dd	          } |||           |                    ||           |                     ||           gd S )
Nr   )r   voidrM   doubleTr   c                     | dk    r-t          |j        d                   D ]}||         dz  ||<   d S t          |j        d                   D ]}||         dz   ||<   d S )Nr   g       @      ?)r   r'   )ur!   r"   s      r   forloop_with_ifz?TestLoopLiftingInAction.test_issue_734.<locals>.forloop_with_if9  s    Avvqwqz** & &AQ4#:AaDD& & qwqz** & &AQ4#:AaDD& &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2222222222	TT%##d	3	3	3	& 	& 
4	3	&  	0 	0A9Rw///D9Rw///DOAt$$$##At,,,##D$////	0 	0r   c                     ddl m}  |dd          d             }t          j        g d          }t          j                            |                    |           ||                     d S )Nr   r   zf8[:](f8[:])Tr   c                    t          j        t          |                     }d}t          t          |                     D ] }|dz  }| |         ||<   | |         dk    r 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  s    (3q66""CCCFFmm  qS6CS6R<<E  
 C!GSVV,,  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  s    	^d	+	+	+	 	 
,	+	 H66677

Qa99999r   c                    ddl m} d }t          j        d          }t	                      }  |d          |          } |||          } |||          }|                     |d         |d                    |                     |d         |d                    |j        |j        d                  j        d         }|j        	                                \  }	| 
                    |	j        j                   d S )Nr   r   c                 J    d}| D ]}||z  }|                                 }||fS r[   )__hash__)r!   bsehs        r   lift_issue2368z?TestLoopLiftingInAction.test_issue_2368.<locals>.lift_issue2368b  s7    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   	 	 	 GBKKHH#d###N33!>!Q''fQll!c!f---!c!f---"6#4Q#78?B&--//
./////r   c                 `   ddl m}  |d          d             }|                     |                    d           |d                     |                     |d           |                     |                    d           |d                     |                     |d           d S )	Nr   r   Tr   c                 2    d}t          |           D ]}|}|S r[   r   nrJ   r"   s      r   r   z?TestLoopLiftingInAction.test_no_iteration_w_redef.<locals>.test}  s(    C1XX  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))4488444tq111a$$q''222tq11111r   c                 `   ddl m}  |d          d             }|                     |                    d           |d                     |                     |d           |                     |                    d           |d                     |                     |d           d S )	Nr   r   Tr   c                 8    d}t          |           D ]}||z  }|S r[   r   r   s      r   r   z7TestLoopLiftingInAction.test_no_iteration.<locals>.test  s,    C1XX  qJr   r   r&   r   r   r   s      r   test_no_iterationz)TestLoopLiftingInAction.test_no_iteration  s    	d				 	 
		 	b))4488444tq111a$$q''222tq11111r   c                     ddl m}  |d          d             }|                     |                    d           |d                     |                     |d           d S )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#    1XX  Jr   r&   r   r   r   s      r   test_define_in_loop_bodyz0TestLoopLiftingInAction.test_define_in_loop_body  s    	d				 	 
		 	a$$q''222tq11111r   c                     ddl m}  |d          d             }t          j        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   r   Tr   c                     t          |           t          j        k    r5| j        dk    r#d}d}t	          | j        d                   D ]}d S 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  sn    CyyBJ&&8q== FA"39Q<00    $O1r   rB   N)r   r   r   r1   rT   r   )rW   r   r   r   s       r   test_invalid_argumentz-TestLoopLiftingInAction.test_invalid_argument  s|     		d				 	 
		 immc**DDII66666r   c                     ddl m}  |d          d             }|                     |                                 |                       |                     |d           d S )Nr   r   Tr   c                  P    d} d}t          d          D ]}|dk    rd} || z  }|| fS )Nr,   r   r:   r   r   )r    yr"   s      r   r   zHTestLoopLiftingInAction.test_conditionally_defined_in_loop.<locals>.test  sC    AA1XX  q551Qa4Kr   r&   r   r   r   s      r   "test_conditionally_defined_in_loopz:TestLoopLiftingInAction.test_conditionally_defined_in_loop  s    	d				 	 
		 	000tq11111r   c                    ddl m} dd l}d }|                                5  |                    d             |d          |          }|                      |d           |d                     d d d            d S # 1 swxY w Y   d S )Nr   r   c                 2    | rt          d          D ]}d S d S )NrB   r   )r!   r"   s     r   r   zRTestLoopLiftingInAction.test_stack_offset_error_when_has_no_return.<locals>.pyfunc  s6     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    	 	 	
 $$&& 	8 	8!!'***&CC&&&v..EVVD\\55;;777		8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8s   ABB	Bc                     ddl m} 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   r   c                     | S r   r   )r    s    r   r   z<TestLoopLiftingInAction.test_variable_scope_bug.<locals>.bar  s    Hr   c                 V    d}t          |           D ]}||z   }| |           z
  }|S )Nr   r   )r    r   kr   s      r   r   z<TestLoopLiftingInAction.test_variable_scope_bug.<locals>.foo  s>    A1XX  ECCFF
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     		 	 		 	 	 	 	 "ssD!!!#&&R$$r((+++++r   c                 l   ddl m} d }  |d          |          }t          j        d          df}|                      ||  ||            |                     t          |j        |j        d                  j                  d           |j        |j        d                  j        d         }|                     t          |j                  d           t          j        d          df}|                      ||  ||            |                     t          |j                  d	           d
S )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  sV     ##A#AA16]] # #$$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=    		 	 	 "ssD!!!#&&y}}adTT4[111T^DOA,>?FGGKKK 23:1=V.//333y}}b dTT4[111V.//33333r   c           	         ddl m}  |d          d             }t          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   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    Xa[["(1++BrLBqE2a5Yqs^^ D D&RU++a"&RU2C2C.CC1Q3&RU++a"&RU2C2C.CC1Q3HHH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DDcmmFmm3;((((A444  Q!555=!23:V78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  s        1 1 10 0 0&: : :,0 0 022 2 2&2 2 2"2 2 27 7 782 2 28 8 8, , ,*4 4 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,     s3                   4 4 4 4 4 4 4 4 > > > > > > > > > >   $ !% (--// 37  0        	 	 	          
 
 
  _I _I _I _I _Iox _I _I _IDGL GL GL GL GLh GL GL GLT{= {= {= {= {=ox {= {= {=| zHMOOOOO r   