B
    u9a                 @   sZ  d dl Z d dlZd dlZd dlZd dlZd dlZd dlT d dlmZ d dl	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G dd deZG dd dej
ZG dd dZG dd deej
ZG dd dZG dd deej
ZG dd deej
ZG dd dej
Zed krVe  dS )!    N)*)supportc               @   s$   e Zd Zdd Zdd Zdd ZdS )TestAbstractContextManagerc             C   s*   G dd dt }| }| | | d S )Nc                   s   e Zd Z fddZ  ZS )z;TestAbstractContextManager.test_enter.<locals>.DefaultEnterc                s   t  j|  d S )N)super__exit__)selfargs)	__class__ %/usr/lib/python3.7/test_contextlib.pyr      s    zDTestAbstractContextManager.test_enter.<locals>.DefaultEnter.__exit__)__name__
__module____qualname__r   __classcell__r
   r
   )r	   r   DefaultEnter   s   r   )AbstractContextManagerassertIs	__enter__)r   r   Zmanagerr
   r
   r   
test_enter   s    z%TestAbstractContextManager.test_enterc          	   C   s0   G dd dt }| t |  W d Q R X d S )Nc               @   s   e Zd ZdS )zETestAbstractContextManager.test_exit_is_abstract.<locals>.MissingExitN)r   r   r   r
   r
   r
   r   MissingExit   s   r   )r   assertRaises	TypeError)r   r   r
   r
   r   test_exit_is_abstract   s    z0TestAbstractContextManager.test_exit_is_abstractc             C   s   G dd d}|  t|t G dd dt}|  t|t G dd d|}| t|t G dd d|}| t|t d S )	Nc               @   s   e Zd Zdd Zdd ZdS )zRTestAbstractContextManager.test_structural_subclassing.<locals>.ManagerFromScratchc             S   s   | S )Nr
   )r   r
   r
   r   r   !   s    z\TestAbstractContextManager.test_structural_subclassing.<locals>.ManagerFromScratch.__enter__c             S   s   d S )Nr
   )r   exc_type	exc_value	tracebackr
   r
   r   r   #   s    z[TestAbstractContextManager.test_structural_subclassing.<locals>.ManagerFromScratch.__exit__N)r   r   r   r   r   r
   r
   r
   r   ManagerFromScratch    s   r   c                   s   e Zd Z fddZ  ZS )zLTestAbstractContextManager.test_structural_subclassing.<locals>.DefaultEnterc                s   t  j|  d S )N)r   r   )r   r   )r	   r
   r   r   )   s    zUTestAbstractContextManager.test_structural_subclassing.<locals>.DefaultEnter.__exit__)r   r   r   r   r   r
   r
   )r	   r   r   (   s   r   c               @   s   e Zd ZdZdS )zGTestAbstractContextManager.test_structural_subclassing.<locals>.NoEnterN)r   r   r   r   r
   r
   r
   r   NoEnter.   s   r   c               @   s   e Zd ZdZdS )zFTestAbstractContextManager.test_structural_subclassing.<locals>.NoExitN)r   r   r   r   r
   r
   r
   r   NoExit3   s   r   )
assertTrue
issubclassr   assertFalse)r   r   r   r   r   r
   r
   r   test_structural_subclassing   s    z6TestAbstractContextManager.test_structural_subclassingN)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d Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zejdd Zejdd Zdd Zdd Zdd Zdd  Zd!S )"ContextManagerTestCasec          	      s`   g  t  fdd}| *}|  dg | |d  | W d Q R X |  dddg d S )Nc               3   s     d dV    d d S )N   *   i  )appendr
   )stater
   r   woohoo=   s    
z@ContextManagerTestCase.test_contextmanager_plain.<locals>.woohoor$   r%   i  )contextmanagerassertEqualr&   )r   r(   xr
   )r'   r   test_contextmanager_plain;   s    z0ContextManagerTestCase.test_contextmanager_plainc          
      s|   g  t  fdd}| tB | 0}|  dg | |d  | t W d Q R X W d Q R X |  dddg d S )Nc            	   3   s&     d z
dV  W d   d X d S )Nr$   r%   i  )r&   r
   )r'   r
   r   r(   J   s    

zBContextManagerTestCase.test_contextmanager_finally.<locals>.woohoor$   r%   i  )r)   r   ZeroDivisionErrorr*   r&   )r   r(   r+   r
   )r'   r   test_contextmanager_finallyH   s    
z2ContextManagerTestCase.test_contextmanager_finallyc             C   s6   t dd }| }|  | |ttdd  d S )Nc               s   s
   d V  d S )Nr
   r
   r
   r
   r   wheeZ   s    zCContextManagerTestCase.test_contextmanager_no_reraise.<locals>.wheefoo)r)   r   r!   r   r   )r   r/   ctxr
   r
   r   test_contextmanager_no_reraiseY   s    z5ContextManagerTestCase.test_contextmanager_no_reraisec             C   s6   t dd }| }|  | t|jttdd  d S )Nc               s   s"   y
d V  W n   d V  Y nX d S )Nr
   r
   r
   r
   r   whooc   s    
zOContextManagerTestCase.test_contextmanager_trap_yield_after_throw.<locals>.whoor0   )r)   r   r   RuntimeErrorr   r   )r   r3   r1   r
   r
   r   *test_contextmanager_trap_yield_after_throwb   s
    zAContextManagerTestCase.test_contextmanager_trap_yield_after_throwc          	      sj   g t  fdd}| 2} dg  |d | tdW d Q R X  dddg d S )Nc           
   3   s^    d y
dV  W nD tk
rX }  z& | jd   dddg W d d } ~ X Y nX d S )Nr$   r%   r   i  )r&   r-   r   r*   )e)r   r'   r
   r   r(   q   s    

zAContextManagerTestCase.test_contextmanager_except.<locals>.woohoor$   r%   i  )r)   r*   r&   r-   )r   r(   r+   r
   )r   r'   r   test_contextmanager_excepto   s    
z1ContextManagerTestCase.test_contextmanager_exceptc          
   C   s   t d}tdd }y2| td | 
 |W d Q R X W d Q R X W n. tk
rt } z| || W d d }~X Y nX | d d S )Nspamc               s   s
   d V  d S )Nr
   r
   r
   r
   r   r(      s    zJContextManagerTestCase.test_contextmanager_except_stopiter.<locals>.woohooStopIterationzStopIteration was suppressed)r9   r)   ZassertWarnsRegexDeprecationWarning	Exceptionr   fail)r   stop_excr(   exr
   r
   r   #test_contextmanager_except_stopiter   s    z:ContextManagerTestCase.test_contextmanager_except_stopiterc          
   C   s|   d}i }t ||| |d }td}y| 
 |W d Q R X W n. tk
rl } z| || W d d }~X Y nX | d d S )Nztfrom __future__ import generator_stop
from contextlib import contextmanager
@contextmanager
def woohoo():
    yield
r(   r8   zStopIteration was suppressed)execr9   r;   r   r<   )r   codelocalsr(   r=   r>   r
   r
   r   !test_contextmanager_except_pep479   s    z8ContextManagerTestCase.test_contextmanager_except_pep479c          
   C   s   t dd }y| 
 tW d Q R X W nR tk
rx } z4| t|t | |jd d | |j	t W d d }~X Y nX y|  t
dW d Q R X W nP tk
r } z2| t|t
 | |jd d | |j	 W d d }~X Y nX d S )Nc           
   s   s<   y
d V  W n, t k
r6 }  ztd| W d d } ~ X Y nX d S )Nzissue29692:Chained)r;   r4   )excr
   r
   r   test_issue29692   s    
zoContextManagerTestCase.test_contextmanager_do_not_unchain_non_stopiteration_exceptions.<locals>.test_issue29692r   zissue29692:Chainedzissue29692:Unchained)r)   r-   r;   r   typer4   r*   r   assertIsInstance	__cause__r9   assertIsNone)r   rE   r>   r
   r
   r   ?test_contextmanager_do_not_unchain_non_stopiteration_exceptions   s     zVContextManagerTestCase.test_contextmanager_do_not_unchain_non_stopiteration_exceptionsc             C   s"   dd }t |dddd }|S )Nc                 s    fdd}|S )Nc                s&   x    D ]\}}t| || q
W | S )N)itemssetattr)funckv)kwr
   r   decorate   s    zXContextManagerTestCase._create_contextmanager_attribs.<locals>.attribs.<locals>.decorater
   )rP   rQ   r
   )rP   r   attribs   s    zFContextManagerTestCase._create_contextmanager_attribs.<locals>.attribsbar)r0   c             S   s   d S )Nr
   )r8   r
   r
   r   baz   s    zBContextManagerTestCase._create_contextmanager_attribs.<locals>.baz)r)   )r   rR   rT   r
   r
   r   _create_contextmanager_attribs   s    z5ContextManagerTestCase._create_contextmanager_attribsc             C   s(   |   }| |jd | |jd d S )NrT   rS   )rU   r*   r   r0   )r   rT   r
   r
   r   test_contextmanager_attribs   s    z2ContextManagerTestCase.test_contextmanager_attribsc             C   s   |   }| |jd d S )NzWhee!)rU   r*   __doc__)r   rT   r
   r
   r   test_contextmanager_doc_attrib   s    z5ContextManagerTestCase.test_contextmanager_doc_attribc             C   s   |   d }| |jd d S )NzWhee!)rU   r*   rW   )r   rT   r
   r
   r   *test_instance_docstring_given_cm_docstring   s    zAContextManagerTestCase.test_instance_docstring_given_cm_docstringc          	   C   s8   t dd }|ddddd}| |d W d Q R X d S )	Nc             s   s   | |||fV  d S )Nr
   )r   rM   r   kwdsr
   r
   r   r(      s    z4ContextManagerTestCase.test_keywords.<locals>.woohoo      !   ,   )r   rM   r   rZ   )r[   r\   r]   r^   )r)   r*   )r   r(   targetr
   r
   r   test_keywords   s    z$ContextManagerTestCase.test_keywordsc          	      s>   G dd d}t  fdd}|| | d W d Q R X d S )Nc               @   s   e Zd ZdS )z0ContextManagerTestCase.test_nokeepref.<locals>.AN)r   r   r   r
   r
   r
   r   A   s   ra   c             3   s6   t | } t |} |    |  d V  d S )N)weakrefrefrI   )ab)r   r
   r   r(      s
    

z5ContextManagerTestCase.test_nokeepref.<locals>.woohoo)re   )r)   )r   ra   r(   r
   )r   r   test_nokeepref   s    z%ContextManagerTestCase.test_nokeeprefc          	   C   sl   t dd }| t |  W d Q R X | t |dd W d Q R X | t |dd W d Q R X d S )Nc            s   s
   d V  d S )Nr
   )rd   re   r
   r
   r   r(      s    z8ContextManagerTestCase.test_param_errors.<locals>.woohoo      )re   )r)   r   r   )r   r(   r
   r
   r   test_param_errors   s    z(ContextManagerTestCase.test_param_errorsc                s@   d t  fdd}|  fdd   d d S )Nr   c              3   s*    }  d7  d V   d8    |  d S )Nr$   )r*   )Zbefore)depthr   r
   r   r(      s
    z5ContextManagerTestCase.test_recursive.<locals>.woohooc                  s    dk r  d S )N
   r
   r
   )rj   	recursiver
   r   rl     s    z8ContextManagerTestCase.test_recursive.<locals>.recursive)r)   r*   )r   r(   r
   )rj   rl   r   r   test_recursive   s
    	z%ContextManagerTestCase.test_recursiveN)r   r   r   r,   r.   r2   r5   r7   r?   rC   rJ   rU   rV   r   requires_docstringsrX   rY   r`   rf   ri   rm   r
   r
   r
   r   r#   9   s    	r#   c               @   s*   e Zd Zejdd Zdd Zdd ZdS )ClosingTestCasec             C   s    t j}t d }| |j| d S )N)closingrW   r*   )r   cm_docstringobjr
   r
   r   test_instance_docs  s    z"ClosingTestCase.test_instance_docsc          	      sZ   g  G  fddd}| }|   g  t|}|  || W d Q R X |   dg d S )Nc                   s   e Zd Z fddZdS )z'ClosingTestCase.test_closing.<locals>.Cc                s     d d S )Nr$   )r&   )r   )r'   r
   r   close  s    z-ClosingTestCase.test_closing.<locals>.C.closeN)r   r   r   rt   r
   )r'   r
   r   C  s   ru   r$   )r*   rp   )r   ru   r+   yr
   )r'   r   test_closing  s    
zClosingTestCase.test_closingc          
      sx   g  G  fddd}| }|   g  | t. t|}|  || dd  W d Q R X W d Q R X |   dg d S )Nc                   s   e Zd Z fddZdS )z-ClosingTestCase.test_closing_error.<locals>.Cc                s     d d S )Nr$   )r&   )r   )r'   r
   r   rt   %  s    z3ClosingTestCase.test_closing_error.<locals>.C.closeN)r   r   r   rt   r
   )r'   r
   r   ru   $  s   ru   r$   r   )r*   r   r-   rp   )r   ru   r+   rv   r
   )r'   r   test_closing_error"  s    
z"ClosingTestCase.test_closing_errorN)r   r   r   r   rn   rs   rw   rx   r
   r
   r
   r   ro     s   ro   c               @   s   e Zd Zdd ZdS )NullcontextTestCasec          	   C   s8   G dd d}| }t |}| || W d Q R X d S )Nc               @   s   e Zd ZdS )z/NullcontextTestCase.test_nullcontext.<locals>.CN)r   r   r   r
   r
   r
   r   ru   2  s   ru   )nullcontextr   )r   ru   cZc_inr
   r
   r   test_nullcontext1  s    
z$NullcontextTestCase.test_nullcontextN)r   r   r   r|   r
   r
   r
   r   ry   0  s   ry   c               @   s   e Zd Zdd ZdS )FileContextTestCasec          
   C   s   t  }zd }t|d}| |j |d W d Q R X | |j d }| t@ t|d*}| |j | 	|
 d dd  W d Q R X W d Q R X | |j W d t| X d S )NwzBooh
rr$   r   )tempfileZmktempopenr!   closedwriter   r   r-   r*   readr   unlink)r   Ztfnfr
   r
   r   testWithOpen;  s    z FileContextTestCase.testWithOpenN)r   r   r   r   r
   r
   r
   r   r}   9  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 )LockContextTestCasec          
   C   s~   |  |  | | |  W d Q R X |  |  | t* | | |  dd  W d Q R X W d Q R X |  |  d S )Nr$   r   )r!   r   r   r-   )r   locklockedr
   r
   r   boilerPlateO  s    zLockContextTestCase.boilerPlatec             C   s   t  }| ||j d S )N)	threadingZLockr   r   )r   r   r
   r
   r   testWithLockZ  s    z LockContextTestCase.testWithLockc             C   s   t  }| ||j d S )N)r   RLockr   	_is_owned)r   r   r
   r
   r   testWithRLock^  s    z!LockContextTestCase.testWithRLockc                s$   t    fdd}|  | d S )Nc                  s      S )N)r   r
   )r   r
   r   r   d  s    z5LockContextTestCase.testWithCondition.<locals>.locked)r   Z	Conditionr   )r   r   r
   )r   r   testWithConditionb  s    z%LockContextTestCase.testWithConditionc                s$   t    fdd}|  | d S )Nc                  s     dr   dS dS d S )NFT)acquirereleaser
   )r   r
   r   r   j  s    
z5LockContextTestCase.testWithSemaphore.<locals>.locked)r   Z	Semaphorer   )r   r   r
   )r   r   testWithSemaphoreh  s    z%LockContextTestCase.testWithSemaphorec                s$   t    fdd}|  | d S )Nc                  s     dr   dS dS d S )NFT)r   r   r
   )r   r
   r   r   t  s    
z<LockContextTestCase.testWithBoundedSemaphore.<locals>.locked)r   ZBoundedSemaphorer   )r   r   r
   )r   r   testWithBoundedSemaphorer  s    z,LockContextTestCase.testWithBoundedSemaphoreN)	r   r   r   r   r   r   r   r   r   r
   r
   r
   r   r   M  s   
r   c               @   s(   e Zd ZdZdZdZdd Zdd ZdS )	mycontextFNc             C   s
   d| _ | S )NT)started)r   r
   r
   r   r     s    zmycontext.__enter__c             G   s   || _ | jS )N)rD   catch)r   rD   r
   r
   r   r     s    zmycontext.__exit__)r   r   r   r   rD   r   r   r   r
   r
   r
   r   r   }  s
   r   c               @   sb   e Zd Zej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 )TestContextDecoratorc             C   s   t j}t  }| |j| d S )N)r   rW   r*   )r   rq   rr   r
   r
   r   rs     s    z'TestContextDecorator.test_instance_docsc          	   C   s@   t  }|}| || | |j W d Q R X | |jd d S )N)NNN)r   r   r   r   r*   rD   )r   contextresultr
   r
   r   test_contextdecorator  s
    z*TestContextDecorator.test_contextdecoratorc          
   C   s   t  }| td | tdW d Q R X W d Q R X | |j | |jd t t  }d|_| tdW d Q R X | |j | |jd t d S )Nr0   r   T)r   assertRaisesRegex	NameErrorassertIsNotNonerD   r   r   )r   r   r
   r
   r   $test_contextdecorator_with_exception  s    z9TestContextDecorator.test_contextdecorator_with_exceptionc                s0   t     fdd}|   jd d S )Nc                  s     j  j d S )N)rI   rD   r   r   r
   )r   r   r
   r   test  s    z1TestContextDecorator.test_decorator.<locals>.test)NNN)r   r*   rD   )r   r   r
   )r   r   r   test_decorator  s    z#TestContextDecorator.test_decoratorc          	      sX   t     fdd}td |  W d Q R X  j  jd t d S )Nc                  s$     j  j tdd S )Nr0   )rI   rD   r   r   r   r
   )r   r   r
   r   r     s    z@TestContextDecorator.test_decorator_with_exception.<locals>.testr0   r   )r   r   r   r   rD   r   )r   r   r
   )r   r   r   test_decorator_with_exception  s    z2TestContextDecorator.test_decorator_with_exceptionc                s   t   G  fdddt}| }|dd | |jd | |jd | |jd  | }|ddd | |jd | |jd | |jd | }|jddd | |jd | |jd d S )	Nc                   s   e Zd Z dddZdS )z9TestContextDecorator.test_decorating_method.<locals>.TestNc             S   s   || _ || _|| _d S )N)rd   re   r{   )r   rd   re   r{   r
   r
   r   method  s    z@TestContextDecorator.test_decorating_method.<locals>.Test.method)N)r   r   r   r   r
   )r   r
   r   Test  s   r   r$      rd   re   r{   )rd   re   )r   objectr   r*   rd   re   r{   )r   r   r   r
   )r   r   test_decorating_method  s     	z+TestContextDecorator.test_decorating_methodc          
   C   s<   G dd dt }| t |  W d Q R X W d Q R X d S )Nc               @   s   e Zd Zdd Zdd ZdS )z7TestContextDecorator.test_typo_enter.<locals>.mycontextc             S   s   d S )Nr
   )r   r
   r
   r   	__unter__  s    zATestContextDecorator.test_typo_enter.<locals>.mycontext.__unter__c             W   s   d S )Nr
   )r   rD   r
   r
   r   r     s    z@TestContextDecorator.test_typo_enter.<locals>.mycontext.__exit__N)r   r   r   r   r   r
   r
   r
   r   r     s   r   )ContextDecoratorr   AttributeError)r   r   r
   r
   r   test_typo_enter  s    z$TestContextDecorator.test_typo_enterc          
   C   s<   G dd dt }| t |  W d Q R X W d Q R X d S )Nc               @   s   e Zd Zdd Zdd ZdS )z6TestContextDecorator.test_typo_exit.<locals>.mycontextc             S   s   d S )Nr
   )r   r
   r
   r   r     s    z@TestContextDecorator.test_typo_exit.<locals>.mycontext.__enter__c             W   s   d S )Nr
   )r   rD   r
   r
   r   __uxit__  s    z?TestContextDecorator.test_typo_exit.<locals>.mycontext.__uxit__N)r   r   r   r   r   r
   r
   r
   r   r     s   r   )r   r   r   )r   r   r
   r
   r   test_typo_exit  s    z#TestContextDecorator.test_typo_exitc                sR   G dd dt }G dd d|t}|    fdd}|   jd d S )Nc               @   s$   e Zd ZdZdZdd Zdd ZdS )zHTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontextFNc             S   s
   d| _ | S )NT)r   )r   r
   r
   r   r     s    zRTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontext.__enter__c             W   s
   || _ d S )N)rD   )r   rD   r
   r
   r   r     s    zQTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontext.__exit__)r   r   r   r   rD   r   r   r
   r
   r
   r   somecontext   s   r   c               @   s   e Zd ZdS )zFTestContextDecorator.test_contextdecorator_as_mixin.<locals>.mycontextN)r   r   r   r
   r
   r
   r   r     s   r   c                  s     j  j d S )N)rI   rD   r   r   r
   )r   r   r
   r   r     s    zATestContextDecorator.test_contextdecorator_as_mixin.<locals>.test)NNN)r   r   r*   rD   )r   r   r   r   r
   )r   r   r   test_contextdecorator_as_mixin  s    z3TestContextDecorator.test_contextdecorator_as_mixinc                sf   t fdd}g |d fdd}|d  dddg g |d  dddg d S )	Nc             3   s     |  d V    d d S )Ni  )r&   )rv   )r'   r
   r   r(     s    
zETestContextDecorator.test_contextmanager_as_decorator.<locals>.woohoor$   c                s     dg |  d S )Nr$   )r*   r&   )r+   )r   r'   r
   r   r     s    zCTestContextDecorator.test_contextmanager_as_decorator.<locals>.testZ	somethingi  zsomething else)r)   r*   )r   r(   r   r
   )r   r'   r    test_contextmanager_as_decorator  s    z5TestContextDecorator.test_contextmanager_as_decoratorN)r   r   r   r   rn   rs   r   r   r   r   r   r   r   r   r   r
   r
   r
   r   r     s   	r   c               @   s   e Zd ZdZej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S )(TestBaseExitStackNc             C   s"   | j j}|   }| |j| d S )N)
exit_stackrW   r*   )r   rq   rr   r
   r
   r   rs   /  s    z$TestBaseExitStack.test_instance_docsc          	   C   s   |    W d Q R X d S )N)r   )r   r
   r
   r   test_no_resources6  s    
z#TestBaseExitStack.test_no_resourcesc                s  di fdi fdi fdt ddfdt ddfdt ddfdt dddfg}g   fd	d
}|  }xpt|D ]d\}}|r|r|j|f||}n4|r|j|f| }n|r|j|f|}n
||}| || qlW xH|jD ]>}| |d j| | |d j|j | 	|d j
|j
 qW W d Q R X |  | g  |  Z}| t |jdd W d Q R X | t | jjdd W d Q R X |j|dd W d Q R X |  dddifg d S )Nr
   )r$   )r$   r   r$   )Zexamplerg      )r   callbackc                 s     | |f d S )N)r&   )r   rZ   )r   r
   r   _exitE  s    z.TestBaseExitStack.test_callback.<locals>._exit)argr   )r   r   r   )dictr   reversedr   r   _exit_callbacks__wrapped__ZassertNotEqualr   rI   rW   r*   r   r   )r   Zexpectedr   stackr   rZ   r   wrapperr
   )r   r   test_callback:  s@    

"
zTestBaseExitStack.test_callbackc          	      s*  t   fdd}dd }fdd}G dd dt} }|| |jd	 d
 | ||}|| |jd	 d
 j| || |jd	 d
 | ||}|| |jd	 d
 j| || |jd	 d
 | || |jd	 d
 | d
d  W d Q R X d S )Nc                s    |   d S )N)r   )r   rD   exc_tb)
exc_raisedr   r
   r   _expect_excd  s    z0TestBaseExitStack.test_push.<locals>._expect_excc              W   s   dS )NTr
   )exc_detailsr
   r
   r   _suppress_excf  s    z2TestBaseExitStack.test_push.<locals>._suppress_excc                s"     |    |   | d S )N)rI   )r   rD   r   )r   r
   r   
_expect_okh  s    

z/TestBaseExitStack.test_push.<locals>._expect_okc               @   s$   e Zd Zdd Zdd Zdd ZdS )z+TestBaseExitStack.test_push.<locals>.ExitCMc             S   s
   || _ d S )N)	check_exc)r   r   r
   r
   r   __init__m  s    z4TestBaseExitStack.test_push.<locals>.ExitCM.__init__c             S   s   |  d d S )NzShould not be called!)r<   )r   r
   r
   r   r   o  s    z5TestBaseExitStack.test_push.<locals>.ExitCM.__enter__c             W   s   | j |  d S )N)r   )r   r   r
   r
   r   r   q  s    z4TestBaseExitStack.test_push.<locals>.ExitCM.__exit__N)r   r   r   r   r   r   r
   r
   r
   r   ExitCMl  s   r   r$   r   )r-   r   r   pushr   r   __self__)r   r   r   r   r   r   cmr
   )r   r   r   	test_pushb  s*    






zTestBaseExitStack.test_pushc          	      s   G  fdddt }g  | }|  N}|j fdd}| | || | |jd d j|  d W d Q R X | 	 dddd	g d S )
Nc                   s$   e Zd Z fddZ fddZdS )z4TestBaseExitStack.test_enter_context.<locals>.TestCMc                s     d d S )Nr$   )r&   )r   )r   r
   r   r     s    z>TestBaseExitStack.test_enter_context.<locals>.TestCM.__enter__c                s     d d S )Nrg   )r&   )r   r   )r   r
   r   r     s    z=TestBaseExitStack.test_enter_context.<locals>.TestCM.__exit__N)r   r   r   r   r   r
   )r   r
   r   TestCM  s   r   c                  s     d d S )Nr   )r&   r
   )r   r
   r   r     s    z3TestBaseExitStack.test_enter_context.<locals>._exitr   r$   r   rg   r   )
r   r   r   r   enter_contextr   r   r   r&   r*   )r   r   r   r   r   r
   )r   r   test_enter_context  s    


z$TestBaseExitStack.test_enter_contextc          	      sZ   g  |   4}|j fdd}| | |   d W d Q R X |  ddg d S )Nc                  s     d d S )Nr$   )r&   r
   )r   r
   r   r     s    z+TestBaseExitStack.test_close.<locals>._exitr   r$   )r   r   r   rt   r&   r*   )r   r   r   r
   )r   r   
test_close  s    

zTestBaseExitStack.test_closec          	      sn   g  |   4}|j fdd}| | | } d W d Q R X  d |  |  dddg d S )Nc                  s     d d S )Nrg   )r&   r
   )r   r
   r   r     s    z-TestBaseExitStack.test_pop_all.<locals>._exitr$   r   rg   )r   r   r   pop_allr&   rt   r*   )r   r   r   	new_stackr
   )r   r   test_pop_all  s    


zTestBaseExitStack.test_pop_allc          
   C   sD   |  t0 |  }|dd  dd  W d Q R X W d Q R X d S )Nc              W   s   dS )NFr
   )rD   r
   r
   r   <lambda>      z3TestBaseExitStack.test_exit_raise.<locals>.<lambda>r$   r   )r   r-   r   r   )r   r   r
   r
   r   test_exit_raise  s    
z!TestBaseExitStack.test_exit_raisec          	   C   s.   |   }|dd  dd  W d Q R X d S )Nc              W   s   dS )NTr
   )rD   r
   r
   r   r     r   z6TestBaseExitStack.test_exit_suppress.<locals>.<lambda>r$   r   )r   r   )r   r   r
   r
   r   test_exit_suppress  s    
z$TestBaseExitStack.test_exit_suppressc             C   s
  G dd d}G dd d}G dd d}y\|t J |tt4 | " |t dd  W d Q R X W d Q R X W d Q R X W d Q R X W nP t k
r } z2| |jt | |jjt | |jjj W d d }~X Y nX | d	 |jd }| |t | |jt	 d S )
Nc               @   s$   e Zd Zdd Zdd Zdd ZdS )zJTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcc             S   s
   || _ d S )N)rD   )r   rD   r
   r
   r   r     s    zSTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__init__c             S   s   | S )Nr
   )r   r
   r
   r   r     s    zTTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__enter__c             W   s
   | j d S )N)rD   )r   r   r
   r
   r   r     s    zSTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__exit__N)r   r   r   r   r   r   r
   r
   r
   r   RaiseExc  s   r   c               @   s$   e Zd Zdd Zdd Zdd ZdS )zUTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContextc             S   s   || _ || _d S )N)outerinner)r   r   r   r
   r
   r   r     s    z^TestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__init__c             S   s   | S )Nr
   )r   r
   r
   r   r     s    z_TestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__enter__c             W   s"   y
| j W n   | jY nX d S )N)r   r   )r   r   r
   r
   r   r     s    
z^TestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__exit__N)r   r   r   r   r   r   r
   r
   r
   r   RaiseExcWithContext  s   r   c               @   s   e Zd Zdd Zdd ZdS )zMTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExcc             S   s   | S )Nr
   )r   r
   r
   r   r     s    zWTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExc.__enter__c             W   s   |t | _dS )NT)rF   saved_details)r   r   r
   r
   r   r     s    
zVTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExc.__exit__N)r   r   r   r   r   r
   r
   r
   r   SuppressExc  s   r   r$   r   z0Expected IndexError, but no exception was raised)

IndexErrorKeyErrorr   
ValueErrorrG   __context__rI   r<   r   r-   )r   r   r   r   rD   	inner_excr
   r
   r   &test_exit_exception_chaining_reference  s"    

4"

z8TestBaseExitStack.test_exit_exception_chaining_referencec          
      s   dd }d   fdd}yZ|   H}||t ||t ||t || ||t dd  W d Q R X W nP tk
r } z2| |jt | |jjt | 	|jjj W d d }~X Y nX | 
d  d }| |t | |jt d S )Nc             S   s   | d S )Nr
   )rD   r
   r
   r   	raise_exc  s    zATestBaseExitStack.test_exit_exception_chaining.<locals>.raise_excc                 s   |  dS )NTr
   )r   )r   r
   r   suppress_exc  s    zDTestBaseExitStack.test_exit_exception_chaining.<locals>.suppress_excr$   r   z0Expected IndexError, but no exception was raised)r   r   r   r   r   r   r   rG   r   rI   r<   r-   )r   r   r   r   rD   r   r
   )r   r   test_exit_exception_chaining  s&    

"
z.TestBaseExitStack.test_exit_exception_chainingc          
   C   s   dd }dd }y2|    }|dd  ||t W d Q R X W n. tk
rp } z| |t W d d }~X Y nX | d y:|   (}||t || ||t W d Q R X W n. tk
r } z| |t W d d }~X Y nX | d d S )	Nc             S   s   | d S )Nr
   )rD   r
   r
   r   r     s    zHTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.raise_excc              W   s   dS )NTr
   )r   r
   r
   r   r     s    zKTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.suppress_excc               S   s   d S )Nr
   r
   r
   r
   r   r     r   zGTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.<lambda>z0Expected IndexError, but no exception was raisedz.Expected KeyError, but no exception was raised)r   r   r   r;   rG   r<   r   r   )r   r   r   r   rD   r
   r
   r   #test_exit_exception_non_suppressing	  s"    



z5TestBaseExitStack.test_exit_exception_non_suppressingc          
   C   s   t dd }td}td}td}td}yF|  4}||| ||| ||| |W d Q R X W np tk
r } zR| || | |j| | |jj| | |jjj| | |jjjj W d d }~X Y nX d S )Nc             s   s   z
d V  W d | X d S )Nr
   )rD   r
   r
   r   gets_the_context_right&  s    
zZTestBaseExitStack.test_exit_exception_with_correct_context.<locals>.gets_the_context_rightr$   r   rg   r   )r)   r;   r   r   r   r   rI   )r   r   exc1exc2exc3exc4r   rD   r
   r
   r   (test_exit_exception_with_correct_context$  s$    
z:TestBaseExitStack.test_exit_exception_with_correct_contextc       	   
   C   s   dd }t d}t d}t d}t d}t d}y8|  &}|||| |||| |W d Q R X W n t k
r } zh| || | |j| | |jj| | |jjj| | |jjjj| | |jjjjj W d d }~X Y nX d S )Nc             S   s   z| W d |X d S )Nr
   )r   Z	outer_excr
   r
   r   raise_nestedF  s    zQTestBaseExitStack.test_exit_exception_with_existing_context.<locals>.raise_nestedr$   r   rg   r   rh   )r;   r   r   r   r   rI   )	r   r   r   r   r   r   Zexc5r   rD   r
   r
   r   )test_exit_exception_with_existing_contextC  s(    
z;TestBaseExitStack.test_exit_exception_with_existing_contextc          
   C   sd   dd }y*|   }|| dd  W d Q R X W n, tk
r^ } z| d W d d }~X Y nX d S )Nc              W   s   dS )NTr
   )r   r
   r
   r   r   `  s    zDTestBaseExitStack.test_body_exception_suppress.<locals>.suppress_excr$   r   z%Expected no exception, got IndexError)r   r   r   r<   )r   r   r   rD   r
   r
   r   test_body_exception_suppress_  s    

z.TestBaseExitStack.test_body_exception_suppressc          	   C   sB   |   0}|dd  |dd  |dd  W d Q R X d S )Nc              W   s   dS )NTr
   )rD   r
   r
   r   r   k  r   zITestBaseExitStack.test_exit_exception_chaining_suppress.<locals>.<lambda>c              W   s   dd S )Nr$   r   r
   )rD   r
   r
   r   r   l  r   c              W   s   i d S )Nr$   r
   )rD   r
   r
   r   r   m  r   )r   r   )r   r   r
   r
   r   %test_exit_exception_chaining_suppressi  s    
z7TestBaseExitStack.test_exit_exception_chaining_suppressc          	   C   s4   |   "}xtdD ]}|t qW W d Q R X d S )Ni'  )r   ranger   int)r   r   ir
   r
   r   test_excessive_nestingo  s    
z(TestBaseExitStack.test_excessive_nestingc             C   sZ   G dd dt }| }t  |_|  }| t|j| || | |jd d | d S )Nc               @   s   e Zd ZdS )z7TestBaseExitStack.test_instance_bypass.<locals>.ExampleN)r   r   r   r
   r
   r
   r   Examplev  s    r   r   r$   )	r   r   r   r   r   r   r   r   r   )r   r   r   r   r
   r
   r   test_instance_bypassu  s    
z&TestBaseExitStack.test_instance_bypassc          
      s   G dd dt  G dd dt}t fdd}tdd }|  :}|  &}||  ||  |d	W d Q R X W d Q R X |j}| |  | |j| | 	|jj | 	|jj
 | |j
|j d S )
Nc               @   s   e Zd ZdS )zITestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.UniqueExceptionN)r   r   r   r
   r
   r
   r   UniqueException  s    r   c               @   s   e Zd ZdS )zLTestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.UniqueRuntimeErrorN)r   r   r   r
   r
   r
   r   UniqueRuntimeError  s    r   c           
   3   s<   y
dV  W n, t k
r6 }  z d| W d d } ~ X Y nX d S )Nr$   znew exception)r;   )rD   )r   r
   r   second  s    
z@TestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.secondc           
   s   s6   y
dV  W n& t k
r0 }  z| W d d } ~ X Y nX d S )Nr$   )r;   )rD   r
   r
   r   first  s    
z?TestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.firstzplease no infinite loop.)r;   r4   r)   r   r   r   Z	exceptionrG   r   rI   rH   r   )r   r   r   r   Zerr_ctxZes_ctxrD   r
   )r   r   test_dont_reraise_RuntimeError~  s    	
z0TestBaseExitStack.test_dont_reraise_RuntimeError)r   r   r   r   r   rn   rs   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r
   r
   r   r   ,  s(   ("0
	r   c               @   s   e Zd ZeZdS )TestExitStackN)r   r   r   	ExitStackr   r
   r
   r
   r   r     s   r   c               @   sJ   e Zd ZdZdZejdd Zdd Zdd Z	dd	 Z
d
d Zdd ZdS )TestRedirectStreamNc             C   s$   | j j}|  d }| |j| d S )N)redirect_streamrW   r*   )r   rq   rr   r
   r
   r   rs     s    
z%TestRedirectStream.test_instance_docsc             C   s.   t t| j}| d  | t t| j| d S )N)getattrsysorig_streamr  r   )r   orig_stdoutr
   r
   r   test_no_redirect_in_init  s    
z+TestRedirectStream.test_no_redirect_in_initc          	   C   sr   t  }d}tt| j}| | t|tt| jd W d Q R X | tt| j| | 	 }| 
|| d S )Nz<Consider an API like help(), which prints directly to stdout)file)ioStringIOr  r  r  r  printr   getvaluestripr*   )r   r   msgr  sr
   r
   r   test_redirect_to_string_io  s    z-TestRedirectStream.test_redirect_to_string_ioc          	   C   s.   t  }| |}| || W d Q R X d S )N)r  r	  r  r   )r   r   enter_resultr
   r
   r   test_enter_result_is_target  s    z.TestRedirectStream.test_enter_result_is_targetc          	   C   s   t  }| |}tt| j}| tddtt| jd W d Q R X | tdtt| jd W d Q R X | tt| j| | }| 	|d d S )NHello )endr  zWorld!)r  zHello World!
)
r  r	  r  r  r  r  r
  r   r  r*   )r   r   
write_to_fr  r  r
   r
   r   test_cm_is_reusable  s    
 z&TestRedirectStream.test_cm_is_reusablec          
   C   s   t  }| |}tt| j}|@ tddtt| jd | tdtt| jd W d Q R X W d Q R X | tt| j| | }| 	|d d S )Nr  r  )r  r  zWorld!)r  zHello World!
)
r  r	  r  r  r  r  r
  r   r  r*   )r   r   r  r  r  r
   r
   r   test_cm_is_reentrant  s    
(z'TestRedirectStream.test_cm_is_reentrant)r   r   r   r  r  r   rn   rs   r  r  r  r  r  r
   r
   r
   r   r     s   
r   c               @   s   e Zd ZeZdZdS )TestRedirectStdoutstdoutN)r   r   r   redirect_stdoutr  r  r
   r
   r
   r   r    s   r  c               @   s   e Zd ZeZdZdS )TestRedirectStderrstderrN)r   r   r   redirect_stderrr  r  r
   r
   r
   r   r    s   r  c               @   sZ   e Zd Zej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 )TestSuppressc             C   s   t j}t  }| |j| d S )N)suppressrW   r*   )r   rq   rr   r
   r
   r   rs     s    zTestSuppress.test_instance_docsc          	   C   s"   t t}| | W d Q R X d S )N)r  r   rI   )r   r  r
   r
   r   test_no_result_from_enter  s    
z&TestSuppress.test_no_result_from_enterc          	   C   s*   t t | tddd W d Q R X d S )Nr   rh       )r  r   r*   pow)r   r
   r
   r   test_no_exception  s    
zTestSuppress.test_no_exceptionc          	   C   s    t t td W d Q R X d S )Nrh   )r  r   len)r   r
   r
   r   test_exact_exception  s    
z!TestSuppress.test_exact_exceptionc          	   C   s    t t dd  W d Q R X d S )Nr  2   )r  LookupError)r   r
   r
   r   test_exception_hierarchy  s    
z%TestSuppress.test_exception_hierarchyc          
   C   s6   |  t" tt dd  W d Q R X W d Q R X d S )Nr$   r   )r   r-   r  r   )r   r
   r
   r   test_other_exception  s    
z!TestSuppress.test_other_exceptionc          
   C   s4   |  t  t  dd  W d Q R X W d Q R X d S )Nr$   r   )r   r-   r  )r   r
   r
   r   test_no_args  s    zTestSuppress.test_no_argsc          	   C   s@   t tt dd  W d Q R X t tt td W d Q R X d S )Nr$   r   rh   )r  r-   r   r$  )r   r
   r
   r   test_multiple_exception_args  s    z)TestSuppress.test_multiple_exception_argsc          
   C   sr   t t}| W d Q R X | td W d Q R X |* | td W d Q R X d}dd  W d Q R X | | d S )Nrh   Tr$   r   )r  r;   r$  r   )r   Zignore_exceptionsZouter_continuedr
   r
   r   r    s    
z!TestSuppress.test_cm_is_reentrantN)r   r   r   r   rn   rs   r   r#  r%  r(  r)  r*  r+  r  r
   r
   r
   r   r    s   r  __main__)Zasyncior  r  r   r   Zunittest
contextlibr   r   rb   ZTestCaser   r#   ro   ry   r}   r   r   r   r   r   r   r   r  r  r  r   mainr
   r
   r
   r   <module>   s8   + V"	0 !  w96
