a
    ze                     @   sV  d 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rRe  dS )"z9Unit tests for contextlib.py, and other context managers.    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.9/test/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   managerr   r   r   
test_enter   s    z%TestAbstractContextManager.test_enterc                 C   sD   G dd dt }| t |  W d    n1 s60    Y  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 r   r   r
   r   r   r   r       s    z\TestAbstractContextManager.test_structural_subclassing.<locals>.ManagerFromScratch.__enter__c                 S   s   d S r   r   )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 r   r   r	   r   r   r   r   (   s    zUTestAbstractContextManager.test_structural_subclassing.<locals>.DefaultEnter.__exit__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   NoExit2   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                    sr   g  t  fdd}| 4}|  dg | |d  | W d    n1 sT0    Y  |  g d d S )Nc                   3   s     d dV    d d S N   *     appendr   stater   r   woohoo<   s    
z@ContextManagerTestCase.test_contextmanager_plain.<locals>.woohoor.   r/   r.   r/   r0   )contextmanagerassertEqualr2   r
   r5   xr   r3   r   test_contextmanager_plain:   s    (z0ContextManagerTestCase.test_contextmanager_plainc              	      s   g  t  fdd}| t` | :}|  dg | |d  | t W d    n1 sf0    Y  W d    n1 s0    Y  |  g d d S )Nc                	   3   s0     d zdV  W   d n  d 0 d S r-   r1   r   r3   r   r   r5   I   s    
zBContextManagerTestCase.test_contextmanager_finally.<locals>.woohoor.   r/   r6   )r7   r   ZeroDivisionErrorr8   r2   r9   r   r3   r   test_contextmanager_finallyG   s    
Bz2ContextManagerTestCase.test_contextmanager_finallyc                 C   s6   t dd }| }|  | |ttdd  d S )Nc                   s   s
   d V  d S r   r   r   r   r   r   wheeY   s    zCContextManagerTestCase.test_contextmanager_no_reraise.<locals>.wheefoo)r7   r   r*   r   r   )r
   r>   ctxr   r   r   test_contextmanager_no_reraiseX   s
    
z5ContextManagerTestCase.test_contextmanager_no_reraisec                 C   s6   t dd }| }|  | t|jttdd  d S )Nc                   s   s"   z
d V  W n   d V  Y n0 d S r   r   r   r   r   r   whoob   s    
zOContextManagerTestCase.test_contextmanager_trap_yield_after_throw.<locals>.whoor?   )r7   r   r   RuntimeErrorr   r   )r
   rB   r@   r   r   r   *test_contextmanager_trap_yield_after_throwa   s    
zAContextManagerTestCase.test_contextmanager_trap_yield_after_throwc                    s|   g t  fdd}| <} dg  |d | tdW d    n1 s^0    Y   g d d S )Nc               
   3   s^    d z
dV  W nD tyX }  z, | jd   g d W Y d } ~ n
d } ~ 0 0 d S )Nr.   r/   r   r6   )r2   r<   r   r8   )er
   r4   r   r   r5   p   s    

zAContextManagerTestCase.test_contextmanager_except.<locals>.woohoor.   r/   r0   r6   )r7   r8   r2   r<   r9   r   rF   r   test_contextmanager_exceptn   s    
&z1ContextManagerTestCase.test_contextmanager_exceptc                 C   s   t dd }G dd dt}td|dfD ]}| jt|d z.|  |W d    n1 sd0    Y  W n0 ty } z| || W Y d }~nd }~0 0 | | d W d    q,1 s0    Y  q,d S )Nc                   s   s
   d V  d S r   r   r   r   r   r   r5      s    zJContextManagerTestCase.test_contextmanager_except_stopiter.<locals>.woohooc                   @   s   e Zd ZdS )zYContextManagerTestCase.test_contextmanager_except_stopiter.<locals>.StopIterationSubclassNr   r   r   r   r   StopIterationSubclass   s   rH   spam)typez was suppressed)r7   StopIterationsubTestrJ   	Exceptionr   fail)r
   r5   rH   stop_excexr   r   r   #test_contextmanager_except_stopiter   s    
&"z:ContextManagerTestCase.test_contextmanager_except_stopiterc              
   C   s   d}i }t ||| |d }td}z.|  |W d    n1 sF0    Y  W n0 ty } z| || W Y d }~nd }~0 0 | d d S )Nztfrom __future__ import generator_stop
from contextlib import contextmanager
@contextmanager
def woohoo():
    yield
r5   rI   zStopIteration was suppressed)execrK   rM   r   rN   )r
   codelocalsr5   rO   rP   r   r   r   !test_contextmanager_except_pep479   s    &"z8ContextManagerTestCase.test_contextmanager_except_pep479c              
   C   s  t dd }z.|  tW d    n1 s.0    Y  W nT ty } z<| t|t | |jd d | |j	t W Y d }~n
d }~0 0 z2|  t
dW d    n1 s0    Y  W nT ty } z:| t|t
 | |jd d | |j	 W Y d }~n
d }~0 0 d S )Nc               
   s   s>   z
d V  W n. t y8 }  ztd| W Y d } ~ n
d } ~ 0 0 d S )Nissue29692:Chained)rM   rC   excr   r   r   test_issue29692   s    
zoContextManagerTestCase.test_contextmanager_do_not_unchain_non_stopiteration_exceptions.<locals>.test_issue29692r   rV   zissue29692:Unchained)r7   r<   rM   r   rJ   rC   r8   r   assertIsInstance	__cause__rK   assertIsNone)r
   rY   rP   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"      D ]\}}t| || q| S r   )itemssetattr)funckvkwr   r   decorate   s    zXContextManagerTestCase._create_contextmanager_attribs.<locals>.attribs.<locals>.decorater   )rd   re   r   rc   r   attribs   s    zFContextManagerTestCase._create_contextmanager_attribs.<locals>.attribsbar)r?   c                 S   s   dS )Whee!Nr   )rI   r   r   r   baz   s    zBContextManagerTestCase._create_contextmanager_attribs.<locals>.bazr7   )r
   rf   ri   r   r   r   _create_contextmanager_attribs   s
    z5ContextManagerTestCase._create_contextmanager_attribsc                 C   s(   |   }| |jd | |jd d S )Nri   rg   )rk   r8   r   r?   r
   ri   r   r   r   test_contextmanager_attribs   s    z2ContextManagerTestCase.test_contextmanager_attribsc                 C   s   |   }| |jd d S Nrh   rk   r8   __doc__rl   r   r   r   test_contextmanager_doc_attrib   s    z5ContextManagerTestCase.test_contextmanager_doc_attribc                 C   s   |   d }| |jd d S rn   ro   rl   r   r   r   *test_instance_docstring_given_cm_docstring   s    zAContextManagerTestCase.test_instance_docstring_given_cm_docstringc                 C   sL   t dd }|ddddd}| |d W d    n1 s>0    Y  d S )	Nc                 s   s   | |||fV  d S r   r   r
   r`   r   kwdsr   r   r   r5      s    z4ContextManagerTestCase.test_keywords.<locals>.woohoo      !   ,   rs   )ru   rv   rw   rx   r7   r8   )r
   r5   targetr   r   r   test_keywords   s    
z$ContextManagerTestCase.test_keywordsc                    sR   G dd d}t  fdd}|| | d W d    n1 sD0    Y  d S )Nc                   @   s   e Zd ZdS )z0ContextManagerTestCase.test_nokeepref.<locals>.ANr   r   r   r   r   A   s   r|   c                 3   s6   t | } t |} |    |  d V  d S r   )weakrefrefr\   abr    r   r   r5      s
    

z5ContextManagerTestCase.test_nokeepref.<locals>.woohoor   rj   )r
   r|   r5   r   r    r   test_nokeepref   s
    z%ContextManagerTestCase.test_nokeeprefc                 C   s   t dd }| t |  W d    n1 s20    Y  | t |dd W d    n1 sf0    Y  | t |dd W d    n1 s0    Y  d S )Nc                s   s
   d V  d S r   r   r   r   r   r   r5      s    z8ContextManagerTestCase.test_param_errors.<locals>.woohoo      r   )r7   r   r   r
   r5   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.   )r8   )before)depthr
   r   r   r5      s
    z5ContextManagerTestCase.test_recursive.<locals>.woohooc                      s    dk r  d S )N
   r   r   )r   	recursiver   r   r     s    z8ContextManagerTestCase.test_recursive.<locals>.recursivery   r   r   )r   r   r
   r   test_recursive   s    z%ContextManagerTestCase.test_recursiveN)r   r   r   r;   r=   rA   rD   rG   rQ   rU   r]   rk   rm   r   requires_docstringsrq   rr   r{   r   r   r   r   r   r   r   r,   8   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 r   )closingrp   r8   r
   Zcm_docstringobjr   r   r   test_instance_docs  s    z"ClosingTestCase.test_instance_docsc                    sn   g  G  fddd}| }|   g  t|}|  || W d    n1 sR0    Y  |   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 r   r1   r    r3   r   r   close  s    z-ClosingTestCase.test_closing.<locals>.C.closeNr   r   r   r   r   r3   r   r   C  s   r   r.   )r8   r   r
   r   r:   yr   r3   r   test_closing  s    
*zClosingTestCase.test_closingc              	      s   g  G  fddd}| }|   g  | tL t|$}|  || dd  W d    n1 sf0    Y  W d    n1 s0    Y  |   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 r   r1   r    r3   r   r   r   '  s    z3ClosingTestCase.test_closing_error.<locals>.C.closeNr   r   r3   r   r   r   &  s   r   r.   r   )r8   r   r<   r   r   r   r3   r   test_closing_error$  s    
Dz"ClosingTestCase.test_closing_errorN)r   r   r   r   r   r   r   r   r   r   r   r   r     s   
r   c                   @   s   e Zd Zdd ZdS )NullcontextTestCasec                 C   sL   G dd d}| }t |}| || W d    n1 s>0    Y  d S )Nc                   @   s   e Zd ZdS )z/NullcontextTestCase.test_nullcontext.<locals>.CNr   r   r   r   r   r   4  s   r   )nullcontextr   )r
   r   cZc_inr   r   r   test_nullcontext3  s    
z$NullcontextTestCase.test_nullcontextN)r   r   r   r   r   r   r   r   r   2  s   r   c                   @   s   e Zd Zdd ZdS )FileContextTestCasec              	   C   s   t  }zd }t|d&}| |j |d W d    n1 sD0    Y  | |j d }| t^ t|d4}| |j | 	|
 d dd  W d    n1 s0    Y  W d    n1 s0    Y  | |j W t| nt| 0 d S )NwzBooh
rr.   r   )tempfilemktempopenr*   closedwriter(   r   r<   r8   readr   unlink)r
   Ztfnfr   r   r   testWithOpen=  s    (Dz FileContextTestCase.testWithOpenN)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 )LockContextTestCasec              	   C   s   |  |  | | |  W d    n1 s20    Y  |  |  | tH |$ | |  dd  W d    n1 s0    Y  W d    n1 s0    Y  |  |  d S Nr.   r   )r*   r(   r   r<   )r
   locklockedr   r   r   boilerPlateQ  s    *DzLockContextTestCase.boilerPlatec                 C   s   t  }| ||j d S r   )	threadingLockr   r   r
   r   r   r   r   testWithLock\  s    z LockContextTestCase.testWithLockc                 C   s   t  }| ||j d S r   )r   RLockr   	_is_ownedr   r   r   r   testWithRLock`  s    z!LockContextTestCase.testWithRLockc                    s$   t    fdd}|  | d S )Nc                      s      S r   )r   r   r   r   r   r   f  s    z5LockContextTestCase.testWithCondition.<locals>.locked)r   	Conditionr   r
   r   r   r   r   testWithConditiond  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   l  s    
z5LockContextTestCase.testWithSemaphore.<locals>.locked)r   	Semaphorer   r   r   r   r   testWithSemaphorej  s    z%LockContextTestCase.testWithSemaphorec                    s$   t    fdd}|  | d S )Nc                      s     dr   dS dS d S r   r   r   r   r   r   r   v  s    
z<LockContextTestCase.testWithBoundedSemaphore.<locals>.locked)r   BoundedSemaphorer   r   r   r   r   testWithBoundedSemaphoret  s    z,LockContextTestCase.testWithBoundedSemaphoreN)	r   r   r   r   r   r   r   r   r   r   r   r   r   r   O  s   
r   c                   @   s,   e Zd ZdZdZdZdZdd Zdd ZdS )	mycontextz9Example decoration-compatible context manager for testingFNc                 C   s
   d| _ | S NTstartedr    r   r   r   r     s    zmycontext.__enter__c                 G   s   || _ | jS r   )rX   catchr
   rX   r   r   r   r     s    zmycontext.__exit__)	r   r   r   rp   r   rX   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 r   )r   rp   r8   r   r   r   r   r     s    z'TestContextDecorator.test_instance_docsc                 C   sT   t  }|(}| || | |j W d    n1 s80    Y  | |jd d S )NNNN)r   r   r(   r   r8   rX   )r
   contextresultr   r   r   test_contextdecorator  s
    *z*TestContextDecorator.test_contextdecoratorc              	   C   s   t  }| td< | tdW d    n1 s60    Y  W d    n1 sT0    Y  | |j | |jd t t  }d|_| tdW d    n1 s0    Y  | |j | |jd t d S )Nr?   r   T)r   assertRaisesRegex	NameErrorassertIsNotNonerX   r   r   )r
   r   r   r   r   $test_contextdecorator_with_exception  s    D&z9TestContextDecorator.test_contextdecorator_with_exceptionc                    s0   t     fdd}|   jd d S )Nc                      s     j  j d S r   r\   rX   r(   r   r   r   r
   r   r   test  s    z1TestContextDecorator.test_decorator.<locals>.testr   )r   r8   rX   r
   r   r   r   r   test_decorator  s
    z#TestContextDecorator.test_decoratorc                    sl   t     fdd}td |  W d    n1 s@0    Y   j  jd t d S )Nc                      s$     j  j tdd S )Nr?   )r\   rX   r(   r   r   r   r   r   r   r     s    z@TestContextDecorator.test_decorator_with_exception.<locals>.testr?   r   )r   r   r   r   rX   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 r   )r   r   r   )r
   r   r   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.      r   r   r   r   )r   objectr   r8   r   r   r   )r
   r   r   r   r   r   test_decorating_method  s     	z+TestContextDecorator.test_decorating_methodc              	   C   sd   G dd dt }| t6 |  W d    n1 s80    Y  W d    n1 sV0    Y  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 r   r   r    r   r   r   	__unter__  s    zATestContextDecorator.test_typo_enter.<locals>.mycontext.__unter__c                 W   s   d S r   r   r   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   sd   G dd dt }| t6 |  W d    n1 s80    Y  W d    n1 sV0    Y  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 r   r   r    r   r   r   r     s    z@TestContextDecorator.test_typo_exit.<locals>.mycontext.__enter__c                 W   s   d S r   r   r   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   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 r   r   r    r   r   r   r     s    zRTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontext.__enter__c                 W   s
   || _ d S r   rW   r   r   r   r   r   
  s    zQTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontext.__exit__)r   r   r   r   rX   r   r   r   r   r   r   somecontext  s   r   c                   @   s   e Zd ZdS )zFTestContextDecorator.test_contextdecorator_as_mixin.<locals>.mycontextNr   r   r   r   r   r     s   r   c                      s     j  j d S r   r   r   r   r   r   r     s    zATestContextDecorator.test_contextdecorator_as_mixin.<locals>.testr   )r   r   r8   rX   )r
   r   r   r   r   r   r   test_contextdecorator_as_mixin  s    z3TestContextDecorator.test_contextdecorator_as_mixinc                    sb   t fdd}g |d fdd}|d  g d g |d  g d	 d S )
Nc                 3   s     |  d V    d d S )Nr0   r1   )r   r3   r   r   r5     s    
zETestContextDecorator.test_contextmanager_as_decorator.<locals>.woohoor.   c                    s     dg |  d S r   )r8   r2   )r:   rF   r   r   r   !  s    zCTestContextDecorator.test_contextmanager_as_decorator.<locals>.test	something)r.   r   r0   something else)r.   r   r0   ry   )r
   r5   r   r   rF   r    test_contextmanager_as_decorator  s    z5TestContextDecorator.test_contextmanager_as_decoratorN)r   r   r   r   r   r   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(d) ZdS )*TestBaseExitStackNc                 C   s"   | j j}|   }| |j| d S r   )
exit_stackrp   r8   r   r   r   r   r   1  s    z$TestBaseExitStack.test_instance_docsc                 C   s,   |    W d    n1 s0    Y  d S r   )r   r    r   r   r   test_no_resources8  s    
z#TestBaseExitStack.test_no_resourcesc                    s8  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
}|  }t|D ]p\}}|r|r|j|g|R i |}n:|r|j|g|R  }n"|r|j|fi |}n
||}| || qj|jD ]>}| |d j| | |d j|j | 	|d j
|j
 qW d    n1 s80    Y  |  | g  |  }| t |jdd W d    n1 s0    Y  | t | jjdd W d    n1 s0    Y  | t |j|dd W d    n1 s0    Y  W d    n1 s0    Y  |  g  d S )Nr   )r.   )r.   r   r.   )Zexampler      )r
   callbackc                     s     | |f dS )zTest metadata propagationNr1   )r   rt   r   r   r   _exitG  s    z.TestBaseExitStack.test_callback.<locals>._exit)argr   )r   r   )dictr   reversedr   r   _exit_callbacks__wrapped__ZassertNotEqualr   r\   rp   r8   r   r   )r
   Zexpectedr   stackr   rt   r   wrapperr   r   r   test_callback<  sD    	


6
,.N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    n1 s20    Y  d S )Nc                    s    |   d S r   )r   r!   rX   exc_tbZ
exc_raisedr
   r   r   _expect_excg  s    z0TestBaseExitStack.test_push.<locals>._expect_excc                  W   s   dS r   r   exc_detailsr   r   r   _suppress_exci  s    z2TestBaseExitStack.test_push.<locals>._suppress_excc                    s"     |    |   | d S r   )r\   r   r    r   r   
_expect_okk  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 r   	check_exc)r
   r  r   r   r   __init__p  s    z4TestBaseExitStack.test_push.<locals>.ExitCM.__init__c                 S   s   |  d d S )NzShould not be called!)rN   r    r   r   r   r   r  s    z5TestBaseExitStack.test_push.<locals>.ExitCM.__enter__c                 W   s   | j |  d S r   r  r
   r   r   r   r   r   t  s    z4TestBaseExitStack.test_push.<locals>.ExitCM.__exit__Nr   r   r   r  r   r   r   r   r   r   ExitCMo  s   r  r.   r   )r<   r   r   pushr   r   __self__)r
   r   r   r  r  r   cmr   r   r   	test_pushe  s*    






zTestBaseExitStack.test_pushc                    s   G  fdddt }g  | }|  X}|j fdd}| | || | |jd d j|  d W d    n1 s0    Y  | 	 g d 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 r   r1   r    r   r   r   r     s    z>TestBaseExitStack.test_enter_context.<locals>.TestCM.__enter__c                    s     d d S Nr   r1   r  r   r   r   r     s    z=TestBaseExitStack.test_enter_context.<locals>.TestCM.__exit__Nr$   r   r   r   r   TestCM  s   r  c                      s     d d S )Nr   r1   r   r   r   r   r     s    z3TestBaseExitStack.test_enter_context.<locals>._exitr  r.   r   )r.   r   r   r   )
r   r   r   r   enter_contextr   r   r
  r2   r8   )r
   r  r  r   r   r   r   r   test_enter_context  s    


(z$TestBaseExitStack.test_enter_contextc                    sn   g  |   >}|j fdd}| | |   d W d    n1 sP0    Y  |  ddg d S )Nc                      s     d d S r   r1   r   r   r   r   r     s    z+TestBaseExitStack.test_close.<locals>._exitr   r.   )r   r   r   r   r2   r8   )r
   r   r   r   r   r   
test_close  s    

(zTestBaseExitStack.test_closec                    s   g  |   >}|j fdd}| | | } d W d    n1 sP0    Y   d |  |  g d d S )Nc                      s     d d S r  r1   r   r   r   r   r     s    z-TestBaseExitStack.test_pop_all.<locals>._exitr.   r   )r.   r   r   )r   r   r   pop_allr2   r   r8   )r
   r   r   	new_stackr   r   r   test_pop_all  s    

(
zTestBaseExitStack.test_pop_allc              	   C   sl   |  tN |  &}|dd  dd  W d    n1 s@0    Y  W d    n1 s^0    Y  d S )Nc                  W   s   dS )NFr   rW   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   sB   |   &}|dd  dd  W d    n1 s40    Y  d S )Nc                  W   s   dS r   r   rW   r   r   r   r    r  z6TestBaseExitStack.test_exit_suppress.<locals>.<lambda>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}z|t  |ttf | @ |t dd  W d    n1 sp0    Y  W d    n1 s0    Y  W d    n1 s0    Y  W d    n1 s0    Y  W nT t y* } z:| |jt | |jjt | |jjj W Y d }~nd }~0 0 | 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 r   rW   r   r   r   r   r    s    zSTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__init__c                 S   s   | S r   r   r    r   r   r   r     s    zTTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__enter__c                 W   s
   | j d S r   rW   r  r   r   r   r     s    zSTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__exit__Nr  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 r   )outerinner)r
   r  r  r   r   r   r    s    z^TestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__init__c                 S   s   | S r   r   r    r   r   r   r     s    z_TestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__enter__c                 W   s"   z
| j W n   | jY n0 d S r   )r  r  r  r   r   r   r     s    
z^TestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__exit__Nr  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 r   r   r    r   r   r   r     s    zWTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExc.__enter__c                 W   s   |t | _dS r   )rJ   saved_detailsr  r   r   r   r     s    
zVTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExc.__exit__Nr$   r   r   r   r   SuppressExc  s   r   r.   r   0Expected IndexError, but no exception was raised)

IndexErrorKeyErrorr   
ValueErrorrZ   __context__r\   rN   r  r<   )r
   r  r  r   rX   	inner_excr   r   r   &test_exit_exception_chaining_reference  s"    

&

z8TestBaseExitStack.test_exit_exception_chaining_referencec              
      s
  dd }d   fdd}zn|   R}||t ||t ||t || ||t dd  W d    n1 sz0    Y  W nR ty } z:| |jt | |jjt | 	|jjj W Y d }~nd }~0 0 | 
d  d }| |t | |jt d S )Nc                 S   s   | d S r   r   rW   r   r   r   	raise_exc  s    zATestBaseExitStack.test_exit_exception_chaining.<locals>.raise_excc                     s   |  dS r   r   r   r  r   r   suppress_exc  s    zDTestBaseExitStack.test_exit_exception_chaining.<locals>.suppress_excr.   r   r!  )r   r   r"  r#  r   r	  r$  rZ   r%  r\   rN   r<   )r
   r(  r*  r   rX   r&  r   r)  r   test_exit_exception_chaining  s&    

*&
z.TestBaseExitStack.test_exit_exception_chainingc                    s   G dd dt  t fddtfdd}|fD ]} | z0|  t W d    n1 sl0    Y  W n0  y } z|j W Y d }~nd }~0 0 d W d    q:1 s0    Y  q:d S )Nc                   @   s   e Zd ZdS )zPTestBaseExitStack.test_exit_exception_explicit_none_context.<locals>.MyExceptionNr   r   r   r   r   MyException  s   r,  c                  3   s@   z
d V  W n0 t y:     } z| W d | _nd | _0 Y n0 d S r   )BaseExceptionr%  rW   )r,  r   r   my_cm  s    
zJTestBaseExitStack.test_exit_exception_explicit_none_context.<locals>.my_cmc                  3   s>     "} |    | V  W d    n1 s00    Y  d S r   )r   r  )r   )r.  r
   r   r   my_cm_with_exit_stack  s    
zZTestBaseExitStack.test_exit_exception_explicit_none_context.<locals>.my_cm_with_exit_stackr!  )rM   r7   rL   r"  r\   r%  rN   )r
   r/  r  rX   r   )r,  r.  r
   r   )test_exit_exception_explicit_none_context  s    

("z;TestBaseExitStack.test_exit_exception_explicit_none_contextc              
   C   s"  dd }dd }zF|   *}|dd  ||t W d    n1 sJ0    Y  W n0 ty } z| |t W Y d }~nd }~0 0 | d zN|   2}||t || ||t W d    n1 s0    Y  W n2 ty } z| |t W Y d }~nd }~0 0 | d d S )	Nc                 S   s   | d S r   r   rW   r   r   r   r(  0  s    zHTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.raise_excc                  W   s   dS r   r   r   r   r   r   r*  3  s    zKTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.suppress_excc                   S   s   d S r   r   r   r   r   r   r  8  r  zGTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.<lambda>r!  z.Expected KeyError, but no exception was raised)r   r   r"  rM   rZ   rN   r#  r	  )r
   r(  r*  r   rX   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}zZ|  >}||| ||| ||| |W d    n1 sz0    Y  W nr ty } zZ| || | |j| | |jj| | |jjj| | |jjjj W Y d }~n
d }~0 0 d S )Nc                 s   s   zd V  W | n| 0 d S r   r   rW   r   r   r   gets_the_context_rightK  s    zZTestBaseExitStack.test_exit_exception_with_correct_context.<locals>.gets_the_context_rightr.   r   r   r   )r7   rM   r   r  r   r%  r\   )r
   r2  exc1exc2exc3exc4r   rX   r   r   r   (test_exit_exception_with_correct_contextI  s(    

&
z:TestBaseExitStack.test_exit_exception_with_correct_contextc           	   
   C   s  dd }t d}t d}t d}t d}t d}zL|  0}|||| |||| |W d    n1 sp0    Y  W n t y } zp| || | |j| | |jj| | |jjj| | |jjjj| | |jjjjj W Y d }~n
d }~0 0 d S )Nc                 S   s   z| W |n|0 d S r   r   )r&  Z	outer_excr   r   r   raise_nestedk  s    zQTestBaseExitStack.test_exit_exception_with_existing_context.<locals>.raise_nestedr.   r   r   r   r   )rM   r   r   r   r%  r\   )	r
   r8  r3  r4  r5  r6  Zexc5r   rX   r   r   r   )test_exit_exception_with_existing_contexth  s,    
&z;TestBaseExitStack.test_exit_exception_with_existing_contextc              
   C   sz   dd }z>|   "}|| dd  W d    n1 s:0    Y  W n. tyt } z| d W Y d }~n
d }~0 0 d S )Nc                  W   s   dS r   r   r   r   r   r   r*    s    zDTestBaseExitStack.test_body_exception_suppress.<locals>.suppress_excr.   r   z%Expected no exception, got IndexError)r   r	  r"  rN   )r
   r*  r   rX   r   r   r   test_body_exception_suppress  s    

*z.TestBaseExitStack.test_body_exception_suppressc                 C   sV   |   :}|dd  |dd  |dd  W d    n1 sH0    Y  d S )Nc                  W   s   dS r   r   rW   r   r   r   r    r  zITestBaseExitStack.test_exit_exception_chaining_suppress.<locals>.<lambda>c                  W   s   dd S r   r   rW   r   r   r   r    r  c                  W   s   i d S r   r   rW   r   r   r   r    r  r  r  r   r   r   %test_exit_exception_chaining_suppress  s    
z7TestBaseExitStack.test_exit_exception_chaining_suppressc                 C   sD   |   (}tdD ]}|t qW d    n1 s60    Y  d S )Ni'  )r   ranger   int)r
   r   ir   r   r   test_excessive_nesting  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>.ExampleNr   r   r   r   r   Example  r  r@  r  r.   )	r   r   r   r   r   r  r	  r   r   )r
   r@  r  r   r   r   r   test_instance_bypass  s    
z&TestBaseExitStack.test_instance_bypassc              	      s   G dd dt  G dd dt}t fdd}tdd }|  X}|  0}||  ||  |d	W d    n1 s0    Y  W d    n1 s0    Y  |j}| |  | |j| | 	|jj | 	|jj
 | |j
|j d S )
Nc                   @   s   e Zd ZdS )zITestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.UniqueExceptionNr   r   r   r   r   UniqueException  r  rB  c                   @   s   e Zd ZdS )zLTestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.UniqueRuntimeErrorNr   r   r   r   r   UniqueRuntimeError  r  rC  c               
   3   s>   z
dV  W n. t y8 }  z d| W Y d } ~ n
d } ~ 0 0 d S )Nr.   znew exceptionrM   rW   rB  r   r   second  s    
z@TestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.secondc               
   s   s8   z
dV  W n( t y2 }  z| W Y d } ~ n
d } ~ 0 0 d S r   rD  rW   r   r   r   first  s    
z?TestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.firstzplease no infinite loop.)rM   rC   r7   r   r   r  	exceptionrZ   r%  r\   r[   r   )r
   rC  rF  rG  Zerr_ctxZes_ctxrX   r   rE  r   test_dont_reraise_RuntimeError  s"    

Dz0TestBaseExitStack.test_dont_reraise_RuntimeError)r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r'  r+  r0  r1  r7  r9  r:  r;  r?  rA  rI  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   rJ    s   rJ  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 r   )redirect_streamrp   r8   r   r   r   r   r     s    
z%TestRedirectStream.test_instance_docsc                 C   s.   t t| j}| d  | t t| j| d S r   )getattrsysorig_streamrM  r   )r
   orig_stdoutr   r   r   test_no_redirect_in_init  s    
z+TestRedirectStream.test_no_redirect_in_initc                 C   s   t  }d}tt| j}| |$ t|tt| jd W d    n1 sL0    Y  | tt| j| | 	 }| 
|| d S )Nz<Consider an API like help(), which prints directly to stdoutfile)ioStringIOrN  rO  rP  rM  printr   getvaluestripr8   )r
   r   msgrQ  sr   r   r   test_redirect_to_string_io  s    2z-TestRedirectStream.test_redirect_to_string_ioc                 C   sB   t  }| |}| || W d    n1 s40    Y  d S r   )rU  rV  rM  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    n1 sN0    Y  |$ tdtt| jd W d    n1 s0    Y  | tt| j| | }| 	|d d S NHello )endrT  zWorld!rS  zHello World!

rU  rV  rM  rN  rO  rP  rW  r   rX  r8   r
   r   Z
write_to_frQ  r[  r   r   r   test_cm_is_reusable  s    
42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    n1 sh0    Y  W d    n1 s0    Y  | tt| j| | }| 	|d d S r_  rc  rd  r   r   r   test_cm_is_reentrant  s    
Pz'TestRedirectStream.test_cm_is_reentrant)r   r   r   rM  rP  r   r   r   rR  r\  r^  re  rf  r   r   r   r   rL    s   

rL  c                   @   s   e Zd ZeZdZdS )TestRedirectStdoutstdoutN)r   r   r   redirect_stdoutrM  rP  r   r   r   r   rg    s   rg  c                   @   s   e Zd ZeZdZdS )TestRedirectStderrstderrN)r   r   r   redirect_stderrrM  rP  r   r   r   r   rj  	  s   rj  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 r   )suppressrp   r8   r   r   r   r   r     s    zTestSuppress.test_instance_docsc                 C   s6   t t}| | W d    n1 s(0    Y  d S r   )rn  r$  r\   )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    n1 s00    Y  d S )Nr   r       )rn  r$  r8   powr    r   r   r   test_no_exception  s    
zTestSuppress.test_no_exceptionc                 C   s4   t t td W d    n1 s&0    Y  d S )Nr   )rn  r   lenr    r   r   r   test_exact_exception   s    
z!TestSuppress.test_exact_exceptionc                 C   s4   t t dd  W d    n1 s&0    Y  d S )Nr`  2   )rn  LookupErrorr    r   r   r   test_exception_hierarchy$  s    
z%TestSuppress.test_exception_hierarchyc              	   C   s^   |  t@ tt dd  W d    n1 s20    Y  W d    n1 sP0    Y  d S r   )r   r<   rn  r   r    r   r   r   test_other_exception(  s    
z!TestSuppress.test_other_exceptionc              	   C   s\   |  t> t  dd  W d    n1 s00    Y  W d    n1 sN0    Y  d S r   )r   r<   rn  r    r   r   r   test_no_args-  s    zTestSuppress.test_no_argsc                 C   sh   t tt dd  W d    n1 s(0    Y  t tt td W d    n1 sZ0    Y  d S )Nr.   r   r   )rn  r<   r   rs  r    r   r   r   test_multiple_exception_args2  s    &z)TestSuppress.test_multiple_exception_argsc              	   C   s   t t}| W d    n1 s"0    Y  | td W d    n1 sN0    Y  |H | td W d    n1 s0    Y  d}dd  W d    n1 s0    Y  | | d S )Nr   Tr.   r   )rn  rM   rs  r(   )r
   Zignore_exceptionsZouter_continuedr   r   r   rf  8  s    &&&z!TestSuppress.test_cm_is_reentrantN)r   r   r   r   r   r   ro  rr  rt  rw  rx  ry  rz  rf  r   r   r   r   rm    s   
rm  __main__)rp   rU  rO  r   r   Zunittest
contextlibr   r   r}   ZTestCaser   r,   r   r   r   r   r   r   r   r   rJ  rL  rg  rj  rm  r   mainr   r   r   r   <module>   s:   + Y"	0 !   96
