o
    1&i@                     @   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	m
Z
 d dlmZmZmZmZ d dlZdddZdddZdddZdddd	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dd Z d d! Z!G d"d# d#eZ"e#d$kre$  dS dS )%    N)jitnjit)typeserrorsutils)TestCaseexpected_failure_py311expected_failure_py312expected_failure_py313FT)nopythonforceobj)r   _nrt)r   Znogilr   c                   @      e Zd ZdS )MyErrorN__name__
__module____qualname__ r   r   FC:\wamp64\www\opt\env\Lib\site-packages\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   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                       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(   3   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*   <   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   raiserB   s   zraise_class.<locals>.raiserr   r=   r>   r   r<   r   raise_classA   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 r1   r5   r:   r    r=   r   r   r>   O      

zraise_instance.<locals>.raiserr   )r=   r    r>   r   rA   r   raise_instanceN   s   rC   c                    r0   )Nc                    s>   | dkr	 |d| dkrt |d| dkrtj|d| S r1   r5   )r;   r    r<   r   r   r>   [   rB   z+raise_instance_runtime_args.<locals>.raiserr   r?   r   r<   r   raise_instance_runtime_argsZ   s   rD   c                    C   s    r   r   r   r   r   r   reraisef   s   rE   c                    r0   )Nc                    s   | dkr	t dd | S )Nr4   bar)r   r:   innerr   r   outerk   s   
zouter_function.<locals>.outerr   )rH   rI   r   rG   r   outer_functionj   s   rJ   c                 C   s   | dksJ dd S )Nr2   rF   r   r:   r   r   r   assert_usecaser      rK   c                   C   s   t  r   r   r   r   r   r   ude_bug_usecasev   s   rN   c                 C      t | r   r6   )r    r   r   r   raise_runtime_valuez   s   rQ   c                   @   sX  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d7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)e*e+e,dOdP Z-dQS )RTestRaisingc                 C   s   dd }t ttjddtjf|}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)ar;   r   r   r   pyfunc      
z5TestRaising.test_unituple_index_error.<locals>.pyfuncr2   Ar3   )Zdtyper   ztuple index out of range)r   r   Arrayint32r7   emptyassertEqualassertRaises
IndexErrorstr	exception)r   rU   cfuncrT   cmr   r   r   test_unituple_index_error   s   z%TestRaising.test_unituple_index_errorc                 G   sR  |t tttfv s
J | |}||  W d    n1 sw   Y  | |}||  W d    n1 s7w   Y  | |jj|jj t|jt	t
frW| |j|jk |tu rz||  W n tyw   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_flagsno_pyobj_flags_w_nrtno_gil_flagsr\   r[   r_   r%   r$   r   r.   
assertTrue	Exception	tracebackformat_exceptionsysexc_infozipassertIn)r   Z	exec_moderU   r`   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fi ||}| |dd | |||td | |||td | |||tj	j	j
d d S )Nr   r2   r3   r4   )r@   r   r   r   rY   r[   rp   r6   r7   r8   r9   r   flagsrU   r`   r   r   r   check_raise_class   s   

zTestRaising.check_raise_classc                 C      | j td d S Nrr   )rs   re   r+   r   r   r   test_raise_class_nopython      z%TestRaising.test_raise_class_nopythonc                 C   rt   ru   )rs   rd   r+   r   r   r   test_raise_class_objmode   rx   z$TestRaising.test_raise_class_objmodec                 C   s   t ttfD ]9}t|d}ttjffi ||}| |dd | ||||d | |||t	d | |||t
jjjd qd S )Nsome messager   r2   r3   r4   )r   r   r.   rC   r   r   rY   r[   rp   r6   r7   r8   r9   r   rr   clazzrU   r`   r   r   r   check_raise_instance   s   


z TestRaising.check_raise_instancec                 C   rt   ru   )r}   rd   r+   r   r   r   test_raise_instance_objmode   rx   z'TestRaising.test_raise_instance_objmodec                 C   rt   ru   )r}   re   r+   r   r   r   test_raise_instance_nopython   rx   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.
        rz   r2   r3   r4   Nr   )	r   r   r.   rC   rJ   r   rp   r6   r   )r   rr   Zjit_argsr|   Zinner_pyfuncrU   Zinner_cfuncr`   r   r   r   check_raise_nested   s   
zTestRaising.check_raise_nestedc                 C      | j tdd d S )NT)r   )r   rd   r+   r   r   r   test_raise_nested_objmode      z%TestRaising.test_raise_nested_objmodec                 C   r   )NT)r   )r   re   r+   r   r   r   test_raise_nested_nopython   r   z&TestRaising.test_raise_nested_nopythonc                    s   dd t }td
i ||}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   r<   r   r   r   	raise_exc   s   z,TestRaising.check_reraise.<locals>.raise_excr   c                      s    t S r   )ZeroDivisionErrorr   r   r   r   <lambda>   s    z+TestRaising.check_reraise.<locals>.<lambda>c                          t ddS Nmsgr2   rM   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   )rE   r   r   r   r.   rp   )r   rr   rU   r`   r   ZpybasedZcbasedr   )r   r   r   r   check_reraise   s$   


zTestRaising.check_reraisec                 C   rt   ru   )r   rd   r+   r   r   r   test_reraise_objmode  rx   z TestRaising.test_reraise_objmodec                 C   rt   ru   )r   re   r+   r   r   r   test_reraise_nopython  rx   z!TestRaising.test_reraise_nopythonc                 C   sh   t |}ttjffi ||}| t}|d W d    n1 s$w   Y  | t|jd d S )Nr2   z)exceptions must derive from BaseException)	r@   r   r   rY   r\   	TypeErrorr[   r^   r_   )r   clsrr   rU   r`   ra   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 )Nrv   r2   )r   intrd   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 exceptionrv   r2   )	r\   r   UnsupportedErrorr   r   re   ro   r^   r_   )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   r\   r   r   ro   r^   r_   )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fi ||}|d | |||td d S )Nr2   r3   )rK   r   r   rY   rp   AssertionErrorrq   r   r   r   check_assert_statement)  s   z"TestRaising.check_assert_statementc                 C   rt   ru   )r   rd   r+   r   r   r   test_assert_statement_objmode/  rx   z)TestRaising.test_assert_statement_objmodec                 C   rt   ru   )r   re   r+   r   r   r   test_assert_statement_nopython2  rx   z*TestRaising.test_assert_statement_nopythonc                 C   s   d}d}d}d}|t f|tf|tfg}tjdkr&||t f ||t f |D ]'\}}i }	t|d|i|	 |	d }
ttj	ffi ||
}| 
||
||d q(d S )	Nz def f(a):
  raise exc('msg', 10)zdef f(a):
  assert a != 1z)def f[T: int](a: T) -> T:
  assert a != 1z*def f[T: int
](a: T) -> T:
  assert a != 1)r4      r=   fr2   )r   r   r.   r   Z	PYVERSIONappendexecr   r   rY   rp   )r   rr   Zsimple_raiseZassert_raiseZpy312_pep695_raiseZpy312_pep695_raise_2Z
test_casesZf_textr=   locrU   r`   r   r   r   check_raise_from_exec_string5  s$   
z(TestRaising.check_raise_from_exec_stringc                 C   rt   ru   )r   rd   r+   r   r   r   $test_assert_from_exec_string_objmodeK  rx   z0TestRaising.test_assert_from_exec_string_objmodec                 C   rt   ru   )r   re   r+   r   r   r   %test_assert_from_exec_string_nopythonN  rx   z1TestRaising.test_assert_from_exec_string_nopythonc                 C   s*   t }tdi ||}| |||t d S )Nr   r   )rN   r   rp   r   rq   r   r   r   check_user_code_error_tracebackQ  s   z+TestRaising.check_user_code_error_tracebackc                 C   rt   ru   )r   rd   r+   r   r   r   &test_user_code_error_traceback_objmodeY  rx   z2TestRaising.test_user_code_error_traceback_objmodec                 C   rt   ru   )r   re   r+   r   r   r   'test_user_code_error_traceback_nopython\  rx   z3TestRaising.test_user_code_error_traceback_nopythonc                 C   s2   t }ttjffi ||}| |||td d S )Nhello)rQ   r   r   stringrp   r6   rq   r   r   r   check_raise_runtime_value_  s   z%TestRaising.check_raise_runtime_valuec                 C   rt   ru   )r   rd   r+   r   r   r    test_raise_runtime_value_objmoded  rx   z,TestRaising.test_raise_runtime_value_objmodec                 C   rt   ru   )r   rf   r+   r   r   r   !test_raise_runtime_value_nopythong  rx   z-TestRaising.test_raise_runtime_value_nopythonc                 C   rt   ru   )r   rg   r+   r   r   r   test_raise_runtime_value_nogilj  rx   z*TestRaising.test_raise_runtime_value_nogilc              	   C   s   t ttfD ]>}t|}ttjtjffi ||}| |ddd | 	||||dd | 	|||t
dd | 	|||tjjjdd qd S )	Nr   testr2   r   r3   Zworldr4   r8   )r   r   r.   rD   r   r   rY   r   r[   rp   r6   r7   r8   r9   r{   r   r   r   &check_raise_instance_with_runtime_argsm  s   
z2TestRaising.check_raise_instance_with_runtime_argsc                 C   rt   ru   )r   rd   r+   r   r   r   -test_raise_instance_with_runtime_args_objmodez  rx   z9TestRaising.test_raise_instance_with_runtime_args_objmodec                 C   rt   ru   )r   rf   r+   r   r   r   .test_raise_instance_with_runtime_args_nopython}  rx   z:TestRaising.test_raise_instance_with_runtime_args_nopythonc                 C   rt   ru   )r   rg   r+   r   r   r   +test_raise_instance_with_runtime_args_nogil  rx   z7TestRaising.test_raise_instance_with_runtime_args_nogilc              	   C   s   dd }dd }dd }|df|df|t jffg}|D ]#\}}d}| tj| t|| W d    n1 s9w   Y  qd S )	Nc                   S   s   t dtdd)Nr2      rT   b)r6   r7   onesr   r   r   r   raise_literal_dict  rL   zCTestRaising.test_dynamic_raise_bad_args.<locals>.raise_literal_dictc                   S   s   t td)Nr4   )r6   ranger   r   r   r   raise_range  s   z<TestRaising.test_dynamic_raise_bad_args.<locals>.raise_rangec                 S   s
   t | jr   )r6   Zbit_generator)rngr   r   r   	raise_rng  rV   z:TestRaising.test_dynamic_raise_bad_args.<locals>.raise_rngr   z/.*Cannot convert native .* to a Python object.*)r   Znpy_rngassertRaisesRegexr   ZTypingErrorr   )r   r   r   r   funcsrU   argtypesr   r   r   r   test_dynamic_raise_bad_args  s   
z'TestRaising.test_dynamic_raise_bad_argsc                 C   sH   t dd }d}| t| |  W d    d S 1 sw   Y  d S )Nc                   S   s   t ddd)Nr2   r4   r   rP   r   r   r   r   raise_literal_dict2  s   z@TestRaising.test_dynamic_raise_dict.<locals>.raise_literal_dict2z{a: 1, b: 3})r   r   r6   )r   r   r   r   r   r   test_dynamic_raise_dict  s   
"z#TestRaising.test_dynamic_raise_dictc                 C   sR   t dddd }d}| tj| |d W d    d S 1 s"w   Y  d S )NF)r   c                 S   rO   r   rP   r:   r   r   r   raise_with_no_nrt     z7TestRaising.test_disable_nrt.<locals>.raise_with_no_nrtzNRT required but not enabled{   )r   r   r   ZNumbaRuntimeError)r   r   r   r   r   r   test_disable_nrt  s   

"zTestRaising.test_disable_nrtc                    s6   t dd  t  fdd}| |d|d d S )Nc                 S   rO   r   rP   rT   r   r   r   raise_  r   z*TestRaising.test_try_raise.<locals>.raise_c                    s,   z	 |  W | d S  t y   Y | d S w )Nr2   )ri   r   r   r   r   	try_raise  s   
z-TestRaising.test_try_raise.<locals>.try_raiser4   )r   r[   Zpy_func)r   r   r   r   r   test_try_raise  s
   
zTestRaising.test_try_raisec              
      s   t dd  t  fdd}dddtddd	gd
tdd	gg} |fD ])}|D ]$}| t}|| W d    n1 s>w   Y  | |f|jj q(q$d S )Nc                 S   rO   r   rP   r   r   r   r   r     r   z.TestRaising.test_dynamic_raise.<locals>.raise_c                    s&   z |  W d S  t y   t| w r   )ri   r6   r   r   r   r   
try_raise_  s
   z2TestRaising.test_dynamic_raise.<locals>.try_raise_r2   g?r   r4   r3   )r2   r3   )	r   r7   r   setr\   r6   r[   r_   r%   )r   r   r%   r   r    er   r   r   test_dynamic_raise  s(   

	
zTestRaising.test_dynamic_raiseN).r   r   r   rb   rp   rs   rw   ry   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   r   r   r   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   rR   ~   sX    *		
		rR   __main__)%numpyr7   rl   rj   Znumbar   r   Z
numba.corer   r   r   Znumba.tests.supportr   r   r	   r
   Zunittestrd   re   rf   rg   ri   r   r   r   r.   r@   rC   rD   rE   rJ   rK   rN   rQ   rR   r   mainr   r   r   r   <module>   s:    


  ^