o
    i                     @   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   k/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/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   sB    | j | jf}z|| _ || _d V  W |\| _ | _d S |\| _ | _w 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      | j j}|dur
|S | jS )z5
        The target context for DPU targets.
        N)_nestedr   r#   r   r    r   r   r   r   >      z CustomTargetDescr.target_contextc                 C   r&   )z5
        The typing context for CPU targets.
        N)r'   r   r%   r(   r   r   r   r   I   r)   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|vsJ d|vsJ t | dti|S )Ntarget_target)r   CUSTOM_TARGET)argskwargsr   r   r   
custom_jitg   s   r4   c                   @   s    e Zd Zedd Zdd ZdS )CustomCPURetargetc                 C   s   t S r   )r1   r"   r   r   r   output_targets      zCustomCPURetarget.output_targetc                 C   s   t td|j}|S )Nr0   )r   r1   Zpy_func)r   Zcpu_dispZkernelr   r   r   compile_retargetw   s   z"CustomCPURetarget.compile_retargetN)r   r   r   r,   r6   r9   r   r   r   r   r5   r   s    
r5   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   r8   c                 S      | d S )zo
            This has a fixed target to "cpu".
            Cannot be used in CUSTOM_TARGET target.
            
   r   xr   r   r   fixed_target      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   r;   )zf
            This has a flexible target.
            Can be used in CUSTOM_TARGET target.
            i  r   r=   r   r   r   flex_target   r@   z*TestRetargeting.setUp.<locals>.flex_target)r   locals	functionsr5   retarget)r   rC   rD   r   rB   r   setUp}   s   

zTestRetargeting.setUpc                 C   s   t | jS r   )r	   switch_targetrG   r"   r   r   r   rI      s   zTestRetargeting.switch_targetc                 c   sP    |  tj}d V  W d    n1 sw   Y  | t dt|j d S )Nz != cpu)assertRaisesr   Z
NumbaErrorZassertInr1   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   )rG   cachestatsZassertGreater)r   rQ   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?   rD   c                        | } | } | S r   r   r=   r?   rD   r   r   foo      z'TestRetargeting.test_case0.<locals>.foo{   im  r   )rN   rO   )rF   r   assertEqualrG   rP   rQ   dict)r   rU   rrQ   r   rT   r   
test_case0   s   

zTestRetargeting.test_case0c                    sb   | j d  t fdd}|   |d}W d    n1 s w   Y  | |d |   d S )NrD   c                    s    | } | S r   r   r=   rD   r   r   rU      s   z'TestRetargeting.test_case1.<locals>.foorW   ic  )rF   r   rI   rX   rR   )r   rU   rZ   r   r\   r   
test_case1   s   


zTestRetargeting.test_case1c              	      s   | j d  | j d t fdd}|  ) |   |d W d   n1 s+w   Y  W d   dS W d   dS 1 sCw   Y  dS )zK
        The non-nested call into fixed_target should raise error.
        r?   rD   c                    rS   r   r   r=   rT   r   r   rU      rV   z'TestRetargeting.test_case2.<locals>.foorW   NrF   r   rM   rI   r   rU   r   rT   r   
test_case2      




"zTestRetargeting.test_case2c              	      s   | j d | j d  t fdd}|  ) |   |d W d   n1 s+w   Y  W d   dS W d   dS 1 sCw   Y  dS )zF
        The nested call into fixed_target should raise error
        rD   rC   c                    rS   r   r   r=   rC   rD   r   r   rU      rV   z'TestRetargeting.test_case3.<locals>.foorW   Nr^   r_   r   rb   r   
test_case3   ra   zTestRetargeting.test_case3c              	      s   | j d | j d   d}| |d t fdd}|  ) |   |d W d   n1 s5w   Y  W d   dS W d   dS 1 sMw   Y  dS )z
        Same as case2 but flex_call_fixed() is invoked outside of CUSTOM_TARGET
        target before the switch_target.
        rD   rC   rW      c                    rS   r   r   r=   rb   r   r   rU      rV   z'TestRetargeting.test_case4.<locals>.fooN)rF   rX   r   rM   rI   )r   rZ   rU   r   rb   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 d   n1 sIw   Y  |   dS )zA
        Tests overload resolution with target switching
        c                 S      d S r   r   r=   r   r   r   overloaded_func     z3TestRetargeting.test_case5.<locals>.overloaded_func)r/   c                 S      dd }|S )Nc                 S      dS )Nin  r   r=   r   r   r   impl  rh   zRTestRetargeting.test_case5.<locals>.ol_overloaded_func_custom_target.<locals>.implr   r>   rk   r   r   r    ol_overloaded_func_custom_target	     zDTestRetargeting.test_case5.<locals>.ol_overloaded_func_custom_targetr   c                 S   ri   )Nc                 S   rj   )Niz  r   r=   r   r   r   rk     rh   zHTestRetargeting.test_case5.<locals>.ol_overloaded_func_cpu.<locals>.implr   rl   r   r   r   ol_overloaded_func_cpu  rn   z:TestRetargeting.test_case5.<locals>.ol_overloaded_func_cpuc                 S   rf   r   r   r=   r   r   r   flex_resolve_overload  r7   z9TestRetargeting.test_case5.<locals>.flex_resolve_overloadc                    s   |  |  S r   r   r=   rg   r   r   rU     s   z'TestRetargeting.test_case5.<locals>.foorW   i2{  i  N)r   r1   r   rX   rI   rR   )r   rm   ro   rp   rU   rZ   r   rq   r   
test_case5  s    





zTestRetargeting.test_case5N)r   r   r   rH   rI   r   rM   rR   r[   r]   r`   rc   re   rr   r   r   r   r   r:   |   s     
r:   )&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   r1   r   objectr   r!   r.   r-   r4   r5   ZTestCaser:   r   r   r   r   <module>   s,    ,
