U
    iâË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rŒe ¡  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   úW/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/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ÚvaluesÚ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©r2   ©r   r   r   r
   Útest_local_func_lifetime6   s    z)TestFuncLifetime.test_local_func_lifetimec                 C   s   | j dd� d S ©NT)Znopythonr4   r5   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/   r0   r)   r1   r&   r   r   r
   Úcheck_global_func_lifetime<   s    

z+TestFuncLifetime.check_global_func_lifetimec                 C   s   | j dd� d S r3   ©r9   r5   r   r   r
   Útest_global_func_lifetimeH   s    z*TestFuncLifetime.test_global_func_lifetimec                 C   s   | j dd� d S r7   r:   r5   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/   r1   r&   r   r   r
   Úcheck_global_obj_lifetimeN   s    z*TestFuncLifetime.check_global_obj_lifetimec                 C   s   | j dd� d S r3   )r>   r5   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     )rC   )rC   )r   Zdisable_compiler   r   r+   r,   r-   r   )r   r.   rB   rF   Z	c_do_mathZwrsr&   r   rE   r
   Úcheck_inner_function_lifetime`   s     

þÿz.TestFuncLifetime.check_inner_function_lifetimec                 C   s   | j dd� d S r3   ©rI   r5   r   r   r
   Útest_inner_function_lifetime|   s    z-TestFuncLifetime.test_inner_function_lifetimec                 C   s   | j dd� d S r7   rJ   r5   r   r   r
   Ú test_inner_function_lifetime_npm   s    z1TestFuncLifetime.test_inner_function_lifetime_npmN)r   r   r   Ú__doc__r   r2   r6   r8   r9   r;   r<   r>   r?   rI   rK   rL   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)Ú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      à?)ÚnumbarO   ÚnumpyÚarray)	r   rO   rS   rW   rR   ZpointsrA   ÚbÚcr   rU   r
   Útest_double_free„   s    

z"TestLifeTimeIssue.test_double_freeN)r   r   r   r]   r   r   r   r
   rN   ƒ   s   rN   Ú__main__)r,   r"   rX   r   Z
numba.corer   Znumba.tests.supportr   ZunittestÚobjectr   r   r   r   rN   r   Úmainr   r   r   r
   Ú<module>   s   l 