U
    9%e                     @   s0  d dl Z d dlmZ d dlmZ d dlmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZmZmZmZ dedgZG dd deZG dd deZG dd deZeeZ G dd deZ!eee< e!eee < dd Z"e"eee < G dd deZ#G dd de j$Z%dS )    N)contextmanager)cached_property)njit)errorscputyping)TargetDescriptor)TargetConfigurationStack)BasicRetarget)overload)dispatcher_registryCPUDispatcherCPUtarget_registryjit_registry.	CustomCPUc                   @   s   e Zd ZdZdS )r   zExtend from the CPU target
    N)__name__
__module____qualname____doc__ r   r   [/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/numba/tests/test_retargeting.pyr      s   c                   @   s    e Zd ZdZdZedd ZdS )_NestedContextNc              	   c   s6   | j | jf}z|| _ || _d V  W 5 |\| _ | _X d S N)_typing_context_target_context)selftyping_contexttarget_contextZ
old_nestedr   r   r   nested$   s    
z_NestedContext.nested)r   r   r   r   r   r   r    r   r   r   r   r       s   r   c                   @   sP   e Zd ZejZe Zedd Z	edd Z
edd Zedd Zd	d
 ZdS )CustomTargetDescrc                 C   s   t | j| jS r   )r   Z
CPUContextr   Z_target_namer   r   r   r   _toplevel_target_context4   s    z*CustomTargetDescr._toplevel_target_contextc                 C   s   t  S r   )r   Contextr"   r   r   r   _toplevel_typing_context9   s    z*CustomTargetDescr._toplevel_typing_contextc                 C   s   | j j}|dk	r|S | jS dS )z5
        The target context for DPU targets.
        N)_nestedr   r#   r   r    r   r   r   r   >   s    z CustomTargetDescr.target_contextc                 C   s   | j j}|dk	r|S | jS dS )z5
        The typing context for CPU targets.
        N)r&   r   r%   r'   r   r   r   r   I   s    z CustomTargetDescr.typing_contextc                 C   s   | j ||S )z
        A context manager temporarily replacing the contexts with the
        given ones, for the current thread of execution.
        )r&   r    )r   r   r   r   r   r   nested_contextT   s    z CustomTargetDescr.nested_contextN)r   r   r   r   ZCPUTargetOptionsoptionsr   r&   r   r#   r%   propertyr   r   r(   r   r   r   r   r!   0   s   





r!   c                   @   s   e Zd ZeZdS )CustomCPUDispatcherN)r   r   r   custom_targetZtargetdescrr   r   r   r   r+   _   s   r+   c                  O   s*   d|kst d|kst t| dti|S )Ntarget_target)AssertionErrorr   CUSTOM_TARGET)argskwargsr   r   r   
custom_jitg   s    r3   c                   @   s    e Zd Zedd Zdd ZdS )CustomCPURetargetc                 C   s   t S r   )r0   r"   r   r   r   output_targets   s    zCustomCPURetarget.output_targetc                 C   s   t td|j}|S )Nr.   )r   r0   Zpy_func)r   Zcpu_dispZkernelr   r   r   compile_retargetw   s    z"CustomCPURetarget.compile_retargetN)r   r   r   r*   r5   r7   r   r   r   r   r4   r   s   
r4   c                   @   s`   e Zd Zdd Zdd Ze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 )TestRetargetingc                    sB   t dddd  t  fdd}t dd }t | _t | _d S )	Nr   r6   c                 S   s   | d S )zo
            This has a fixed target to "cpu".
            Cannot be used in CUSTOM_TARGET target.
            
   r   xr   r   r   fixed_target   s    z+TestRetargeting.setUp.<locals>.fixed_targetc                    s    | d S )z
            This has a flexible target, but uses a fixed target function.
            Cannot be used in CUSTOM_TARGET target.
            d   r   r:   r<   r   r   flex_call_fixed   s    z.TestRetargeting.setUp.<locals>.flex_call_fixedc                 S   s   | d S )zf
            This has a flexible target.
            Can be used in CUSTOM_TARGET target.
            i  r   r:   r   r   r   flex_target   s    z*TestRetargeting.setUp.<locals>.flex_target)r   locals	functionsr4   retarget)r   r?   r@   r   r>   r   setUp}   s    

zTestRetargeting.setUpc                 C   s   t | jS r   )r	   switch_targetrC   r"   r   r   r   rE      s    zTestRetargeting.switch_targetc              	   c   s:   |  tj}d V  W 5 Q R X | t dt|j d S )Nz != cpu)assertRaisesr   Z
NumbaErrorZassertInr0   str	exception)r   Zraisesr   r   r   check_retarget_error   s    z$TestRetargeting.check_retarget_errorc                 C   s(   | j j }| |d |d  d d S )Nhitmissr   )rC   cachestatsZassertGreater)r   rM   r   r   r   check_non_empty_cache   s    z%TestRetargeting.check_non_empty_cachec                    s^   | j d  | j d t fdd}|d}| |d | jj }| |tddd d S )	Nr<   r@   c                    s    | } | } | S r   r   r:   r<   r@   r   r   foo   s    z'TestRetargeting.test_case0.<locals>.foo{   im  r   )rJ   rK   )rB   r   assertEqualrC   rL   rM   dict)r   rP   rrM   r   rO   r   
test_case0   s    

zTestRetargeting.test_case0c              	      sN   | j d  t fdd}|   |d}W 5 Q R X | |d |   d S )Nr@   c                    s    | } | S r   r   r:   r@   r   r   rP      s    z'TestRetargeting.test_case1.<locals>.foorQ   ic  )rB   r   rE   rR   rN   )r   rP   rT   r   rV   r   
test_case1   s    

zTestRetargeting.test_case1c              
      sZ   | j d  | j d t fdd}|  " |   |d W 5 Q R X W 5 Q R X dS )zK
        The non-nested call into fixed_target should raise error.
        r<   r@   c                    s    | } | } | S r   r   r:   rO   r   r   rP      s    z'TestRetargeting.test_case2.<locals>.foorQ   NrB   r   rI   rE   r   rP   r   rO   r   
test_case2   s    



zTestRetargeting.test_case2c              
      sZ   | j d | j d  t fdd}|  " |   |d W 5 Q R X W 5 Q R X dS )zF
        The nested call into fixed_target should raise error
        r@   r?   c                    s    | } | } | S r   r   r:   r?   r@   r   r   rP      s    z'TestRetargeting.test_case3.<locals>.foorQ   NrX   rY   r   r[   r   
test_case3   s    



zTestRetargeting.test_case3c              
      sn   | j d | j d   d}| |d t fdd}|  " |   |d W 5 Q R X W 5 Q R X dS )z
        Same as case2 but flex_call_fixed() is invoked outside of CUSTOM_TARGET
        target before the switch_target.
        r@   r?   rQ      c                    s    | } | } | S r   r   r:   r[   r   r   rP      s    z'TestRetargeting.test_case4.<locals>.fooN)rB   rR   r   rI   rE   )r   rT   rP   r   r[   r   
test_case4   s    



zTestRetargeting.test_case4c              	      s   dd  t  tddd }t  dddd }td	d
 }t fdd}|d}| |d |   |d}| |d W 5 Q R X |   dS )zA
        Tests overload resolution with target switching
        c                 S   s   d S r   r   r:   r   r   r   overloaded_func  s    z3TestRetargeting.test_case5.<locals>.overloaded_func)r-   c                 S   s   dd }|S )Nc                 S   s   dS )Nin  r   r:   r   r   r   impl  s    zRTestRetargeting.test_case5.<locals>.ol_overloaded_func_custom_target.<locals>.implr   r;   r`   r   r   r    ol_overloaded_func_custom_target	  s    zDTestRetargeting.test_case5.<locals>.ol_overloaded_func_custom_targetr   c                 S   s   dd }|S )Nc                 S   s   dS )Niz  r   r:   r   r   r   r`     s    zHTestRetargeting.test_case5.<locals>.ol_overloaded_func_cpu.<locals>.implr   ra   r   r   r   ol_overloaded_func_cpu  s    z:TestRetargeting.test_case5.<locals>.ol_overloaded_func_cpuc                 S   s   d S r   r   r:   r   r   r   flex_resolve_overload  s    z9TestRetargeting.test_case5.<locals>.flex_resolve_overloadc                    s   |  |  S r   r   r:   r_   r   r   rP     s    z'TestRetargeting.test_case5.<locals>.foorQ   i2{  i  N)r   r0   r   rR   rE   rN   )r   rb   rc   rd   rP   rT   r   re   r   
test_case5  s    





zTestRetargeting.test_case5N)r   r   r   rD   rE   r   rI   rN   rU   rW   rZ   r\   r^   rf   r   r   r   r   r8   |   s    
r8   )&Zunittest
contextlibr   	functoolsr   Znumbar   Z
numba.corer   r   r   Znumba.core.descriptorsr   Znumba.core.dispatcherr	   Znumba.core.retargetr
   Znumba.core.extendingr   Znumba.core.target_extensionr   r   r   r   r   joinr   r0   r   objectr   r!   r,   r+   r3   r4   ZTestCaser8   r   r   r   r   <module>   s*   ,
