o
    8ήc`,                     @   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mZ d dlmZ d dlZe Zde_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 dS )    N)compile_isolatedFlags)jitnjit)typeserrors)TestCaseTc                   @      e Zd ZdS )MyErrorN__name__
__module____qualname__ r   r   B/tmp/pip-target-vg8gfxp4/lib/python/numba/tests/test_exceptions.pyr
          r
   c                   @   r	   )
OtherErrorNr   r   r   r   r   r      r   r   c                       0   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 r   )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                       r   )UDENoArgSuperc                    s   t t|   || _|| _d S r   )r   r*   r   deferargr   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 r   )r   r   r    r+   r   r!   r   r   r   r$   0   s   zUDENoArgSuper.__eq__c                    s   t tt | j| jfS r   )r%   r   r*   r&   r+   r   r'   r   r   r   r&   9   s   zUDENoArgSuper.__hash__r(   r   r   r   r   r*   *   s    	r*   c                        fdd}|S )Nc                    s,   | dkr | dkrt | dkrtjj| S N         
ValueErrornplinalgLinAlgErroriexcr   r   raiser?   s   zraise_class.<locals>.raiserr   )r9   r:   r   r8   r   raise_class>   s   	r;   c                    s    fdd}|S )Nc                    s>   | dkr	 d| dkrt  d| dkrtj d| S r-   r1   r6   r   r9   r   r   r:   L   s   

zraise_instance.<locals>.raiserr   )r9   r   r:   r   r<   r   raise_instanceK   s   r=   c                    C   s    r   r   r   r   r   r   reraiseW   s   r>   c                    r,   )Nc                    s   | dkr	t dd | S )Nr0   bar)r   r6   innerr   r   outer\   s   
zouter_function.<locals>.outerr   )rA   rB   r   r@   r   outer_function[   s   rC   c                 C   s   | dksJ dd S )Nr.   r?   r   r6   r   r   r   assert_usecasec   s   rD   c                   C   s   t  r   r   r   r   r   r   ude_bug_usecaseg   s   rF   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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/d0 Zd1d2 Zd3d4 Zd5d6 Zd7S )8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    n1 s>w   Y  | t
|jd d S )	Nc                 S   s
   | j | S r   )shape)ar7   r   r   r   pyfuncn   s   
z5TestRaising.test_unituple_index_error.<locals>.pyfuncr.   Ar/   )dtyper   ztuple index out of range)r   r   Arrayint32entry_pointr3   emptyassertEqualassertRaises
IndexErrorstr	exception)r   rJ   crescfuncrI   cmr   r   r   test_unituple_index_errorm   s   z%TestRaising.test_unituple_index_errorc                 G   sN  |t tfv sJ | |}||  W d    n1 sw   Y  | |}||  W d    n1 s5w   Y  | |jj|jj t|jttfrU| 	|j|jk |tu rz||  W n t
yu   tjt  }|dd  }	Y nw z||  W n t
y   tjt  }
|
dd  }Y nw t|	|D ]\}}| || qd S d S )N)force_pyobj_flagsno_pyobj_flagsrR   rQ   rU   r    r   r   r*   
assertTrue	Exception	tracebackformat_exceptionsysexc_infozip)r   	exec_moderJ   rW   expected_error_classr    pyerrjiterr	py_framesexpected_framesc_frames
got_framesexpfgotfr   r   r   check_against_python}   s6   

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/   r0   )r;   r
   r   r   rN   rO   rQ   rn   r2   r3   r4   r5   r   rp   rJ   rV   rW   r   r   r   check_raise_class   s   

zTestRaising.check_raise_classc                 C      | j td d S Nro   )rr   r\   r'   r   r   r   test_raise_class_nopython      z%TestRaising.test_raise_class_nopythonc                 C   rs   rt   )rr   r[   r'   r   r   r   test_raise_class_objmode   rv   z$TestRaising.test_raise_class_objmodec                 C   s   t ttfD ]9}t|d}t|tjf|d}|j}| |dd | 	||||d | 	|||t
d | 	|||tjjjd qd S )Nsome messagero   r   r.   r/   r0   )r
   r   r*   r=   r   r   rN   rO   rQ   rn   r2   r3   r4   r5   )r   rp   clazzrJ   rV   rW   r   r   r   check_raise_instance   s   


z TestRaising.check_raise_instancec                 C   rs   rt   )rz   r[   r'   r   r   r   test_raise_instance_objmode   rv   z'TestRaising.test_raise_instance_objmodec                 C   rs   rt   )rz   r\   r'   r   r   r   test_raise_instance_nopython   rv   z(TestRaising.test_raise_instance_nopythonc                 K   s   t ttfD ]:}t|d}t|}tdi ||}tdi |t|}| ||||d | |||td | |||td qdS )zD
        Check exception propagation from nested functions.
        rx   r.   r/   r0   Nr   )	r
   r   r*   r=   rC   r   rn   r2   r   )r   rp   jit_argsry   inner_pyfuncrJ   inner_cfuncrW   r   r   r   check_raise_nested   s   
zTestRaising.check_raise_nestedc                 C      | j tdd d S )NT)forceobj)r   r[   r'   r   r   r   test_raise_nested_objmode      z%TestRaising.test_raise_nested_objmodec                 C   r   )NT)nopython)r   r\   r'   r   r   r   test_raise_nested_nopython   r   z&TestRaising.test_raise_nested_nopythonc                    s   dd t }t|d|d}|j}fddtffddtffddtffD ]\  fd	d
}||}||}| |||  q'd S )Nc                 S   s   | r   r   r8   r   r   r   	raise_exc   s   z,TestRaising.check_reraise.<locals>.raise_excr   ro   c                      s    t S r   )ZeroDivisionErrorr   r   r   r   <lambda>   s    z+TestRaising.check_reraise.<locals>.<lambda>c                          t ddS Nmsgr.   rE   r   r   r   r   r          c                      r   r   )r*   r   r   r   r   r      r   c                    s    fdd}|S )Nc                      s(   z  W d S   y     Y d S w r   r   r   )errfnopr   r   impl   s
   z9TestRaising.check_reraise.<locals>.gen_impl.<locals>.implr   )r   r   )r   r   )r   r   gen_impl   s   z+TestRaising.check_reraise.<locals>.gen_impl)r>   r   rO   r   r   r*   rn   )r   rp   rJ   rV   rW   r   pybasedcbasedr   )r   r   r   r   check_reraise   s&   


zTestRaising.check_reraisec                 C   rs   rt   )r   r[   r'   r   r   r   test_reraise_objmode   rv   z TestRaising.test_reraise_objmodec                 C   rs   rt   )r   r\   r'   r   r   r   test_reraise_nopython   rv   z!TestRaising.test_reraise_nopythonc                 C   sh   t |}t|tjf|d}|j}| t}|d W d    n1 s$w   Y  | t|j	d d S )Nro   r.   z)exceptions must derive from BaseException)
r;   r   r   rN   rO   rR   	TypeErrorrQ   rT   rU   )r   clsrp   rJ   rV   rW   rX   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 )Nro   r.   )r   intr[   r'   r   r   r    test_raise_invalid_class_objmode  s   z,TestRaising.test_raise_invalid_class_objmodec                 C   s   d}|  tj}| jttd W d    n1 sw   Y  | |t|j |  tj}| jdtd W d    n1 s@w   Y  | |t|j d S )Nz8Encountered unsupported constant type used for exceptionro   r.   )	rR   r   UnsupportedErrorr   r   r\   assertInrT   rU   )r   r   raisesr   r   r   !test_raise_invalid_class_nopython  s   z-TestRaising.test_raise_invalid_class_nopythonc                 C   sX   t dd }d}| tj}|  W d    n1 sw   Y  | |t|j d S )Nc                   S   s   d)Nillegalr   r   r   r   r   foo  s   z8TestRaising.test_raise_bare_string_nopython.<locals>.foozCDirectly raising a string constant as an exception is not supported)r   rR   r   r   r   rT   rU   )r   r   r   r   r   r   r   test_raise_bare_string_nopython  s   
z+TestRaising.test_raise_bare_string_nopythonc                 C   s:   t }t|tjf|d}|j}|d | |||td d S )Nro   r.   r/   )rD   r   r   rN   rO   rn   AssertionErrorrq   r   r   r   check_assert_statement  s
   z"TestRaising.check_assert_statementc                 C   rs   rt   )r   r[   r'   r   r   r   test_assert_statement_objmode  rv   z)TestRaising.test_assert_statement_objmodec                 C   rs   rt   )r   r\   r'   r   r   r   test_assert_statement_nopython"  rv   z*TestRaising.test_assert_statement_nopythonc           
      C   sr   d}d}|t f|tf|tffD ]'\}}i }t|d|i| |d }t|tjf|d}|j}	| |||	|d qd S )Nz def f(a):
  raise exc('msg', 10)zdef f(a):
  assert a != 1r9   fro   r.   )	r   r   r*   execr   r   rN   rO   rn   )
r   rp   simple_raiseassert_raisef_textr9   locrJ   rV   rW   r   r   r   check_raise_from_exec_string%  s   z(TestRaising.check_raise_from_exec_stringc                 C   rs   rt   )r   r[   r'   r   r   r   $test_assert_from_exec_string_objmode3  rv   z0TestRaising.test_assert_from_exec_string_objmodec                 C   rs   rt   )r   r\   r'   r   r   r   %test_assert_from_exec_string_nopython6  rv   z1TestRaising.test_assert_from_exec_string_nopythonc                 C   s,   t }t|d|d}|j}| |||t d S )Nr   ro   )rF   r   rO   rn   r   rq   r   r   r   check_user_code_error_traceback9  s   z+TestRaising.check_user_code_error_tracebackc                 C   rs   rt   )r   r[   r'   r   r   r   &test_user_code_error_traceback_objmodeB  rv   z2TestRaising.test_user_code_error_traceback_objmodec                 C   rs   rt   )r   r\   r'   r   r   r   'test_user_code_error_traceback_nopythonE  rv   z3TestRaising.test_user_code_error_traceback_nopythonN)r   r   r   rY   rn   rr   ru   rw   rz   r{   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rG   k   s8    &
		
	rG   __main__)!numpyr3   ra   r_   numba.core.compilerr   r   numbar   r   
numba.corer   r   numba.tests.supportr   unittestr[   force_pyobjectr\   r^   r
   r   r   r*   r;   r=   r>   rC   rD   rF   rG   r   mainr   r   r   r   <module>   s4     _