U
    ,d,                     @   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re  dS )    N)jit)types)TestCasec                   @   s   e Zd Zdd ZdS )Dummyc                 C   s   |d S )N    )selfotherr   r   B/tmp/pip-unpacked-wheel-eu7e0c37/numba/tests/test_func_lifetime.py__add__   s    zDummy.__add__N)__name__
__module____qualname__r   r   r   r   r
   r   
   s   r   c                 C   s   | d S N   r   xr   r   r
   global_usecase1   s    r   c                   C   s   t d S r   )
global_objr   r   r   r
   global_usecase2   s    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lenZ	overloadslistvaluesZentry_point)r   
dispatcherZcresr   r   r
   get_impl   s    zTestFuncLifetime.get_implc                 K   s   dd }t d	||}| |dd | |}dd |||jfD }d  } } }}t  | dd |D d gt|  d S )
Nc                 S   s   | d S r   r   r   r   r   r
   f%   s    z5TestFuncLifetime.check_local_func_lifetime.<locals>.fint32(int32)r      c                 S   s   g | ]}t |qS r   weakrefref.0objr   r   r
   
<listcomp>1   s     z>TestFuncLifetime.check_local_func_lifetime.<locals>.<listcomp>c                 S   s   g | ]
}| qS r   r   r$   wrr   r   r
   r&   4   s     )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   s   | j dd d S NT)Zforceobjr1   r   r   r   r
   test_local_func_lifetime6   s    z)TestFuncLifetime.test_local_func_lifetimec                 C   s   | j dd d S NT)Znopythonr3   r4   r   r   r
   test_local_func_lifetime_npm9   s    z-TestFuncLifetime.test_local_func_lifetime_npmc                 K   s~   t f |t}| |dd | |}t|}dd ||jfD }d  } }}t  | 	dd |D d gt
|  d S )Nr   r   c                 S   s   g | ]}t |qS r   r    r#   r   r   r
   r&   C   s     z?TestFuncLifetime.check_global_func_lifetime.<locals>.<listcomp>c                 S   s   g | ]
}| qS r   r   r'   r   r   r
   r&   F   s     )r   r   r)   r   r!   r"   r*   r+   r,   r   r   )r   r-   r.   r/   r(   r0   r%   r   r   r
   check_global_func_lifetime<   s    

z+TestFuncLifetime.check_global_func_lifetimec                 C   s   | j dd d S r2   r8   r4   r   r   r
   test_global_func_lifetimeH   s    z*TestFuncLifetime.test_global_func_lifetimec                 C   s   | j dd d S r6   r9   r4   r   r   r
   test_global_func_lifetime_npmK   s    z.TestFuncLifetime.test_global_func_lifetime_npmc                 K   sl   t  atf |t}| | d dd |tfD }d  } }at  | dd |D d gt|  d S )N   c                 S   s   g | ]}t |qS r   r    r#   r   r   r
   r&   X   s     z>TestFuncLifetime.check_global_obj_lifetime.<locals>.<listcomp>c                 S   s   g | ]
}| qS r   r   r'   r   r   r
   r&   [   s     )	r   r   r   r   r)   r+   r,   r   r   )r   r-   r.   r0   r%   r   r   r
   check_global_obj_lifetimeN   s    z*TestFuncLifetime.check_global_obj_lifetimec                 C   s   | j dd d S r2   )r=   r4   r   r   r
   test_global_obj_lifetime]   s    z)TestFuncLifetime.test_global_obj_lifetimec              	      s   dd }t d||     fdd}t d||}|  | |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   s    z?TestFuncLifetime.check_inner_function_lifetime.<locals>.mult_10
intp(intp)c                    s    | d S )N   r   r   Z	c_mult_10r   r
   do_mathk   s    z?TestFuncLifetime.check_inner_function_lifetime.<locals>.do_mathr   2   c                 S   s   g | ]}t |qS r   r    r#   r   r   r
   r&   s   s     zBTestFuncLifetime.check_inner_function_lifetime.<locals>.<listcomp>Nc                 S   s   g | ]
}| qS r   r   )r$   wr   r   r
   r&   z   s     )rB   )rB   )r   Zdisable_compiler   r   r*   r+   r,   r   )r   r-   rA   rE   Z	c_do_mathZwrsr%   r   rD   r
   check_inner_function_lifetime`   s     

z.TestFuncLifetime.check_inner_function_lifetimec                 C   s   | j dd d S r2   rH   r4   r   r   r
   test_inner_function_lifetime|   s    z-TestFuncLifetime.test_inner_function_lifetimec                 C   s   | j dd d S r6   rI   r4   r   r   r
    test_inner_function_lifetime_npm   s    z1TestFuncLifetime.test_inner_function_lifetime_npmN)r   r   r   __doc__r   r1   r5   r7   r8   r:   r;   r=   r>   rH   rJ   rK   r   r   r   r
   r      s   r   c                   @   s   e Zd Zdd ZdS )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)ZpointpolygonsZnum_polygonsZ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 )Nr?   )Zdtype)emptyZint64r   npr   r
   dummy   s    z1TestLifeTimeIssue.test_double_free.<locals>.dummyr   g      g      ?)numbarN   Znumpyarray)	r   rN   rQ   rU   rP   Zpointsr@   bcr   rS   r
   test_double_free   s    

z"TestLifeTimeIssue.test_double_freeN)r   r   r   rZ   r   r   r   r
   rM      s   rM   __main__)r+   r!   rV   r   Z
numba.corer   Znumba.tests.supportr   Zunittestobjectr   r   r   r   rM   r   mainr   r   r   r
   <module>   s   l 