o
    ۾i,                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlZG dd de	Z
dd Zd	d
 ZG dd deZG dd deZedkrHe  dS dS )    N)jit)types)TestCasec                   @      e Zd Zdd ZdS )Dummyc                 C   s   |d S )N    )selfotherr   r   R/home/ubuntu/.local/lib/python3.10/site-packages/numba/tests/test_func_lifetime.py__add__      zDummy.__add__N)__name__
__module____qualname__r   r   r   r   r   r   
   s    r   c                 C      | d S N   r   xr   r   r   global_usecase1   r   r   c                   C   s   t d S r   )
global_objr   r   r   r   global_usecase2   r   r   c                   @   sp   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )TestFuncLifetimezP
    Test the lifetime of compiled function objects and their dependencies.
    c                 C   s*   |  t|jd t|j d }|jS )zV
        Get the single implementation (a C function object) of a dispatcher.
        r   r   )assertEquallen	overloadslistvaluesentry_point)r	   
dispatchercresr   r   r   get_impl   s   zTestFuncLifetime.get_implc                 K   s   dd }t d	i ||}| |dd | |}dd |||jfD }d  } } }}t  | dd |D d gt|  d S )
Nc                 S   r   r   r   r   r   r   r   f%   r   z5TestFuncLifetime.check_local_func_lifetime.<locals>.fint32(int32)r      c                 S      g | ]}t |qS r   weakrefref.0objr   r   r   
<listcomp>1       z>TestFuncLifetime.check_local_func_lifetime.<locals>.<listcomp>c                 S      g | ]}| qS r   r   r+   wrr   r   r   r-   4       )r$   )r   assertPreciseEqualr"   __self__gccollectr   r   )r	   jitargsr#   c_fcfuncrefsr,   r   r   r   check_local_func_lifetime$   s   
$z*TestFuncLifetime.check_local_func_lifetimec                 C      | j dd d S NT)forceobjr;   r	   r   r   r   test_local_func_lifetime6      z)TestFuncLifetime.test_local_func_lifetimec                 C   r<   NT)nopythonr?   r@   r   r   r   test_local_func_lifetime_npm9   rB   z-TestFuncLifetime.test_local_func_lifetime_npmc                 K   s   t di |t}| |dd | |}t|}dd ||jfD }d  } }}t  | 	dd |D d gt
|  d S )Nr   r%   c                 S   r&   r   r'   r*   r   r   r   r-   C   r.   z?TestFuncLifetime.check_global_func_lifetime.<locals>.<listcomp>c                 S   r/   r   r   r0   r   r   r   r-   F   r2   r   )r   r   r3   r"   r(   r)   r4   r5   r6   r   r   )r	   r7   r8   r9   r1   r:   r,   r   r   r   check_global_func_lifetime<   s   

$z+TestFuncLifetime.check_global_func_lifetimec                 C   r<   r=   rF   r@   r   r   r   test_global_func_lifetimeH   rB   z*TestFuncLifetime.test_global_func_lifetimec                 C   r<   rC   rG   r@   r   r   r   test_global_func_lifetime_npmK   rB   z.TestFuncLifetime.test_global_func_lifetime_npmc                 K   sp   t  atdi |t}| | d dd |tfD }d  } }at  | dd |D d gt|  d S )N   c                 S   r&   r   r'   r*   r   r   r   r-   X   r.   z>TestFuncLifetime.check_global_obj_lifetime.<locals>.<listcomp>c                 S   r/   r   r   r0   r   r   r   r-   [   r2   r   )	r   r   r   r   r3   r5   r6   r   r   )r	   r7   r8   r:   r,   r   r   r   check_global_obj_lifetimeN   s   $z*TestFuncLifetime.check_global_obj_lifetimec                 C   r<   r=   )rK   r@   r   r   r   test_global_obj_lifetime]   rB   z)TestFuncLifetime.test_global_obj_lifetimec              	      s   dd }t di ||     fdd}t di ||}|  | |dd dd	 | |||  j| |jfD }d
 } }   }}t  | dd	 |D d
gt|  d
S )z
        When a jitted function calls into another jitted function, check
        that everything is collected as desired.
        c                 S   s   | d S )N
   r   )ar   r   r   mult_10e   r   z?TestFuncLifetime.check_inner_function_lifetime.<locals>.mult_10
intp(intp)c                    s    | d S )N   r   r   	c_mult_10r   r   do_mathk   s   z?TestFuncLifetime.check_inner_function_lifetime.<locals>.do_mathr   2   c                 S   r&   r   r'   r*   r   r   r   r-   s   r.   zBTestFuncLifetime.check_inner_function_lifetime.<locals>.<listcomp>Nc                 S   r/   r   r   )r+   wr   r   r   r-   z   r2   )rP   )r   disable_compiler   r"   r4   r5   r6   r   )r	   r7   rO   rT   	c_do_mathwrsr,   r   rR   r   check_inner_function_lifetime`   s    

$z.TestFuncLifetime.check_inner_function_lifetimec                 C   r<   r=   rZ   r@   r   r   r   test_inner_function_lifetime|   rB   z-TestFuncLifetime.test_inner_function_lifetimec                 C   r<   rC   r[   r@   r   r   r    test_inner_function_lifetime_npm   rB   z1TestFuncLifetime.test_inner_function_lifetime_npmN)r   r   r   __doc__r"   r;   rA   rE   rF   rH   rI   rK   rL   rZ   r\   r]   r   r   r   r   r      s    r   c                   @   r   )TestLifeTimeIssuec           	         s   ddl m} dd l |dd }| fdd} ddggg} dd	gg}| }||d | | }||d | | }d S )
Nr   )njitc                 S   s   |j d }|dkr|d }dS )Nr   )shape)pointpolygonsnum_polygonsintentionally_unused_variabler   r   r   is_point_in_polygons   s   
z@TestLifeTimeIssue.test_double_free.<locals>.is_point_in_polygonsc                      s    j d jdS )NrM   )dtype)emptyint64r   npr   r   dummy   s   z1TestLifeTimeIssue.test_double_free.<locals>.dummyr   g      g      ?)numbar`   numpyarray)	r	   r`   rf   rl   rc   pointsrN   bcr   rj   r   test_double_free   s   


z"TestLifeTimeIssue.test_double_freeN)r   r   r   rs   r   r   r   r   r_      s    r_   __main__)r5   r(   rm   r   
numba.corer   numba.tests.supportr   unittestobjectr   r   r   r   r_   r   mainr   r   r   r   <module>   s    l 