a
    –§»a`,  ã                   @   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rþe  ¡  dS )é    N)Úcompile_isolatedÚFlags)ÚjitÚnjit)ÚtypesÚerrors)ÚTestCaseTc                   @   s   e Zd ZdS )ÚMyErrorN©Ú__name__Ú
__module__Ú__qualname__© r   r   úU/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/numba/tests/test_exceptions.pyr	      s   r	   c                   @   s   e Zd ZdS )Ú
OtherErrorNr
   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   ÚotherZ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                       s0   e Zd Z‡ fdd„Zdd„ Z‡ fdd„Z‡  ZS )Ú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                    s   ‡ fdd„}|S )Nc                    s0   | dkrˆ ‚n| dkrt ‚n| dkr,tjj‚| S ©Né   é   é   ©Ú
ValueErrorÚnpÚlinalgÚLinAlgError©Úi©Úexcr   r   Úraiser?   s    zraise_class.<locals>.raiserr   )r4   r5   r   r3   r   Úraise_class>   s    	r6   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 r(   r,   r1   ©r   r4   r   r   r5   L   s    zraise_instance.<locals>.raiserr   )r4   r   r5   r   r7   r   Úraise_instanceK   s    r8   c                    C   s   ‚ d S r   r   r   r   r   r   ÚreraiseW   s    r9   c                    s   ‡ fdd„}|S )Nc                    s   | dkrt ddƒ‚ˆ | ƒS )Nr+   Úbar)r   r1   ©Úinnerr   r   Úouter\   s    
zouter_function.<locals>.outerr   )r<   r=   r   r;   r   Úouter_function[   s    r>   c                 C   s   | dksJ dƒ‚d S )Nr)   r:   r   r1   r   r   r   Úassert_usecasec   s    r?   c                   C   s
   t ƒ ‚d S r   ©r   r   r   r   r   Úude_bug_usecaseg   s    rA   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|0    Y  |  t
|jƒd¡ d S )	Nc                 S   s
   | j | S r   )Úshape)Úar2   r   r   r   Úpyfuncn   s    z5TestRaising.test_unituple_index_error.<locals>.pyfuncr)   ÚAr*   )Zdtyper   ztuple index out of range)r   r   ÚArrayÚint32Úentry_pointr.   ÚemptyÚassertEqualÚassertRaisesÚ
IndexErrorÚstrÚ	exception)r   rE   ÚcresÚcfuncrD   Úcmr   r   r   Útest_unituple_index_errorm   s    ÿ(z%TestRaising.test_unituple_index_errorc                 G   sP  |t tfv sJ ‚|  |¡�}||Ž  W d   ƒ n1 s80    Y  |  |¡�}||Ž  W d   ƒ n1 sj0    Y  |  |jj|jj¡ t|jttfƒrª|  	|j|jk¡ |tu �rLz||Ž  W n, t
yì   tjt ¡ Ž }|dd … }	Y n0 z||Ž  W n. t
�y(   tjt ¡ Ž }
|
dd … }Y n0 t|	|ƒD ]\}}|  ||¡ �q4d S )Néþÿÿÿ)Úforce_pyobj_flagsÚno_pyobj_flagsrL   rK   rO   r   r   r   r&   Ú
assertTrueÚ	ExceptionÚ	tracebackÚformat_exceptionÚsysÚexc_infoÚzip)r   Z	exec_moderE   rQ   Zexpected_error_classr   ZpyerrZjiterrZ	py_framesZexpected_framesZc_framesZ
got_framesZexpfZgotfr   r   r   Úcheck_against_python}   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+   )r6   r	   r   r   rH   rI   rK   r^   r-   r.   r/   r0   ©r   r`   rE   rP   rQ   r   r   r   Úcheck_raise_class£   s    

ÿzTestRaising.check_raise_classc                 C   s   | j td� d S ©Nr_   )rb   rV   r#   r   r   r   Útest_raise_class_nopython­   s    z%TestRaising.test_raise_class_nopythonc                 C   s   | j td� d S rc   )rb   rU   r#   r   r   r   Útest_raise_class_objmode°   s    z$TestRaising.test_raise_class_objmodec                 C   s‚   t ttfD ]r}t|dƒ}t|tjf|d�}|j}|  |dƒd¡ |  	||||d¡ |  	|||t
d¡ |  	|||tjjjd¡ q
d S )Núsome messager_   r   r)   r*   r+   )r	   r   r&   r8   r   r   rH   rI   rK   r^   r-   r.   r/   r0   )r   r`   ÚclazzrE   rP   rQ   r   r   r   Úcheck_raise_instance³   s    ÿ


ÿz TestRaising.check_raise_instancec                 C   s   | j td� d S rc   )rh   rU   r#   r   r   r   Útest_raise_instance_objmodeÀ   s    z'TestRaising.test_raise_instance_objmodec                 C   s   | j td� d S rc   )rh   rV   r#   r   r   r   Útest_raise_instance_nopythonÃ   s    z(TestRaising.test_raise_instance_nopythonc                 K   s„   t ttfD ]t}t|dƒ}t|ƒ}tf i |¤Ž|ƒ}tf i |¤Žt|ƒƒ}|  ||||d¡ |  |||td¡ |  |||td¡ q
dS )zD
        Check exception propagation from nested functions.
        rf   r)   r*   r+   N)	r	   r   r&   r8   r>   r   r^   r-   r   )r   r`   Zjit_argsrg   Zinner_pyfuncrE   Zinner_cfuncrQ   r   r   r   Úcheck_raise_nestedÆ   s    ÿ
zTestRaising.check_raise_nestedc                 C   s   | j tdd� d S )NT)Zforceobj)rk   rU   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)rk   rV   r#   r   r   r   Útest_raise_nested_nopythonØ   s    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 ]6\‰‰ ‡ ‡fd	d
„}||ƒ}||ƒ}|  |||ˆ ¡ qNd S )Nc                 S   s   | ‚d S r   r   r3   r   r   r   Ú	raise_excÜ   s    z,TestRaising.check_reraise.<locals>.raise_excr   r_   c                      s   ˆ t ƒS r   )ÚZeroDivisionErrorr   ©rn   r   r   Ú<lambda>á   ó    z+TestRaising.check_reraise.<locals>.<lambda>c                      s   ˆ t ddƒƒS ©NÚmsgr)   r@   r   rp   r   r   rq   ã   rr   c                      s   ˆ t ddƒƒS rs   )r&   r   rp   r   r   rq   å   rr   c                    s   ‡‡ ‡fdd„}|S )Nc                      s(   z
ˆƒ  W n ˆ y"   ˆƒ  Y n0 d S r   r   r   )ÚerrÚfnÚopr   r   Úimplè   s    
z9TestRaising.check_reraise.<locals>.gen_impl.<locals>.implr   )rv   rx   )ru   rw   )rv   r   Úgen_implç   s    z+TestRaising.check_reraise.<locals>.gen_impl)r9   r   rI   ro   r   r&   r^   )r   r`   rE   rP   rQ   ry   ZpybasedZcbasedr   )ru   rw   rn   r   Úcheck_reraiseÛ   s$    
ÿ
ÿ
ÿüzTestRaising.check_reraisec                 C   s   | j td� d S rc   )rz   rU   r#   r   r   r   Útest_reraise_objmodeò   s    z TestRaising.test_reraise_objmodec                 C   s   | j td� d S rc   )rz   rV   r#   r   r   r   Útest_reraise_nopythonõ   s    z!TestRaising.test_reraise_nopythonc                 C   sh   t |ƒ}t|tjf|d�}|j}|  t¡�}|dƒ W d   ƒ n1 sH0    Y  |  t|j	ƒd¡ d S )Nr_   r)   z)exceptions must derive from BaseException)
r6   r   r   rH   rI   rL   Ú	TypeErrorrK   rN   rO   )r   Úclsr`   rE   rP   rQ   rR   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 )Nr_   r)   )r   ÚintrU   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 s40    Y  |  |t|jƒ¡ |   tj¡�}| jdtd� W d   ƒ n1 s€0    Y  |  |t|jƒ¡ d S )Nz8Encountered unsupported constant type used for exceptionr_   r)   )	rL   r   ÚUnsupportedErrorr   r€   rV   ÚassertInrN   rO   )r   rt   Ú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 s80    Y  |  |t|jƒ¡ d S )Nc                   S   s   d‚d S )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   rL   r   r‚   rƒ   rN   rO   )r   r‡   rt   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 )Nr_   r)   r*   )r?   r   r   rH   rI   r^   ÚAssertionErrorra   r   r   r   Úcheck_assert_statement  s
    z"TestRaising.check_assert_statementc                 C   s   | j td� d S rc   )rŠ   rU   r#   r   r   r   Útest_assert_statement_objmode  s    z)TestRaising.test_assert_statement_objmodec                 C   s   | j td� d S rc   )rŠ   rV   r#   r   r   r   Útest_assert_statement_nopython"  s    z*TestRaising.test_assert_statement_nopythonc           
      C   sr   d}d}|t f|tf|tffD ]N\}}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 != 1r4   Úfr_   r)   )	r‰   r   r&   Úexecr   r   rH   rI   r^   )
r   r`   Zsimple_raiseZassert_raiseZf_textr4   ÚlocrE   rP   rQ   r   r   r   Úcheck_raise_from_exec_string%  s    þz(TestRaising.check_raise_from_exec_stringc                 C   s   | j td� d S rc   )r�   rU   r#   r   r   r   Ú$test_assert_from_exec_string_objmode3  s    z0TestRaising.test_assert_from_exec_string_objmodec                 C   s   | j td� d S rc   )r�   rV   r#   r   r   r   Ú%test_assert_from_exec_string_nopython6  s    z1TestRaising.test_assert_from_exec_string_nopythonc                 C   s,   t }t|d|d�}|j}|  |||t¡ d S )Nr   r_   )rA   r   rI   r^   r}   ra   r   r   r   Úcheck_user_code_error_traceback9  s    z+TestRaising.check_user_code_error_tracebackc                 C   s   | j td� d S rc   )r“   rU   r#   r   r   r   Ú&test_user_code_error_traceback_objmodeB  s    z2TestRaising.test_user_code_error_traceback_objmodec                 C   s   | j td� d S rc   )r“   rV   r#   r   r   r   Ú'test_user_code_error_traceback_nopythonE  s    z3TestRaising.test_user_code_error_traceback_nopythonN)r   r   r   rS   r^   rb   rd   re   rh   ri   rj   rk   rl   rm   rz   r{   r|   r   r�   r…   rˆ   rŠ   r‹   rŒ   r�   r‘   r’   r“   r”   r•   r   r   r   r   rB   k   s6   &
		
	rB   Ú__main__)!Únumpyr.   r[   rY   Znumba.core.compilerr   r   Znumbar   r   Z
numba.corer   r   Znumba.tests.supportr   ZunittestrU   Zforce_pyobjectrV   rX   r	   r   r   r&   r6   r8   r9   r>   r?   rA   rB   r   Úmainr   r   r   r   Ú<module>   s0    _