
    J/Ph)                         d Z ddlZddlmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZ ddlmZmZ ddlmZmZ d	 Z G d
 dee          ZdS )z/
Tests for practical lowering specific errors.
    N)njit)typesir)CompilerBaseDefaultPassBuilder)NopythonTypeInference)register_passFunctionPass)MemoryLeakMixinTestCasec                     dS )zDummy function used in testN as    j/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/numba/tests/test_practical_lowering_issues.pyissue7507_lroundr      s    D    c                   8    e Zd Zd Zd Zd Zd Zd Zd Zd Z	dS )	TestLoweringc                     t           d             } |d          }|                    d          }|                     ||           dS )a  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                     t          j        d          }t          |           D ]-}|dk    rt          j        d          }|dk    r||d         z  }.|S N   r      npzerosrangeones)Nsum_vecnvecs       r   udtz7TestLowering.test_issue4156_loop_vars_leak.<locals>.udt!   sW    hqkkG1XX & &66'!**C66s1v%GNr      Nr   py_funcassertPreciseEqualselfr$   gotexpects       r   test_issue4156_loop_vars_leakz*TestLowering.test_issue4156_loop_vars_leak   sW     
		 		 
		 c!ffQV,,,,,r   c                     t           d             } |d          }|                    d          }|                     ||           dS )zQVariant of test_issue4156_loop_vars_leak.

        Adding an outer loop.
        c                     t          j        d          }t          |           D ]D}t          |           D ]2}||z   }|dk    rt          j        d          }|dk    r||d         z  }3E|S r   r   )r    r!   xyr"   r#   s         r   r$   z@TestLowering.test_issue4156_loop_vars_leak_variant1.<locals>.udt6   s|    hqkkG1XX * *q * *AAAAvv gajjAvv3q6)* Nr   r%   Nr&   r)   s       r   &test_issue4156_loop_vars_leak_variant1z3TestLowering.test_issue4156_loop_vars_leak_variant11   sW    
 
	 	 
	 c!ffQV,,,,,r   c                     t           d             } |d          }|                    d          }|                     ||           dS )zUVariant of test_issue4156_loop_vars_leak.

        Adding deeper outer loop.
        c                     t          j        d          }t          |           D ]Y}t          |           D ]G}t          |           D ]5}||z   |z   }|dk    rt          j        d          }|dk    r||d         z  }6HZ|S r   r   )r    r!   zr0   r1   r"   r#   s          r   r$   z@TestLowering.test_issue4156_loop_vars_leak_variant2.<locals>.udtM   s    hqkkG1XX . .q . .A"1XX . .EAI66"$'!**C66#s1v-G.. Nr   r%   Nr&   r)   s       r   &test_issue4156_loop_vars_leak_variant2z3TestLowering.test_issue4156_loop_vars_leak_variant2H   W    
 
	 	 
	 c!ffQV,,,,,r   c                     t           d             } |d          }|                    d          }|                     ||           dS )z_Variant of test_issue4156_loop_vars_leak.

        Adding inner loop around allocation
        c                     t          j        d          }t          |           D ]V}t          |           D ]D}||z   }|dk    r&t          |           D ]}t          j        |          }|dk    r||d         z  }EW|S )Nr   r   r   )r    r!   r5   r0   r"   r1   r#   s          r   r$   z@TestLowering.test_issue4156_loop_vars_leak_variant3.<locals>.udte   s    hqkkG1XX * *q * *AAAAvv!&q - -A"$'!**CCAvv3q6)* Nr   r%   Nr&   r)   s       r   &test_issue4156_loop_vars_leak_variant3z3TestLowering.test_issue4156_loop_vars_leak_variant3`   r7   r   c                     t           d             } |d          }|                    d          }|                     ||           dS )z]Variant of test_issue4156_loop_vars_leak.

        Interleaves loops and allocations
        c                     d}t          |           D ]P}t          j        d          }t          |           D ]}t          j        d          }||d         |d         z   z  }Q|S )Nr      )r   r   r   )r    r!   r"   r#   r5   s        r   r$   z@TestLowering.test_issue4156_loop_vars_leak_variant4.<locals>.udt}   sj    G1XX ) )hqkkq $ $AAA3q6AaD=(Nr   r%   Nr&   r)   s       r   &test_issue4156_loop_vars_leak_variant4z3TestLowering.test_issue4156_loop_vars_leak_variant4x   sW    
 
		 		 
		 c!ffQV,,,,,r   c                 L   t          dd           G d dt                                 G fddt                    }t          |          d             }t	          j        d	          } ||          }|                    |          }|                     ||           d
S )zHTest an issue with literal type used as index of static_getitem
        F)mutates_CFGanalysis_onlyc                       e Zd ZdZd Zd ZdS )YTestLowering.test_issue_with_literal_in_static_getitem.<locals>.ForceStaticGetitemLiteralforce_static_getitem_literalc                 .    t          j        |            d S N)r
   __init__)r*   s    r   rG   zbTestLowering.test_issue_with_literal_in_static_getitem.<locals>.ForceStaticGetitemLiteral.__init__   s    %d+++++r   c                 B   i }|j                                         D ]h\  }}t          |t          j                  rI|j        dk    r>|j        \  }}|                    |t          j	        |j
                  f          }|||<   i|j                             |           dS )Nstatic_getitem)argsT)	calltypesitems
isinstancer   ExproprJ   replacer   literalindexupdate)r*   statereplinstsigobjidxnew_sigs           r   run_passzbTestLowering.test_issue_with_literal_in_static_getitem.<locals>.ForceStaticGetitemLiteral.run_pass   s     "'!6!6!8!8 - -ID#"411 - G'777%(X
c"%++C49M$*4M4M4O+ #P #P%,T
&&t,,,tr   N)__name__
__module____qualname___namerG   r[   r   r   r   ForceStaticGetitemLiteralrC      s9         3E, , ,    r   r`   c                       e Zd Z fdZdS )NTestLowering.test_issue_with_literal_in_static_getitem.<locals>.CustomPipelinec                     t          j        | j                  }|                    t                     |                                 |gS rF   )r   define_nopython_pipelinerT   add_pass_afterr   finalize)r*   pmr`   s     r   define_pipelinesz_TestLowering.test_issue_with_literal_in_static_getitem.<locals>.CustomPipeline.define_pipelines   sG    '@LL!!";"79 9 9tr   N)r\   r]   r^   rh   )r`   s   r   CustomPipelinerb      s.              r   ri   )pipeline_classc                     | d         S )Nr%   r   )arrs    r   foozCTestLowering.test_issue_with_literal_in_static_getitem.<locals>.foo   s    q6Mr   
   N)r	   r
   r   r   r   aranger'   assertEqual)r*   ri   rm   rl   r+   r,   r`   s         @r   )test_issue_with_literal_in_static_getitemz6TestLowering.test_issue_with_literal_in_static_getitem   s     
5	>	>	>	 	 	 	 	 	 	 
?	>	*	 	 	 	 	 	 	\ 	 	 	 
^	,	,	,	 	 
-	,	 immc#hhS!!f%%%%%r   c                     ddl m}m} ddlm  |t
                     G fdd|                      }t          d          d             }|                      |d          d	           d
S )z
        Test a problem with BaseContext.get_function() because of changes
        related to the new style error handling.
        r   )AbstractTemplateinfer_globallower_builtinc                       e Zd ZeZ fdZdS )3TestLowering.test_issue7507.<locals>.lroundTemplatec                     t          j        t           j                  } t          t           j                  d             }|S )Nc                 ^    |                      ||d         |j        d         |j                  S )Nr   )castrJ   return_type)contextbuilderrW   rJ   s       r   codegenzLTestLowering.test_issue7507.<locals>.lroundTemplate.generic.<locals>.codegen   s/     #<<a#(1+(+9 9 9r   )r   int64float64r   )r*   rJ   kws	signaturer   rv   s        r   genericz;TestLowering.test_issue7507.<locals>.lroundTemplate.generic   sH    !K66	 /??9 9 @?9
 ! r   N)r\   r]   r^   r   keyr   ru   s   r   lroundTemplaterx      s3        "C
! 
! 
! 
! 
! 
! 
!r   r   zint64(float64)c                      t          |           S rF   )r   r   s    r   rm   z(TestLowering.test_issue7507.<locals>.foo   s    #A&&&r   g333333@r   N)numba.core.typing.templatesrs   rt   numba.core.imputilsrv   r   r   rp   )r*   rs   rt   r   rm   rv   s        @r   test_issue7507zTestLowering.test_issue7507   s    
 	ONNNNNNN555555	&	'	'	! 	! 	! 	! 	! 	! 	!- 	! 	! 
(	'	! 
			' 	' 
 		' 	S1%%%%%r   N)
r\   r]   r^   r-   r2   r6   r:   r>   rq   r   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      s                               @ @ @ @ @ @ @ @ 9 9 9 9 9 9 E E E E E E E E 9 9 9 9 9 9 9 9	 	 	
@& @& @& @& @&?H @& @& @& @& @&r   