Your IP : 216.73.217.32


Current Path : /opt/alt/python39/lib64/python3.9/test/__pycache__/
Upload File :
Current File : //opt/alt/python39/lib64/python3.9/test/__pycache__/test_contextlib.cpython-39.opt-1.pyc

a

XC?h���@sVdZddlZddlZddlZddlZddlZddlTddlmZddl	Z	Gdd�dej
�ZGdd�dej
�ZGd	d
�d
ej
�Z
Gdd�dej
�ZGd
d�dej
�ZGdd�dej
�ZGdd�de�ZGdd�dej
�ZGdd�d�ZGdd�deej
�ZGdd�d�ZGdd�deej
�ZGdd�deej
�ZGdd �d ej
�Zed!k�rRe��dS)"z9Unit tests for contextlib.py, and other context managers.�N)�*)�supportc@s$eZdZdd�Zdd�Zdd�ZdS)�TestAbstractContextManagercCs*Gdd�dt�}|�}|�|��|�dS)NcseZdZ�fdd�Z�ZS)z;TestAbstractContextManager.test_enter.<locals>.DefaultEntercst�j|�dS�N��super�__exit__��self�args��	__class__��9/opt/alt/python39/lib64/python3.9/test/test_contextlib.pyrszDTestAbstractContextManager.test_enter.<locals>.DefaultEnter.__exit__��__name__�
__module__�__qualname__r�
__classcell__rrrr�DefaultEntersr)�AbstractContextManager�assertIs�	__enter__)r
rZmanagerrrr�
test_entersz%TestAbstractContextManager.test_entercCsDGdd�dt�}|�t��|�Wd�n1s60YdS)Nc@seZdZdS)zETestAbstractContextManager.test_exit_is_abstract.<locals>.MissingExitN�rrrrrrr�MissingExitsr)r�assertRaises�	TypeError)r
rrrr�test_exit_is_abstractsz0TestAbstractContextManager.test_exit_is_abstractcCs�Gdd�d�}|�t|t��Gdd�dt�}|�t|t��Gdd�d|�}|�t|t��Gdd�d|�}|�t|t��dS)	Nc@seZdZdd�Zdd�ZdS)zRTestAbstractContextManager.test_structural_subclassing.<locals>.ManagerFromScratchcSs|Srr�r
rrrr sz\TestAbstractContextManager.test_structural_subclassing.<locals>.ManagerFromScratch.__enter__cSsdSrr)r
�exc_type�	exc_value�	tracebackrrrr"sz[TestAbstractContextManager.test_structural_subclassing.<locals>.ManagerFromScratch.__exit__N�rrrrrrrrr�ManagerFromScratchsr$cseZdZ�fdd�Z�ZS)zLTestAbstractContextManager.test_structural_subclassing.<locals>.DefaultEntercst�j|�dSrrr	rrrr(szUTestAbstractContextManager.test_structural_subclassing.<locals>.DefaultEnter.__exit__rrrrrr'src@seZdZdZdS)zGTestAbstractContextManager.test_structural_subclassing.<locals>.NoEnterN)rrrrrrrr�NoEnter-sr%c@seZdZdZdS)zFTestAbstractContextManager.test_structural_subclassing.<locals>.NoExitN)rrrrrrrr�NoExit2sr&)�
assertTrue�
issubclassr�assertFalse)r
r$rr%r&rrr�test_structural_subclassingsz6TestAbstractContextManager.test_structural_subclassingN)rrrrrr*rrrrr
src@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Ze
jdd��Ze
jdd��Zdd�Zdd�Zdd�Zdd �Zd!S)"�ContextManagerTestCasecsrg�t�fdd��}|��4}|��dg�|�|d���|�Wd�n1sT0Y|��gd��dS)Nc3s��d�dV��d�dS�N��*����appendr��staterr�woohoo<s
z@ContextManagerTestCase.test_contextmanager_plain.<locals>.woohoor-r.�r-r.r/)�contextmanager�assertEqualr1�r
r4�xrr2r�test_contextmanager_plain:s(z0ContextManagerTestCase.test_contextmanager_plainc	s�g�t�fdd��}|�t��`|��:}|��dg�|�|d���|�t��Wd�n1sf0YWd�n1s�0Y|��gd��dS)Nc	3s0��d�zdVW��d�n��d�0dSr,r0rr2rrr4Is
zBContextManagerTestCase.test_contextmanager_finally.<locals>.woohoor-r.r5)r6r�ZeroDivisionErrorr7r1r8rr2r�test_contextmanager_finallyGs
Bz2ContextManagerTestCase.test_contextmanager_finallycCs6tdd��}|�}|��|�|�ttd�d��dS)Ncss
dVdSrrrrrr�wheeYszCContextManagerTestCase.test_contextmanager_no_reraise.<locals>.whee�foo)r6rr)rr)r
r=�ctxrrr�test_contextmanager_no_reraiseXs

z5ContextManagerTestCase.test_contextmanager_no_reraisecCs6tdd��}|�}|��|�t|jttd�d�dS)Ncss"z
dVWndVYn0dSrrrrrr�whoobs
zOContextManagerTestCase.test_contextmanager_trap_yield_after_throw.<locals>.whoor>)r6rr�RuntimeErrorrr)r
rAr?rrr�*test_contextmanager_trap_yield_after_throwas
�zAContextManagerTestCase.test_contextmanager_trap_yield_after_throwcs|g�t��fdd��}|��<}���dg���|d���|�td��Wd�n1s^0Y���gd��dS)Nc
3s^��d�z
dVWnDtyX}z,��|jd����gd��WYd}~n
d}~00dS)Nr-r.rr5)r1r;rr7)�e�r
r3rrr4ps

zAContextManagerTestCase.test_contextmanager_except.<locals>.woohoor-r.r/r5)r6r7r1r;r8rrEr�test_contextmanager_exceptns
&z1ContextManagerTestCase.test_contextmanager_exceptcCs�tdd��}Gdd�dt�}td�|d�fD]�}|jt|�d���z.|��|�Wd�n1sd0YWn0ty�}z|�||�WYd}~nd}~00|�|�d��Wd�q,1s�0Yq,dS)Ncss
dVdSrrrrrrr4�szJContextManagerTestCase.test_contextmanager_except_stopiter.<locals>.woohooc@seZdZdS)zYContextManagerTestCase.test_contextmanager_except_stopiter.<locals>.StopIterationSubclassNrrrrr�StopIterationSubclass�srG�spam)�typez was suppressed)r6�
StopIteration�subTestrI�	Exceptionr�fail)r
r4rG�stop_exc�exrrr�#test_contextmanager_except_stopiters
&"z:ContextManagerTestCase.test_contextmanager_except_stopiterc
Cs�d}i}t|||�|d}td�}z.|��|�Wd�n1sF0YWn0ty�}z|�||�WYd}~nd}~00|�d�dS)Nztfrom __future__ import generator_stop
from contextlib import contextmanager
@contextmanager
def woohoo():
    yield
r4rHzStopIteration was suppressed)�execrJrLrrM)r
�code�localsr4rNrOrrr�!test_contextmanager_except_pep479�s&"z8ContextManagerTestCase.test_contextmanager_except_pep479c
Cstdd��}z.|��t�Wd�n1s.0YWnTty�}z<|�t|�t�|�|jdd�|�|j	t�WYd}~n
d}~00z2|��t
d��Wd�n1s�0YWnTt�y}z:|�t|�t
�|�|jdd�|�|j	�WYd}~n
d}~00dS)Nc
ss>z
dVWn.ty8}ztd�|�WYd}~n
d}~00dS)N�issue29692:Chained)rLrB��excrrr�test_issue29692�s
zoContextManagerTestCase.test_contextmanager_do_not_unchain_non_stopiteration_exceptions.<locals>.test_issue29692rrUzissue29692:Unchained)r6r;rLrrIrBr7r�assertIsInstance�	__cause__rJ�assertIsNone)r
rXrOrrr�?test_contextmanager_do_not_unchain_non_stopiteration_exceptions�s 
&$*zVContextManagerTestCase.test_contextmanager_do_not_unchain_non_stopiteration_exceptionscCs"dd�}t|dd�dd���}|S)Ncs�fdd�}|S)Ncs"���D]\}}t|||�q|Sr)�items�setattr)�func�k�v��kwrr�decorate�szXContextManagerTestCase._create_contextmanager_attribs.<locals>.attribs.<locals>.decorater)rcrdrrbr�attribs�szFContextManagerTestCase._create_contextmanager_attribs.<locals>.attribs�bar)r>cSsdS)�Whee!Nr)rHrrr�baz�szBContextManagerTestCase._create_contextmanager_attribs.<locals>.baz�r6)r
rerhrrr�_create_contextmanager_attribs�s
z5ContextManagerTestCase._create_contextmanager_attribscCs(|��}|�|jd�|�|jd�dS)Nrhrf)rjr7rr>�r
rhrrr�test_contextmanager_attribs�sz2ContextManagerTestCase.test_contextmanager_attribscCs|��}|�|jd�dS�Nrg�rjr7�__doc__rkrrr�test_contextmanager_doc_attrib�sz5ContextManagerTestCase.test_contextmanager_doc_attribcCs|��d�}|�|jd�dSrmrnrkrrr�*test_instance_docstring_given_cm_docstring�szAContextManagerTestCase.test_instance_docstring_given_cm_docstringcCsLtdd��}|ddddd��}|�|d�Wd�n1s>0YdS)	Ncss||||fVdSrr�r
r_r�kwdsrrrr4�sz4ContextManagerTestCase.test_keywords.<locals>.woohoo���!�,rr)rtrurvrw�r6r7)r
r4�targetrrr�
test_keywords�s
z$ContextManagerTestCase.test_keywordscsRGdd�d�}t�fdd��}||�|�d��Wd�n1sD0YdS)Nc@seZdZdS)z0ContextManagerTestCase.test_nokeepref.<locals>.ANrrrrr�A�sr{c3s6t�|�}t�|�}��|����|��dVdSr)�weakref�refr[��a�brrrr4�s


z5ContextManagerTestCase.test_nokeepref.<locals>.woohoo�r�ri)r
r{r4rrr�test_nokeepref�s
z%ContextManagerTestCase.test_nokeeprefcCs�tdd��}|�t��|�Wd�n1s20Y|�t��|dd�Wd�n1sf0Y|�t��|dd�Wd�n1s�0YdS)Ncss
dVdSrrr~rrrr4�sz8ContextManagerTestCase.test_param_errors.<locals>.woohoo��r�)r6rr�r
r4rrr�test_param_errors�s
$(z(ContextManagerTestCase.test_param_errorscs@d�t��fdd��}|���fdd��������d�dS)Nrc3s*�}�d7�dV�d8����|�dS�Nr-)r7)Zbefore)�depthr
rrr4�s
z5ContextManagerTestCase.test_recursive.<locals>.woohoocs�dkr��dS)N�
rr)r��	recursiverrr�sz8ContextManagerTestCase.test_recursive.<locals>.recursiverxr�r)r�r�r
r�test_recursive�sz%ContextManagerTestCase.test_recursiveN)rrrr:r<r@rCrFrPrTr\rjrlr�requires_docstringsrprqrzr�r�r�rrrrr+8s$
	



r+c@s*eZdZejdd��Zdd�Zdd�ZdS)�ClosingTestCasecCs tj}td�}|�|j|�dSr)�closingror7�r
Zcm_docstring�objrrr�test_instance_docssz"ClosingTestCase.test_instance_docscsng�G�fdd�d�}|�}|��g�t|��}|�||�Wd�n1sR0Y|��dg�dS)NcseZdZ�fdd�ZdS)z'ClosingTestCase.test_closing.<locals>.Ccs��d�dSr�r0rr2rr�closesz-ClosingTestCase.test_closing.<locals>.C.closeN�rrrr�rr2rr�Csr�r-)r7r��r
r�r9�yrr2r�test_closings
*zClosingTestCase.test_closingc	s�g�G�fdd�d�}|�}|��g�|�t��Lt|��$}|�||�ddWd�n1sf0YWd�n1s�0Y|��dg�dS)NcseZdZ�fdd�ZdS)z-ClosingTestCase.test_closing_error.<locals>.Ccs��d�dSr�r0rr2rrr�'sz3ClosingTestCase.test_closing_error.<locals>.C.closeNr�rr2rrr�&sr�r-r)r7rr;r�r�rr2r�test_closing_error$s
Dz"ClosingTestCase.test_closing_errorN)rrrrr�r�r�r�rrrrr�s
r�c@seZdZdd�ZdS)�NullcontextTestCasecCsLGdd�d�}|�}t|��}|�||�Wd�n1s>0YdS)Nc@seZdZdS)z/NullcontextTestCase.test_nullcontext.<locals>.CNrrrrrr�4sr�)�nullcontextr)r
r��cZc_inrrr�test_nullcontext3s
z$NullcontextTestCase.test_nullcontextN)rrrr�rrrrr�2sr�c@seZdZdd�ZdS)�FileContextTestCasec	Cst��}z�d}t|d��&}|�|j�|�d�Wd�n1sD0Y|�|j�d}|�t��^t|d��4}|�|j�|�	|�
�d�ddWd�n1s�0YWd�n1s�0Y|�|j�Wt�|�nt�|�0dS)N�wzBooh
�rr-r)
�tempfileZmktemp�openr)�closed�writer'rr;r7�readr�unlink)r
Ztfn�frrr�testWithOpen=s(Dz FileContextTestCase.testWithOpenN)rrrr�rrrrr�;sr�c@s<eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�LockContextTestCasec	Cs�|�|��|�|�|��Wd�n1s20Y|�|��|�t��H|�$|�|��ddWd�n1s�0YWd�n1s�0Y|�|��dS�Nr-r)r)r'rr;)r
�lock�lockedrrr�boilerPlateQs*DzLockContextTestCase.boilerPlatecCst��}|�||j�dSr)�	threadingZLockr�r��r
r�rrr�testWithLock\sz LockContextTestCase.testWithLockcCst��}|�||j�dSr)r��RLockr��	_is_ownedr�rrr�
testWithRLock`sz!LockContextTestCase.testWithRLockcs$t����fdd�}|��|�dS)Ncs���Sr)r�r�r�rrr�fsz5LockContextTestCase.testWithCondition.<locals>.locked)r�Z	Conditionr��r
r�rr�r�testWithConditiondsz%LockContextTestCase.testWithConditioncs$t����fdd�}|��|�dS)Ncs��d�r���dSdSdS�NFT��acquire�releaserr�rrr�ls
z5LockContextTestCase.testWithSemaphore.<locals>.locked)r�Z	Semaphorer�r�rr�r�testWithSemaphorejsz%LockContextTestCase.testWithSemaphorecs$t����fdd�}|��|�dS)Ncs��d�r���dSdSdSr�r�rr�rrr�vs
z<LockContextTestCase.testWithBoundedSemaphore.<locals>.locked)r�ZBoundedSemaphorer�r�rr�r�testWithBoundedSemaphoretsz,LockContextTestCase.testWithBoundedSemaphoreN)	rrrr�r�r�r�r�r�rrrrr�Os
r�c@s,eZdZdZdZdZdZdd�Zdd�ZdS)�	mycontextz9Example decoration-compatible context manager for testingFNcCs
d|_|S�NT��startedrrrrr�szmycontext.__enter__cGs||_|jSr)rW�catch�r
rWrrrr�szmycontext.__exit__)	rrrror�rWr�rrrrrrr�sr�c@sbeZdZejdd��Zdd�Zdd�Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dd�ZdS)�TestContextDecoratorcCstj}t�}|�|j|�dSr)r�ror7r�rrrr��sz'TestContextDecorator.test_instance_docscCsTt�}|�(}|�||�|�|j�Wd�n1s80Y|�|jd�dS)N�NNN)r�rr'r�r7rW)r
�context�resultrrr�test_contextdecorator�s
*z*TestContextDecorator.test_contextdecoratorc	Cs�t�}|�td��<|�td��Wd�n1s60YWd�n1sT0Y|�|j�|�|jdt�t�}d|_|�td��Wd�n1s�0Y|�|j�|�|jdt�dS)Nr>rT)r��assertRaisesRegex�	NameError�assertIsNotNonerWrr�)r
r�rrr�$test_contextdecorator_with_exception�sD&z9TestContextDecorator.test_contextdecorator_with_exceptioncs0t�����fdd��}|����jd�dS)Ncs���j����j�dSr�r[rWr'r�r�r�r
rr�test�sz1TestContextDecorator.test_decorator.<locals>.testr�)r�r7rW�r
r�rr�r�test_decorator�s
z#TestContextDecorator.test_decoratorcslt�����fdd��}��td��|�Wd�n1s@0Y���j����jdt�dS)Ncs$���j����j�td��dS)Nr>)r[rWr'r�r�rr�rrr��sz@TestContextDecorator.test_decorator_with_exception.<locals>.testr>r)r�r�r�r�rWrr�rr�r�test_decorator_with_exception�s$z2TestContextDecorator.test_decorator_with_exceptioncs�t��G�fdd�dt�}|�}|�dd�|�|jd�|�|jd�|�|jd�|�}|�ddd�|�|jd�|�|jd�|�|jd�|�}|jddd�|�|jd�|�|jd�dS)	NcseZdZ�ddd��ZdS)z9TestContextDecorator.test_decorating_method.<locals>.TestNcSs||_||_||_dSr)rr�r�)r
rr�r�rrr�method�sz@TestContextDecorator.test_decorating_method.<locals>.Test.method)N)rrrr�r�r�rr�Test�sr�r-�rr�r�r~)r��objectr�r7rr�r�)r
r�r�rr�r�test_decorating_method�s 	z+TestContextDecorator.test_decorating_methodc	CsdGdd�dt�}|�t��6|��Wd�n1s80YWd�n1sV0YdS)Nc@seZdZdd�Zdd�ZdS)z7TestContextDecorator.test_typo_enter.<locals>.mycontextcSsdSrrrrrr�	__unter__�szATestContextDecorator.test_typo_enter.<locals>.mycontext.__unter__cWsdSrrr�rrrr�sz@TestContextDecorator.test_typo_enter.<locals>.mycontext.__exit__N)rrrr�rrrrrr��sr���ContextDecoratorr�AttributeError�r
r�rrr�test_typo_enter�sz$TestContextDecorator.test_typo_enterc	CsdGdd�dt�}|�t��6|��Wd�n1s80YWd�n1sV0YdS)Nc@seZdZdd�Zdd�ZdS)z6TestContextDecorator.test_typo_exit.<locals>.mycontextcSsdSrrrrrrr�sz@TestContextDecorator.test_typo_exit.<locals>.mycontext.__enter__cWsdSrrr�rrr�__uxit__�sz?TestContextDecorator.test_typo_exit.<locals>.mycontext.__uxit__N)rrrrr�rrrrr��sr�r�r�rrr�test_typo_exit�sz#TestContextDecorator.test_typo_exitcsRGdd�dt�}Gdd�d|t�}|�����fdd��}|����jd�dS)Nc@s$eZdZdZdZdd�Zdd�ZdS)zHTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontextFNcSs
d|_|Sr�r�rrrrrszRTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontext.__enter__cWs
||_dSrrVr�rrrr
szQTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontext.__exit__)rrrr�rWrrrrrr�somecontextsr�c@seZdZdS)zFTestContextDecorator.test_contextdecorator_as_mixin.<locals>.mycontextNrrrrrr�
sr�cs���j����j�dSrr�rr�rrr�szATestContextDecorator.test_contextdecorator_as_mixin.<locals>.testr�)r�r�r7rW)r
r�r�r�rr�r�test_contextdecorator_as_mixinsz3TestContextDecorator.test_contextdecorator_as_mixincsbt�fdd��}g�|d���fdd��}|d����gd��g�|d����gd	��dS)
Nc3s��|�dV��d�dS)Nr/r0)r�r2rrr4s
zETestContextDecorator.test_contextmanager_as_decorator.<locals>.woohoor-cs���dg���|�dSr�)r7r1)r9rErrr�!szCTestContextDecorator.test_contextmanager_as_decorator.<locals>.test�	something)r-r�r/�something else)r-r�r/rx)r
r4r�rrEr� test_contextmanager_as_decoratorsz5TestContextDecorator.test_contextmanager_as_decoratorN)rrrrr�r�r�r�r�r�r�r�r�r�r�rrrrr��s
	r�c@s�eZdZdZejdd��Zdd�Zdd�Zdd	�Z	d
d�Z
dd
�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�ZdS)*�TestBaseExitStackNcCs"|jj}|��}|�|j|�dSr)�
exit_stackror7r�rrrr�1sz$TestBaseExitStack.test_instance_docscCs,|���Wd�n1s0YdSr)r�rrrr�test_no_resources8s
z#TestBaseExitStack.test_no_resourcescs8difdifdifdtdd�fdtdd�fdtdd�fdtddd�fg}g��fd	d
�}|����}t|�D]p\}}|r�|r�|j|g|�Ri|��}n:|r�|j|g|�R�}n"|r�|j|fi|��}n
|�|�}|�||�qj|jD]>}|�|dj|�|�|dj|j�|�	|dj
|j
�q�Wd�n1�s80Y|��|�g�|����}|�t
��|jdd�Wd�n1�s�0Y|�t
��|jjdd�Wd�n1�s�0Y|�t
��|j|dd
�Wd�n1�s�0YWd�n1�s0Y|��g�dS)Nr)r-)r-r�r-)Zexampler��)r
�callbackcs��||f�dS)zTest metadata propagationNr0)rrs�r�rr�_exitGsz.TestBaseExitStack.test_callback.<locals>._exit)�argr�)r�r�)�dictr��reversedr�r�_exit_callbacks�__wrapped__ZassertNotEqualrr[ror7rr)r
Zexpectedr��stackrrsr��wrapperrr�r�
test_callback<sD�	


6
,.NzTestBaseExitStack.test_callbackcs@t���fdd�}dd�}�fdd�}Gdd�dt�}�����}|�|���|jd	d
|�||�}|�|���|jd	d
j|�|�|���|jd	d
|�||�}|�|���|jd	d
j|�|�|���|jd	d
|�|�|���|jd	d
|�d
dWd�n1�s20YdS)Ncs��|��dSr)r�r rWZexc_tb�Z
exc_raisedr
rr�_expect_excgsz0TestBaseExitStack.test_push.<locals>._expect_exccWsdSr�r��exc_detailsrrr�
_suppress_excisz2TestBaseExitStack.test_push.<locals>._suppress_exccs"��|���|���|�dSr)r[r�rrr�
_expect_okks

z/TestBaseExitStack.test_push.<locals>._expect_okc@s$eZdZdd�Zdd�Zdd�ZdS)z+TestBaseExitStack.test_push.<locals>.ExitCMcSs
||_dSr��	check_exc)r
r�rrr�__init__psz4TestBaseExitStack.test_push.<locals>.ExitCM.__init__cSs|�d�dS)NzShould not be called!)rMrrrrrrsz5TestBaseExitStack.test_push.<locals>.ExitCM.__enter__cWs|j|�dSrr��r
r�rrrrtsz4TestBaseExitStack.test_push.<locals>.ExitCM.__exit__N�rrrr�rrrrrr�ExitCMosr����r-r)r;r�r��pushrr��__self__)r
r�r�r�r�r��cmrr�r�	test_pushes*






zTestBaseExitStack.test_pushcs�G�fdd�dt�}g�|�}|���X}|j�fdd��}|�|�|�|�|�|jddj|���d�Wd�n1s�0Y|�	�gd��dS)	Ncs$eZdZ�fdd�Z�fdd�ZdS)z4TestBaseExitStack.test_enter_context.<locals>.TestCMcs��d�dSr�r0rr�rrr�sz>TestBaseExitStack.test_enter_context.<locals>.TestCM.__enter__cs��d�dS�Nr�r0r�r�rrr�sz=TestBaseExitStack.test_enter_context.<locals>.TestCM.__exit__Nr#rr�rr�TestCM�srcs��d�dS)Nr�r0rr�rrr��sz3TestBaseExitStack.test_enter_context.<locals>._exitrr-r�)r-r�r�r�)
r�r�r�r��
enter_contextrr�rr1r7)r
rrr�r�rr�r�test_enter_context�s


(z$TestBaseExitStack.test_enter_contextcsng�|���>}|j�fdd��}|�|�|����d�Wd�n1sP0Y|��ddg�dS)Ncs��d�dSr�r0rr�rrr��sz+TestBaseExitStack.test_close.<locals>._exitr�r-)r�r�r�r�r1r7)r
r�r�rr�r�
test_close�s

(zTestBaseExitStack.test_closecs�g�|���>}|j�fdd��}|�|�|��}��d�Wd�n1sP0Y��d�|��|��gd��dS)Ncs��d�dSrr0rr�rrr��sz-TestBaseExitStack.test_pop_all.<locals>._exitr-r�)r-r�r�)r�r�r��pop_allr1r�r7)r
r�r��	new_stackrr�r�test_pop_all�s

(
zTestBaseExitStack.test_pop_allc	Csl|�t��N|���&}|�dd��ddWd�n1s@0YWd�n1s^0YdS)NcWsdS)NFrrVrrr�<lambda>��z3TestBaseExitStack.test_exit_raise.<locals>.<lambda>r-r)rr;r�r�r
r�rrr�test_exit_raise�s
z!TestBaseExitStack.test_exit_raisecCsB|���&}|�dd��ddWd�n1s40YdS)NcWsdSr�rrVrrrr
�rz6TestBaseExitStack.test_exit_suppress.<locals>.<lambda>r-r�r�rrrrr�test_exit_suppress�s
z$TestBaseExitStack.test_exit_suppresscCs^Gdd�d�}Gdd�d�}Gdd�d�}z�|t���|tt��f|��@|t��ddWd�n1sp0YWd�n1s�0YWd�n1s�0YWd�n1s�0YWnTt�y*}z:|�|jt�|�|jjt�|�|jjj�WYd}~nd}~00|�d	�|jd}|�|t�|�|jt	�dS)
Nc@s$eZdZdd�Zdd�Zdd�ZdS)zJTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExccSs
||_dSrrVr�rrrr��szSTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__init__cSs|Srrrrrrr�szTTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__enter__cWs
|j�dSrrVr�rrrr�szSTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__exit__Nr�rrrr�RaiseExc�src@s$eZdZdd�Zdd�Zdd�ZdS)zUTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContextcSs||_||_dSr)�outer�inner)r
rrrrrr��sz^TestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__init__cSs|Srrrrrrr�sz_TestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__enter__cWs"z
|j�Wn|j�Yn0dSr)rrr�rrrr�s
z^TestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__exit__Nr�rrrr�RaiseExcWithContext�src@seZdZdd�Zdd�ZdS)zMTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExccSs|Srrrrrrr�szWTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExc.__enter__cWs|t|�_dSr�)rI�
saved_detailsr�rrrr�s
zVTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExc.__exit__Nr#rrrr�SuppressExc�srr-r�0Expected IndexError, but no exception was raised)
�
IndexError�KeyErrorr��
ValueErrorrY�__context__r[rMrr;)r
rrrrW�	inner_excrrr�&test_exit_exception_chaining_reference�s"

�&

z8TestBaseExitStack.test_exit_exception_chaining_referencec
s
dd�}d��fdd�}zn|���R}|�|t�|�|t�|�|t�|�|�|�|t�ddWd�n1sz0YWnRty�}z:|�|jt�|�|jjt�|�	|jjj�WYd}~nd}~00|�
d��d}|�|t�|�|jt�dS)NcSs|�dSrrrVrrr�	raise_exc�szATestBaseExitStack.test_exit_exception_chaining.<locals>.raise_exccs|�dSr�rr��rrr�suppress_exc�szDTestBaseExitStack.test_exit_exception_chaining.<locals>.suppress_excr-rr)r�r�rrr�rrrYrr[rMr;)r
r r"r�rWrrr!r�test_exit_exception_chaining�s&

*&
z.TestBaseExitStack.test_exit_exception_chainingcs�Gdd�dt��t�fdd���t��fdd��}�|fD]�}����|z0|��t��Wd�n1sl0YWn0�y�}z��|j�WYd}~nd}~00��d�Wd�q:1s�0Yq:dS)Nc@seZdZdS)zPTestBaseExitStack.test_exit_exception_explicit_none_context.<locals>.MyExceptionNrrrrr�MyExceptionsr$c3s@z
dVWn0ty:��}z|�Wd|_nd|_0Yn0dSr)�
BaseExceptionrrV)r$rr�my_cms
zJTestBaseExitStack.test_exit_exception_explicit_none_context.<locals>.my_cmc3s>����"}|����|VWd�n1s00YdSr)r�r)r�)r&r
rr�my_cm_with_exit_stacks
zZTestBaseExitStack.test_exit_exception_explicit_none_context.<locals>.my_cm_with_exit_stackr)rLr6rKrr[rrM)r
r'rrWr)r$r&r
r�)test_exit_exception_explicit_none_contexts

("z;TestBaseExitStack.test_exit_exception_explicit_none_contextc
Cs"dd�}dd�}zF|���*}|�dd��|�|t�Wd�n1sJ0YWn0ty�}z|�|t�WYd}~nd}~00|�d�zN|���2}|�|t�|�|�|�|t�Wd�n1s�0YWn2t�y}z|�|t�WYd}~nd}~00|�d�dS)	NcSs|�dSrrrVrrrr 0szHTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.raise_exccWsdSr�rr�rrrr"3szKTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.suppress_exccSsdSrrrrrrr
8rzGTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.<lambda>rz.Expected KeyError, but no exception was raised)r�r�rrLrYrMrr)r
r r"r�rWrrr�#test_exit_exception_non_suppressing.s"
."


."z5TestBaseExitStack.test_exit_exception_non_suppressingc
Cs�tdd��}td�}td�}td�}td�}zZ|���>}|�||��|�||��|�||��|�Wd�n1sz0YWnrty�}zZ|�||�|�|j|�|�|jj|�|�|jjj|�|�|jjjj�WYd}~n
d}~00dS)NcsszdVW|�n|�0dSrrrVrrr�gets_the_context_rightKszZTestBaseExitStack.test_exit_exception_with_correct_context.<locals>.gets_the_context_rightr-r�r�r�)r6rLr�rrrr[)r
r*�exc1�exc2�exc3�exc4r�rWrrr�(test_exit_exception_with_correct_contextIs(

&
�z:TestBaseExitStack.test_exit_exception_with_correct_contextc	
Csdd�}td�}td�}td�}td�}td�}zL|���0}|�|||�|�|||�|�Wd�n1sp0YWn�t�y}zp|�||�|�|j|�|�|jj|�|�|jjj|�|�|jjjj|�|�|jjjjj�WYd}~n
d}~00dS)NcSsz|�W|�n|�0dSrr)rZ	outer_excrrr�raise_nestedkszQTestBaseExitStack.test_exit_exception_with_existing_context.<locals>.raise_nestedr-r�r�r�r�)rLr�r�rrr[)	r
r0r+r,r-r.Zexc5r�rWrrr�)test_exit_exception_with_existing_contexths,
&��z;TestBaseExitStack.test_exit_exception_with_existing_contextc
Cszdd�}z>|���"}|�|�ddWd�n1s:0YWn.tyt}z|�d�WYd}~n
d}~00dS)NcWsdSr�rr�rrrr"�szDTestBaseExitStack.test_body_exception_suppress.<locals>.suppress_excr-rz%Expected no exception, got IndexError)r�rrrM)r
r"r�rWrrr�test_body_exception_suppress�s

*z.TestBaseExitStack.test_body_exception_suppresscCsV|���:}|�dd��|�dd��|�dd��Wd�n1sH0YdS)NcWsdSr�rrVrrrr
�rzITestBaseExitStack.test_exit_exception_chaining_suppress.<locals>.<lambda>cWsddSr�rrVrrrr
�rcWsidSr�rrVrrrr
�rrrrrr�%test_exit_exception_chaining_suppress�s
z7TestBaseExitStack.test_exit_exception_chaining_suppresscCsD|���(}td�D]}|�t�qWd�n1s60YdS)Ni')r��ranger��int)r
r��irrr�test_excessive_nesting�s
z(TestBaseExitStack.test_excessive_nestingcCsZGdd�dt�}|�}t�|_|��}|�t|j|�|�|�|�|jdd|�dS)Nc@seZdZdS)z7TestBaseExitStack.test_instance_bypass.<locals>.ExampleNrrrrr�Example�rr8rr-)	r�rr�rr�rrrr�)r
r8rr�rrr�test_instance_bypass�s
z&TestBaseExitStack.test_instance_bypassc	s�Gdd�dt��Gdd�dt�}t�fdd��}tdd��}|����X}|���0}|�|��|�|��|d	��Wd�n1s�0YWd�n1s�0Y|j}|�|��|�|j|�|�	|jj�|�	|jj
�|�|j
|j�dS)
Nc@seZdZdS)zITestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.UniqueExceptionNrrrrr�UniqueException�rr:c@seZdZdS)zLTestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.UniqueRuntimeErrorNrrrrr�UniqueRuntimeError�rr;c
3s>z
dVWn.ty8}z�d�|�WYd}~n
d}~00dS)Nr-z
new exception�rLrV�r:rr�second�s
z@TestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.secondc
ss8z
dVWn(ty2}z|�WYd}~n
d}~00dSr�r<rVrrr�first�s
z?TestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.firstzplease no infinite loop.)rLrBr6rr�rZ	exceptionrYrr[rZr)r
r;r>r?Zerr_ctxZes_ctxrWrr=r�test_dont_reraise_RuntimeError�s"

Dz0TestBaseExitStack.test_dont_reraise_RuntimeError)rrrr�rr�r�r�r�rrr	rrrrr#r(r)r/r1r2r3r7r9r@rrrrr�.s,
)"
0"
	r�c@seZdZeZdS)�
TestExitStackN)rrr�	ExitStackr�rrrrrA�srAc@sJeZdZdZdZejdd��Zdd�Zdd�Z	dd	�Z
d
d�Zdd
�ZdS)�TestRedirectStreamNcCs$|jj}|�d�}|�|j|�dSr)�redirect_streamror7r�rrrr��s
z%TestRedirectStream.test_instance_docscCs.tt|j�}|�d�|�tt|j�|�dSr)�getattr�sys�orig_streamrDr)r
�orig_stdoutrrr�test_no_redirect_in_init�s
z+TestRedirectStream.test_no_redirect_in_initcCs�t��}d}tt|j�}|�|��$t|tt|j�d�Wd�n1sL0Y|�tt|j�|�|���	�}|�
||�dS)Nz<Consider an API like help(), which prints directly to stdout��file)�io�StringIOrErFrGrD�printr�getvalue�stripr7)r
r��msgrH�srrr�test_redirect_to_string_io�s2z-TestRedirectStream.test_redirect_to_string_iocCsBt��}|�|��}|�||�Wd�n1s40YdSr)rLrMrDr)r
r��enter_resultrrr�test_enter_result_is_target�sz.TestRedirectStream.test_enter_result_is_targetcCs�t��}|�|�}tt|j�}|�&tddtt|j�d�Wd�n1sN0Y|�$tdtt|j�d�Wd�n1s�0Y|�tt|j�|�|��}|�	|d�dS�N�Hello� )�endrKzWorld!rJz
Hello World!
�
rLrMrDrErFrGrNrrOr7�r
r�Z
write_to_frHrRrrr�test_cm_is_reusable�s
42z&TestRedirectStream.test_cm_is_reusablec	Cs�t��}|�|�}tt|j�}|�^tddtt|j�d�|�$tdtt|j�d�Wd�n1sh0YWd�n1s�0Y|�tt|j�|�|��}|�	|d�dSrVrZr[rrr�test_cm_is_reentrant�s
Pz'TestRedirectStream.test_cm_is_reentrant)
rrrrDrGrr�r�rIrSrUr\r]rrrrrC�s

rCc@seZdZeZdZdS)�TestRedirectStdout�stdoutN)rrr�redirect_stdoutrDrGrrrrr^sr^c@seZdZeZdZdS)�TestRedirectStderr�stderrN)rrr�redirect_stderrrDrGrrrrra	srac@sZeZdZejdd��Zdd�Zdd�Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dS)�TestSuppresscCstj}t�}|�|j|�dSr)�suppressror7r�rrrr�szTestSuppress.test_instance_docscCs6tt��}|�|�Wd�n1s(0YdSr)rerr[)r
rTrrr�test_no_result_from_enters
z&TestSuppress.test_no_result_from_entercCs>tt��"|�tdd�d�Wd�n1s00YdS)Nr�r�� )rerr7�powrrrr�test_no_exceptions
zTestSuppress.test_no_exceptioncCs4tt��td�Wd�n1s&0YdS)Nr�)rer�lenrrrr�test_exact_exception s
z!TestSuppress.test_exact_exceptioncCs4tt��ddWd�n1s&0YdS)NrW�2)re�LookupErrorrrrr�test_exception_hierarchy$s
z%TestSuppress.test_exception_hierarchyc	Cs^|�t��@tt��ddWd�n1s20YWd�n1sP0YdSr�)rr;rerrrrr�test_other_exception(s
z!TestSuppress.test_other_exceptionc	Cs\|�t��>t��ddWd�n1s00YWd�n1sN0YdSr�)rr;rerrrr�test_no_args-szTestSuppress.test_no_argscCshttt��ddWd�n1s(0Yttt��td�Wd�n1sZ0YdS)Nr-rr�)rer;rrjrrrr�test_multiple_exception_args2s&z)TestSuppress.test_multiple_exception_argsc	Cs�tt�}|�Wd�n1s"0Y|�td�Wd�n1sN0Y|�H|�td�Wd�n1s�0Yd}ddWd�n1s�0Y|�|�dS)Nr�Tr-r)rerLrjr')r
Zignore_exceptionsZouter_continuedrrrr]8s&&&z!TestSuppress.test_cm_is_reentrantN)rrrrr�r�rfrirkrnrorprqr]rrrrrds
rd�__main__)rorLrFr�r�Zunittest�
contextlibr�rr|ZTestCaserr+r�r�r�r�r�r�r�r�rArCr^rardr�mainrrrr�<module>s:+Y"	0!96