B
    u9a3                 @   s   d Z ddlmZ ddlZddlZddlZdd ZG dd dZG dd	 d	ejZ	G d
d dejZ
G dd dejZG dd dejZG dd dejZG dd dejZedkre  dS )zTests for the raise statement.    )supportNc               C   s"   y
t  W n   t d S d S )N   )OSErrorsysexc_info r   r    /usr/lib/python3.7/test_raise.pyget_tb   s    
r	   c               @   s   e Zd Zdd Zdd ZdS )Contextc             C   s   | S )Nr   )selfr   r   r   	__enter__   s    zContext.__enter__c             C   s   dS )NTr   )r   exc_type	exc_valueZexc_tbr   r   r   __exit__   s    zContext.__exit__N)__name__
__module____qualname__r   r   r   r   r   r   r
      s   r
   c               @   sl   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S )	TestRaisec          
   C   sH   y W n2 t k
r8 } z| dt| W d d }~X Y nX | d d S )NzNo active exceptionzNo exception raised)RuntimeErrorassertInstrfail)r   er   r   r   test_invalid_reraise   s
    "zTestRaise.test_invalid_reraisec          
   C   sv   y8y
t  W n( t k
r4 } z
|} W d d }~X Y nX W n. t k
rf } z| || W d d }~X Y nX | d d S )NzNo exception raised)
IndexErrorassertIsr   )r   r   Zexc1Zexc2r   r   r   test_reraise#   s    
zTestRaise.test_reraisec             C   s   dd }|  t| d S )Nc               S   sB   yt dW n0   ytdW n tk
r4   Y nX  Y nX d S )Nfoocaught)	TypeErrorKeyErrorr   r   r   r   reraise0   s    z.TestRaise.test_except_reraise.<locals>.reraise)assertRaisesr   )r   r!   r   r   r   test_except_reraise/   s    	zTestRaise.test_except_reraisec             C   s   dd }|  t| d S )Nc            	   S   s0   yt dW n   ztdW d  X Y nX d S )Nr   r   )r   r    r   r   r   r   r!   <   s    z/TestRaise.test_finally_reraise.<locals>.reraise)r"   r    )r   r!   r   r   r   test_finally_reraise;   s    zTestRaise.test_finally_reraisec                s$   dd   fdd}|  t| d S )Nc                S   s    d S )Nr   r   r   r   r   nested_reraiseG   s    z5TestRaise.test_nested_reraise.<locals>.nested_reraisec                  s$   yt dW n      Y nX d S )Nr   )r   r   )r%   r   r   r!   I   s    z.TestRaise.test_nested_reraise.<locals>.reraise)r"   r   )r   r!   r   )r%   r   test_nested_reraiseF   s    zTestRaise.test_nested_reraisec          
   C   sh   y&yt dW n   t d Y nX W n< tk
rb } z| |jt  | |j W d d }~X Y nX d S )Nr   )r   
ValueErrorassertIsInstance__context__assertIsNone	__cause__)r   r   r   r   r   test_raise_from_NoneP   s    zTestRaise.test_raise_from_Nonec             C   s   dd }|  t| d S )Nc               S   s2   yt dW n    t  W d Q R X  Y nX d S )Nr   )r   r
   r   r   r   r   r!   [   s    
z-TestRaise.test_with_reraise1.<locals>.reraise)r"   r   )r   r!   r   r   r   test_with_reraise1Z   s    zTestRaise.test_with_reraise1c             C   s   dd }|  t| d S )Nc               S   s:   yt dW n(   t  tdW d Q R X  Y nX d S )Nr   r   )r   r
   r    r   r   r   r   r!   e   s    z-TestRaise.test_with_reraise2.<locals>.reraise)r"   r   )r   r!   r   r   r   test_with_reraise2d   s    zTestRaise.test_with_reraise2c                sB   dd }|  t   | t fdd | t fdd d S )Nc               s   s&   yt dW n   dV   Y nX d S )Nr      )r   r   r   r   r   r!   o   s
    z-TestRaise.test_yield_reraise.<locals>.reraisec                  s   t  S )N)nextr   )gr   r   <lambda>w       z.TestRaise.test_yield_reraise.<locals>.<lambda>c                  s   t  S )N)r0   r   )r1   r   r   r2   x   r3   )r0   r"   r   StopIteration)r   r!   r   )r1   r   test_yield_reraisen   s
    zTestRaise.test_yield_reraisec             C   s<   G dd dt }y|W n tk
r,   Y nX | d d S )Nc               @   s   e Zd Zdd ZdS )z7TestRaise.test_erroneous_exception.<locals>.MyExceptionc             S   s
   t  d S )N)r   )r   r   r   r   __init__|   s    z@TestRaise.test_erroneous_exception.<locals>.MyException.__init__N)r   r   r   r6   r   r   r   r   MyException{   s   r7   zNo exception raised)	Exceptionr   r   )r   r7   r   r   r   test_erroneous_exceptionz   s    z"TestRaise.test_erroneous_exceptionc          	   C   s.   G dd dt }| t
 |W d Q R X d S )Nc               @   s   e Zd Zdd ZdS )z@TestRaise.test_new_returns_invalid_instance.<locals>.MyExceptionc             W   s   t  S )N)object)clsargsr   r   r   __new__   s    zHTestRaise.test_new_returns_invalid_instance.<locals>.MyException.__new__N)r   r   r   r=   r   r   r   r   r7      s   r7   )r8   r"   r   )r   r7   r   r   r   !test_new_returns_invalid_instance   s    z+TestRaise.test_new_returns_invalid_instancec          
   C   sH   ydst dW n2 t k
rB } z| t|d W d d }~X Y nX d S )NF)   z(3,))AssertionErrorassertEqualr   )r   r   r   r   r   test_assert_with_tuple_arg   s    z$TestRaise.test_assert_with_tuple_argN)r   r   r   r   r   r#   r$   r&   r,   r-   r.   r5   r9   r>   rB   r   r   r   r   r      s   



	r   c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )	TestCausec          
   C   s   yry(yt W n tk
r&   td Y nX W nD tk
rn } z&| |j | |j d|_|W d d }~X Y nX W n& tk
r } z|}W d d }~X Y nX | |j | |j | |j	t  d S )NF)
r   r8   r'   r*   r+   Z
assertTrue__suppress_context__ZassertFalser(   r)   )r   excr   r   r   r   testCauseSyntax   s     zTestCause.testCauseSyntaxc          
   C   sL   y
t dW n2 tk
r< } z| dt| W d d }~X Y nX | d d S )N   zexception causezNo exception raised)r   r   r   r   r   )r   r   r   r   r   test_invalid_cause   s
    
"zTestCause.test_invalid_causec          
   C   sJ   y
t tW n0 t k
r: } z| |jt W d d }~X Y nX | d d S )NzNo exception raised)r   r    r(   r+   r   )r   r   r   r   r   test_class_cause   s
    
 zTestCause.test_class_causec          
   C   sP   t  }y
t|W n0 tk
r@ } z| |j| W d d }~X Y nX | d d S )NzNo exception raised)r    r   r   r+   r   )r   causer   r   r   r   test_instance_cause   s    
 zTestCause.test_instance_causec             C   s>   G dd dt }y
t|W n tk
r.   Y nX | d d S )Nc               @   s   e Zd Zdd ZdS )z3TestCause.test_erroneous_cause.<locals>.MyExceptionc             S   s
   t  d S )N)r   )r   r   r   r   r6      s    z<TestCause.test_erroneous_cause.<locals>.MyException.__init__N)r   r   r   r6   r   r   r   r   r7      s   r7   zNo exception raised)r8   r   r   r   )r   r7   r   r   r   test_erroneous_cause   s    
zTestCause.test_erroneous_causeN)r   r   r   rF   rH   rI   rK   rL   r   r   r   r   rC      s
   	rC   c               @   s   e Zd Zdd Zdd ZdS )TestTracebackc          
   C   sL   y
t  W n2 t k
r< } z| |jtj W d d }~X Y nX | d d S )NzNo exception raised)r   r(   __traceback__typesTracebackTyper   )r   r   r   r   r   test_sets_traceback   s
    
"z!TestTraceback.test_sets_tracebackc          
   C   sf   t  }yt |W n@ tk
rV } z"| |j| | |jj| W d d }~X Y nX | d d S )NzNo exception raised)r	   r   with_tracebackassertNotEqualrN   rA   tb_nextr   )r   tbr   r   r   r   test_accepts_traceback   s    "z$TestTraceback.test_accepts_tracebackN)r   r   r   rQ   rV   r   r   r   r   rM      s   rM   c               @   s$   e Zd Zdd Zdd Zdd ZdS )TestTracebackTypec             C   s   t d S )N)r'   )r   r   r   r   raiser   s    zTestTracebackType.raiserc          
   C   s&  y|    W n( tk
r4 } z
|j}W d d }~X Y nX | |jtj | |jt	
  | |jt | |jt | |jjd  | t
 |`W d Q R X | t d|_W d Q R X | t ||_W d Q R X | t ||j_W d Q R X d |_| |jd  t }||_| |j| d S )NZasdf)rX   r8   rN   r(   rT   rO   rP   r   tb_framer   	_getframetb_lastiint	tb_linenor"   r   r'   r	   )r   rE   rU   Znew_tbr   r   r   
test_attrs   s,    zTestTracebackType.test_attrsc          	   C   s  t  }t }t||dd}| |j| | |j| | |jd | |j	d td |dd}| |jd  | 
t td|dd W d Q R X | 
t t|ddd W d Q R X | 
t t||dd W d Q R X | 
t t||dd W d Q R X d S )Nr/   r   noznuh-uh)r	   r   rZ   rO   rP   rA   rT   rY   r[   r]   r"   r   )r   Zother_tbframerU   r   r   r   test_constructor  s"    z"TestTracebackType.test_constructorN)r   r   r   rX   r^   ra   r   r   r   r   rW      s   #rW   c               @   sl   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S )TestContextc          
   C   sf   t  }y y|W n   t Y nX W n0 tk
rV } z| |j| W d d }~X Y nX | d d S )NzNo exception raised)r   r   rA   r)   r   )r   contextr   r   r   r   $test_instance_context_instance_raise*  s     z0TestContext.test_instance_context_instance_raisec          
   C   sr   t }y y|W n   t Y nX W n> tk
rb } z | |j| | |j| W d d }~X Y nX | d d S )NzNo exception raised)r   r   rS   r)   r(   r   )r   rc   r   r   r   r   !test_class_context_instance_raise6  s     z-TestContext.test_class_context_instance_raisec          
   C   sp   t }yy|W n   tY nX W n> tk
r` } z | |j| | |j| W d d }~X Y nX | d d S )NzNo exception raised)r   r   rS   r)   r(   r   )r   rc   r   r   r   r   test_class_context_class_raiseC  s     z*TestContext.test_class_context_class_raisec          
   C   sb   y"ydd  W n   t Y nX W n0 t k
rR } z| |jt W d d }~X Y nX | d d S )Nr/   r   zNo exception raised)r   r(   r)   ZeroDivisionErrorr   )r   r   r   r   r   test_c_exception_contextP  s     z$TestContext.test_c_exception_contextc          
   C   sb   y"ydd  W n   t  Y nX W n0 tk
rR } z| |jt W d d }~X Y nX | d d S )Nr/   r   zNo exception raised)xyzzy	NameErrorr(   r)   rg   r   )r   r   r   r   r   test_c_exception_raise[  s     z"TestContext.test_c_exception_raisec          
   C   sN   yzW d t X W n. t k
r> } z| |j W d d }~X Y nX | d d S )NzNo exception raised)r   r*   r)   r   )r   r   r   r   r   test_noraise_finallyf  s    
z TestContext.test_noraise_finallyc          
   C   sX   yzdd  W d t X W n0 t k
rH } z| |jt W d d }~X Y nX | d d S )Nr/   r   zNo exception raised)r   r(   r)   rg   r   )r   r   r   r   r   test_raise_finallyq  s    
 zTestContext.test_raise_finallyc          
   C   sl   G dd d}y|  dd  W d Q R X W n0 t k
r\ } z| |jt W d d }~X Y nX | d d S )Nc               @   s   e Zd Zdd Zdd ZdS )z8TestContext.test_context_manager.<locals>.ContextManagerc             S   s   d S )Nr   )r   r   r   r   r   ~  s    zBTestContext.test_context_manager.<locals>.ContextManager.__enter__c             S   s   t  d S )N)ri   )r   tvrU   r   r   r   r     s    zATestContext.test_context_manager.<locals>.ContextManager.__exit__N)r   r   r   r   r   r   r   r   r   ContextManager}  s   rp   r/   r   zNo exception raised)rj   r(   r)   rg   r   )r   rp   r   r   r   r   test_context_manager|  s     z TestContext.test_context_managerc          
   C   sl   y8ydd  W n& t k
r4 } z|W d d }~X Y nX W n. t k
rf } z| |j W d d }~X Y nX d S )Nr/   r   )rg   r*   r)   )r   r   r   r   r   test_cycle_broken  s    zTestContext.test_cycle_brokenc             C   s   yVyt  W nH tk
rR } z*ydd  W n tk
r@   |Y nX W d d }~X Y nX W n0 tk
r } z| |jj W d d }~X Y nX d S )Nr/   r   )ri   rj   rg   r*   r)   )r   ar   r   r   r   test_reraise_cycle_broken  s     z%TestContext.test_reraise_cycle_brokenc                s    dd   fdd}|  d S )Nc               s   s   z
dV  W d X d S )Nr/   r   r   r   r   r   gen  s    
z"TestContext.test_3118.<locals>.genc           
      sd     } t |  y ytW n   ~ tY nX W n0 tk
r^ } z|jt W d d }~X Y nX d S )N)r0   r'   r    r8   r(   r)   )r1   r   )ru   r   r   r   f  s    z TestContext.test_3118.<locals>.fr   )r   rv   r   )ru   r   r   	test_3118  s    zTestContext.test_3118c          	      s<   G dd d  fdd}t d |  W d Q R X d S )Nc               @   s   e Zd Zdd ZdS )z TestContext.test_3611.<locals>.Cc             S   s    ydd  W n    Y nX d S )Nr/   r   r   )r   r   r   r   __del__  s    z(TestContext.test_3611.<locals>.C.__del__N)r   r   r   rx   r   r   r   r   C  s   ry   c           
      st     } y*y
| j  W n tk
r,   ~ tY nX W n> tk
rn } z |jd  |jt W d d }~X Y nX d S )N)xAttributeErrorr   r8   rS   r)   r(   )rz   r   )ry   r   r   r   rv     s    
z TestContext.test_3611.<locals>.fstderr)r   Zcaptured_output)r   rv   r   )ry   r   r   	test_3611  s    zTestContext.test_3611N)r   r   r   rd   re   rf   rh   rk   rl   rm   rq   rr   rt   rw   r}   r   r   r   r   rb   )  s   
rb   c               @   s   e Zd Zdd Zdd ZdS )TestRemovedFunctionalityc             C   s0   yt tfW n tk
r    Y nX | d d S )NzNo exception raised)r   r    r   r   )r   r   r   r   test_tuples  s
    z$TestRemovedFunctionality.test_tuplesc             C   s,   ydW n t k
r   Y nX | d d S )Nr   zNo exception raised)r   r   )r   r   r   r   test_strings  s
    z%TestRemovedFunctionality.test_stringsN)r   r   r   r   r   r   r   r   r   r~     s   r~   __main__)__doc__Ztestr   r   rO   Zunittestr	   r
   ZTestCaser   rC   rM   rW   rb   r~   r   mainr   r   r   r   <module>   s   };B )