B
     \d*                 @   s   d dl Zd dlZd dlZd dlmZmZ d dlmZm	Z	 d dlm
Z ddlmZmZ e Zed e ZG dd	 d	eZG d
d deZG dd deZG dd deZdd Zdd Zdd Zdd Zdd Zdd ZG dd deZedkre  dS )    N)compile_isolatedFlags)jittypes)unittest_support   )TestCasetagZforce_pyobjectc               @   s   e Zd ZdS )MyErrorN)__name__
__module____qualname__ r   r   :lib/python3.7/site-packages/numba/tests/test_exceptions.pyr
      s   r
   c               @   s   e Zd ZdS )
OtherErrorN)r   r   r   r   r   r   r   r      s   r   c                   s0   e Zd Z fddZdd Z fddZ  ZS )UDEArgsToSuperc                s   t t| | || _d S )N)superr   __init__value0)selfargr   )	__class__r   r   r      s    zUDEArgsToSuper.__init__c             C   s8   t || jsdS d}|| j|jkO }|| j|jkO }|S )NFT)
isinstancer   argsr   )r   othersamer   r   r   __eq__   s    zUDEArgsToSuper.__eq__c                s   t tt | jfS )N)hashr   r   __hash__r   )r   )r   r   r   r   #   s    zUDEArgsToSuper.__hash__)r   r   r   r   r   r   __classcell__r   r   )r   r   r      s   r   c                   s0   e Zd Z fddZdd Z fddZ  ZS )UDENoArgSuperc                s   t t|   || _|| _d S )N)r   r    r   deferargr   )r   r   r   )r   r   r   r   (   s    zUDENoArgSuper.__init__c             C   sH   t || jsdS d}|| j|jkO }|| j|jkO }|| j|jkO }|S )NFT)r   r   r   r!   r   )r   r   r   r   r   r   r   -   s    zUDENoArgSuper.__eq__c                s   t tt | j| jfS )N)r   r   r    r   r!   r   )r   )r   r   r   r   6   s    zUDENoArgSuper.__hash__)r   r   r   r   r   r   r   r   r   )r   r   r    '   s   	r    c                s    fdd}|S )Nc                s0   | dkr n| dkrt n| dkr,tjj| S )Nr         )
ValueErrornplinalgLinAlgError)i)excr   r   raiser<   s    zraise_class.<locals>.raiserr   )r)   r*   r   )r)   r   raise_class;   s    	r+   c                s    fdd}|S )Nc                sB   | dkr dn*| dkr(t  dn| dkr>tj d| S )Nr   r"   r#   )r$   r%   r&   r'   )r(   )r   r)   r   r   r*   H   s    zraise_instance.<locals>.raiserr   )r)   r   r*   r   )r   r)   r   raise_instanceG   s    r,   c                C   s    d S )Nr   r   r   r   r   reraiseR   s    r-   c                s    fdd}|S )Nc                s   | dkrt dd | S )Nr#   bar)r   )r(   )innerr   r   outerV   s    
zouter_function.<locals>.outerr   )r/   r0   r   )r/   r   outer_functionU   s    r1   c             C   s   | dkst dd S )Nr   r.   )AssertionError)r(   r   r   r   assert_usecase\   s    r3   c               C   s
   t  d S )N)r   r   r   r   r   ude_bug_usecase_   s    r4   c               @   s   e Zd Zdd Zdd Zdd Zeddd	 Zd
d Zdd Z	dd Z
eddd Zdd Zdd Zeddd Zdd Zdd Zed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d0d1 Zd2d3 Zd4d3 Zd5S )6TestRaisingc          	   C   s   dd }t |ttjddtjf}|j}tjdtjd}| ||d||d | t	}||d W d Q R X | t
|jd d S )	Nc             S   s
   | j | S )N)shape)ar(   r   r   r   pyfuncf   s    z5TestRaising.test_unituple_index_error.<locals>.pyfuncr   Ar"   )Zdtyper   ztuple index out of range)r   r   ZArrayint32entry_pointr%   emptyassertEqualassertRaises
IndexErrorstr	exception)r   r8   crescfuncr7   cmr   r   r   test_unituple_index_errore   s    
z%TestRaising.test_unituple_index_errorc          
   G   sL  |t tfkst| |}||  W d Q R X | |}||  W d Q R X | |jj|jj t|jtt	fr| 
|j|jk |tkrHy||  W n< tk
r } ztjt  }	|	dd  }
W d d }~X Y nX y||  W n> tk
r  } ztjt  }|dd  }W d d }~X Y nX x$t|
|D ]\}}| || q.W d S )N)force_pyobj_flagsno_pyobj_flagsr2   r>   r=   rA   r   r   r   r    Z
assertTrueBaseException	tracebackformat_exceptionsysexc_infozip)r   Z	exec_moder8   rC   Zexpected_error_classr   ZpyerrZjiterreZ	py_framesZexpected_framesZc_framesZ
got_framesZexpfZgotfr   r   r   check_against_pythonu   s*    
z TestRaising.check_against_pythonc             C   sp   t t}t|tjf|d}|j}| |dd | |||td | |||td | |||t	j
j
jd d S )N)flagsr   r   r"   r#   )r+   r
   r   r   r:   r;   r=   rP   r$   r%   r&   r'   )r   rQ   r8   rB   rC   r   r   r   check_raise_class   s    
zTestRaising.check_raise_classZ	importantc             C   s   | j td d S )N)rQ   )rR   rH   )r   r   r   r   test_raise_class_nopython   s    z%TestRaising.test_raise_class_nopythonc             C   s   | j td d S )N)rQ   )rR   rG   )r   r   r   r   test_raise_class_objmode   s    z$TestRaising.test_raise_class_objmodec             C   s   xt ttgD ]r}t|d}t|tjf|d}|j}| |dd | 	||||d | 	|||t
d | 	|||tjjjd qW d S )Nzsome message)rQ   r   r   r"   r#   )r
   r   r    r,   r   r   r:   r;   r=   rP   r$   r%   r&   r'   )r   rQ   clazzr8   rB   rC   r   r   r   check_raise_instance   s    


z TestRaising.check_raise_instancec             C   s   | j td d S )N)rQ   )rV   rG   )r   r   r   r   test_raise_instance_objmode   s    z'TestRaising.test_raise_instance_objmodec             C   s   | j td d S )N)rQ   )rV   rH   )r   r   r   r   test_raise_instance_nopython   s    z(TestRaising.test_raise_instance_nopythonc             K   s   xzt ttgD ]l}t|d}t|}tf ||}tf |t|}| ||||d | |||td | |||td qW dS )zD
        Check exception propagation from nested functions.
        zsome messager   r"   r#   N)	r
   r   r    r,   r1   r   rP   r$   r   )r   rQ   Zjit_argsrU   Zinner_pyfuncr8   Zinner_cfuncrC   r   r   r   check_raise_nested   s    

zTestRaising.check_raise_nestedc             C   s   | j tdd d S )NT)Zforceobj)rY   rG   )r   r   r   r   test_raise_nested_objmode   s    z%TestRaising.test_raise_nested_objmodec             C   s   | j tdd d S )NT)Znopython)rY   rH   )r   r   r   r   test_raise_nested_nopython   s    z&TestRaising.test_raise_nested_nopythonc                s   dd t }t|d|d}|j}xhfddtffddtffddtfgD ]6\  fd	d
}||}||}| |||  qPW d S )Nc             S   s   | d S )Nr   )r)   r   r   r   	raise_exc   s    z,TestRaising.check_reraise.<locals>.raise_excr   )rQ   c                  s    t S )N)ZeroDivisionErrorr   )r\   r   r   <lambda>   s    z+TestRaising.check_reraise.<locals>.<lambda>c                  s    t ddS )Nmsgr   )r   r   )r\   r   r   r^      s    c                  s    t ddS )Nr_   r   )r    r   )r\   r   r   r^      s    c                s    fdd}|S )Nc           
      s8   y
  W n(  k
r2 }  z
  W d d } ~ X Y nX d S )Nr   )rO   )errfnopr   r   impl   s    
z9TestRaising.check_reraise.<locals>.gen_impl.<locals>.implr   )ra   rc   )r`   rb   )ra   r   gen_impl   s    z+TestRaising.check_reraise.<locals>.gen_impl)r-   r   r;   r]   r   r    rP   )r   rQ   r8   rB   rC   rd   ZpybasedZcbasedr   )r`   rb   r\   r   check_reraise   s    

zTestRaising.check_reraisec             C   s   | j td d S )N)rQ   )re   rG   )r   r   r   r   test_reraise_objmode   s    z TestRaising.test_reraise_objmodec             C   s   | j td d S )N)rQ   )re   rH   )r   r   r   r   test_reraise_nopython   s    z!TestRaising.test_reraise_nopythonc          	   C   sT   t |}t|tjf|d}|j}| t}|d W d Q R X | t|j	d d S )N)rQ   r   z)exceptions must derive from BaseException)
r+   r   r   r:   r;   r>   	TypeErrorr=   r@   rA   )r   clsrQ   r8   rB   rC   rD   r   r   r   check_raise_invalid_class   s    z%TestRaising.check_raise_invalid_classc             C   s    | j ttd | j dtd d S )N)rQ   r   )rj   intrG   )r   r   r   r    test_raise_invalid_class_objmode   s    z,TestRaising.test_raise_invalid_class_objmodec          	   C   sH   |    | jttd W d Q R X |    | jdtd W d Q R X d S )N)rQ   r   )ZassertTypingErrorrj   rk   rH   )r   r   r   r   !test_raise_invalid_class_nopython  s    

z-TestRaising.test_raise_invalid_class_nopythonc             C   s:   t }t|tjf|d}|j}|d | |||td d S )N)rQ   r   r"   )r3   r   r   r:   r;   rP   r2   )r   rQ   r8   rB   rC   r   r   r   check_assert_statement  s
    z"TestRaising.check_assert_statementc             C   s   | j td d S )N)rQ   )rn   rG   )r   r   r   r   test_assert_statement_objmode  s    z)TestRaising.test_assert_statement_objmodec             C   s   | j td d S )N)rQ   )rn   rH   )r   r   r   r   test_assert_statement_nopython  s    z*TestRaising.test_assert_statement_nopythonc       
      C   sv   d}d}xh|t f|tf|tfgD ]N\}}i }t|d|i| |d }t|tjf|d}|j}	| |||	|d q W d S )Nz def f(a):
  raise exc('msg', 10)zdef f(a):
  assert a != 1r)   f)rQ   r   )	r2   r   r    execr   r   r:   r;   rP   )
r   rQ   Zsimple_raiseZassert_raiseZf_textr)   Zlocr8   rB   rC   r   r   r   check_raise_from_exec_string  s    z(TestRaising.check_raise_from_exec_stringc             C   s   | j td d S )N)rQ   )rs   rG   )r   r   r   r   $test_assert_from_exec_string_objmode#  s    z0TestRaising.test_assert_from_exec_string_objmodec             C   s   | j td d S )N)rQ   )rs   rH   )r   r   r   r   %test_assert_from_exec_string_nopython&  s    z1TestRaising.test_assert_from_exec_string_nopythonc             C   s,   t }t|d|d}|j}| |||t d S )Nr   )rQ   )r4   r   r;   rP   rh   )r   rQ   r8   rB   rC   r   r   r   check_user_code_error_traceback)  s    z+TestRaising.check_user_code_error_tracebackc             C   s   | j td d S )N)rQ   )rv   rG   )r   r   r   r   test_user_code_error_traceback2  s    z*TestRaising.test_user_code_error_tracebackc             C   s   | j td d S )N)rQ   )rv   rH   )r   r   r   r   rw   5  s    N)r   r   r   rE   rP   rR   r	   rS   rT   rV   rW   rX   rY   rZ   r[   re   rf   rg   rj   rl   rm   rn   ro   rp   rs   rt   ru   rv   rw   r   r   r   r   r5   c   s4   '
		r5   __main__) Znumpyr%   rL   rJ   Znumba.compilerr   r   Znumbar   r   r   ZunittestZsupportr   r	   rG   setrH   	Exceptionr
   r   r   r    r+   r,   r-   r1   r3   r4   r5   r   mainr   r   r   r   <module>   s.   
 V