
    i)                         S r SSKrSSKJr  SSKJrJr  SSKJ	r	J
r
  SSKJr  SSKJrJr  SSKJrJr  S	 r " S
 S\\5      rg)z/
Tests for practical lowering specific errors.
    N)njit)typesir)CompilerBaseDefaultPassBuilder)NopythonTypeInference)register_passFunctionPass)MemoryLeakMixinTestCasec                     g)zDummy function used in testN as    e/var/www/html/trading/venv/lib/python3.13/site-packages/numba/tests/test_practical_lowering_issues.pyissue7507_lroundr      s        c                   >    \ rS rSrS rS rS rS rS rS r	S r
S	rg
)TestLowering   c                 p    [         S 5       nU" S5      nUR                  S5      nU R                  X#5        g)aN  Test issues with zero-filling of refct'ed variables inside loops.

Before the fix, the in-loop variables are always zero-filled at their
definition location. As a result, their state from the previous
iteration is erased. No decref is applied. To fix this, the
zero-filling must only happen once after the alloca at the function
entry block. The loop variables are technically defined once per
function (one alloca per definition per function), but semantically
defined once per assignment. Semantically, their lifetime stop only
when the variable is re-assigned or when the function ends.
c                     [         R                  " S5      n[        U 5       H/  nUS:  a  [         R                  " S5      nUS:  d  M'  UWS   -  nM1     U$ N   r      npzerosrangeones)Nsum_vecnvecs       r   udt7TestLowering.test_issue4156_loop_vars_leak.<locals>.udt!   sM    hhqkG1X6''!*C6s1v%G  Nr      Nr   py_funcassertPreciseEqualselfr%   gotexpects       r   test_issue4156_loop_vars_leak*TestLowering.test_issue4156_loop_vars_leak   s<     
		 
		 !fQ,r   c                 p    [         S 5       nU" S5      nUR                  S5      nU R                  X#5        g)zAVariant of test_issue4156_loop_vars_leak.

Adding an outer loop.
c                     [         R                  " S5      n[        U 5       HE  n[        U 5       H3  nX#-   nUS:  a  [         R                  " S5      nUS:  d  M+  UWS   -  nM5     MG     U$ r   r   )r!   r"   xyr#   r$   s         r   r%   @TestLowering.test_issue4156_loop_vars_leak_variant1.<locals>.udt6   sb    hhqkG1XqAAAv ggajAv3q6) "  Nr   r'   Nr(   r+   s       r   &test_issue4156_loop_vars_leak_variant13TestLowering.test_issue4156_loop_vars_leak_variant11   s<    
 
	 
	 !fQ,r   c                 p    [         S 5       nU" S5      nUR                  S5      nU R                  X#5        g)zEVariant of test_issue4156_loop_vars_leak.

Adding deeper outer loop.
c                    [         R                  " S5      n[        U 5       HZ  n[        U 5       HH  n[        U 5       H6  nX4-   U-   nUS:  a  [         R                  " S5      nUS:  d  M.  UWS   -  nM8     MJ     M\     U$ r   r   )r!   r"   zr3   r4   r#   r$   s          r   r%   @TestLowering.test_issue4156_loop_vars_leak_variant2.<locals>.udtM   st    hhqkG1XqA"1XEAI6"$''!*C6#s1v-G & "  Nr   r'   Nr(   r+   s       r   &test_issue4156_loop_vars_leak_variant23TestLowering.test_issue4156_loop_vars_leak_variant2H   <    
 
	 
	 !fQ,r   c                 p    [         S 5       nU" S5      nUR                  S5      nU R                  X#5        g)zOVariant of test_issue4156_loop_vars_leak.

Adding inner loop around allocation
c                     [         R                  " S5      n[        U 5       HW  n[        U 5       HE  nX2-   nUS:  a(  [        U 5       H  n[         R                  " U5      nM     US:  d  M=  UWS   -  nMG     MY     U$ )Nr   r   r   )r!   r"   r:   r3   r#   r4   r$   s          r   r%   @TestLowering.test_issue4156_loop_vars_leak_variant3.<locals>.udte   sr    hhqkG1XqAAAv!&qA"$''!*C "* Av3q6) "  Nr   r'   Nr(   r+   s       r   &test_issue4156_loop_vars_leak_variant33TestLowering.test_issue4156_loop_vars_leak_variant3`   r>   r   c                 p    [         S 5       nU" S5      nUR                  S5      nU R                  X#5        g)zMVariant of test_issue4156_loop_vars_leak.

Interleaves loops and allocations
c                     Sn[        U 5       HN  n[        R                  " S5      n[        U 5       H  n[        R                  " S5      nM     XS   WS   -   -  nMP     U$ )Nr      )r   r   r   )r!   r"   r#   r$   r:   s        r   r%   @TestLowering.test_issue4156_loop_vars_leak_variant4.<locals>.udt}   sX    G1XhhqkqAA "q6AaD=(	  Nr   r'   Nr(   r+   s       r   &test_issue4156_loop_vars_leak_variant43TestLowering.test_issue4156_loop_vars_leak_variant4x   s<    
 
		 
		 !fQ,r   c                    ^ [        SSS9 " S S[        5      5       m " U4S jS[        5      n[        US9S 5       n[        R
                  " S	5      nU" U5      nUR                  U5      nU R                  XE5        g
)zHTest an issue with literal type used as index of static_getitem
        F)mutates_CFGanalysis_onlyc                   $    \ rS rSrSrS rS rSrg)YTestLowering.test_issue_with_literal_in_static_getitem.<locals>.ForceStaticGetitemLiteral   force_static_getitem_literalc                 0    [         R                  " U 5        g N)r
   __init__)r,   s    r   rS   bTestLowering.test_issue_with_literal_in_static_getitem.<locals>.ForceStaticGetitemLiteral.__init__   s    %%d+r   c                 l   0 nUR                   R                  5        Hy  u  p4[        U[        R                  5      (       d  M&  UR
                  S:X  d  M8  UR                  u  pVUR                  U[        R                  " UR                  5      4S9nXrU'   M{     UR                   R                  U5        g)Nstatic_getitem)argsT)	calltypesitems
isinstancer   ExproprW   replacer   literalindexupdate)r,   statereplinstsigobjidxnew_sigs           r   run_passbTestLowering.test_issue_with_literal_in_static_getitem.<locals>.ForceStaticGetitemLiteral.run_pass   s     "'!6!6!8ID"411 GG'77%(XX
"%++C49MM$**4M4O+ #P%,T
 "9 &&t,r   r   N)__name__
__module____qualname____firstlineno___namerS   rh   __static_attributes__r   r   r   ForceStaticGetitemLiteralrN      s     3E,r   rp   c                   "   > \ rS rSrU 4S jrSrg)NTestLowering.test_issue_with_literal_in_static_getitem.<locals>.CustomPipeline   c                    > [         R                  " U R                  5      nUR                  T[        5        UR                  5         U/$ rR   )r   define_nopython_pipelinera   add_pass_afterr   finalize)r,   pmrp   s     r   define_pipelines_TestLowering.test_issue_with_literal_in_static_getitem.<locals>.CustomPipeline.define_pipelines   s;    '@@L!!";"79tr   r   N)rj   rk   rl   rm   ry   ro   )rp   s   r   CustomPipelinerr      s     r   r{   )pipeline_classc                     U S   $ )Nr'   r   )arrs    r   fooCTestLowering.test_issue_with_literal_in_static_getitem.<locals>.foo   s    q6Mr   
   N)r	   r
   r   r   r   aranger)   assertEqual)r,   r{   r   r~   r-   r.   rp   s         @r   )test_issue_with_literal_in_static_getitem6TestLowering.test_issue_with_literal_in_static_getitem   s~     
5	>	 	 
?	*	\ 	 
^	,	 
-	 iim#hS!%r   c                    ^ SSK JnJn  SSKJm  U" [
        5       " U4S jSU5      5       n[        S5      S 5       nU R                  U" S5      S	5        g
)zl
Test a problem with BaseContext.get_function() because of changes
related to the new style error handling.
r   )AbstractTemplateinfer_globallower_builtinc                   &   > \ rS rSr\rU 4S jrSrg)3TestLowering.test_issue7507.<locals>.lroundTemplate   c                    > [         R                  " [         R                  5      nT" [        [         R                  5      S 5       nU$ )Nc                 \    U R                  XS   UR                  S   UR                  5      $ )Nr   )castrW   return_type)contextbuilderrd   rW   s       r   codegenLTestLowering.test_issue7507.<locals>.lroundTemplate.generic.<locals>.codegen   s,     #<<a#((1+(+9 9r   )r   int64float64r   )r,   rW   kws	signaturer   r   s        r   generic;TestLowering.test_issue7507.<locals>.lroundTemplate.generic   s>    !KK6	 /?9 @9
 ! r   r   N)rj   rk   rl   rm   r   keyr   ro   r   s   r   lroundTemplater      s    "C
! 
!r   r   zint64(float64)c                     [        U 5      $ rR   )r   r   s    r   r   (TestLowering.test_issue7507.<locals>.foo   s    #A&&r   g333333@r   N)numba.core.typing.templatesr   r   numba.core.imputilsr   r   r   r   )r,   r   r   r   r   r   s        @r   test_issue7507TestLowering.test_issue7507   s\    
 	O5	&	'	!- 	! 
(	! 
		' 
 	' 	S1%r   r   N)rj   rk   rl   rm   r/   r6   r<   rB   rH   r   r   ro   r   r   r   r   r      s&    -8-.-0-0-*)&V&r   r   )__doc__numpyr   numbar   
numba.corer   r   numba.core.compilerr   r   numba.core.typed_passesr   numba.core.compiler_machineryr	   r
   numba.tests.supportr   r   r   r   r   r   r   <module>r      s6        @ 9 E 9	
@&?H @&r   