o
    g                     @   sb  	 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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re  dS dS )"    N)*)support)	os_helperc                   @   $   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                          e Zd Z fddZ  ZS )z;TestAbstractContextManager.test_enter.<locals>.DefaultEnterc                       t  j|  d S Nsuper__exit__selfargs	__class__ J/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/test/test_contextlib.pyr         zDTestAbstractContextManager.test_enter.<locals>.DefaultEnter.__exit____name__
__module____qualname__r   __classcell__r   r   r   r   DefaultEnter       r   )AbstractContextManagerassertIs	__enter__)r   r   managerr   r   r   
test_enter   s   z%TestAbstractContextManager.test_enterc                 C   sF   G dd dt }| t |  W d    d S 1 sw   Y  d S )Nc                   @      e Zd ZdS )zETestAbstractContextManager.test_exit_is_abstract.<locals>.MissingExitNr   r   r   r   r   r   r   MissingExit       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                   @      e Zd Zdd Zdd ZdS )zRTestAbstractContextManager.test_structural_subclassing.<locals>.ManagerFromScratchc                 S      | S r	   r   r   r   r   r   r   !      z\TestAbstractContextManager.test_structural_subclassing.<locals>.ManagerFromScratch.__enter__c                 S      d S r	   r   )r   exc_type	exc_value	tracebackr   r   r   r   #   r+   z[TestAbstractContextManager.test_structural_subclassing.<locals>.ManagerFromScratch.__exit__Nr   r   r   r   r   r   r   r   r   ManagerFromScratch        r1   c                       r   )zLTestAbstractContextManager.test_structural_subclassing.<locals>.DefaultEnterc                    r   r	   r
   r   r   r   r   r   )   r   zUTestAbstractContextManager.test_structural_subclassing.<locals>.DefaultEnter.__exit__r   r   r   r   r   r   (   r   r   c                   @      e Zd ZdZdS )zGTestAbstractContextManager.test_structural_subclassing.<locals>.NoEnterN)r   r   r   r   r   r   r   r   NoEnter.       r4   c                   @   r3   )zFTestAbstractContextManager.test_structural_subclassing.<locals>.NoExitN)r   r   r   r   r   r   r   r   NoExit3   r5   r6   )
assertTrue
issubclassr   assertFalse)r   r1   r   r4   r6   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}| }|  dg | |d  | W d    n1 s*w   Y  |  g d d S )Nc                   3   s       d dV    d d S N   *     appendr   stater   r   woohoo=      
z@ContextManagerTestCase.test_contextmanager_plain.<locals>.woohoor=   r>   r=   r>   r?   )contextmanagerassertEqualrA   r   rD   xr   rB   r   test_contextmanager_plain;   s   z0ContextManagerTestCase.test_contextmanager_plainc              	      s   g  t  fdd}| t) | }|  dg | |d  | t 1 s,w   Y  W d    n1 s;w   Y  |  g d d S )Nc                	   3   s0      d zdV  W   d d S   d w r<   r@   r   rB   r   r   rD   J   s
   
zBContextManagerTestCase.test_contextmanager_finally.<locals>.woohoor=   r>   rF   )rG   r%   ZeroDivisionErrorrH   rA   rI   r   rB   r   test_contextmanager_finallyH   s   
z2ContextManagerTestCase.test_contextmanager_finallyc                 C   s6   t dd }| }|  | |ttdd  d S )Nc                   s       d V  d S r	   r   r   r   r   r   wheeZ      
zCContextManagerTestCase.test_contextmanager_no_reraise.<locals>.wheefoo)rG   r   r9   r   r&   )r   rO   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"    zd V  W d S    d V  Y d S r	   r   r   r   r   r   whooc   s
   zOContextManagerTestCase.test_contextmanager_trap_yield_after_throw.<locals>.whoorQ   )rG   r   r%   RuntimeErrorr   r&   )r   rT   rR   r   r   r   *test_contextmanager_trap_yield_after_throwb   s   
zAContextManagerTestCase.test_contextmanager_trap_yield_after_throwc                    sn   g t  fdd}| } dg  |d | td1 s(w   Y   g d d S )Nc               
   3   s`     d zdV  W d S  ty/ }  z | jd   g d W Y d } ~ d S d } ~ ww )Nr=   r>   r   rF   )rA   rL   r   rH   )er   rC   r   r   rD   q   s   
zAContextManagerTestCase.test_contextmanager_except.<locals>.woohoor=   r>   r?   rF   )rG   rH   rA   rL   rI   r   rX   r   test_contextmanager_excepto   s   
z1ContextManagerTestCase.test_contextmanager_exceptc                 C   s   t dd }G dd dt}td|dfD ]K}| jt|d9 z|  |1 s+w   Y  W n tyI } z| || W Y d }~nd }~ww | | d W d    q1 s\w   Y  qd S )Nc                   s   rN   r	   r   r   r   r   r   rD      rP   zJContextManagerTestCase.test_contextmanager_except_stopiter.<locals>.woohooc                   @   r!   )zYContextManagerTestCase.test_contextmanager_except_stopiter.<locals>.StopIterationSubclassNr"   r   r   r   r   StopIterationSubclass   r$   rZ   spam)typez was suppressed)rG   StopIterationZsubTestr\   	Exceptionr   fail)r   rD   rZ   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|  |1 sw   Y  W n ty; } z| || W Y d }~d S d }~ww | d d S )Nztfrom __future__ import generator_stop
from contextlib import contextmanager
@contextmanager
def woohoo():
    yield
rD   r[   zStopIteration was suppressed)execr]   r^   r   r_   )r   codelocalsrD   r`   ra   r   r   r   !test_contextmanager_except_pep479   s   z8ContextManagerTestCase.test_contextmanager_except_pep479c              
   C   s   t dd }z|  t1 sw   Y  W n* ty@ } z| t|t | |jd d | |j	t W Y d }~nd }~ww z|  t
d1 sMw   Y  W d S  ty~ } z| t|t
 | |jd d | |j	 W Y d }~d S d }~ww )Nc               
   s   s2    zd V  W d S  t y }  ztd| d } ~ ww )Nissue29692:Chained)r^   rU   excr   r   r   test_issue29692      
zoContextManagerTestCase.test_contextmanager_do_not_unchain_non_stopiteration_exceptions.<locals>.test_issue29692r   rg   zissue29692:Unchained)rG   rL   r^   r   r\   rU   rH   r   assertIsInstance	__cause__r]   assertIsNone)r   rj   ra   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   )rv   rw   r   ru   r   attribs   s   zFContextManagerTestCase._create_contextmanager_attribs.<locals>.attribsbar)rQ   c                 S   r,   r	   r   )r[   r   r   r   baz   s   zBContextManagerTestCase._create_contextmanager_attribs.<locals>.bazrG   )r   rx   rz   r   r   r   _create_contextmanager_attribs   s
   z5ContextManagerTestCase._create_contextmanager_attribsc                 C   s(   |   }| |jd | |jd d S )Nrz   ry   )r|   rH   r   rQ   r   rz   r   r   r   test_contextmanager_attribs   s   z2ContextManagerTestCase.test_contextmanager_attribsc                 C   s   |   }| |jd d S NzWhee!r|   rH   __doc__r}   r   r   r   test_contextmanager_doc_attrib   s   z5ContextManagerTestCase.test_contextmanager_doc_attribc                 C   s   |   d }| |jd d S r   r   r}   r   r   r   *test_instance_docstring_given_cm_docstring   s   zAContextManagerTestCase.test_instance_docstring_given_cm_docstringc                 C   sN   t dd }|ddddd}| |d W d    d S 1 s w   Y  d S )	Nc                 s   s    | |||fV  d S r	   r   r   rr   r   kwdsr   r   r   rD      s   z4ContextManagerTestCase.test_keywords.<locals>.woohoo      !   ,   r   )r   r   r   r   rG   rH   )r   rD   targetr   r   r   test_keywords   s
   
"z$ContextManagerTestCase.test_keywordsc                    sT   G dd d}t  fdd}|| | d	 W d    d S 1 s#w   Y  d S )Nc                   @   r!   )z0ContextManagerTestCase.test_nokeepref.<locals>.ANr"   r   r   r   r   A   r$   r   c                 3   s@    t | } t |}t   |    |  d V  d S r	   )weakrefrefr   
gc_collectrn   abr*   r   r   rD      s   


z5ContextManagerTestCase.test_nokeepref.<locals>.woohoor   r{   )r   r   rD   r   r*   r   test_nokeepref   s   	"z%ContextManagerTestCase.test_nokeeprefc                 C   s   t dd }| t |  W d    n1 sw   Y  | t |dd W d    n1 s3w   Y  | t |dd W d    d S 1 sNw   Y  d S )Nc                s   rN   r	   r   r   r   r   r   rD      rP   z8ContextManagerTestCase.test_param_errors.<locals>.woohoo      r   )rG   r%   r&   r   rD   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=   )rH   )before)depthr   r   r   rD     s   z5ContextManagerTestCase.test_recursive.<locals>.woohooc                      s    dk r	  d S d S )N
   r   r   )r   	recursiver   r   r   
  s   
z8ContextManagerTestCase.test_recursive.<locals>.recursiver   r   r   )r   r   r   r   test_recursive   s   z%ContextManagerTestCase.test_recursiveN)r   r   r   rK   rM   rS   rV   rY   rb   rf   ro   r|   r~   r   requires_docstringsr   r   r   r   r   r   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 r	   )closingr   rH   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 s)w   Y  |   dg d S )Nc                          e Zd Z fddZdS )z'ClosingTestCase.test_closing.<locals>.Cc                         d d S r   r@   r*   rB   r   r   close     z-ClosingTestCase.test_closing.<locals>.C.closeNr   r   r   r   r   rB   r   r   C      r   r=   )rH   r   r   r   rJ   yr   rB   r   test_closing  s   
zClosingTestCase.test_closingc              	      s   g  G  fddd}| }|   g  | t& t|}|  || dd  W d    n1 s3w   Y  W d    n1 sBw   Y  |   dg d S )Nc                       r   )z-ClosingTestCase.test_closing_error.<locals>.Cc                    r   r   r@   r*   rB   r   r   r   *  r   z3ClosingTestCase.test_closing_error.<locals>.C.closeNr   r   rB   r   r   r   )  r   r   r=   r   )rH   r%   rL   r   r   r   rB   r   test_closing_error'  s   

z"ClosingTestCase.test_closing_errorN)r   r   r   r   r   r   r   r   r   r   r   r   r     s
    
r   c                   @      e Zd Zdd ZdS )NullcontextTestCasec                 C   sN   G dd d}| }t |}| || W d    d S 1 s w   Y  d S )Nc                   @   r!   )z/NullcontextTestCase.test_nullcontext.<locals>.CNr"   r   r   r   r   r   7  r$   r   )nullcontextr   )r   r   cZc_inr   r   r   test_nullcontext6  s
   
"z$NullcontextTestCase.test_nullcontextN)r   r   r   r   r   r   r   r   r   5  s    r   c                   @   r   )FileContextTestCasec              	   C   s  t  }zxd }t|ddd}| |j |d W d    n1 s$w   Y  | |j d }| t1 t|ddd}| |j | 	|
 d dd  W d    n1 s[w   Y  W d    n1 sjw   Y  | |j W t| d S t| w )Nwzutf-8)encodingzBooh
rr=   r   )tempfilemktempopenr9   closedwriter7   r%   rL   rH   readr   unlink)r   Ztfnfr   r   r   testWithOpen@  s&   
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 sw   Y  |  |  | t$ | | |  dd  W d    n1 sAw   Y  W d    n1 sPw   Y  |  |  d S Nr=   r   )r9   r7   r%   rL   )r   locklockedr   r   r   boilerPlateT  s   
zLockContextTestCase.boilerPlatec                 C      t  }| ||j d S r	   )	threadingLockr   r   r   r   r   r   r   testWithLock_     z LockContextTestCase.testWithLockc                 C   r   r	   )r   RLockr   	_is_ownedr   r   r   r   testWithRLockc  r   z!LockContextTestCase.testWithRLockc                    $   t    fdd}|  | d S )Nc                      s      S r	   )r   r   r   r   r   r   i  s   z5LockContextTestCase.testWithCondition.<locals>.locked)r   	Conditionr   r   r   r   r   r   testWithConditiong  s   z%LockContextTestCase.testWithConditionc                    r   )Nc                           dr   dS dS NFTacquirereleaser   r   r   r   r   o     
z5LockContextTestCase.testWithSemaphore.<locals>.locked)r   	Semaphorer   r   r   r   r   testWithSemaphorem     z%LockContextTestCase.testWithSemaphorec                    r   )Nc                      r   r   r   r   r   r   r   r   y  r   z<LockContextTestCase.testWithBoundedSemaphore.<locals>.locked)r   BoundedSemaphorer   r   r   r   r   testWithBoundedSemaphorew  r   z,LockContextTestCase.testWithBoundedSemaphoreN)	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dZdZdd Zdd ZdS )	mycontextFNc                 C   
   d| _ | S NTstartedr*   r   r   r   r        zmycontext.__enter__c                 G   s   || _ | jS r	   )ri   catchr   ri   r   r   r   r     s   zmycontext.__exit__)r   r   r   r   ri   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      t j}t  }| |j| d S r	   )r   r   rH   r   r   r   r   r        z'TestContextDecorator.test_instance_docsc                 C   sT   t  }|}| || | |j W d    n1 sw   Y  | |jd d S )NNNN)r   r   r7   r   rH   ri   )r   contextresultr   r   r   test_contextdecorator  s   z*TestContextDecorator.test_contextdecoratorc              	   C   s   t  }| td | td1 sw   Y  W d    n1 s#w   Y  | |j | |jd t t  }d|_| td1 sGw   Y  | |j | |jd t d S )NrQ   r   T)r   assertRaisesRegex	NameErrorassertIsNotNoneri   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                           j  j d S r	   rn   ri   r7   r   r   r   r   r   r   test     z1TestContextDecorator.test_decorator.<locals>.testr   )r   rH   ri   r   r   r   r   r   test_decorator  s
   z#TestContextDecorator.test_decoratorc                    sl   t     fdd}td |  W d    n1 s w   Y   j  jd t d S )Nc                      s      j  j td)NrQ   )rn   ri   r7   r   r   r   r   r   r   r     s   z@TestContextDecorator.test_decorator_with_exception.<locals>.testrQ   r   )r   r   r   r   ri   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.methodr	   )r   r   r   r   r   r   r   r   Test  s    r   r=      r   r   r   r   )r   objectr   rH   r   r   r   )r   r   r   r   r   r   test_decorating_method  s    	z+TestContextDecorator.test_decorating_methodc              	   C   v   G dd dt }| t$ |  W d    n1 sw   Y  W d    d S W d    d S 1 s4w   Y  d S )Nc                   @   r(   )z7TestContextDecorator.test_typo_enter.<locals>.mycontextc                 S   r,   r	   r   r*   r   r   r   	__unter__  r+   zATestContextDecorator.test_typo_enter.<locals>.mycontext.__unter__c                 W   r,   r	   r   r   r   r   r   r     r+   z@TestContextDecorator.test_typo_enter.<locals>.mycontext.__exit__N)r   r   r   r  r   r   r   r   r   r     r2   r   ContextDecoratorr%   AttributeErrorr   r   r   r   r   test_typo_enter     "z$TestContextDecorator.test_typo_enterc              	   C   r  )Nc                   @   r(   )z6TestContextDecorator.test_typo_exit.<locals>.mycontextc                 S   r,   r	   r   r*   r   r   r   r     r+   z@TestContextDecorator.test_typo_exit.<locals>.mycontext.__enter__c                 W   r,   r	   r   r   r   r   r   __uxit__  r+   z?TestContextDecorator.test_typo_exit.<locals>.mycontext.__uxit__N)r   r   r   r   r
  r   r   r   r   r     r2   r   r  r  r   r   r   test_typo_exit  r	  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   r   r   r   r*   r   r   r   r   	  r   zRTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontext.__enter__c                 W   
   || _ d S r	   rh   r   r   r   r   r        
zQTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontext.__exit__)r   r   r   r   ri   r   r   r   r   r   r   somecontext  s
    r  c                   @   r!   )zFTestContextDecorator.test_contextdecorator_as_mixin.<locals>.mycontextNr"   r   r   r   r   r     r$   r   c                      r   r	   r   r   r   r   r   r     r   zATestContextDecorator.test_contextdecorator_as_mixin.<locals>.testr   )r   r  rH   ri   )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 )Nr?   r@   )r   rB   r   r   rD     rE   zETestContextDecorator.test_contextmanager_as_decorator.<locals>.woohoor=   c                    s     dg |  d S r   )rH   rA   )rJ   rX   r   r   r   $  s   zCTestContextDecorator.test_contextmanager_as_decorator.<locals>.test	something)r=   r  r?   something else)r=   r  r?   r   )r   rD   r   r   rX   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S )(TestBaseExitStackNc                 C   s"   | j j}|   }| |j| d S r	   )
exit_stackr   rH   r   r   r   r   r   4  s   z$TestBaseExitStack.test_instance_docsc                 C   s.   |   	 W d    d S 1 sw   Y  d S r	   )r  r*   r   r   r   test_no_resources;  s   
"z#TestBaseExitStack.test_no_resourcesc                    s0  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
}|  h}t|D ]8\}}|rJ|rJ|j|g|R i |}n|rV|j|g|R  }n|rb|j|fi |}n||}| || q5|jD ]}| |d j| | |d j|j | 	|d j
|j
 qqW d    n1 sw   Y  |  | g  |  [}| t |jdd W d    n1 sw   Y  | t | jjdd W d    n1 sw   Y  | t |j|dd W d    n1 sw   Y  W d    n	1 sw   Y  |  g  d S )Nr   )r=   )r=   r   r=   )Zexampler      )r   callbackc                     s   	   | |f d S r	   r@   )r   r   r   r   r   _exitJ  s   z.TestBaseExitStack.test_callback.<locals>._exit)argr   )r  r  )dictr  reversedr  r   _exit_callbacks__wrapped__ZassertNotEqualr   rn   r   rH   r%   r&   )r   Zexpectedr  stackr   r   r   wrapperr   r  r   test_callback?  sR   	



zTestBaseExitStack.test_callbackc                    s@  t   fdd}dd }fdd}G dd dt} w}|| |jd	 d
 | ||}|| |jd	 d
 j| || |jd	 d
 | ||}|| |jd	 d
 j| || |jd	 d
 | || |jd	 d
 | d
d  W d    d S 1 sw   Y  d S )Nc                    s    |   d S r	   )r   r-   ri   exc_tbZ
exc_raisedr   r   r   _expect_excj  r   z0TestBaseExitStack.test_push.<locals>._expect_excc                  W      dS r   r   exc_detailsr   r   r   _suppress_excl  r+   z2TestBaseExitStack.test_push.<locals>._suppress_excc                    s"     |    |   | d S r	   )rn   r"  r*   r   r   
_expect_okn  s   

z/TestBaseExitStack.test_push.<locals>._expect_okc                   @   r   )z+TestBaseExitStack.test_push.<locals>.ExitCMc                 S   r  r	   	check_exc)r   r,  r   r   r   __init__s  r  z4TestBaseExitStack.test_push.<locals>.ExitCM.__init__c                 S   s   |  d d S )NzShould not be called!)r_   r*   r   r   r   r   u  r   z5TestBaseExitStack.test_push.<locals>.ExitCM.__enter__c                 W   s   | j |  d S r	   r+  r   r(  r   r   r   r   w  r   z4TestBaseExitStack.test_push.<locals>.ExitCM.__exit__Nr   r   r   r-  r   r   r   r   r   r   ExitCMr      r0  r=   r   )rL   r   r  pushr   r  __self__)r   r%  r)  r*  r0  r  cmr   r$  r   	test_pushh  s,   







"zTestBaseExitStack.test_pushc                    s   G  fdddt }g  | }|  ,}|j fdd}| | || | |jd d j|  d W d    n1 sBw   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                    r   r   r@   r*   r  r   r   r     r   z>TestBaseExitStack.test_enter_context.<locals>.TestCM.__enter__c                    r   Nr   r@   r.  r  r   r   r     r   z=TestBaseExitStack.test_enter_context.<locals>.TestCM.__exit__Nr0   r   r  r   r   TestCM  s    r8  c                      r   )Nr  r@   r   r  r   r   r       z3TestBaseExitStack.test_enter_context.<locals>._exitr2  r=   r   )r=   r   r   r  )
r   r  r  r   enter_contextr   r  r4  rA   rH   )r   r8  r5  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 s(w   Y  |  ddg d S )Nc                      r   r   r@   r   r  r   r   r    r9  z+TestBaseExitStack.test_close.<locals>._exitr   r=   )r  r  r   r   rA   rH   )r   r  r  r   r  r   
test_close  s   

zTestBaseExitStack.test_closec                    s   g  |   }|j fdd}| | | } d W d    n1 s(w   Y   d |  |  g d d S )Nc                      r   r7  r@   r   r  r   r   r    r9  z-TestBaseExitStack.test_pop_all.<locals>._exitr=   r   )r=   r   r   )r  r  r   pop_allrA   r   rH   )r   r  r  	new_stackr   r  r   test_pop_all  s   


zTestBaseExitStack.test_pop_allc              	   C   s~   |  t0 |  }|dd  dd  W d    n1 s w   Y  W d    d S W d    d S 1 s8w   Y  d S )Nc                  W   r&  )NFr   rh   r   r   r   <lambda>      z3TestBaseExitStack.test_exit_raise.<locals>.<lambda>r=   r   )r%   rL   r  r3  r   r  r   r   r   test_exit_raise  s   

"z!TestBaseExitStack.test_exit_raisec                 C   sD   |   }|dd  dd  W d    d S 1 sw   Y  d S )Nc                  W   r&  r   r   rh   r   r   r   r@    rA  z6TestBaseExitStack.test_exit_suppress.<locals>.<lambda>r=   r   r  r3  rB  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}zV|t H |tt3 |   |t dd  W d    n1 s8w   Y  W d    n1 sGw   Y  W d    n1 sVw   Y  W d    n1 sew   Y  W n) t y } z| |jt | |jjt | |jjj W Y d }~n
d }~ww | d	 |jd }| |t | |jt	 d S )
Nc                   @   r   )zJTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcc                 S   r  r	   rh   r   r   r   r   r-    r  zSTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__init__c                 S   r)   r	   r   r*   r   r   r   r     r+   zTTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__enter__c                 W   s   | j r	   rh   r.  r   r   r   r     s   zSTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__exit__Nr/  r   r   r   r   RaiseExc  r1  rF  c                   @   r   )zUTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContextc                 S   s   || _ || _d S r	   )outerinner)r   rG  rH  r   r   r   r-    s   
z^TestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__init__c                 S   r)   r	   r   r*   r   r   r   r     r+   z_TestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__enter__c                 W   s   z| j    | jr	   )rH  rG  r.  r   r   r   r     s   z^TestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__exit__Nr/  r   r   r   r   RaiseExcWithContext  s    rI  c                   @   r(   )zMTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExcc                 S   r)   r	   r   r*   r   r   r   r     r+   zWTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExc.__enter__c                 W   s   |t | _dS r   )r\   saved_detailsr.  r   r   r   r     s   
zVTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExc.__exit__Nr0   r   r   r   r   SuppressExc  r2   rK  r=   r   0Expected IndexError, but no exception was raised)

IndexErrorKeyErrorr  
ValueErrorrl   __context__rn   r_   rJ  rL   )r   rF  rI  rK  ri   	inner_excr   r   r   &test_exit_exception_chaining_reference  s6   




z8TestBaseExitStack.test_exit_exception_chaining_referencec              
      s
  dd }d   fdd}z7|   )}||t ||t ||t || ||t dd  W d    n1 s=w   Y  W n) tyl } z| |jt | |jjt | 	|jjj W Y d }~n
d }~ww | 
d  d }| |t | |jt d S )Nc                 S      | r	   r   rh   r   r   r   	raise_exc  r+   zATestBaseExitStack.test_exit_exception_chaining.<locals>.raise_excc                     s   |  dS r   r   r'  rJ  r   r   suppress_exc  s   zDTestBaseExitStack.test_exit_exception_chaining.<locals>.suppress_excr=   r   rL  )r  r  rM  rN  r  r3  rO  rl   rP  rn   r_   rL   )r   rT  rV  r  ri   rQ  r   rU  r   test_exit_exception_chaining  s.   



z.TestBaseExitStack.test_exit_exception_chainingc              
   C   s"  dd }dd }z#|   }|dd  ||t W d    n1 s%w   Y  W n tyC } z| |t W Y d }~n
d }~ww | d z'|   }||t || ||t W d    n1 sjw   Y  W n ty } z| |t W Y d }~d S d }~ww | d d S )	Nc                 S   rS  r	   r   rh   r   r   r   rT    r+   zHTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.raise_excc                  W   r&  r   r   r'  r   r   r   rV    r+   zKTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.suppress_excc                   S   r,   r	   r   r   r   r   r   r@    rA  zGTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.<lambda>rL  z.Expected KeyError, but no exception was raised)r  r  rM  r^   rl   r_   rN  r3  )r   rT  rV  r  ri   r   r   r   #test_exit_exception_non_suppressing  s2   



z5TestBaseExitStack.test_exit_exception_non_suppressingc              
   C   s   t dd }td}td}td}td}z'|  }||| ||| ||| |1 s6w   Y  W d S  tyw } z.| || | |j| | |jj| | |jjj| | |jjjj W Y d }~d S d }~ww )Nc                 s   s    zd V  W | | r	   r   rh   r   r   r   gets_the_context_right,  s   zZTestBaseExitStack.test_exit_exception_with_correct_context.<locals>.gets_the_context_rightr=   r   r   r  )rG   r^   r  r:  r   rP  rn   )r   rY  exc1exc2exc3exc4r  ri   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}z |  }|||| |||| |1 s1w   Y  W d S  t y} } z9| || | |j| | |jj| | |jjj| | |jjjj| | |jjjjj W Y d }~d S d }~ww )Nc                 S   s
   z| |r	   r   )rQ  Z	outer_excr   r   r   raise_nestedL  s   zQTestBaseExitStack.test_exit_exception_with_existing_context.<locals>.raise_nestedr=   r   r   r  r   )r^   r  r  r   rP  rn   )	r   r_  rZ  r[  r\  r]  Zexc5r  ri   r   r   r   )test_exit_exception_with_existing_contextI  s2   
z;TestBaseExitStack.test_exit_exception_with_existing_contextc              
   C   s~   dd }z"|   }|| dd  W d    W d S 1 sw   Y  W d S  ty> } z| d W Y d }~d S d }~ww )Nc                  W   r&  r   r   r'  r   r   r   rV  f  r+   zDTestBaseExitStack.test_body_exception_suppress.<locals>.suppress_excr=   r   z%Expected no exception, got IndexError)r  r3  rM  r_   )r   rV  r  ri   r   r   r   test_body_exception_suppresse  s   


&z.TestBaseExitStack.test_body_exception_suppressc                 C   sX   |   }|dd  |dd  |dd  W d    d S 1 s%w   Y  d S )Nc                  W   r&  r   r   rh   r   r   r   r@  q  rA  zITestBaseExitStack.test_exit_exception_chaining_suppress.<locals>.<lambda>c                  W   s   dd S r   r   rh   r   r   r   r@  r      c                  W   s   i d S r   r   rh   r   r   r   r@  s  rb  rD  rB  r   r   r   %test_exit_exception_chaining_suppresso  s
   
"z7TestBaseExitStack.test_exit_exception_chaining_suppressc                 C   sF   |   }tdD ]}|t q	W d    d S 1 sw   Y  d S )Ni'  )r  ranger  int)r   r  ir   r   r   test_excessive_nestingu  s
   
"z(TestBaseExitStack.test_excessive_nestingc                 C   sZ   G dd dt }| }t  |_|  }| t|j| || | |jd d | d S )Nc                   @   r!   )z7TestBaseExitStack.test_instance_bypass.<locals>.ExampleNr"   r   r   r   r   Example|      rh  r2  r=   )	r   r   r  r%   r  r:  r3  r   r  )r   rh  r5  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 }|  %}|  }||  ||  |d	1 s<w   Y  W d    n1 sKw   Y  |j}| |  | |j| | 	|jj | 	|jj
 | |j
|j d S )
Nc                   @   r!   )zITestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.UniqueExceptionNr"   r   r   r   r   UniqueException  ri  rk  c                   @   r!   )zLTestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.UniqueRuntimeErrorNr"   r   r   r   r   UniqueRuntimeError  ri  rl  c               
   3   s2    zdV  W d S  t y }  z d| d } ~ ww )Nr=   znew exceptionr^   rh   rk  r   r   second  rk   z@TestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.secondc               
   s   s,    zdV  W d S  t y }  z| d } ~ ww r   rm  rh   r   r   r   first  s   z?TestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.firstzplease no infinite loop.)r^   rU   rG   r%   r  r:  	exceptionrl   rP  rn   rm   r   )r   rl  ro  rp  Zerr_ctxZes_ctxri   r   rn  r   test_dont_reraise_RuntimeError  s&   

z0TestBaseExitStack.test_dont_reraise_RuntimeError)r   r   r   r  r   r   r   r  r!  r6  r;  r<  r?  rC  rE  rR  rW  rX  r^  r`  ra  rc  rg  rj  rr  r   r   r   r   r  1  s,    
)"0
	r  c                   @   s   e Zd ZeZdS )TestExitStackN)r   r   r   	ExitStackr  r   r   r   r   rs    r5   rs  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_streamr   rH   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_streamrv  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 s&w   Y  | tt| j| | 	 }| 
|| d S )Nz<Consider an API like help(), which prints directly to stdoutfile)ioStringIOrw  rx  ry  rv  printr   getvaluestriprH   )r   r   msgrz  sr   r   r   test_redirect_to_string_io  s   z-TestRedirectStream.test_redirect_to_string_ioc                 C   sD   t  }| |}| || W d    d S 1 sw   Y  d S r	   )r~  r  rv  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 s'w   Y  | tdtt| jd W d    n1 sCw   Y  | tt| j| | }| 	|d d S NHello )endr}  zWorld!r|  zHello World!

r~  r  rv  rw  rx  ry  r  r   r  rH   r   r   Z
write_to_frz  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    n1 s4w   Y  W d    n1 sCw   Y  | tt| j| | }| 	|d d S r  r  r  r   r   r   test_cm_is_reentrant  s   
z'TestRedirectStream.test_cm_is_reentrant)r   r   r   rv  ry  r   r   r   r{  r  r  r  r  r   r   r   r   ru    s    

ru  c                   @      e Zd ZeZdZdS )TestRedirectStdoutstdoutN)r   r   r   redirect_stdoutrv  ry  r   r   r   r   r        r  c                   @   r  )TestRedirectStderrstderrN)r   r   r   redirect_stderrrv  ry  r   r   r   r   r    r  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   r   r	   )suppressr   rH   r   r   r   r   r     r   zTestSuppress.test_instance_docsc                 C   s8   t t}| | W d    d S 1 sw   Y  d S r	   )r  rO  rn   )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    d S 1 sw   Y  d S )Nr   r       )r  rO  rH   powr*   r   r   r   test_no_exception  s   
"zTestSuppress.test_no_exceptionc                 C   s6   t t td W d    d S 1 sw   Y  d S )Nr   )r  r&   lenr*   r   r   r   test_exact_exception     

"z!TestSuppress.test_exact_exceptionc                 C   s6   t t dd  W d    d S 1 sw   Y  d S )Nr  2   )r  LookupErrorr*   r   r   r   test_exception_hierarchy  r  z%TestSuppress.test_exception_hierarchyc              	   C   sp   |  t) tt dd  W d    n1 sw   Y  W d    d S W d    d S 1 s1w   Y  d S r   )r%   rL   r  r&   r*   r   r   r   test_other_exception	  s   

"z!TestSuppress.test_other_exceptionc              	   C   sn   |  t( t  dd  W d    n1 sw   Y  W d    d S W d    d S 1 s0w   Y  d S r   )r%   rL   r  r*   r   r   r   test_no_args  s   
"zTestSuppress.test_no_argsc                 C   sj   t tt dd  W d    n1 sw   Y  t tt td W d    d S 1 s.w   Y  d S )Nr=   r   r   )r  rL   r&   r  r*   r   r   r   test_multiple_exception_args  s   

"z)TestSuppress.test_multiple_exception_argsc              	   C   s   t t}| W d    n1 sw   Y  | td W d    n1 s'w   Y  |$ | td W d    n1 s@w   Y  d}dd  W d    n1 sUw   Y  | | d S )Nr   Tr=   r   )r  r^   r  r7   )r   Zignore_exceptionsZouter_continuedr   r   r   r    s   


z!TestSuppress.test_cm_is_reentrantN)r   r   r   r   r   r   r  r  r  r  r  r  r  r  r   r   r   r   r    s    
r  __main__)r~  rx  r   r   Zunittest
contextlibr   r   Ztest.supportr   r   ZTestCaser   r;   r   r   r   r   r  r   r   r  rs  ru  r  r  r  r   mainr   r   r   r   <module>   s>    + ["	0 !  x96