o
    GXh                     @   sj  	 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	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__ >/opt/python-3.10.19/usr/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   NoExit4   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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   K   s
   
zBContextManagerTestCase.test_contextmanager_finally.<locals>.woohoor=   r>   rF   )rG   r%   ZeroDivisionErrorrH   rA   rI   r   rB   r   test_contextmanager_finallyI   s   
z2ContextManagerTestCase.test_contextmanager_finallyc                 C   s@  t dd }z|  dd  W d    n1 sw   Y  W n ty7 } zt|j}W Y d }~nd }~ww | t|d | |d jd | |d jd G dd dt	}z|  |d	1 sfw   Y  W n |y } zt|j}W Y d }~nd }~ww | t|d | |d jd | |d jd
 G dd dt
}t
d|dfD ]n}| jt|d[ z|  |1 sw   Y  W n  t|y } z| || t|j}W Y d }~nd }~ww | | d | t|d | |d jd | |d jd W d    n	1 sw   Y  qd S )Nc                   s       d V  d S r	   r   r   r   r   r   f[      
z?ContextManagerTestCase.test_contextmanager_traceback.<locals>.fr=   r   test_contextmanager_traceback1/0c                   @   r!   )zRContextManagerTestCase.test_contextmanager_traceback.<locals>.RuntimeErrorSubclassNr"   r   r   r   r   RuntimeErrorSubclassj   r$   rS   r>   zraise RuntimeErrorSubclass(42)c                   @   r!   )zSContextManagerTestCase.test_contextmanager_traceback.<locals>.StopIterationSubclassNr"   r   r   r   r   StopIterationSubclassw   r$   rT   spamtype was suppressedzraise stop_exc)rG   rL   r/   
extract_tb__traceback__rH   lennamelineRuntimeErrorStopIterationsubTestrW   r   fail)r   rO   eZframesrS   rT   stop_excr   r   r   rQ   Z   s^   

z4ContextManagerTestCase.test_contextmanager_tracebackc                 C   s6   t dd }| }|  | |ttdd  d S )Nc                   s   rN   r	   r   r   r   r   r   whee   rP   zCContextManagerTestCase.test_contextmanager_no_reraise.<locals>.wheefoo)rG   r   r9   r   r&   )r   rd   ctxr   r   r   test_contextmanager_no_reraise   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   whoo   s
   zOContextManagerTestCase.test_contextmanager_trap_yield_after_throw.<locals>.whoore   )rG   r   r%   r^   r   r&   )r   rh   rf   r   r   r   *test_contextmanager_trap_yield_after_throw   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   )rb   r   rC   r   r   rD      s   
zAContextManagerTestCase.test_contextmanager_except.<locals>.woohoor=   r>   r?   rF   )rG   rH   rA   rL   rI   r   rj   r   test_contextmanager_except   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    n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   rT      r$   rT   rU   rV   rX   )rG   r_   r`   rW   	Exceptionr   ra   )r   rD   rT   rc   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   rU   zStopIteration was suppressed)execr_   rl   r   ra   )r   codelocalsrD   rc   rm   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)rl   r^   excr   r   r   test_issue29692      
zoContextManagerTestCase.test_contextmanager_do_not_unchain_non_stopiteration_exceptions.<locals>.test_issue29692r   rs   zissue29692:Unchained)rG   rL   rl   r   rW   r^   rH   r   assertIsInstance	__cause__r_   assertIsNone)r   rv   rm   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   )r   r   r   r   r   attribs   s   zFContextManagerTestCase._create_contextmanager_attribs.<locals>.attribsbar)re   c                 S   r,   r	   r   )rU   r   r   r   baz   s   zBContextManagerTestCase._create_contextmanager_attribs.<locals>.bazrG   )r   r   r   r   r   r   _create_contextmanager_attribs   s
   z5ContextManagerTestCase._create_contextmanager_attribsc                 C   s(   |   }| |jd | |jd d S )Nr   r   )r   rH   r   re   r   r   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   r~   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_collectrz   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   4  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_recursive2  s   z%ContextManagerTestCase.test_recursiveN)r   r   r   rK   rM   rQ   rg   ri   rk   rn   rr   r{   r   r   r   requires_docstringsr   r   r   r   r   r   r   r   r   r   r;   :   s(    2	

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_docsH  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   closeR     z-ClosingTestCase.test_closing.<locals>.C.closeNr   r   r   r   r   rB   r   r   CQ      r   r=   )rH   r   r   r   rJ   yr   rB   r   test_closingO  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_errorZ  s   

z"ClosingTestCase.test_closing_errorN)r   r   r   r   r   r   r   r   r   r   r   r   r   F  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   j  r$   r   )nullcontextr   )r   r   cZc_inr   r   r   test_nullcontexti  s
   
"z$NullcontextTestCase.test_nullcontextN)r   r   r   r   r   r   r   r   r   h  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tfnrO   r   r   r   testWithOpens  s&   
z FileContextTestCase.testWithOpenN)r   r   r   r   r   r   r   r   r   q  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   boilerPlate  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   testWithRLock  r   z!LockContextTestCase.testWithRLockc                    $   t    fdd}|  | d S )Nc                      s      S r	   )r   r   r   r   r   r     s   z5LockContextTestCase.testWithCondition.<locals>.locked)r   	Conditionr   r   r   r   r   r   testWithCondition  s   z%LockContextTestCase.testWithConditionc                    r   )Nc                           dr   dS dS NFTacquirereleaser   r   r   r   r        
z5LockContextTestCase.testWithSemaphore.<locals>.locked)r   	Semaphorer   r   r   r   r   testWithSemaphore     z%LockContextTestCase.testWithSemaphorec                    r   )Nc                      r   r   r   r   r   r   r   r     r   z<LockContextTestCase.testWithBoundedSemaphore.<locals>.locked)r   BoundedSemaphorer   r   r   r   r   testWithBoundedSemaphore  r   z,LockContextTestCase.testWithBoundedSemaphoreN)	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	   )ru   catchr   ru   r   r   r   r     s   zmycontext.__exit__)r   r   r   r   ru   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   ru   )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 )Nre   r   T)r   assertRaisesRegex	NameErrorassertIsNotNoneru   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	   rz   ru   r7   r   r   r   r   r   r   test     z1TestContextDecorator.test_decorator.<locals>.testr   )r   rH   ru   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)Nre   )rz   ru   r7   r   r   r   r  r   r   r    s   z@TestContextDecorator.test_decorator_with_exception.<locals>.testre   r   )r   r   r   r   ru   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	   rt   r   r   r   r   r   @     
zQTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontext.__exit__)r   r   r   r   ru   r   r   r   r   r   r   somecontext8  s
    r  c                   @   r!   )zFTestContextDecorator.test_contextdecorator_as_mixin.<locals>.mycontextNr"   r   r   r   r   r   C  r$   r   c                      r   r	   r   r   r  r   r   r  G  r  zATestContextDecorator.test_contextdecorator_as_mixin.<locals>.testr   )r  r  rH   ru   )r   r  r   r  r   r  r   test_contextdecorator_as_mixin7  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   P  rE   zETestContextDecorator.test_contextmanager_as_decorator.<locals>.woohoor=   c                    s     dg |  d S r   )rH   rA   )rJ   rj   r   r   r  W  s   zCTestContextDecorator.test_contextmanager_as_decorator.<locals>.test	something)r=   r  r?   something else)r=   r  r?   r   )r   rD   r  r   rj   r    test_contextmanager_as_decoratorO  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*d+ ZdS ),TestBaseExitStackNc                 C   s"   | j j}|   }| |j| d S r	   )
exit_stackr   rH   r   r   r   r   r   g  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_resourcesn  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   _exit}  s   z.TestBaseExitStack.test_callback.<locals>._exit)argr
  )r"  r%  )dictr  reversedr"  r   _exit_callbacks__wrapped__ZassertNotEqualr   rz   r   rH   r%   r&   )r   expectedr$  stackr   r   rO   wrapperr   r#  r   test_callbackr  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-   ru   exc_tbZ
exc_raisedr   r   r   _expect_exc  r   z0TestBaseExitStack.test_push.<locals>._expect_excc                  W      dS r   r   exc_detailsr   r   r   _suppress_exc  r+   z2TestBaseExitStack.test_push.<locals>._suppress_excc                    s"     |    |   | d S r	   )rz   r.  r*   r   r   
_expect_ok  s   

z/TestBaseExitStack.test_push.<locals>._expect_okc                   @   r   )z+TestBaseExitStack.test_push.<locals>.ExitCMc                 S   r  r	   	check_exc)r   r8  r   r   r   __init__  r  z4TestBaseExitStack.test_push.<locals>.ExitCM.__init__c                 S   s   |  d d S )NzShould not be called!)ra   r*   r   r   r   r     r   z5TestBaseExitStack.test_push.<locals>.ExitCM.__enter__c                 W   s   | j |  d S r	   r7  r   r4  r   r   r   r     r   z4TestBaseExitStack.test_push.<locals>.ExitCM.__exit__Nr   r   r   r9  r   r   r   r   r   r   ExitCM      r<  r=   r   )rL   r  r  pushr   r(  __self__)r   r1  r5  r6  r<  r+  cmr   r0  r   	test_push  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    rD  c                      r   )Nr!  r@   r   r#  r   r   r$       z3TestBaseExitStack.test_enter_context.<locals>._exitr>  r=   r
  )r=   r
  r   r!  )
r  r  r"  r   enter_contextr   r(  r@  rA   rH   )r   rD  rA  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$    rE  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   rC  r@   r   r#  r   r   r$    rE  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   r2  )NFr   rt   r   r   r   <lambda>      z3TestBaseExitStack.test_exit_raise.<locals>.<lambda>r=   r   )r%   rL   r  r?  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   r2  r   r   rt   r   r   r   rL    rM  z6TestBaseExitStack.test_exit_suppress.<locals>.<lambda>r=   r   r  r?  rN  r   r   r   test_exit_suppress  s   

"z$TestBaseExitStack.test_exit_suppressc              
   C   s   dd }z |   }||t dd  W d    n1 sw   Y  W n ty8 } z|}W Y d }~nd }~ww | |t t|j}dg| j ddg }| dd	 |D | | |j	t
 t|j	j}| d
d	 |D dg d S )Nc                 S      | r	   r   rt   r   r   r   	raise_exc  r+   zBTestBaseExitStack.test_exit_exception_traceback.<locals>.raise_excr=   r   )test_exit_exception_tracebackz with self.exit_stack() as stack:)_exit_wrapperzcallback(*args, **kwds))rS  z	raise excc                 S      g | ]}|j |jfqS r   r\   r]   .0rO   r   r   r   
<listcomp>      zCTestBaseExitStack.test_exit_exception_traceback.<locals>.<listcomp>c                 S   rV  r   rW  rX  r   r   r   rZ    r[  )rT  rR   )r  r"  
ValueErrorrx   r/   rY   rZ   callback_error_internal_framesrH   __context__rL   )r   rS  r+  rb   ru   Z	ve_framesr*  Z
zde_framesr   r   r   rT    s:   

z/TestBaseExitStack.test_exit_exception_tracebackc                 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	   rt   r   r   r   r   r9    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	   rt   r:  r   r   r   r     s   zSTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__exit__Nr;  r   r   r   r   RaiseExc  r=  r_  c                   @   r   )zUTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContextc                 S   s   || _ || _d S r	   )outerinner)r   r`  ra  r   r   r   r9    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	   )ra  r`  r:  r   r   r   r   $  s   z^TestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__exit__Nr;  r   r   r   r   RaiseExcWithContext  s    rb  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   )rW   saved_detailsr:  r   r   r   r   -  s   
zVTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExc.__exit__Nr0   r   r   r   r   SuppressExc*  r2   rd  r=   r   0Expected IndexError, but no exception was raised)

IndexErrorKeyErrorr  r\  rx   r^  rz   ra   rc  rL   )r   r_  rb  rd  ru   	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   rR  r	   r   rt   r   r   r   rS  E  r+   zATestBaseExitStack.test_exit_exception_chaining.<locals>.raise_excc                     s   |  dS r   r   r3  rc  r   r   suppress_excI  s   zDTestBaseExitStack.test_exit_exception_chaining.<locals>.suppress_excr=   r   re  )r  r"  rf  rg  r  r?  r\  rx   r^  rz   ra   rL   )r   rS  rk  r+  ru   rh  r   rj  r   test_exit_exception_chainingC  s.   



z.TestBaseExitStack.test_exit_exception_chainingc                    s   G dd dt  t fddtfdd}|fD ]E} 7 z|  t 1 s/w   Y  W n  yM } z|j W Y d }~n
d }~ww d W d    n1 s]w   Y  qd S )Nc                   @   r!   )zPTestBaseExitStack.test_exit_exception_explicit_none_context.<locals>.MyExceptionNr"   r   r   r   r   MyExceptionf  r$   rm  c                  3   s2    zd V  W d S  t y     } z| d | _w w r	   )BaseExceptionr^  rt   )rm  r   r   my_cmi  s   zJTestBaseExitStack.test_exit_exception_explicit_none_context.<locals>.my_cmc                  3   sB      } |    | V  W d    d S 1 sw   Y  d S r	   )r  rF  )r+  )ro  r   r   r   my_cm_with_exit_stackt  s
   
"zZTestBaseExitStack.test_exit_exception_explicit_none_context.<locals>.my_cm_with_exit_stackre  )rl   rG   r`   rf  rz   r^  ra   )r   rp  rA  ru   r   )rm  ro  r   r   )test_exit_exception_explicit_none_contextb  s(   


z;TestBaseExitStack.test_exit_exception_explicit_none_contextc              
   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   rR  r	   r   rt   r   r   r   rS    r+   zHTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.raise_excc                  W   r2  r   r   r3  r   r   r   rk    r+   zKTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.suppress_excc                   S   r,   r	   r   r   r   r   r   rL    rM  zGTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.<lambda>re  z.Expected KeyError, but no exception was raised)r  r"  rf  rl   rx   ra   rg  r?  )r   rS  rk  r+  ru   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   rt   r   r   r   gets_the_context_right  s   zZTestBaseExitStack.test_exit_exception_with_correct_context.<locals>.gets_the_context_rightr=   r
  r   r!  )rG   rl   r  rF  r   r^  rz   )r   rs  exc1exc2exc3exc4r+  ru   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   )rh  Z	outer_excr   r   r   raise_nested  s   zQTestBaseExitStack.test_exit_exception_with_existing_context.<locals>.raise_nestedr=   r
  r   r!  r   )rl   r  r"  r   r^  rz   )	r   ry  rt  ru  rv  rw  Zexc5r+  ru   r   r   r   )test_exit_exception_with_existing_context  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   r2  r   r   r3  r   r   r   rk    r+   zDTestBaseExitStack.test_body_exception_suppress.<locals>.suppress_excr=   r   z%Expected no exception, got IndexError)r  r?  rf  ra   )r   rk  r+  ru   r   r   r   test_body_exception_suppress  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   r2  r   r   rt   r   r   r   rL    rM  zITestBaseExitStack.test_exit_exception_chaining_suppress.<locals>.<lambda>c                  W   s   dd S r   r   rt   r   r   r   rL        c                  W   s   i d S r   r   rt   r   r   r   rL    r|  rP  rN  r   r   r   %test_exit_exception_chaining_suppress  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_nesting  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      r  r>  r=   )	r  r   r  r%   r  rF  r?  r   r(  )r   r  rA  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  r  r  c                   @   r!   )zLTestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.UniqueRuntimeErrorNr"   r   r   r   r   UniqueRuntimeError  r  r  c               
   3   s2    zdV  W d S  t y }  z d| d } ~ ww )Nr=   znew exceptionrl   rt   r  r   r   second  rw   z@TestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.secondc               
   s   s,    zdV  W d S  t y }  z| d } ~ ww r   r  rt   r   r   r   first  s   z?TestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.firstzplease no infinite loop.)rl   r^   rG   r%   r  rF  	exceptionrx   r^  rz   ry   r   )r   r  r  r  Zerr_ctxZes_ctxru   r   r  r   test_dont_reraise_RuntimeError  s&   

z0TestBaseExitStack.test_dont_reraise_RuntimeError)r   r   r   r  r   r   r   r   r-  rB  rG  rH  rK  rO  rQ  rT  ri  rl  rq  rr  rx  rz  r{  r}  r  r  r  r   r   r   r   r  d  s0    
)" 0"
	r  c                   @   s   e Zd ZeZddgZdS )TestExitStack)r   zraise exc_details[1])r   zif cb(*exc_details):N)r   r   r   	ExitStackr  r]  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 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_streamr  r   )r   orig_stdoutr   r   r   test_no_redirect_in_init0  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StringIOr  r  r  r  printr   getvaluestriprH   )r   rO   msgr  sr   r   r   test_redirect_to_string_io5  s   z-TestRedirectStream.test_redirect_to_string_ioc                 C   sD   t  }| |}| || W d    d S 1 sw   Y  d S r	   )r  r  r  r   )r   rO   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  r  r  r  r  r  r   r  rH   r   rO   Z
write_to_fr  r  r   r   r   test_cm_is_reusableD  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_reentrantP  s   
z'TestRedirectStream.test_cm_is_reentrant)r   r   r   r  r  r   r   r   r  r  r  r  r  r   r   r   r   r  $  s    

r  c                   @      e Zd ZeZdZdS )TestRedirectStdoutstdoutN)r   r   r   redirect_stdoutr  r  r   r   r   r   r  ]      r  c                   @   r  )TestRedirectStderrstderrN)r   r   r   redirect_stderrr  r  r   r   r   r   r  c  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   k  r   zTestSuppress.test_instance_docsc                 C   s8   t t}| | W d    d S 1 sw   Y  d S r	   )r  r\  rz   )r   r  r   r   r   test_no_result_from_enterr  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  r\  rH   powr*   r   r   r   test_no_exceptionv  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&   r[   r*   r   r   r   test_exact_exceptionz     

"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  rl   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  i  s    
r  __main__)r  r  r   r   r/   Zunittest
contextlibr  r   Ztest.supportr   r   ZTestCaser   r;   r   r   r   r   r  r   r   r  r  r  r  r  r  r   mainr   r   r   r   <module>   sD    +  "	0 !   ;96