o
    8ήc]                     @   s  d dl Z d dlZd dlmZ d dlZd dlmZmZm	Z	m
Z
 d dlmZ d dlmZmZ d dlmZmZmZmZ d dlmZmZmZmZmZm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 G dd deeZ!G dd deZ"eG dd deZ#e$dkre%  dS dS )    N)product)njittypedobjmodeprange)	PYVERSION)ir_utilsir)UnsupportedErrorCompilerErrorNumbaPerformanceWarningTypingError)TestCaseunittestcaptured_stdoutMemoryLeakMixinskip_parfors_unsupportedskip_unless_scipyc                   @   s   e Zd ZdS )MyErrorN)__name__
__module____qualname__ r   r   B/tmp/pip-target-vg8gfxp4/lib/python/numba/tests/test_try_except.pyr      s    r   c                   @   s   e Zd 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edd Zdd Zdd Zdd Zd S )!TestTryBareExceptzhTest the following pattern:

        try:
            <body>
        except:
            <handling>
    c                    s@   t dd  t  fdd}| |dd | |dd d S )	Nc                 S   s   | rt d S Nr   xr   r   r   inner    s   z5TestTryBareExcept.test_try_inner_raise.<locals>.innerc                    s   z |  W dS    Y dS )N
not raisedcaughtr   r   r   r   r   udt%   s
   z3TestTryBareExcept.test_try_inner_raise.<locals>.udtFr    Tr!   r   assertEqualselfr#   r   r"   r   test_try_inner_raise   s   
z&TestTryBareExcept.test_try_inner_raisec                    s   t dd  t  fdd}| t}|d W d    n1 s"w   Y  | t|jd | |dd | |d	d
 d S )Nc                 S   s$   | dkrt d| dkrt dd S )N   one   twor   r   r   r   r   r   1   s
   z5TestTryBareExcept.test_try_state_reset.<locals>.innerc                    s2   z |  d}W n   d}Y | dkr d |S )Nr    r!   r   r+   r   )r   resr"   r   r   r#   8   s   z3TestTryBareExcept.test_try_state_reset.<locals>.udtr   r,   r)   r!   r    )r   assertRaisesr   r%   str	exceptionr'   r#   raisesr   r"   r   test_try_state_reset0   s   


z&TestTryBareExcept.test_try_state_resetc                 C   s   t dd }|S )Nc                 S   sT   | dkrt d td| dkrt d td| dkr$t d td	t d
 d S )Nr)   call_oner*   r+   call_twor,      
call_threethree
call_otherprintr   r   r   r   r   r   J   s   z-TestTryBareExcept._multi_inner.<locals>.inner)r   )r'   r   r   r   r   _multi_innerI   s   
zTestTryBareExcept._multi_innerc              	      s6  |    t fdd}| t!}t }|ddd W d    n1 s&w   Y  W d    n1 s5w   Y  | |  g d | t|j	d t }|ddd W d    n1 scw   Y  | |  g d	 t }|ddd W d    n1 sw   Y  | |  g d
 d S )Nc                    sn   z$zt d  |  t d W W d S    t d  | t d Y W d S    t d  | t d Y d S )NABCDEF)r<   )r   yzr"   r   r   r#   ]   s   z.TestTryBareExcept.test_nested_try.<locals>.udtr)   r+   r7   )r>   r5   r@   r6   rB   r8   r9   r   )r>   r5   r@   r:   rA   )r>   r5   r@   r6   rB   r:   rC   )
r=   r   r/   r   r   r%   getvaluesplitr0   r1   )r'   r#   r3   stdoutr   r"   r   test_nested_tryZ   s8   


z!TestTryBareExcept.test_nested_tryc                    s  |    t fdd}t }|dd}W d    n1 sw   Y  | |  g d | |d t }|dd}W d    n1 sIw   Y  | |  g d | |d t }|dd}W d    n1 ssw   Y  | |  g d	 | |d
 d S )Nc                    sJ   zt d t|D ]}t | || kr | q	W |S    t d Y |S )Nr>   r?   )r<   range)r   nir"   r   r   r#      s   
z/TestTryBareExcept.test_loop_in_try.<locals>.udtr7      )r>   0123r8   r?   r)   )r>   rN   rO   r5   r?   r   )r>   rN   r:   rO   rP   r+   )r=   r   r   r%   rF   rG   r'   r#   rH   r-   r   r"   r   test_loop_in_try   s6   


z"TestTryBareExcept.test_loop_in_tryc                 C   s   t dd }t }|d}W d    n1 sw   Y  | |  ddg | |d t }|d}W d    n1 sAw   Y  | |  ddg | |d	 d S )
Nc                 S   s8   zt d | rtdt d W dS    t d Y dS )Nr>   my_errorr?   r@   A  {   r;   r   r   r   r   r#      s   
z0TestTryBareExcept.test_raise_in_try.<locals>.udtTr>   r@   rU   Fr?   rV   r   r   r%   rF   rG   rR   r   r   r   test_raise_in_try   s$   




z#TestTryBareExcept.test_raise_in_tryc                    sb   t  fdd t } d}W d    n1 sw   Y  | | | |  dg d S )Nc                    sB   | dkrz | d  W n
   t d Y dS | dkrtdd S )Nr   r)   CAUGHT   r1   )r<   
ValueErrorr   foor   r   r]      s   z-TestTryBareExcept.test_recursion.<locals>.foo
   rY   )r   r   assertIsNoner%   rF   rG   )r'   rH   r-   r   r\   r   test_recursion   s   


z TestTryBareExcept.test_recursionc                    sn   t dd  t  fdd}t }|d}W d    n1 s w   Y  | |d | |  g  d S )Nc                 s   s:    | dkrzdV  t d ty   td Y d S w d S )Nr      r1   rY   )r[   	Exceptionr<   r   r   r   r   r]      s   z)TestTryBareExcept.test_yield.<locals>.fooc                    s   t  | S r   )nextrE   r\   r   r   bar   s   z)TestTryBareExcept.test_yield.<locals>.barr^   ra   rW   )r'   re   rH   r-   r   r\   r   
test_yield   s   

zTestTryBareExcept.test_yieldc                 C   sT   t dd }t }|d W d    n1 sw   Y  | |  dg d S )Nc                 S   s   dd }|  d S )Nc                   S   s   zt d   td Y dS )Nr1   rY   rZ   r[   r<   r   r   r   r   re      s
   z9TestTryBareExcept.test_closure2.<locals>.foo.<locals>.barr   r   re   r   r   r   r]      s   
z,TestTryBareExcept.test_closure2.<locals>.foor^   rY   rW   r'   r]   rH   r   r   r   test_closure2   s   
	
zTestTryBareExcept.test_closure2c                 C   sh   t dd }t }|d}W d    n1 sw   Y  | |g d | |  dgd  d S )Nc                 S   s    dd }dd t |g dD S )Nc                 S   s    zt d   td |  Y S )Nr1   rY   rg   rd   r   r   r   re     s
   z9TestTryBareExcept.test_closure3.<locals>.foo.<locals>.barc                 S      g | ]}|qS r   r   .0r   r   r   r   
<listcomp>      z@TestTryBareExcept.test_closure3.<locals>.foo.<locals>.<listcomp>r)   r+   r7   )maprh   r   r   r   r]   	  s   z,TestTryBareExcept.test_closure3.<locals>.foor^   rp   rY   r7   rW   r'   r]   rH   r-   r   r   r   test_closure3  s   
	
zTestTryBareExcept.test_closure3c                 C   s   t dd }t }|d}W d    n1 sw   Y  | |  d | | t }|d}W d    n1 s>w   Y  | | d | |g d d S )Nc                 S   s<   dd }zdd t |ddd| gD W S    td Y d S )	Nc                 S   s   | dk rt d| S )Nr   r1   r[   rd   r   r   r   re     s   z9TestTryBareExcept.test_closure4.<locals>.foo.<locals>.barc                 S   rk   r   r   rl   r   r   r   rn   "  ro   z@TestTryBareExcept.test_closure4.<locals>.foo.<locals>.<listcomp>r)   r+   r7   rY   )rq   r<   rh   r   r   r   r]     s
   z,TestTryBareExcept.test_closure4.<locals>.foor.   rY       )r)   r+   r7   ru   )r   r   r%   rF   stripr_   rr   r   r   r   test_closure4  s   



zTestTryBareExcept.test_closure4c                 C   sR   t dd }t }|  W d    n1 sw   Y  | |  dg d S )Nc                  S   s4   t d} z	t j| }W d S    td | Y S )N)ru   ru   rY   )npzeroslinalgcholeskyr<   )acholr   r   r   r]   3  s   
z0TestTryBareExcept.test_real_problem.<locals>.foorY   rW   ri   r   r   r   test_real_problem1  s   
z#TestTryBareExcept.test_real_problemc                 C   sl   t dd }t }|d W d    n1 sw   Y  | |  dgd dd tddD   d S )	Nc                 S   s   t | D ]}z|dkrtW q   td Y qz zz|dkr!tW n	   td tW W d S    td t   td Y d S )NrM   rY   CAUGHT1CAUGHT2CAUGHT3)rJ   r[   r<   )rK   rL   r   r   r   r]   B  s,   z,TestTryBareExcept.test_for_loop.<locals>.foor^   rY   ru   c                 S   s   g | ]}d | qS )zCAUGHT%sr   )rm   rL   r   r   r   rn   _  s    z3TestTryBareExcept.test_for_loop.<locals>.<listcomp>r)   )r   r   r%   rF   rG   rJ   ri   r   r   r   test_for_loopA  s   


zTestTryBareExcept.test_for_loopc                 C   s$   t dd }|d}| |d d S )Nc                 S   s   zW | S    Y | S r   r   r   r   r   r   r]   c  s   z,TestTryBareExcept.test_try_pass.<locals>.foorV   r$   )r'   r]   r-   r   r   r   test_try_passb  s   
zTestTryBareExcept.test_try_passc                 C   R   t dd }| t}|  W d    n1 sw   Y  | dt|j d S )Nc                   S   s   zt d    NERRORrt   r   r   r   r   r#   o  s   z6TestTryBareExcept.test_try_except_reraise.<locals>.udt4The re-raising of an exception is not yet supported.r   r/   r
   assertInr0   r1   r2   r   r   r   test_try_except_reraisen     
z)TestTryBareExcept.test_try_except_reraiseN)r   r   r   __doc__r(   r4   r=   rI   rS   rX   r`   rf   rj   rs   rx   r   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
 Zdd ZdS )TestTryExceptCaughtc                 C   s   t dd }t }|d W d    n1 sw   Y  | |  g d t }|d W d    n1 s;w   Y  | |  g d d S )Nc                 S   sH   zt d | rtdt d W n ty   t d Y nw t d d S )Nr>   321r?   r@   rA   r<   ZeroDivisionErrorrb   r   r   r   r   r#     s   z5TestTryExceptCaught.test_catch_exception.<locals>.udtT)r>   r@   rA   Fr>   r?   rA   rW   )r'   r#   rH   r   r   r   test_catch_exception  s    




z(TestTryExceptCaught.test_catch_exceptionc                 C   s   t dd }t }|d}W d    n1 sw   Y  | |  ddg | |d t }|d}W d    n1 sAw   Y  | |  g d | |d	 d S )
Nc                 S   sP   zt d | r	tt d d}W n ty!   t d d}| Y S w t d |S )Nr>   r?   rV   r@   rU   rA   r   )r   rr   r   r   r#     s   z5TestTryExceptCaught.test_return_in_catch.<locals>.udtTr>   r@   rU   Fr   rV   rW   rR   r   r   r   test_return_in_catch  s$   




z(TestTryExceptCaught.test_return_in_catchc                 C   sT   t dd }| t}|d W d    n1 sw   Y  | dt|j d S )Nc              
   S   s@   z	| rt d}W |S  ty } z
d}|W  Y d }~S d }~ww )NrV   rU   )r   rb   )r   r   er   r   r   r#     s   z1TestTryExceptCaught.test_save_caught.<locals>.udtTz3Exception object cannot be stored into variable (e)r   r2   r   r   r   test_save_caught  s   


z$TestTryExceptCaught.test_save_caughtc                 C   r   )Nc                   S   s   zt d ty    w r   r[   rb   r   r   r   r   r#     s
   z8TestTryExceptCaught.test_try_except_reraise.<locals>.udtr   r   r2   r   r   r   r     r   z+TestTryExceptCaught.test_try_except_reraisec                 C   r   )Nc                   S   s,   zt d ty   z  ty    w w r   r   r   r   r   r   r#     s   z>TestTryExceptCaught.test_try_except_reraise_chain.<locals>.udtr   r   r2   r   r   r   test_try_except_reraise_chain  s   
	z1TestTryExceptCaught.test_try_except_reraise_chainc                 C   s0   t dd }| |dd | |dd d S )Nc                 S   s(   z	d|   W d|  S  t y   Y dS w )Nr)     )rb   rD   r   r   r   r#     s   
z7TestTryExceptCaught.test_division_operator.<locals>.udtr   r   r+   g      ?r$   r&   r   r   r   test_division_operator  s   
z*TestTryExceptCaught.test_division_operatorN)	r   r   r   r   r   r   r   r   r   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 )	TestTryExceptNestedzTests for complicated nestingc                 O   s   t  }||i | W d    n1 sw   Y  | }t  }||i | W d    n1 s3w   Y  | }| j||d||d d S )Nzargs={} kwargs={})msg)r   rF   r%   format)r'   cfuncpyfuncargskwargsrH   expectgotr   r   r   check_compare  s   

z!TestTryExceptNested.check_comparec              	   C   sX   t dd }ttddgdd}| | |D ]\}}}}| j||j||||d qd S )Nc                 S   s   t d | rvt d zt d |rt d tdt d W nO tym   t d z,t d zt d	 |r=t d
 tdt d W n tyO   t d Y nw t d W n tyb   t d Y nw t d t d Y nw t d t d t d d S )Nr>   r?   r@   rA   rD   rB   rC   HIJrE   KLMNOPGQRr<   r   rb   r   rD   rE   pr   r   r   r#     sB   z5TestTryExceptNested.test_try_except_else.<locals>.udtTFru   repeatr   r   listr   
assertTruer   py_func)r'   r#   casesr   rD   rE   r   r   r   r   test_try_except_else  s   
"
z(TestTryExceptNested.test_try_except_elsec                 C   sP   t dd }ttddgdd}| | |D ]\}}| j||j||d qd S )Nc                 S   s  zPzt d | rt d tt d W n   t d Y W z.zt d |r+t d tW n ty9   t d Y nw t d W t d	 d S W t d	 d S t d	 w z,zt d |r_t d tW n tym   t d Y nw t d W t d	 w W t d	 w t d	 w )
Nr>   r?   r@   rA   rB   rC   r   r   r   r   r   qr   r   r   r#   B  sF   



z8TestTryExceptNested.test_try_except_finally.<locals>.udtTFr+   r   r   r   )r'   r#   r   r   r   r   r   r   test_try_except_finallyA  s   

z+TestTryExceptNested.test_try_except_finallyN)r   r   r   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S )TestTryExceptRefctc                 C   sX   t dd }|ddd}| t|ttd |ddd}| t|ttd d S )Nc                 S   sJ   t  }zt| D ]}||krt|| q	W |S  ty$   | Y S w r   )r   ListrJ   
IndexErrorappendrb   )rK   raise_atlstrL   r   r   r   r#   d  s   z6TestTryExceptRefct.test_list_direct_raise.<locals>.udtr^   rM   r   r   r%   r   rJ   r'   r#   outr   r   r   test_list_direct_raisec  s   
z)TestTryExceptRefct.test_list_direct_raisec                    st   t dd  t  fdd}|ddd}| t|dgttd  |ddd}| t|dgttd  d S )	Nc                 S   s(   t |D ]}||krt| | q| S r   )rJ   r   r   )r   rK   r   rL   r   r   r   appenderw  s
   z=TestTryExceptRefct.test_list_indirect_raise.<locals>.appenderc                    s<   t  }|d z	 || | W |S  ty   | Y S w )N  )r   r   r   rb   )rK   r   r   r   r   r   r#     s   
z8TestTryExceptRefct.test_list_indirect_raise.<locals>.udtr^   rM   r   r   r   r   r   r   r   test_list_indirect_raisev  s   

"z+TestTryExceptRefct.test_list_indirect_raisec                 C   sR   t dd }| t}|  W d    n1 sw   Y  | t|jd d S )Nc                  S   sX   zt  } td | d td | d td | W S  ty+   td Y d S w )Nr>   r   r?   fdar@   rA   )r   r   r<   r   rb   )r   r   r   r   r#     s   

z<TestTryExceptRefct.test_incompatible_refinement.<locals>.udtz@Cannot refine type|cannot safely cast unicode_type to int(32|64))r   r/   r   assertRegexpMatchesr0   r1   r2   r   r   r   test_incompatible_refinement  s   
z/TestTryExceptRefct.test_incompatible_refinementN)r   r   r   r   r   r   r   r   r   r   r   b  s    r   c                   @   s<   e Zd Zdd Zdd Zdd Zeedk dd	d
 Z	dS )TestTryExceptOtherControlFlowc                 C   sL   t dd }| t|ddttd | t|ddttd d S )Nc              	   s   s@    t | D ]}z||krt|V  W q ty   Y  d S w d S r   )rJ   r[   rb   )rK   r   rL   r   r   r   r#     s   
z5TestTryExceptOtherControlFlow.test_yield.<locals>.udtr^   rM   r   r&   r   r   r   rf     s   
	"z(TestTryExceptOtherControlFlow.test_yieldc                 C   sd   t dd }| t}|  W d    n1 sw   Y  tdkr%d}nd}| |t|j d S )Nc                   S   sP   zt   tt  W d    W d S 1 sw   Y  W d S  ty'   Y d S w r   )r   r<   objectrb   r   r   r   r   r#     s   &z7TestTryExceptOtherControlFlow.test_objmode.<locals>.udtr7   ra   Dunsupported control flow: due to return statements inside with blocklunsupported control flow: with-context contains branches (i.e. break/return/raise) that can leave the block r   r/   r   r   r   r0   r1   )r'   r#   r3   r   r   r   r   test_objmode  s   
z*TestTryExceptOtherControlFlow.test_objmodec                    sp   dd  t  fdd}| t}|  W d    n1 sw   Y  tdkr+d}nd}| |t|j d S )Nc                 S   s   t tt|  S r   )ry   asarrayr   reversedtolistr   r   r   r   re     s   zCTestTryExceptOtherControlFlow.test_objmode_output_type.<locals>.barc                     sj   t d} t | }z tdd | | 7 }W d    W |S 1 s#w   Y  W |S  ty4   Y |S w )NrM   zintp[:]r   )ry   arange
zeros_liker   rb   )r   rD   re   r   r   r     s   

zLTestTryExceptOtherControlFlow.test_objmode_output_type.<locals>.test_objmoder   r   r   r   )r'   r   r3   r   r   r   r   test_objmode_output_type  s   z6TestTryExceptOtherControlFlow.test_objmode_output_type)r7   	   zPython 3.9+ onlyc           	      C   s   dd }t |D ]	}|jdkr nq	| d ti |j}d}|j D ]\}}|	t
jD ]}d}|jr@||jd v r@d}q0q&|sK| d	 d S d S )
Nc                   S   s   zt  t y   tdw r   )rb   r[   r   r   r   r   r     s
   zMTestTryExceptOtherControlFlow.test_reraise_opcode_unreachable.<locals>.pyfuncRERAISEz!expected RERAISE opcode not foundFz8Unreachable condition reached (op code RERAISE executed)r   Tz.expected RERAISE unreachable message not found)disget_instructionsopnamefailr   get_ir_of_code__code__blocksitems
find_instsr	   StaticRaiseexc_args)	r'   r   instfunc_irfoundlblblkstmtr   r   r   r   test_reraise_opcode_unreachable  s$   

z=TestTryExceptOtherControlFlow.test_reraise_opcode_unreachableN)
r   r   r   rf   r   r   r   skipIfr   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	S )
TestTryExceptParforsc                 C   sN   dd }dg}|| }|  tdd|| | |  tdd|| | d S )Nc              	   S   s<   d}t | D ]}z|d7 }W q ty   |d7 }Y qw |S )Nr   r)   r   rb   rK   crL   r   r   r   r#   
  s   z>TestTryExceptParfors.test_try_in_prange_reduction.<locals>.udtr^   FparallelT)r%   r   r'   r#   r   r   r   r   r   test_try_in_prange_reduction  s
   	z1TestTryExceptParfors.test_try_in_prange_reductionc                 C   s   dd }dg}|| }|  tdd|| | tjdd}tdt |  tdd|| | W d    n1 s<w   Y  |  t|d	 | d
t|d  d S )Nc                 S   s6   d}zt | D ]}|d7 }qW |S  ty   Y dS w )Nr   r)   r   r   r   r   r   r   r#     s   
zCTestTryExceptParfors.test_try_outside_prange_reduction.<locals>.udtr^   Fr   T)recordalwaysr)   z5no transformation for parallel execution was possibler   )	r%   r   warningscatch_warningssimplefilterr   lenr   r0   )r'   r#   r   r   wr   r   r   !test_try_outside_prange_reduction  s   

z6TestTryExceptParfors.test_try_outside_prange_reductionc                 C   V   dd }t ddg}|| }| tdd|| | | tdd|| | d S )Nc              	   S   sV   |   }t| jD ]}z||krt| | | ||< W q	 ty(   d||< Y q	w |S Nr.   copyr   sizer[   rb   arrr   r   rL   r   r   r   r#   0  s   z8TestTryExceptParfors.test_try_in_prange_map.<locals>.udtr^      Fr   Try   r   assertPreciseEqualr   r   r   r   r   test_try_in_prange_map/  
   z+TestTryExceptParfors.test_try_in_prange_mapc                 C   r  )Nc                 S   sX   |   }zt| jD ]}||krt| | | ||< q
W |S  ty+   d||< Y |S w r  r  r  r   r   r   r#   A  s   
z=TestTryExceptParfors.test_try_outside_prange_map.<locals>.udtr^   r  Fr   Tr  r   r   r   r   test_try_outside_prange_map@  r  z0TestTryExceptParfors.test_try_outside_prange_mapN)r   r   r   r   r  r  r  r   r   r   r   r     s
    r   __main__)&r   r   	itertoolsr   numpyry   numbar   r   r   r   numba.core.utilsr   
numba.corer   r	   numba.core.errorsr
   r   r   r   numba.tests.supportr   r   r   r   r   r   rb   r   r   r   r   r   r   r   r   mainr   r   r   r   <module>   s.       i ]C`L