U
    ,dM                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	Z
d dlmZmZmZmZmZ d dlmZmZ d dlmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZmZ d dl m!Z" d dl#Z#e"d Z$zd dl%Z%W n e&k
r   dZ%Y nX zd dl'Z'W n e&k
r.   dZ'Y nX e( dkZ)dd Z*dd Z+dd Z,dDddZ-dEddZ.dFddZ/dGddZ0dHd d!Z1G d"d# d#eZ2G d$d% d%e2Z3G d&d' d'e2Z4G d(d) d)e4Z5G d*d+ d+eZ6G d,d- d-eZ7G d.d/ d/eZ8G d0d1 d1e#jZ9G d2d3 d3e#jZ:edId5d6Z;edJd7d8Z<ed9d: Z=ee=fd;d<Z>d=d> Z?G d?d@ d@ee#jZ@G dAdB dBe#jZAeBdCkre#C  dS )K    N)chain)StringIO)njitjitgenerated_jittypeof	vectorize)typeserrors)_dispatcher)compile_isolated)TestCasecaptured_stdout)as_dtype)
Dispatcher)needs_lapackSerialMixin)_TIMEOUTg      N@armv7lc                 C   s   | S N xr   r   ?/tmp/pip-unpacked-wheel-eu7e0c37/numba/tests/test_dispatcher.pydummy'   s    r   c                 C   s   | | S r   r   r   yr   r   r   add+   s    r   c                 C   s   | | | S r   r   r   r   zr   r   r   addsub/   s    r          c                 C   s   | | | S r   r   r   r   r   r   addsub_defaults3   s    r#   c                 G   s
   | ||fS r   r   r   r   r   r   star_defaults7   s    r$      c                 C   s"   t | tjrdd }ndd }|S )Nc                 S   s   | | S r   r   r   r   r   r   impl=   s    zgenerated_usecase.<locals>.implc                 S   s   | | S r   r   r   r   r   r   r&   @   s    
isinstancer	   Complexr   r   r&   r   r   r   generated_usecase;   s    
r+   c                 C   s$   t | tjrdd }n
ddd}|S )Nc                 S   s   | S r   r   r   r   r   r   r&   G   s    z#bad_generated_usecase.<locals>.impl   c                 S   s   | | S r   r   r   r   r   r   r&   J   s    )r,   r'   r*   r   r   r   bad_generated_usecaseE   s    

r-   c                    sn   t |tjjtjjfr0tjdd | |fD   n,t |tjtjfrLt	| nt
dt| d fdd	}|S )Nc                 s   s   | ]}t |jjV  qd S r   )npdtypename).0Zaryr   r   r   	<genexpr>Q   s   z*dtype_generated_usecase.<locals>.<genexpr>zUnhandled Type %sc                    s   t j| j dS Nr/   )r.   onesshape)abr/   Z	out_dtyper   r   _fnX   s    z$dtype_generated_usecase.<locals>._fn)N)r(   r	   miscZNoneTypeZOmittedr.   Zresult_typeZDTypeZNumberClassr   	TypeErrortype)r7   r8   r/   r:   r   r9   r   dtype_generated_usecaseO   s    


r>   c                   @   s   e Zd ZeddZdd ZdS )BaseTestTZnopythonc                    s(    fdd}t f j  |fS )Nc                     s$   | |} | |} || d S r   )assertPreciseEqual)argskwargsexpectedresultfpyfuncselfr   r   checkc   s    

z$BaseTest.compile_func.<locals>.check)r   jit_args)rI   rH   rJ   r   rF   r   compile_funcb   s    zBaseTest.compile_funcN)__name__
__module____qualname__dictrK   rL   r   r   r   r   r?   ^   s   
r?   c                   @   s   e 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d Zdd Zeeeddd Zeedd d! Zeeedd"d# Zd$d% Zd&d' Zd(S ))TestDispatcherc                 C   sL   t dd }t dd }| ||kd | ||kd | |d kd d S )Nc                 S   s   | S r   r   r   r   r   r   foon   s    z)TestDispatcher.test_equality.<locals>.fooc                 S   s   | S r   r   r   r   r   r   barr   s    z)TestDispatcher.test_equality.<locals>.barTFr   assertEqual)rI   rR   rS   r   r   r   test_equalitym   s    

zTestDispatcher.test_equalityc                 C   s8   t dd }|d |j \}| |jj|jj d S )Nc                 S   s   | S r   r   r   r   r   r   rR   ~   s    z+TestDispatcher.test_dyn_pyfunc.<locals>.foo   )r   	overloadsvaluesrU   entry_pointrN   py_func)rI   rR   crr   r   r   test_dyn_pyfunc}   s
    
zTestDispatcher.test_dyn_pyfuncc                 C   s   t dd }|  d S )Nc                   S   s   dS NrW   r   r   r   r   r   rR      s    z,TestDispatcher.test_no_argument.<locals>.foor   rI   rR   r   r   r   test_no_argument   s    
zTestDispatcher.test_no_argumentc              	   C   s   t ddt}| |ddtdd | |ddtdd | |ddtdd | |ddtdd t d	ddt}| |ddtdd | |ddtd
d | t |dd W 5 Q R X d S )NTr@   {   i  g(@gF@y        F@l    F: z(i4, i4)   -   )r   r   rA   assertRaisesr<   )rI   Zc_addr   r   r   test_coerce_input_types   s    z&TestDispatcher.test_coerce_input_typesc                 C   s   t dd }d}d}| |||||  | t|jd | |||||  | t|jd | |||||  | t|jd | |dd||  | t|jdd d	S )
z8Test compiling new version in an ambiguous case
        c                 S   s   | | S r   r   r7   r8   r   r   r   rR      s    z6TestDispatcher.test_ambiguous_new_version.<locals>.foorW         ?r!   r"      zdidn't compile a new versionN)r   ZassertAlmostEqualrU   lenrX   )rI   rR   INTZFLTr   r   r   test_ambiguous_new_version   s    
z)TestDispatcher.test_ambiguous_new_versionc                    sh   g  t dd  fddfddtdD }|D ]}|  q:|D ]}|  qL  dS )	zz
        Test that (lazy) compiling from several threads at once doesn't
        produce errors (see issue #908).
        c                 S   s   | d S r^   r   r   r   r   r   rR      s    z%TestDispatcher.test_lock.<locals>.fooc               
      sF   z dd W n, tk
r@ }  z |  W 5 d } ~ X Y nX d S NrW   r!   )rU   	Exceptionappend)e)r
   rR   rI   r   r   wrapper   s    z)TestDispatcher.test_lock.<locals>.wrapperc                    s   g | ]}t j d qS ))target)	threadingThreadr1   irq   r   r   
<listcomp>   s     z,TestDispatcher.test_lock.<locals>.<listcomp>   N)r   rangestartjoinZassertFalse)rI   threadstr   )r
   rR   rI   rq   r   	test_lock   s    


zTestDispatcher.test_lockc              	   C   sb  t dt}| |ddd | t|jd|j t ddgt}| |ddd | |ddd | |tddd	 | t	}|d
d
 W 5 Q R X | 
dt|j | t|jd|j t ddgt}| |tdtdd | |ddd t dddgt}| t	}|dd W 5 Q R X | t|jd | dt|j d S )Nz(int64,int64)rh         @r"   rW   z(float64,float64)r!   g      @      @              ?zNo matching definitionz(float32,float32)g      `>      ?g     ?z(float32,float64)z(float64,float32)       @zAmbiguous overloading for <function add [^>]*> \(float64, float64\):\n\(float32, float64\) -> float64\n\(float64, float32\) -> float64Zint64)r   r   rA   rU   rj   rX   r.   int32re   r<   assertInstr	exceptionfloat32assertRegexpMatchesZassertNotInrI   rG   cmr   r   r   test_explicit_signatures   s6    z'TestDispatcher.test_explicit_signaturesc              	   C   s   d}|  t}tdt W 5 Q R X | |d t|j |  t}tdt W 5 Q R X | |d t|j |  t}tdt W 5 Q R X | |d t|j tddd	t |  t}tddd	t W 5 Q R X | |d t|j d S )
NzKSignature mismatch: %d argument types given, but function takes 2 argumentsz()r   z(intc,)rW   z(intc,intc,intc)r"   TZforceobj)re   r<   r   r   r   r   r   )rI   tmplr   r   r   r   test_signature_mismatch   s    z&TestDispatcher.test_signature_mismatchc              	   C   sB   t dt}| t}|dd W 5 Q R X | t|jd d S )Nz(intc,intc)r   zBNo matching definition for argument type(s) complex128, complex128)r   r   re   r<   rU   r   r   r   r   r   r   test_matching_error_message  s    z*TestDispatcher.test_matching_error_messagec              	   C   sf   t dd }|d |  | t}|d W 5 Q R X | t|jd | t|j	d d S )Nc                 S   s   | S r   r   r7   r   r   r   rR     s    z5TestDispatcher.test_disabled_compilation.<locals>.foo
(float32,)(int32,)compilation disabledrW   )
r   compileZdisable_compilere   RuntimeErrorrU   r   r   rj   
signaturesrI   rR   raisesr   r   r   test_disabled_compilation  s    

z(TestDispatcher.test_disabled_compilationc              	   C   s\   t ddgdd }| t}|d W 5 Q R X | t|jd | t|jd d S )Nr   r   c                 S   s   | S r   r   r   r   r   r   rR     s    zBTestDispatcher.test_disabled_compilation_through_list.<locals>.fooz(complex64,)r   r!   )	r   re   r   r   rU   r   r   rj   r   r   r   r   r   &test_disabled_compilation_through_list  s    

z5TestDispatcher.test_disabled_compilation_through_listc              	      sX   t dgdd  t  fdd}| t}|  W 5 Q R X d}| t|j| d S )Nz(intp,)c                 S   s   | S r   r   r   r   r   r   rR   %  s    zATestDispatcher.test_disabled_compilation_nested_call.<locals>.fooc                      s    d  t d d S r^   )r.   r5   r   rR   r   r   rS   )  s    zATestDispatcher.test_disabled_compilation_nested_call.<locals>.barzANo matching definition for argument type(s) array(float64, 1d, C))r   re   r<   rU   r   r   )rI   rS   r   mr   r   r   %test_disabled_compilation_nested_call$  s    
z4TestDispatcher.test_disabled_compilation_nested_callc              	      s   t dd  d}| t}tg  W 5 Q R X | |t|j |  g g  t dd j	}| t}|g  W 5 Q R X | |t|j t  fdd}| | g  |j
 \}| t|jd d	S )
z
        Failure in computing the fingerprint cannot affect a nopython=False
        function.  On the other hand, with nopython=True, a ValueError should
        be raised to report the failure with fingerprint.
        c                 S   s   | S r   r   r   r   r   r   rR   9  s    z4TestDispatcher.test_fingerprint_failure.<locals>.fooz(cannot compute fingerprint of empty listTr@   c                     s$   t   g } tdD ]} | } q| S )N
   )objectrz   )r   rv   r   r   r   rS   K  s
    
z4TestDispatcher.test_fingerprint_failure.<locals>.barrW   N)r   re   
ValueErrorr   Zcompute_fingerprintr   r   r   rU   r[   rX   rY   rj   Zlifted)rI   errmsgr   Z
strict_foorS   r\   r   r   r   test_fingerprint_failure3  s     
z'TestDispatcher.test_fingerprint_failurec           	      C   s   t dddd }| |dd tj}tj  t|}t|}| |d t| t	|}| |d t| | 
|| t|}~~| |d t| t	|}| |t| t|}~tj  | |t| t	| | 
| d dS )z:
        Test serialization of Dispatcher objects
        Tr@   c                 S   s   | d S r^   r   r   r   r   r   rR   \  s    z.TestDispatcher.test_serialization.<locals>.foorW   r!   N)r   rU   r   Z_memoZ_recentclearrj   pickledumpsloadsZassertIsidweakrefref)	rI   rR   memoZ	memo_sizeZserialized_fooZfoo_rebuiltZid_origZnew_foor   r   r   r   test_serializationX  s.    







z!TestDispatcher.test_serializationzUnaligned loads unsupportedc           	         s   dd  t dd dtttj}t j| j }tj| d |d}|d d tj	}|dd  tj	}|j
}|j
} fd	d
}|d| |d| |d| |d| d S )Nc                 S   s   t j| dS r^   r.   ZlinalgZmatrix_powerr   r   r   r   rR     s    z:TestDispatcher.test_misaligned_array_dispatch.<locals>.fooTr@   @   rW   r4   c                    sJ   t jt jd|d d d d f<  |}|}t j|| d S r3   r.   arange
complex128reshapetestingassert_allcloser0   r7   rD   gotrR   jitfoonrr   r   rJ     s    (z<TestDispatcher.test_misaligned_array_dispatch.<locals>.checkC_contig_alignedF_contig_alignedC_contig_misalignedF_contig_misalignedr   intr.   sqrtint8r   itemsizer   viewr   T	rI   dtcounttmpr   r   r   r   rJ   r   r   r   test_misaligned_array_dispatch  s     


z-TestDispatcher.test_misaligned_array_dispatchc           	         s   dd  t dd dtttj}t j| j }tj| d |d}|d d tj	}|dd  tj	}|j
}|j
}d fd
d	}|d|dd |d|dd |d|dd |d|dd d S )Nc                 S   s
   t | S r   )r.   sumr   r   r   r   rR     s    z?TestDispatcher.test_immutability_in_array_dispatch.<locals>.fooTr@   r   rW   r4   r   Fc                    sV   t jt jd|d d d d f< |r4d|j_ |}|}t j|| d S )Nr4   F)r.   r   r   r   flagsZ	writeabler   r   )r0   r7   disable_write_bitrD   r   r   r   r   rJ     s    (zATestDispatcher.test_immutability_in_array_dispatch.<locals>.checkr   )r   r   r   r   )Fr   r   r   r   r   #test_immutability_in_array_dispatch  s(    
z2TestDispatcher.test_immutability_in_array_dispatchc           
         s  dd  t dd fdd}ddtj}t j| j }tj| d	 |d
}|d d tj}||dd |d	d  tj}||dd |j}||dd |j}||dd  fdd}	|	d| |	d| |	d| |	d| d S )Nc              
   S   s&   t j| ddddd d d d f dS Nr   rW   r   r   r   r   r   rR     s    zITestDispatcher.test_misaligned_high_dimension_array_dispatch.<locals>.fooTr@   c                    sD     | jj| |dkr(  | jjd |dkr@  | jjd d S )NCTF)rU   r   alignedc_contiguousf_contiguous)ZarrZlayoutr   rI   r   r   check_properties  s
    zVTestDispatcher.test_misaligned_high_dimension_array_dispatch.<locals>.check_propertiesi  r"   rW   r4   r   r   Fr   c                    sR   t jt jd|d d d d f<  |}|}t j|| d S r3   r   r   r   r   r   rJ     s         zKTestDispatcher.test_misaligned_high_dimension_array_dispatch.<locals>.checkr   r   r   r   )	r   r.   r   r   r   r   r   r   r   )
rI   r   r   r   r   r   r   r   r   rJ   r   )rR   r   r   r   rI   r   -test_misaligned_high_dimension_array_dispatch  sF              	


z<TestDispatcher.test_misaligned_high_dimension_array_dispatchc                 C   s   dd }t dd|}|td |td |td | t|jd tjftjftjfg}| |j| t tjfgdd|}|td |td |td | t|jd	 tjfg}| |j| d S )
Nc                 S   s   | S r   r   r   r   r   r   rR     s    z@TestDispatcher.test_dispatch_recompiles_for_scalars.<locals>.fooTr@   y      ?       @r   Fr"   rW   )	r   r.   r   r   Zbool_rU   rj   r   r	   )rI   rR   r   Zexpected_sigsr   r   r   $test_dispatch_recompiles_for_scalars	  s    
z3TestDispatcher.test_dispatch_recompiles_for_scalarsc              	   C   s   t dddd }| t}t | W 5 Q R X t|j}| d| | d| | d| | t}t t W 5 Q R X t|j}| d| | tt | d S )	NTr@   c                 S   s   | S r   r   r   r   r   r   rR   (  s    zITestDispatcher.test_dispatcher_raises_for_invalid_decoration.<locals>.fooz8A jit decorator was called on an already jitted functionrR   z.py_funcz&The decorated object is not a function)r   re   r<   r   r   r   r?   r=   )rI   rR   r   err_msgr   r   r   -test_dispatcher_raises_for_invalid_decoration%  s     

 
z<TestDispatcher.test_dispatcher_raises_for_invalid_decorationN)rM   rN   rO   rV   r]   ra   rf   rl   r   r   r   r   r   r   r   r   r   r   unittestskipIf
_is_armv7lr   r   r   r   r   r   r   r   r   rQ   k   s0   
&
%-
&

(
1rQ   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	TestSignatureHandlingz<
    Test support for various parameter passing styles.
    c              	   C   s   |  t\}}|dddd |ddd |dddd | t|jd | t}|ddddd	 W 5 Q R X | d
t|j	 | t}|  W 5 Q R X | dt|j	 | t}|dddd W 5 Q R X | dt|j	 dS )z?
        Test passing named arguments to a dispatcher.
        r"   r   ri   r   r   r   rW   r,      r   r   %too many arguments: expected 3, got 4z'not enough arguments: expected 3, got 0r   zmissing argument 'z'N)
rL   r    rU   rj   rX   re   r<   r   r   r   rI   rG   rJ   r   r   r   r   test_named_args@  s$    z%TestSignatureHandling.test_named_argsc              	   C   s  |  t\}}|dddd |ddd |dddd |ddd |dd |ddd |d |dd | t}|ddd	d
d W 5 Q R X | dt|j | t}|  W 5 Q R X | dt|j | t}|d	d
d W 5 Q R X | dt|j dS )z?
        Test omitting arguments with a default value.
        r"   r   ri   r   r   r   r   r   r,   r   r   r   z0not enough arguments: expected at least 1, got 0zmissing argument 'x'N)rL   r#   re   r<   r   r   r   r   r   r   r   test_default_argsW  s,    

z'TestSignatureHandling.test_default_argsc              	   C   s  |  t\}}|d |dd |ddd |dddd |ddddd |dd |ddd |ddd | t}|dddd W 5 Q R X | d	t|j | t}|dddd
 W 5 Q R X | d	t|j | t}|ddd W 5 Q R X | d	t|j dS )zJ
        Test a compiled function with starargs in the signature.
        ri   r%   r,   r      r   r   r   z!some keyword arguments unexpectedr   N)rL   r$   re   r<   r   r   r   r   r   r   r   test_star_argsr  s$    

z$TestSignatureHandling.test_star_argsN)rM   rN   rO   __doc__r   r   r   r   r   r   r   r   ;  s   r   c                   @   s   e Zd ZdZeddZdS )TestSignatureHandlingObjectModez<
    Sams as TestSignatureHandling, but in object mode.
    Tr   N)rM   rN   rO   r   rP   rK   r   r   r   r   r     s   r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	TestGeneratedDispatcherz#
    Tests for @generated_jit.
    c                 C   s~   t ddt}| |dd | |ddd | |dddd | |dd	 | |dd
d | |dddd d S )NTr@   r   r"   r   ri   r   r   y      @      ?*   y      E@      ?r   y      @      ?)r   r+   rU   )rI   rG   r   r   r   test_generated  s    z&TestGeneratedDispatcher.test_generatedc                 C   s   t ddt}tjdtjd}tjdtjd}| |||jtj | |||tddjtj | |||tjdjtj d S )NTr@   )r   r4   r   )	r   r>   r.   r5   r   Zfloat64rU   r/   r   )rI   rG   r7   r8   r   r   r   test_generated_dtype  s     z,TestGeneratedDispatcher.test_generated_dtypec              	   C   sr   t ddt}| t}|d W 5 Q R X | dt|j | t}|d W 5 Q R X | dt|j dS )zz
        Check error reporting when implementation signature doesn't match
        generating function signature.
        Tr@   r   zGshould be compatible with signature '(x, y=5)', but has signature '(x)'rW   zLshould be compatible with signature '(x, y=5)', but has signature '(x, y=6)'N)r   r-   re   r<   r   r   r   )rI   rG   r   r   r   r   test_signature_errors  s    z-TestGeneratedDispatcher.test_signature_errorsN)rM   rN   rO   r   r   r   r   r   r   r   r   r     s   	r   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zdd
dZdd Zdd Z	dd Z
dd Zeedkdeedkddd Zdd Zdd ZdS )TestDispatcherMethodsc                    s   d t  fdd}| |dd | |dd | t|jd d | |dd |  | t|jd | |dd | |dd d S )	NrW   c                    s   |   S r   r   r   closurer   r   rR     s    z1TestDispatcherMethods.test_recompile.<locals>.foor!   rh   r   r"   r   )r   rA   rU   rj   r   	recompiler`   r   r   r   test_recompile  s    z$TestDispatcherMethods.test_recompilec                    sx   d t d fdd}| |dd | |dd d | |dd |  | |dd | |dd d S )NrW   zint32(int32)c                    s   |   S r   r   r   r   r   r   rR     s    z<TestDispatcherMethods.test_recompile_signatures.<locals>.foor!   rh   r"   )r   rA   r   r`   r   r   r   test_recompile_signatures  s    z/TestDispatcherMethods.test_recompile_signaturesc                 C   sx   t dd }|dd |dd |dd | }| t|d | D ](}| d| | d| | d| qJd S )	Nc                 S   s   | | S r   r   explicit_arg1explicit_arg2r   r   r   rR     s    z4TestDispatcherMethods.test_inspect_llvm.<locals>.foorW   r   r"   rR   r   r   )r   Zinspect_llvmrU   rj   rY   r   )rI   rR   ZllvmsZllvm_bcr   r   r   test_inspect_llvm  s    



z'TestDispatcherMethods.test_inspect_llvmc                 C   sb   t dd }|dd |dd |dd | }| t|d | D ]}| d|k qJd S )Nc                 S   s   | | S r   r   r   r   r   r   rR     s    z3TestDispatcherMethods.test_inspect_asm.<locals>.foorW   r   r"   rR   )r   Zinspect_asmrU   rj   rY   
assertTrue)rI   rR   ZasmsZasmr   r   r   test_inspect_asm  s    



z&TestDispatcherMethods.test_inspect_asm c                 C   s^   |rd t||}tddd }t|}d |||}| t|| | t|j d S )Nz{}{}.rW   r   z^digraph "CFG for \'_ZN{}{}{})	formatrj   rM   splitr   r   r   callableZdisplay)rI   cfgrq   module_nameZ
module_lenprefixr   r   r   _check_cfg_display  s    z(TestDispatcherMethods._check_cfg_displayc           
   	   C   s   t dd }td}td}td}|| || || | }| t|d | \}}}| t|||gttdd |||g |	 D ]}	| 
|	 q| tt|	 d |j|jd	 d
}	| 
|	 d S )Nc                 S   s   |   S r   r   Z	the_arrayr   r   r   rR     s    z3TestDispatcherMethods.test_inspect_cfg.<locals>.foorW   rW   rW   rW   rW   rW   r"   c                 S   s
   t | fS r   )r   r   r   r   r   <lambda>2      z8TestDispatcherMethods.test_inspect_cfg.<locals>.<lambda>r   	signature)r   r.   r5   inspect_cfgrU   rj   keyssetmaprY   r  listr   )
rI   rR   a1a2a3Zcfgss1s2Zs3r  r   r   r   test_inspect_cfg  s&    



z&TestDispatcherMethods.test_inspect_cfgc                 C   sh   t dd }td}td}td}|| || || |j|jd dd}| j|d	d
 d S )Nc                 S   s   |   S r   r  r	  r   r   r   rR   @  s    zGTestDispatcherMethods.test_inspect_cfg_with_python_wrapper.<locals>.foorW   r
  r  r   python)r  Zshow_wrappercpythonrw   )r   r.   r5   r  r   r  )rI   rR   r  r  r  r  r   r   r   $test_inspect_cfg_with_python_wrapper<  s    



z:TestDispatcherMethods.test_inspect_cfg_with_python_wrapperc              	   C   sf   t dd }|dd |t  t|j|jd  j}t }|  W 5 Q R X || ksbt	d S )Nc                 S   s   | | S r   r   rg   r   r   r   rR   R  s    z5TestDispatcherMethods.test_inspect_types.<locals>.foorW   r!   r   )
r   inspect_typesr   r   rX   r   Ztype_annotationr   getvalueAssertionError)rI   rR   rD   outr   r   r   test_inspect_typesQ  s    

z(TestDispatcherMethods.test_inspect_typesc              	   C   s   t dd }|d |d t }|  W 5 Q R X t }|j|jd d W 5 Q R X t }|j|jd d W 5 Q R X | | | |   d S )Nc                 S   s   | d S r^   r   r   r   r   r   rR   a  s    zDTestDispatcherMethods.test_inspect_types_with_signature.<locals>.foorW   r   r   r  )r   r   r  r   rU   r  )rI   rR   totalfirstsecondr   r   r   !test_inspect_types_with_signature`  s    
z7TestDispatcherMethods.test_inspect_types_with_signatureNz#please install the 'jinja2' packagez%please install the 'pygments' packagec              	   C   s   t dd }|dd t  |jdd}W 5 Q R X |j D ]4\}}d}|d D ]}d	|d krRd}qR| | q>| t}|jt dd
 W 5 Q R X | 	dt
|j d S )Nc                 S   s   | | S r   r   rg   r   r   r   rR   v  s    z<TestDispatcherMethods.test_inspect_types_pretty.<locals>.foorW   r!   T)prettyFZpygments_linesspan)filer'  z$`file` must be None if `pretty=True`)r   r   r  annitemsr   re   r   r   r   r   r   )rI   rR   r*  kvZ
span_foundliner   r   r   r   test_inspect_types_prettys  s     

z/TestDispatcherMethods.test_inspect_types_prettyc                    sR   t dd   d  d tt fdd jD }  }| || d S )Nc                 S   s   | d S r^   r   r   r   r   r   rR     s    z;TestDispatcherMethods.test_get_annotation_info.<locals>.foorW   g?c                 3   s   | ]}  | V  qd S r   )get_annotation_infor+  ru   r   r   r   r2     s   zATestDispatcherMethods.test_get_annotation_info.<locals>.<genexpr>)r   rP   r   from_iterabler   r0  rU   )rI   rD   rE   r   r   r   test_get_annotation_info  s    

z.TestDispatcherMethods.test_get_annotation_infoc           
      C   s   dd }t |}tdgg}tjddgddggdd}tjddgddggd	d}||d
d}||d
d}| d||dd ||d
d}||d
d}	| || | ||	 dS )aO  
        This test an issue with the dispatcher when an array that is both
        C and F contiguous is supplied as the first signature.
        The dispatcher checks for F contiguous first but the compiler checks
        for C contiguous first. This results in an C contiguous code inserted
        as F contiguous function.
        c                 S   s   | ||f S r   r   )Arv   jr   r   r   rH     s    zKTestDispatcherMethods.test_issue_with_array_layout_conflict.<locals>.pyfuncr   g        r   g      @r   )orderr   rW   r   N)r   r.   arrayrU   )
rI   rH   ZcfuncZary_c_and_fZary_cZary_fZexp_cZexp_fZgot_cZgot_fr   r   r   %test_issue_with_array_layout_conflict  s    z;TestDispatcherMethods.test_issue_with_array_layout_conflict)r   )rM   rN   rO   r   r   r   r   r  r  r  r"  r&  r   r   jinja2pygmentsr/  r2  r7  r   r   r   r   r     s   
"r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
 TestDispatcherFunctionBoundariesc                    s   t dddd  t dddd t dd fdd}d	d
tdg}dd |D }t||D ]\}}| ||| qdt||D ]\}}|  || qd S )NTr@   c                 S   s   | d S r^   r   r   r   r   r   add1  s    zJTestDispatcherFunctionBoundaries.test_pass_dispatcher_as_arg.<locals>.add1c                 S   s   | |S r   r   )fnr   r   r   r   rS     s    zITestDispatcherFunctionBoundaries.test_pass_dispatcher_as_arg.<locals>.barc                    s
    | S r   r   r   r;  rS   r   r   rR     s    zITestDispatcherFunctionBoundaries.test_pass_dispatcher_as_arg.<locals>.foorW   g333333&@r   c                 S   s   g | ]}|d  qS )rW   r   )r1   r   r   r   r   rx     s     zPTestDispatcherFunctionBoundaries.test_pass_dispatcher_as_arg.<locals>.<listcomp>)r   r.   r   ziprA   )rI   rR   inputsZexpected_resultsargexpectr   r=  r   test_pass_dispatcher_as_arg  s    

z<TestDispatcherFunctionBoundaries.test_pass_dispatcher_as_argc                 C   s   t dddd }|ddddgt d	d
 d}| |d |tttdtdd d d t dd
 d}| |d |tttdtdd d d t dd
 d}| |d d S )NTr@   c                 S   s4   | d }| dd  D ]}|||}|dk r|}q|S r   r   )seqcmpfnr   ZeachZcmpvalr   r   r   maximum  s    
zPTestDispatcherFunctionBoundaries.test_dispatcher_as_arg_usecase.<locals>.maximumrW   r!   r"   ri   c                 S   s   | | S r   r   r   r   r   r   r    r  zQTestDispatcherFunctionBoundaries.test_dispatcher_as_arg_usecase.<locals>.<lambda>)rD  r%   r   c                 S   s   | d |d  S Nr   r   r   r   r   r   r    r  )ri   r   c                 S   s   | d |d  S r^   r   r   r   r   r   r    r  )r   ri   )r   rU   r  r>  rz   )rI   rE  r   r   r   r   test_dispatcher_as_arg_usecase  s    
 
 
z?TestDispatcherFunctionBoundaries.test_dispatcher_as_arg_usecasec                 C   s2   t dddd }t dd }| ||| d S )NTr@   c                 S   s   | S r   r   )r<  r   r   r   rR     s    zRTestDispatcherFunctionBoundaries.test_dispatcher_can_return_to_python.<locals>.fooc                 S   s   | S r   r   r   r   r   r   r    r  zWTestDispatcherFunctionBoundaries.test_dispatcher_can_return_to_python.<locals>.<lambda>rT   )rI   rR   r<  r   r   r   $test_dispatcher_can_return_to_python  s    
zETestDispatcherFunctionBoundaries.test_dispatcher_can_return_to_pythonc                    s   t dddd  t dd fdd}t dd fdd}t ddd	d
 }| | ||fdd | |   gdd d S )NTr@   c                 S   s   | d S r^   r   r   r   r   r   one  s    zMTestDispatcherFunctionBoundaries.test_dispatcher_in_sequence_arg.<locals>.onec                    s     | S r   r   r   rI  r   r   two  s    zMTestDispatcherFunctionBoundaries.test_dispatcher_in_sequence_arg.<locals>.twoc                    s      | S r   r   r   rJ  r   r   three  s    zOTestDispatcherFunctionBoundaries.test_dispatcher_in_sequence_arg.<locals>.threec                 S   s"   | d || d || d |fS )Nr   rW   r!   r   )fnsr   r   r   r   choose  s    zPTestDispatcherFunctionBoundaries.test_dispatcher_in_sequence_arg.<locals>.chooserW   )r!   r"   ri   )r!   r!   r!   rT   )rI   rK  rL  rN  r   rJ  r   test_dispatcher_in_sequence_arg  s    

z@TestDispatcherFunctionBoundaries.test_dispatcher_in_sequence_argN)rM   rN   rO   rB  rG  rH  rO  r   r   r   r   r:    s   	r:  c                   @   s   e Zd Zdd Zdd ZdS )TestBoxingDefaultErrorc              	   C   sP   dd }t |tdf}| t}|d  W 5 Q R X | t|jd d S )Nc                 S   s   d S r   r   r   r   r   r   rR   	  s    z<TestBoxingDefaultError.test_unbox_runtime_error.<locals>.fooZ
dummy_typezcan't unbox dummy_type type)	r   r	   ZDummyre   r<   rZ   rU   r   r   )rI   rR   cresr   r   r   r   test_unbox_runtime_error  s
    z/TestBoxingDefaultError.test_unbox_runtime_errorc              	   C   sJ   dd }t |d}| t}|  W 5 Q R X d}| t|j| d S )Nc                   S   s   t S r   )r   r   r   r   r   rR     s    z:TestBoxingDefaultError.test_box_runtime_error.<locals>.foor   z/cannot convert native Module.* to Python object)r   re   r<   rZ   r   r   r   )rI   rR   rQ  r   patr   r   r   test_box_runtime_error  s    
z-TestBoxingDefaultError.test_box_runtime_errorN)rM   rN   rO   rR  rT  r   r   r   r   rP    s   rP  c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )TestNoRetryFailedSignaturez?Test that failed-to-compile signatures are not recompiled.
    c              	   C   s   |j }| t|jd | tj |d W 5 Q R X | t|jd | tj |d W 5 Q R X | t|jd | tj |d W 5 Q R X | t|jd d S )Nr   rW   r   r!   )Z	_compilerrU   rj   Z_failed_cachere   r
   TypingError)rI   funcZfcomr   r   r   run_test"  s    z#TestNoRetryFailedSignature.run_testc                 C   s    t dddd }| | d S )NTr@   c                 S   s   | d S rF  r   r   r   r   r   rR   3  s    z8TestNoRetryFailedSignature.test_direct_call.<locals>.foor   rX  r`   r   r   r   test_direct_call2  s    
z+TestNoRetryFailedSignature.test_direct_callc                    sN   t dddd  t dd fddt dd fdd}| | d S )	NTr@   c                 S   s   | d S rF  r   r   r   r   r   rS   :  s    z8TestNoRetryFailedSignature.test_nested_call.<locals>.barc                    s    |  d S r   r   r   )rS   r   r   foobar>  s    z;TestNoRetryFailedSignature.test_nested_call.<locals>.foobarc                    s    | |  S r   r   r   rS   r[  r   r   rR   B  s    z8TestNoRetryFailedSignature.test_nested_call.<locals>.foorY  r`   r   r\  r   test_nested_call9  s    
z+TestNoRetryFailedSignature.test_nested_callc                    s<    fdd}|dd}|dd}  |d   |d d S )Nc              	      s   d}ddit fdd}tdddd	 }|f fd
d	  |f| }td}rtj}||d W 5 Q R X dt|j	 n
||d d S )Nr   cr   c                    s&    d  d7  < rt ddd S )Nr^  rW   invoke_failedc                 S   s   | S r   r   r   r   r   r   r  W  r  z]TestNoRetryFailedSignature.test_error_count.<locals>.check.<locals>.trigger.<locals>.<lambda>)r
   rV  r   )counter
would_failr   r   triggerQ  s    
zKTestNoRetryFailedSignature.test_error_count.<locals>.check.<locals>.triggerTr@   c                 S   s   d S r   r   r!  r   r   r   r   identY  s    zITestNoRetryFailedSignature.test_error_count.<locals>.check.<locals>.identc                    sD   | d | d d  }t dd fdd}|r<||S |S d S )Nr   Tr@   c                    s"    | | | d  |7  < d S rF  r   rc  innerZtab_headr   r   assign`  s    
z`TestNoRetryFailedSignature.test_error_count.<locals>.check.<locals>.chain_assign.<locals>.assignr_   )fsrf  Ztab_tailrg  )chain_assignre  r   ri  ]  s    
zPTestNoRetryFailedSignature.test_error_count.<locals>.check.<locals>.chain_assignr!   rW   r_  )
r   r   r.   r5   re   r
   rV  r   r   r   )fieldra  r,  rb  rd  r   r!  r   r   )ri  r`  ra  r   rJ   I  s    


z:TestNoRetryFailedSignature.test_error_count.<locals>.checkr7   Fr^  TrW   )rU   )rI   rJ   Zct_okZct_badr   r   r   test_error_countH  s
    -

z+TestNoRetryFailedSignature.test_error_countN)rM   rN   rO   r   rX  rZ  r]  rk  r   r   r   r   rU    s
   rU  rW   c                 C   s   | | S r   r   r   r   r   r   add_y1~  s    rl  c                 C   s   | |r
dnd S rm   r   r   r   r   r   	add_ynone  s    rm  c                 C   s   | | S r   r   r   r   r   r   mult  s    rn  c                 C   s   | || |  S r   r   )r   rW  r   r   r   add_func  s    ro  c                 C   s   | ||  |kstd S r   )r[   r   )f1r@  r   r   r   _checker  s    rq  c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
$TestMultiprocessingDefaultParametersc                 C   sh   zt d}W n tk
r&   t }Y nX dD ]6}|jt||fd}|  |t | |j	d q,d S )Nspawn)rW   r!   r"   )rr   rB   r   )
multiprocessingZget_contextAttributeErrorProcessrq  r{   r|   _TEST_TIMEOUTrU   exitcode)rI   Zfcctxr7   pr   r   r   run_fc_multiproc  s    

z5TestMultiprocessingDefaultParameters.run_fc_multiprocc                 C   s   |  t dS )z Tests issue #4888N)r{  rl  r   r   r   r   test_int_def_param  s    z7TestMultiprocessingDefaultParameters.test_int_def_paramc                 C   s   |  t dS )z" Tests None as a default parameterNr{  ro  r   r   r   r   test_none_def_param  s    z8TestMultiprocessingDefaultParameters.test_none_def_paramc                 C   s   |  t dS )z( Tests a function as a default parameterNr}  r   r   r   r   test_function_def_param  s    z<TestMultiprocessingDefaultParameters.test_function_def_paramN)rM   rN   rO   r{  r|  r~  r  r   r   r   r   rr    s   rr  c                   @   s   e Zd ZdZdd ZdS )TestVectorizeDifferentTargetszDTest that vectorize can be reapplied if the target is different
    c                 C   s<   t dd }tg d dd}|| tg d dd}|| d S )Nc                 S   s   | | S r   r   r   r   r   r   r     s    z?TestVectorizeDifferentTargets.test_cpu_vs_parallel.<locals>.addcpu)identityrr   parallel)r   r   )rI   r   Zcustom_vectorizeZcustom_vectorize_2r   r   r   test_cpu_vs_parallel  s    
z2TestVectorizeDifferentTargets.test_cpu_vs_parallelN)rM   rN   rO   r   r  r   r   r   r   r    s   r  __main__)r!   r"   )r!   )r%   )r%   )N)rW   )N)Drt  platformrs   r   r   	itertoolsr   ior   Znumpyr.   Znumbar   r   r   r   r   Z
numba.corer	   r
   r   Znumba.core.compilerr   Znumba.tests.supportr   r   Znumba.np.numpy_supportr   Znumba.core.dispatcherr   r   r   Znumba.testing.mainr   Z_RUNNER_TIMEOUTr   rw  r8  ImportErrorr9  machiner   r   r   r    r#   r$   r+   r-   r>   r?   rQ   r   r   r   r   r:  rP  rU  rl  rm  rn  ro  rq  rr  r  rM   mainr   r   r   r   <module>   sz   








   SO* }M`
"
