Your IP : 216.73.216.85


Current Path : /opt/alt/python38/lib64/python3.8/test/__pycache__/
Upload File :
Current File : //opt/alt/python38/lib64/python3.8/test/__pycache__/test_contextlib.cpython-38.pyc

U

i�f*��@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/python38/lib64/python3.8/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_enterc	Cs0Gdd�dt�}|�t��|�W5QRXdS)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)"�ContextManagerTestCasec	s`g�t�fdd��}|��*}|��dg�|�|d���|�W5QRX|��dddg�dS)Nc3s��d�dV��d�dS)N��*����appendr��staterr�woohoo<s
z@ContextManagerTestCase.test_contextmanager_plain.<locals>.woohoor,r-r.)�contextmanager�assertEqualr0�r
r3�xrr1r�test_contextmanager_plain:sz0ContextManagerTestCase.test_contextmanager_plainc
s|g�t�fdd��}|�t��B|��0}|��dg�|�|d���|�t��W5QRXW5QRX|��dddg�dS)Nc	3s&��d�z
dVW5��d�XdS)Nr,r.r-r/rr1rrr3Is

zBContextManagerTestCase.test_contextmanager_finally.<locals>.woohoor,r-r.)r4r�ZeroDivisionErrorr5r0r6rr1r�test_contextmanager_finallyGs
z2ContextManagerTestCase.test_contextmanager_finallycCs6tdd��}|�}|��|�|�ttd�d��dS)Ncss
dVdSrrrrrr�wheeYszCContextManagerTestCase.test_contextmanager_no_reraise.<locals>.whee�foo)r4rr)rr)r
r;�ctxrrr�test_contextmanager_no_reraiseXs

z5ContextManagerTestCase.test_contextmanager_no_reraisecCs6tdd��}|�}|��|�t|jttd�d�dS)Ncss"z
dVWndVYnXdSrrrrrr�whoobs
zOContextManagerTestCase.test_contextmanager_trap_yield_after_throw.<locals>.whoor<)r4rr�RuntimeErrorrr)r
r?r=rrr�*test_contextmanager_trap_yield_after_throwas
�zAContextManagerTestCase.test_contextmanager_trap_yield_after_throwc	sjg�t��fdd��}|��2}���dg���|d���|�td��W5QRX���dddg�dS)Nc
3s^��d�z
dVWnDtk
rX}z&��|jd����dddg�W5d}~XYnXdS)Nr,r-rr.)r0r9rr5)�e�r
r2rrr3ps

zAContextManagerTestCase.test_contextmanager_except.<locals>.woohoor,r-r.)r4r5r0r9r6rrCr�test_contextmanager_exceptns
z1ContextManagerTestCase.test_contextmanager_exceptc
Cs�td�}tdd��}z2|�td��|��
|�W5QRXW5QRXWn.tk
rt}z|�||�W5d}~XYnX|�d�dS)N�spamcss
dVdSrrrrrrr3�szJContextManagerTestCase.test_contextmanager_except_stopiter.<locals>.woohoo�
StopIteration�StopIteration was suppressed)rFr4ZassertWarnsRegex�DeprecationWarning�	Exceptionr�fail)r
�stop_excr3�exrrr�#test_contextmanager_except_stopiters
�z:ContextManagerTestCase.test_contextmanager_except_stopiterc
Cs|d}i}t|||�|d}td�}z|��
|�W5QRXWn.tk
rl}z|�||�W5d}~XYnX|�d�dS)Nztfrom __future__ import generator_stop
from contextlib import contextmanager
@contextmanager
def woohoo():
    yield
r3rErG)�execrFrIrrJ)r
�code�localsr3rKrLrrr�!test_contextmanager_except_pep479�sz8ContextManagerTestCase.test_contextmanager_except_pep479c
Cs�tdd��}z|��
t�W5QRXWnRtk
rx}z4|�t|�t�|�|jdd�|�|j	t�W5d}~XYnXz|��t
d��W5QRXWnPtk
r�}z2|�t|�t
�|�|jdd�|�|j	�W5d}~XYnXdS)Nc
ss<z
dVWn,tk
r6}ztd�|�W5d}~XYnXdS)N�issue29692:Chained)rIr@��excrrr�test_issue29692�s
zoContextManagerTestCase.test_contextmanager_do_not_unchain_non_stopiteration_exceptions.<locals>.test_issue29692rrRzissue29692:Unchained)r4r9rIr�typer@r5r�assertIsInstance�	__cause__rF�assertIsNone)r
rUrLrrr�?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)rarbrr`r�attribs�szFContextManagerTestCase._create_contextmanager_attribs.<locals>.attribs�bar)r<cSsdS)�Whee!Nr)rErrr�baz�szBContextManagerTestCase._create_contextmanager_attribs.<locals>.baz�r4)r
rcrfrrr�_create_contextmanager_attribs�s
z5ContextManagerTestCase._create_contextmanager_attribscCs(|��}|�|jd�|�|jd�dS)Nrfrd)rhr5rr<�r
rfrrr�test_contextmanager_attribs�sz2ContextManagerTestCase.test_contextmanager_attribscCs|��}|�|jd�dS�Nre�rhr5�__doc__rirrr�test_contextmanager_doc_attrib�sz5ContextManagerTestCase.test_contextmanager_doc_attribcCs|��d�}|�|jd�dSrkrlrirrr�*test_instance_docstring_given_cm_docstring�szAContextManagerTestCase.test_instance_docstring_given_cm_docstringc	Cs8tdd��}|ddddd��}|�|d�W5QRXdS)	Ncss||||fVdSrr�r
r]r�kwdsrrrr3�sz4ContextManagerTestCase.test_keywords.<locals>.woohoo���!�,rp)rrrsrtru�r4r5)r
r3�targetrrr�
test_keywords�s
z$ContextManagerTestCase.test_keywordsc	s>Gdd�d�}t�fdd��}||�|�d��W5QRXdS)Nc@seZdZdS)z0ContextManagerTestCase.test_nokeepref.<locals>.ANrrrrr�A�sryc3s6t�|�}t�|�}��|����|��dVdSr)�weakref�refrY��a�brrrr3�s


z5ContextManagerTestCase.test_nokeepref.<locals>.woohoo�r~rg)r
ryr3rrr�test_nokeepref�s
z%ContextManagerTestCase.test_nokeeprefc	Csltdd��}|�t��|�W5QRX|�t��|dd�W5QRX|�t��|dd�W5QRXdS)Ncss
dVdSrrr|rrrr3�sz8ContextManagerTestCase.test_param_errors.<locals>.woohoo��r)r4rr�r
r3rrr�test_param_errors�s
z(ContextManagerTestCase.test_param_errorscs@d�t��fdd��}|���fdd��������d�dS)Nrc3s*�}�d7�dV�d8����|�dS�Nr,)r5)Zbefore)�depthr
rrr3�s
z5ContextManagerTestCase.test_recursive.<locals>.woohoocs�dkr��dS)N�
rr)r��	recursiverrr�sz8ContextManagerTestCase.test_recursive.<locals>.recursivervr�r)r�r�r
r�test_recursive�sz%ContextManagerTestCase.test_recursiveN)rrrr8r:r>rArDrMrQrZrhrjr�requires_docstringsrnrorxr�r�r�rrrrr+8s$
	



r+c@s*eZdZejdd��Zdd�Zdd�ZdS)�ClosingTestCasecCs tj}td�}|�|j|�dSr)�closingrmr5�r
Zcm_docstring�objrrr�test_instance_docssz"ClosingTestCase.test_instance_docsc	sZg�G�fdd�d�}|�}|��g�t|��}|�||�W5QRX|��dg�dS)NcseZdZ�fdd�ZdS)z'ClosingTestCase.test_closing.<locals>.Ccs��d�dSr�r/rr1rr�closesz-ClosingTestCase.test_closing.<locals>.C.closeN�rrrr�rr1rr�Csr�r,)r5r��r
r�r7�yrr1r�test_closings
zClosingTestCase.test_closingc
sxg�G�fdd�d�}|�}|��g�|�t��.t|��}|�||�ddW5QRXW5QRX|��dg�dS)NcseZdZ�fdd�ZdS)z-ClosingTestCase.test_closing_error.<locals>.Ccs��d�dSr�r/rr1rrr�$sz3ClosingTestCase.test_closing_error.<locals>.C.closeNr�rr1rrr�#sr�r,r)r5rr9r�r�rr1r�test_closing_error!s
z"ClosingTestCase.test_closing_errorN)rrrrr�r�r�r�rrrrr�
s
r�c@seZdZdd�ZdS)�NullcontextTestCasec	Cs8Gdd�d�}|�}t|��}|�||�W5QRXdS)Nc@seZdZdS)z/NullcontextTestCase.test_nullcontext.<locals>.CNrrrrrr�1sr�)�nullcontextr)r
r��cZc_inrrr�test_nullcontext0s
z$NullcontextTestCase.test_nullcontextN)rrrr�rrrrr�/sr�c@seZdZdd�ZdS)�FileContextTestCasec
Cs�t��}z�d}t|d��}|�|j�|�d�W5QRX|�|j�d}|�	t
��@t|d��*}|�|j�|�|��d�ddW5QRXW5QRX|�|j�W5t�|�XdS)N�wzBooh
�rr,r)
�tempfileZmktempr�unlink�openr)�closed�writer'rr9r5�read)r
Ztfn�frrr�testWithOpen:sz FileContextTestCase.testWithOpenN)rrrr�rrrrr�8sr�c@s<eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�LockContextTestCasec
Cs~|�|��|�|�|��W5QRX|�|��|�t��*|�|�|��ddW5QRXW5QRX|�|��dS�Nr,r)r)r'rr9)r
�lock�lockedrrr�boilerPlateNszLockContextTestCase.boilerPlatecCst��}|�||j�dSr)�	threadingZLockr�r��r
r�rrr�testWithLockYsz LockContextTestCase.testWithLockcCst��}|�||j�dSr)r��RLockr��	_is_ownedr�rrr�
testWithRLock]sz!LockContextTestCase.testWithRLockcs$t����fdd�}|��|�dS)Ncs���Sr)r�r�r�rrr�csz5LockContextTestCase.testWithCondition.<locals>.locked)r�Z	Conditionr��r
r�rr�r�testWithConditionasz%LockContextTestCase.testWithConditioncs$t����fdd�}|��|�dS)Ncs��d�r���dSdSdS�NFT��acquire�releaserr�rrr�is
z5LockContextTestCase.testWithSemaphore.<locals>.locked)r�Z	Semaphorer�r�rr�r�testWithSemaphoregsz%LockContextTestCase.testWithSemaphorecs$t����fdd�}|��|�dS)Ncs��d�r���dSdSdSr�r�rr�rrr�ss
z<LockContextTestCase.testWithBoundedSemaphore.<locals>.locked)r�ZBoundedSemaphorer�r�rr�r�testWithBoundedSemaphoreqsz,LockContextTestCase.testWithBoundedSemaphoreN)	rrrr�r�r�r�r�r�rrrrr�Ls
r�c@s,eZdZdZdZdZdZdd�Zdd�ZdS)�	mycontextz9Example decoration-compatible context manager for testingFNcCs
d|_|S�NT��startedrrrrr�szmycontext.__enter__cGs||_|jSr)rT�catch�r
rTrrrr�szmycontext.__exit__)	rrrrmr�rTr�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�rmr5r�rrrr��sz'TestContextDecorator.test_instance_docsc	Cs@t�}|�}|�||�|�|j�W5QRX|�|jd�dS)N�NNN)r�rr'r�r5rT)r
�context�resultrrr�test_contextdecorator�s
z*TestContextDecorator.test_contextdecoratorc
Cs�t�}|�td��|�td��W5QRXW5QRX|�|j�|�|jdt�t�}d|_|�td��W5QRX|�|j�|�|jdt�dS)Nr<rT)r��assertRaisesRegex�	NameError�assertIsNotNonerTrr�)r
r�rrr�$test_contextdecorator_with_exception�sz9TestContextDecorator.test_contextdecorator_with_exceptioncs0t�����fdd��}|����jd�dS)Ncs���j����j�dSr�rYrTr'r�r�r�r
rr�test�sz1TestContextDecorator.test_decorator.<locals>.testr�)r�r5rT�r
r�rr�r�test_decorator�s
z#TestContextDecorator.test_decoratorc	sXt�����fdd��}��td��|�W5QRX���j����jdt�dS)Ncs$���j����j�td��dS)Nr<)rYrTr'r�r�rr�rrr��sz@TestContextDecorator.test_decorator_with_exception.<locals>.testr<r)r�r�r�r�rTrr�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)r}r~r�)r
r}r~r�rrr�method�sz@TestContextDecorator.test_decorating_method.<locals>.Test.method)N)rrrr�r�r�rr�Test�sr�r,�r}r~r�r|)r��objectr�r5r}r~r�)r
r�r�rr�r�test_decorating_method�s 	z+TestContextDecorator.test_decorating_methodc
Cs<Gdd�dt�}|�t��|��W5QRXW5QRXdS)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
Cs<Gdd�dt�}|�t��|��W5QRXW5QRXdS)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
||_dSrrSr�rrrrszQTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontext.__exit__)rrrr�rTrrrrrr�somecontext�sr�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�r5rT)r
r�r�r�rr�r�test_contextdecorator_as_mixin�sz3TestContextDecorator.test_contextdecorator_as_mixincsft�fdd��}g�|d���fdd��}|d����dddg�g�|d����dddg�dS)	Nc3s��|�dV��d�dS)Nr.r/)r�r1rrr3s
zETestContextDecorator.test_contextmanager_as_decorator.<locals>.woohoor,cs���dg���|�dSr�)r5r0)r7rCrrr�szCTestContextDecorator.test_contextmanager_as_decorator.<locals>.testZ	somethingr.zsomething elserv)r
r3r�rrCr� 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'�ZdS)(�TestBaseExitStackNcCs"|jj}|��}|�|j|�dSr)�
exit_stackrmr5r�rrrr�.sz$TestBaseExitStack.test_instance_docsc	Cs|���W5QRXdSr)r�rrrr�test_no_resources5s
z#TestBaseExitStack.test_no_resourcescs�difdifdifdtdd�fdtdd�fdtdd�fdtddd�fg}g��fd	d
�}|����}t|�D]d\}}|r�|r�|j|f|�|�}n4|r�|j|f|��}n|r�|j|f|�}n
|�|�}|�||�qj|jD]>}|�|dj|�|�|dj|j�|�	|dj
|j
�q�W5QRX|��|�g�|���p}|�t
��|jdd�W5QRX|�t
��|jjdd�W5QRX|�t��|j|dd
�W5QRXW5QRX|��dddifg�dS)Nr)r,)r,r�r,)Zexampler��)r
�callbackcs��||f�dS)zTest metadata propagationNr/)rrq�r�rr�_exitDsz.TestBaseExitStack.test_callback.<locals>._exit)�argr�)r�r�r�)�dictr��reversedr�r�_exit_callbacks�__wrapped__ZassertNotEqualrrYrmr5rrZassertWarnsrH)r
Zexpectedr��stackrrqr��wrapperrr�r�
test_callback9sD�	


 
"zTestBaseExitStack.test_callbackc	s*t���fdd�}dd�}�fdd�}Gdd�dt�}�����}|�|���|jd	d
|�||�}|�|���|jd	d
j|�|�|���|jd	d
|�||�}|�|���|jd	d
j|�|�|���|jd	d
|�|�|���|jd	d
|�d
dW5QRXdS)Ncs��|��dSr)r�r rTZexc_tb�Z
exc_raisedr
rr�_expect_excdsz0TestBaseExitStack.test_push.<locals>._expect_exccWsdSr�r��exc_detailsrrr�
_suppress_excfsz2TestBaseExitStack.test_push.<locals>._suppress_exccs"��|���|���|�dSr)rYr�rrr�
_expect_okhs

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__msz4TestBaseExitStack.test_push.<locals>.ExitCM.__init__cSs|�d�dS)NzShould not be called!)rJrrrrrosz5TestBaseExitStack.test_push.<locals>.ExitCM.__enter__cWs|j|�dSrr��r
r�rrrrqsz4TestBaseExitStack.test_push.<locals>.ExitCM.__exit__N�rrrr�rrrrrr�ExitCMlsr����r,r)r9r�r��pushrr��__self__)r
r�r�r�r�r��cmrr�r�	test_pushbs*






zTestBaseExitStack.test_pushc	s�G�fdd�dt�}g�|�}|���N}|j�fdd��}|�|�|�|�|�|jddj|���d�W5QRX|�	�dddd	g�dS)
Ncs$eZdZ�fdd�Z�fdd�ZdS)z4TestBaseExitStack.test_enter_context.<locals>.TestCMcs��d�dSr�r/rr�rrr�sz>TestBaseExitStack.test_enter_context.<locals>.TestCM.__enter__cs��d�dS�Nr�r/r�r�rrr�sz=TestBaseExitStack.test_enter_context.<locals>.TestCM.__exit__Nr#rr�rr�TestCM�srcs��d�dS)Nr�r/rr�rrr��sz3TestBaseExitStack.test_enter_context.<locals>._exitr�r,r�r�r�)
r�r�r�r��
enter_contextrr�r�r0r5)r
rr�r�r�rr�r�test_enter_context�s


z$TestBaseExitStack.test_enter_contextc	sZg�|���4}|j�fdd��}|�|�|����d�W5QRX|��ddg�dS)Ncs��d�dSr�r/rr�rrr��sz+TestBaseExitStack.test_close.<locals>._exitr�r,)r�r�r�r�r0r5)r
r�r�rr�r�
test_close�s

zTestBaseExitStack.test_closec	sng�|���4}|j�fdd��}|�|�|��}��d�W5QRX��d�|��|��dddg�dS)Ncs��d�dSrr/rr�rrr��sz-TestBaseExitStack.test_pop_all.<locals>._exitr,r�r�)r�r�r��pop_allr0r�r5)r
r�r��	new_stackrr�r�test_pop_all�s


zTestBaseExitStack.test_pop_allc
CsD|�t��0|���}|�dd��ddW5QRXW5QRXdS)NcWsdS)NFrrSrrr�<lambda>��z3TestBaseExitStack.test_exit_raise.<locals>.<lambda>r,r)rr9r�r��r
r�rrr�test_exit_raise�s
z!TestBaseExitStack.test_exit_raisec	Cs.|���}|�dd��ddW5QRXdS)NcWsdSr�rrSrrrr	�r
z6TestBaseExitStack.test_exit_suppress.<locals>.<lambda>r,r�r�r�rrrr�test_exit_suppress�s
z$TestBaseExitStack.test_exit_suppresscCs
Gdd�d�}Gdd�d�}Gdd�d�}z\|t��J|tt��4|��"|t��ddW5QRXW5QRXW5QRXW5QRXWnPtk
r�}z2|�|jt�|�|jjt�|�|jjj�W5d}~XYnX|�d	�|jd}|�|t�|�|jt	�dS)
Nc@s$eZdZdd�Zdd�Zdd�ZdS)zJTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExccSs
||_dSrrSr�rrrr��szSTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__init__cSs|Srrrrrrr�szTTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__enter__cWs
|j�dSrrSr�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�YnXdSr)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�)rV�
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��
ValueErrorrW�__context__rYrJrr9)r
rrrrT�	inner_excrrr�&test_exit_exception_chaining_reference�s"

4"

z8TestBaseExitStack.test_exit_exception_chaining_referencec
s�dd�}d��fdd�}zZ|���H}|�|t�|�|t�|�|t�|�|�|�|t�ddW5QRXWnPtk
r�}z2|�|jt�|�|jjt�|�	|jjj�W5d}~XYnX|�
d��d}|�|t�|�|jt�dS)NcSs|�dSrrrSrrr�	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�r�rrWrrYrJr9)r
rrr�rTrrrr�test_exit_exception_chaining�s&

"
z.TestBaseExitStack.test_exit_exception_chainingc
Cs�dd�}dd�}z2|��� }|�dd��|�|t�W5QRXWn.tk
rp}z|�|t�W5d}~XYnX|�d�z:|���(}|�|t�|�|�|�|t�W5QRXWn.tk
r�}z|�|t�W5d}~XYnX|�d�dS)	NcSs|�dSrrrSrrrrszHTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.raise_exccWsdSr�rr�rrrrszKTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.suppress_exccSsdSrrrrrrr	r
zGTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.<lambda>rz.Expected KeyError, but no exception was raised)r�r�rrIrWrJrr�)r
rrr�rTrrr�#test_exit_exception_non_suppressing	s"



z5TestBaseExitStack.test_exit_exception_non_suppressingc
Cs�tdd��}td�}td�}td�}td�}zF|���4}|�||��|�||��|�||��|�W5QRXWnptk
r�}zR|�||�|�|j|�|�|jj|�|�|jjj|�|�|jjjj�W5d}~XYnXdS)Ncssz
dVW5|�XdSrrrSrrr�gets_the_context_right&s
zZTestBaseExitStack.test_exit_exception_with_correct_context.<locals>.gets_the_context_rightr,r�r�r�)r4rIr�rrrrY)r
r!�exc1�exc2�exc3�exc4r�rTrrr�(test_exit_exception_with_correct_context$s(


�z:TestBaseExitStack.test_exit_exception_with_correct_contextc	
Cs�dd�}td�}td�}td�}td�}td�}z8|���&}|�|||�|�|||�|�W5QRXWn�tk
r�}zh|�||�|�|j|�|�|jj|�|�|jjj|�|�|jjjj|�|�|jjjjj�W5d}~XYnXdS)NcSsz|�W5|�XdSrr)rZ	outer_excrrr�raise_nestedFszQTestBaseExitStack.test_exit_exception_with_existing_context.<locals>.raise_nestedr,r�r�r�r�)rIr�r�rrrY)	r
r'r"r#r$r%Zexc5r�rTrrr�)test_exit_exception_with_existing_contextCs.

��z;TestBaseExitStack.test_exit_exception_with_existing_contextc
Csddd�}z*|���}|�|�ddW5QRXWn,tk
r^}z|�d�W5d}~XYnXdS)NcWsdSr�rr�rrrr`szDTestBaseExitStack.test_body_exception_suppress.<locals>.suppress_excr,rz%Expected no exception, got IndexError)r�r�rrJ)r
rr�rTrrr�test_body_exception_suppress_s

z.TestBaseExitStack.test_body_exception_suppressc	CsB|���0}|�dd��|�dd��|�dd��W5QRXdS)NcWsdSr�rrSrrrr	kr
zITestBaseExitStack.test_exit_exception_chaining_suppress.<locals>.<lambda>cWsddSr�rrSrrrr	lr
cWsidSr�rrSrrrr	mr
r
rrrr�%test_exit_exception_chaining_suppressis
z7TestBaseExitStack.test_exit_exception_chaining_suppressc	Cs0|���}td�D]}|�t�qW5QRXdS)Ni')r��ranger��int)r
r��irrr�test_excessive_nestingos
z(TestBaseExitStack.test_excessive_nestingcCsZGdd�dt�}|�}t�|_|��}|�t|j|�|�|�|�|jdd|�dS)Nc@seZdZdS)z7TestBaseExitStack.test_instance_bypass.<locals>.ExampleNrrrrr�Examplevsr/r�r,)	r�rr�rr�rr�rr�)r
r/r�r�rrr�test_instance_bypassus
z&TestBaseExitStack.test_instance_bypassc
s�Gdd�dt��Gdd�dt�}t�fdd��}tdd��}|����:}|���&}|�|��|�|��|d	��W5QRXW5QRX|j}|�|��|�|j|�|�	|jj�|�	|jj
�|�|j
|j�dS)
Nc@seZdZdS)zITestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.UniqueExceptionNrrrrr�UniqueException�sr1c@seZdZdS)zLTestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.UniqueRuntimeErrorNrrrrr�UniqueRuntimeError�sr2c
3s<z
dVWn,tk
r6}z�d�|�W5d}~XYnXdS)Nr,z
new exception�rIrS�r1rr�second�s
z@TestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.secondc
ss6z
dVWn&tk
r0}z|�W5d}~XYnXdSr�r3rSrrr�first�s
z?TestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.firstzplease no infinite loop.)rIr@r4rr�rZ	exceptionrWrrYrXr)r
r2r5r6Zerr_ctxZes_ctxrTrr4r�test_dont_reraise_RuntimeError~s"

z0TestBaseExitStack.test_dont_reraise_RuntimeError)rrrr�rr�r�r�r�rrrrrrrrr r&r(r)r*r.r0r7rrrrr�+s*
)"
0
	r�c@seZdZeZdS)�
TestExitStackN)rrr�	ExitStackr�rrrrr8�sr8c@sJeZdZdZdZejdd��Zdd�Zdd�Z	dd	�Z
d
d�Zdd
�ZdS)�TestRedirectStreamNcCs$|jj}|�d�}|�|j|�dSr)�redirect_streamrmr5r�rrrr��s
z%TestRedirectStream.test_instance_docscCs.tt|j�}|�d�|�tt|j�|�dSr)�getattr�sys�orig_streamr;r)r
�orig_stdoutrrr�test_no_redirect_in_init�s
z+TestRedirectStream.test_no_redirect_in_initc	Csrt��}d}tt|j�}|�|��t|tt|j�d�W5QRX|�tt|j�|�|���	�}|�
||�dS)Nz<Consider an API like help(), which prints directly to stdout��file)�io�StringIOr<r=r>r;�printr�getvalue�stripr5)r
r��msgr?�srrr�test_redirect_to_string_io�sz-TestRedirectStream.test_redirect_to_string_ioc	Cs.t��}|�|��}|�||�W5QRXdSr)rCrDr;r)r
r��enter_resultrrr�test_enter_result_is_target�sz.TestRedirectStream.test_enter_result_is_targetc	Cs�t��}|�|�}tt|j�}|�tddtt|j�d�W5QRX|�tdtt|j�d�W5QRX|�tt|j�|�|��}|�	|d�dS�N�Hello� )�endrBzWorld!rAz
Hello World!
�
rCrDr;r<r=r>rErrFr5�r
r�Z
write_to_fr?rIrrr�test_cm_is_reusable�s
 z&TestRedirectStream.test_cm_is_reusablec
Cs�t��}|�|�}tt|j�}|�@tddtt|j�d�|�tdtt|j�d�W5QRXW5QRX|�tt|j�|�|��}|�	|d�dSrMrQrRrrr�test_cm_is_reentrant�s
(z'TestRedirectStream.test_cm_is_reentrant)
rrrr;r>rr�r�r@rJrLrSrTrrrrr:�s

r:c@seZdZeZdZdS)�TestRedirectStdout�stdoutN)rrr�redirect_stdoutr;r>rrrrrU�srUc@seZdZeZdZdS)�TestRedirectStderr�stderrN)rrr�redirect_stderrr;r>rrrrrX�srXc@sZeZdZejdd��Zdd�Zdd�Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dS)�TestSuppresscCstj}t�}|�|j|�dSr)�suppressrmr5r�rrrr��szTestSuppress.test_instance_docsc	Cs"tt��}|�|�W5QRXdSr)r\rrY)r
rKrrr�test_no_result_from_enter�s
z&TestSuppress.test_no_result_from_enterc	Cs*tt��|�tdd�d�W5QRXdS)Nr�r�� )r\rr5�powrrrr�test_no_exception�s
zTestSuppress.test_no_exceptionc	Cs tt��td�W5QRXdS)Nr�)r\r�lenrrrr�test_exact_exception�s
z!TestSuppress.test_exact_exceptionc	Cs tt��ddW5QRXdS)NrN�2)r\�LookupErrorrrrr�test_exception_hierarchy�s
z%TestSuppress.test_exception_hierarchyc
Cs6|�t��"tt��ddW5QRXW5QRXdSr�)rr9r\rrrrr�test_other_exceptions
z!TestSuppress.test_other_exceptionc
Cs4|�t�� t��ddW5QRXW5QRXdSr�)rr9r\rrrr�test_no_argsszTestSuppress.test_no_argsc	Cs@ttt��ddW5QRXttt��td�W5QRXdS)Nr,rr�)r\r9rrarrrr�test_multiple_exception_args
sz)TestSuppress.test_multiple_exception_argsc
Csrtt�}|�W5QRX|�td�W5QRX|�*|�td�W5QRXd}ddW5QRX|�|�dS)Nr�Tr,r)r\rIrar')r
Zignore_exceptionsZouter_continuedrrrrTs
z!TestSuppress.test_cm_is_reentrantN)rrrrr�r�r]r`rbrerfrgrhrTrrrrr[�s
r[�__main__)rmrCr=r�r�Zunittest�
contextlibr�rrzZTestCaserr+r�r�r�r�r�r�r�r�r8r:rUrXr[r�mainrrrr�<module>s8+V"	0!x96