Your IP : 216.73.216.52


Current Path : /opt/alt/python35/lib64/python3.5/test/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/test/__pycache__/test_contextlib.cpython-35.opt-2.pyc



���]v�
@s�ddlZddlZddlZddlZddlTddlmZyddlZWnek
rtdZYnXGdd�dej	�Z
Gdd�dej	�ZGdd	�d	ej	�Zej
ed
�Gdd�dej	��ZGd
d�de�ZGdd�dej	�ZGdd�dej	�ZGdd�d�ZGdd�deej	�ZGdd�deej	�ZGdd�dej	�Zedkr�ej�dS)�N)�*)�supportc@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Ze
jdd��Ze
jdd��Zdd�ZdS)�ContextManagerTestCasecs~g�t�fdd��}|��7}|j�dg�|j|d��j|�WdQRX|j�dddg�dS)Nc3s#�jd�dV�jd�dS)N��*i�)�append�)�stater�4/opt/alt/python35/lib64/python3.5/test_contextlib.py�woohoos
z@ContextManagerTestCase.test_contextmanager_plain.<locals>.woohoorri�)�contextmanager�assertEqualr)�selfr�xr)r	r
�test_contextmanager_plainsz0ContextManagerTestCase.test_contextmanager_plaincs�g�t�fdd��}|jt��Q|��@}|j�dg�|j|d��j|�t��WdQRXWdQRX|j�dddg�dS)Nc3s+�jd�z	dVWd�jd�XdS)Nrri�)rr)r	rr
r s
	zBContextManagerTestCase.test_contextmanager_finally.<locals>.woohoorri�)r�assertRaises�ZeroDivisionErrorr
r)rrrr)r	r
�test_contextmanager_finallys
z2ContextManagerTestCase.test_contextmanager_finallycCsKtdd��}|�}|j�|j|jttd�d��dS)Ncss	dVdS)Nrrrrr
�whee0szCContextManagerTestCase.test_contextmanager_no_reraise.<locals>.whee�foo)r�	__enter__�assertFalse�__exit__�	TypeError)rr�ctxrrr
�test_contextmanager_no_reraise/s	
z5ContextManagerTestCase.test_contextmanager_no_reraisecCsKtdd��}|�}|j�|jt|jttd�d�dS)Nc	ssy	dVWn
dVYnXdS)Nrrrrr
�whoo9s	zOContextManagerTestCase.test_contextmanager_trap_yield_after_throw.<locals>.whoor)rrr�RuntimeErrorrr)rrrrrr
�*test_contextmanager_trap_yield_after_throw8s
	
zAContextManagerTestCase.test_contextmanager_trap_yield_after_throwcs�g�t��fdd��}|��C}�j�dg��j|d��j|�td��WdQRX�j�dddg�dS)Nc3so�jd�y	dVWnRtk
rj}z2�j|jd��j�dddg�WYdd}~XnXdS)Nrrri�)rr�argsr
)�e)rr	rr
rGs
	zAContextManagerTestCase.test_contextmanager_except.<locals>.woohoorri�)rr
rr)rrrr)rr	r
�test_contextmanager_exceptEs
z1ContextManagerTestCase.test_contextmanager_exceptcCs�td�}tdd��}y5|jtd��|��|�WdQRXWdQRXWn5tk
r�}z|j||�WYdd}~XnX|jd�dS)N�spamcss	dVdS)Nrrrrr
rXszJContextManagerTestCase.test_contextmanager_except_stopiter.<locals>.woohoo�
StopIterationzStopIteration was suppressed)r#rZassertWarnsRegex�PendingDeprecationWarning�	Exception�assertIs�fail)r�stop_excr�exrrr
�#test_contextmanager_except_stopiterVs	

#z:ContextManagerTestCase.test_contextmanager_except_stopitercCs�d}i}t|||�|d}td�}y|��|�WdQRXWn5tk
r�}z|j||�WYdd}~XnX|jd�dS)Nztfrom __future__ import generator_stop
from contextlib import contextmanager
@contextmanager
def woohoo():
    yield
rr"zStopIteration was suppressed)�execr#r%r&r')r�code�localsrr(r)rrr
�!test_contextmanager_except_pep479es

#z8ContextManagerTestCase.test_contextmanager_except_pep479cCstdd��}y|��t�WdQRXWnetk
r�}zE|jt|�t�|j|jdd�|j|j	t�WYdd}~XnXy!|��t
d��WdQRXWnbtk
r}zB|jt|�t
�|j|jdd�|j|j	�WYdd}~XnXdS)NcssDy	dVWn4tk
r?}ztd�|�WYdd}~XnXdS)Nzissue29692:Chained)r%r)�excrrr
�test_issue29692{s	zoContextManagerTestCase.test_contextmanager_do_not_unchain_non_stopiteration_exceptions.<locals>.test_issue29692rzissue29692:Chainedzissue29692:Unchained)rrr%r&�typerr
r�assertIsInstance�	__cause__r#�assertIsNone)rr0r)rrr
�?test_contextmanager_do_not_unchain_non_stopiteration_exceptionszs
&
zVContextManagerTestCase.test_contextmanager_do_not_unchain_non_stopiteration_exceptionscCs1dd�}t|dd�dd���}|S)Ncs�fdd�}|S)Ncs1x*�j�D]\}}t|||�q
W|S)N)�items�setattr)�func�k�v)�kwrr
�decorate�szXContextManagerTestCase._create_contextmanager_attribs.<locals>.attribs.<locals>.decorater)r;r<r)r;r
�attribs�szFContextManagerTestCase._create_contextmanager_attribs.<locals>.attribsr�barcSsdS)Nr)r"rrr
�baz�szBContextManagerTestCase._create_contextmanager_attribs.<locals>.baz)r)rr=r?rrr
�_create_contextmanager_attribs�sz5ContextManagerTestCase._create_contextmanager_attribscCs6|j�}|j|jd�|j|jd�dS)Nr?r>)r@r
�__name__r)rr?rrr
�test_contextmanager_attribs�sz2ContextManagerTestCase.test_contextmanager_attribscCs#|j�}|j|jd�dS)NzWhee!)r@r
�__doc__)rr?rrr
�test_contextmanager_doc_attrib�sz5ContextManagerTestCase.test_contextmanager_doc_attribcCs)|j�d�}|j|jd�dS)NzWhee!)r@r
rC)rr?rrr
�*test_instance_docstring_given_cm_docstring�szAContextManagerTestCase.test_instance_docstring_given_cm_docstringc
CsQtdd��}|ddddddd	d
��}|j|d�WdQRXdS)Ncss||||fVdS)Nr)rr8r�kwdsrrr
r�sz4ContextManagerTestCase.test_keywords.<locals>.woohoor�r8�r�!rF�,)rGrHrIrJ)rr
)rr�targetrrr
�
test_keywords�s$z$ContextManagerTestCase.test_keywordsN)rA�
__module__�__qualname__rrrrr!r*r.r5r@rBr�requires_docstringsrDrErLrrrr
rs
	

rc@s=eZdZejdd��Zdd�Zdd�ZdS)�ClosingTestCasecCs,tj}td�}|j|j|�dS)N)�closingrCr
)r�cm_docstring�objrrr
�test_instance_docs�s	z"ClosingTestCase.test_instance_docsc
sug�G�fdd�d�}|�}|j�g�t|��}|j||�WdQRX|j�dg�dS)Ncs"eZdZ�fdd�ZdS)z'ClosingTestCase.test_closing.<locals>.Ccs�jd�dS)Nr)r)r)r	rr
�close�sz-ClosingTestCase.test_closing.<locals>.C.closeN)rArMrNrUr)r	rr
�C�srVr)r
rQ)rrVr�yr)r	r
�test_closing�s	zClosingTestCase.test_closingcs�g�G�fdd�d�}|�}|j�g�|jt��3t|��}|j||�ddWdQRXWdQRX|j�dg�dS)Ncs"eZdZ�fdd�ZdS)z-ClosingTestCase.test_closing_error.<locals>.Ccs�jd�dS)Nr)r)r)r	rr
rU�sz3ClosingTestCase.test_closing_error.<locals>.C.closeN)rArMrNrUr)r	rr
rV�srVrr)r
rrrQ)rrVrrWr)r	r
�test_closing_error�s	z"ClosingTestCase.test_closing_errorN)rArMrNrrOrTrXrYrrrr
rP�srPc@seZdZdd�ZdS)�FileContextTestCasecCs�tj�}z�d}t|d��$}|j|j�|jd�WdQRX|j|j�d}|jt��Lt|d��5}|j|j�|j	|j
�d�ddWdQRXWdQRX|j|j�Wdtj|�XdS)N�wzBooh
�rrr)
�tempfile�mktemp�openr�closed�write�
assertTruerrr
�readr�unlink)rZtfn�frrr
�testWithOpen�sz FileContextTestCase.testWithOpenN)rArMrNrfrrrr
rZ�srZz!Threading required for this test.c@sXeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�LockContextTestCasecCs�|j|��|�|j|��WdQRX|j|��|jt��+|�|j|��ddWdQRXWdQRX|j|��dS)Nrr)rrbrr)r�lock�lockedrrr
�boilerPlate�szLockContextTestCase.boilerPlatecCs#tj�}|j||j�dS)N)�	threading�Lockrjri)rrhrrr
�testWithLock�sz LockContextTestCase.testWithLockcCs#tj�}|j||j�dS)N)rk�RLockrj�	_is_owned)rrhrrr
�
testWithRLock�sz!LockContextTestCase.testWithRLockcs2tj���fdd�}|j�|�dS)Ncs
�j�S)N)ror)rhrr
risz5LockContextTestCase.testWithCondition.<locals>.locked)rk�	Conditionrj)rrir)rhr
�testWithConditionsz%LockContextTestCase.testWithConditioncs2tj���fdd�}|j�|�dS)Ncs%�jd�r�j�dSdSdS)NFT)�acquire�releaser)rhrr
ri	s
z5LockContextTestCase.testWithSemaphore.<locals>.locked)rk�	Semaphorerj)rrir)rhr
�testWithSemaphoresz%LockContextTestCase.testWithSemaphorecs2tj���fdd�}|j�|�dS)Ncs%�jd�r�j�dSdSdS)NFT)rsrtr)rhrr
ris
z<LockContextTestCase.testWithBoundedSemaphore.<locals>.locked)rk�BoundedSemaphorerj)rrir)rhr
�testWithBoundedSemaphoresz,LockContextTestCase.testWithBoundedSemaphoreN)	rArMrNrjrmrprrrvrxrrrr
rg�s
rgc@s:eZdZdZdZdZdd�Zdd�ZdS)�	mycontextFNcCs
d|_|S)NT)�started)rrrr
r"s	zmycontext.__enter__cGs||_|jS)N)r/�catch)rr/rrr
r&s	zmycontext.__exit__)rArMrNrzr/r{rrrrrr
rys
ryc@s�eZdZejdd��Zdd�Zdd�Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dd�ZdS)�TestContextDecoratorcCs)tj}t�}|j|j|�dS)N)ryrCr
)rrRrSrrr
rT-s		z'TestContextDecorator.test_instance_docscCsPt�}|�'}|j||�|j|j�WdQRX|j|jd�dS)N)NNN)ryr&rbrzr
r/)r�context�resultrrr
�test_contextdecorator4s
		z*TestContextDecorator.test_contextdecoratorcCs�t�}|jtd��|�td��WdQRXWdQRX|j|j�|j|jdt�t�}d|_|�td��WdQRX|j|j�|j|jdt�dS)NrrT)ry�assertRaisesRegex�	NameError�assertIsNotNoner/r&r{)rr}rrr
�$test_contextdecorator_with_exception=s			z9TestContextDecorator.test_contextdecorator_with_exceptioncsBt�����fdd��}|��j�jd�dS)Ncs$�j�j��j�j�dS)N)r4r/rbrzr)r}rrr
�testQsz1TestContextDecorator.test_decorator.<locals>.test)NNN)ryr
r/)rr�r)r}rr
�test_decoratorNs	z#TestContextDecorator.test_decoratorc
spt�����fdd��}�jtd��|�WdQRX�j�j��j�jdt�dS)Ncs0�j�j��j�j�td��dS)Nr)r4r/rbrzr�r)r}rrr
r�\sz@TestContextDecorator.test_decorator_with_exception.<locals>.testrr)ryr�r�r�r/r&)rr�r)r}rr
�test_decorator_with_exceptionYs	z2TestContextDecorator.test_decorator_with_exceptioncst��G�fdd�dt�}|�}|jdd�|j|jd�|j|jd�|j|jd�|�}|jddd�|j|jd�|j|jd�|j|jd�|�}|jdddd�|j|jd�|j|jd�dS)Ncs%eZdZ�ddd��ZdS)z9TestContextDecorator.test_decorating_method.<locals>.TestNcSs||_||_||_dS)N)�a�b�c)rr�r�r�rrr
�methodms		z@TestContextDecorator.test_decorating_method.<locals>.Test.method)rArMrNr�r)r}rr
�Testksr�r�r�r�r�)ry�objectr�r
r�r�r�)rr�r�r)r}r
�test_decorating_methodhs 					z+TestContextDecorator.test_decorating_methodcCsBGdd�dt�}|jt��|��WdQRXWdQRXdS)Nc@s(eZdZdd�Zdd�ZdS)z7TestContextDecorator.test_typo_enter.<locals>.mycontextcSsdS)Nr)rrrr
�	__unter__�szATestContextDecorator.test_typo_enter.<locals>.mycontext.__unter__cWsdS)Nr)rr/rrr
r�sz@TestContextDecorator.test_typo_enter.<locals>.mycontext.__exit__N)rArMrNr�rrrrr
ry�sry)�ContextDecoratorr�AttributeError)rryrrr
�test_typo_enter�s
z$TestContextDecorator.test_typo_entercCsBGdd�dt�}|jt��|��WdQRXWdQRXdS)Nc@s(eZdZdd�Zdd�ZdS)z6TestContextDecorator.test_typo_exit.<locals>.mycontextcSsdS)Nr)rrrr
r�sz@TestContextDecorator.test_typo_exit.<locals>.mycontext.__enter__cWsdS)Nr)rr/rrr
�__uxit__�sz?TestContextDecorator.test_typo_exit.<locals>.mycontext.__uxit__N)rArMrNrr�rrrr
ry�sry)r�rr�)rryrrr
�test_typo_exit�s
z#TestContextDecorator.test_typo_exitcsqGdd�dt�}Gdd�d|t�}|�����fdd��}|��j�jd�dS)Nc@s4eZdZdZdZdd�Zdd�ZdS)zHTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontextFNcSs
d|_|S)NT)rz)rrrr
r�s	zRTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontext.__enter__cWs
||_dS)N)r/)rr/rrr
r�szQTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontext.__exit__)rArMrNrzr/rrrrrr
�somecontext�sr�c@seZdZdS)zFTestContextDecorator.test_contextdecorator_as_mixin.<locals>.mycontextN)rArMrNrrrr
ry�srycs$�j�j��j�j�dS)N)r4r/rbrzr)r}rrr
r��szATestContextDecorator.test_contextdecorator_as_mixin.<locals>.test)NNN)r�r�r
r/)rr�ryr�r)r}rr
�test_contextdecorator_as_mixin�s	z3TestContextDecorator.test_contextdecorator_as_mixincs�t�fdd��}g�|d���fdd��}|d��j�dddg�g�|d��j�dddg�dS)	Nc3s#�j|�dV�jd�dS)Ni�)r)rW)r	rr
r�s
zETestContextDecorator.test_contextmanager_as_decorator.<locals>.woohoorcs$�j�dg��j|�dS)Nr)r
r)r)rr	rr
r��szCTestContextDecorator.test_contextmanager_as_decorator.<locals>.testZ	somethingi�zsomething else)rr
)rrr�r)rr	r
� test_contextmanager_as_decorator�s!

z5TestContextDecorator.test_contextmanager_as_decoratorN)rArMrNrrOrTrr�r�r�r�r�r�r�r�rrrr
r|+s	r|c@s�eZdZejdd��Zdd�Zdd�Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'S)(�
TestExitStackcCs)tj}t�}|j|j|�dS)N)�	ExitStackrCr
)rrRrSrrr
rT�s		z TestExitStack.test_instance_docsc	Cst��WdQRXdS)N)r�)rrrr
�test_no_resources�s
zTestExitStack.test_no_resourcescsfifdifdifftdd�fdtdd�fd	tdd�fg}g��fdd�}t���}x�t|�D]�\}}|r�|r�|j|||�}nE|r�|j||�}n*|r�|j||�}n|j|�}|j||�q�WxP|jD]E}|j|j|�|j|j|j�|j	|j
|j
�qWWdQRX|j�|�dS)
Nrr�Zexamplecs�j||f�dS)N)r)rrF)r~rr
�_exit�sz*TestExitStack.test_callback.<locals>._exit)r)rr�)r)rr�)�dictr��reversed�callbackr&�_exit_callbacks�__wrapped__ZassertNotEqualrAr4rCr
)rZexpectedr��stackrrFre�wrapperr)r~r
�
test_callback�s.			!zTestExitStack.test_callbackc
sdt���fdd�}dd�}�fdd�}Gdd�dt�}t��}|j|��j|jd|�||�}|j|��j|jdj|�|j|��j|jd
|�||�}|j|��j|jdj|�|j|��j|jd|�|j|��j|jd|�d	d
WdQRXdS)Ncs�j|��dS)N)r&)�exc_typer/�exc_tb)�
exc_raisedrrr
�_expect_exc�sz,TestExitStack.test_push.<locals>._expect_exccWsdS)NTr)�exc_detailsrrr
�
_suppress_exc�sz.TestExitStack.test_push.<locals>._suppress_exccs+�j|��j|��j|�dS)N)r4)r�r/r�)rrr
�
_expect_ok�s

z+TestExitStack.test_push.<locals>._expect_okc@s4eZdZdd�Zdd�Zdd�ZdS)z'TestExitStack.test_push.<locals>.ExitCMcSs
||_dS)N)�	check_exc)rr�rrr
�__init__sz0TestExitStack.test_push.<locals>.ExitCM.__init__cSs|jd�dS)NzShould not be called!)r')rrrr
rsz1TestExitStack.test_push.<locals>.ExitCM.__enter__cWs|j|�dS)N)r�)rr�rrr
rsz0TestExitStack.test_push.<locals>.ExitCM.__exit__N)rArMrNr�rrrrrr
�ExitCMsr�rr���r�r�r�r�r�)rr�r��pushr&r��__self__)rr�r�r�r�r��cmr)r�rr
�	test_push�s*





zTestExitStack.test_pushc
s�G�fdd�dt�}g�|�}t��c}|j�fdd��}|j|�|j|�|j|jd	j|��jd�WdQRX|j	�ddddg�dS)
Ncs4eZdZ�fdd�Z�fdd�ZdS)z0TestExitStack.test_enter_context.<locals>.TestCMcs�jd�dS)Nr)r)r)r~rr
rsz:TestExitStack.test_enter_context.<locals>.TestCM.__enter__cs�jd�dS)N�)r)rr�)r~rr
rsz9TestExitStack.test_enter_context.<locals>.TestCM.__exit__N)rArMrNrrr)r~rr
�TestCMsr�cs�jd�dS)N�)rr)r~rr
r�"sz/TestExitStack.test_enter_context.<locals>._exitrr�r�r�r�)
r�r�r�r��
enter_contextr&r�r�rr
)rr�r�r�r�r)r~r
�test_enter_contexts	

z TestExitStack.test_enter_contextcsrg�t��F}|j�fdd��}|j|�|j��jd�WdQRX|j�ddg�dS)Ncs�jd�dS)Nr)rr)r~rr
r�.sz'TestExitStack.test_close.<locals>._exitr�r)r�r�r�rUrr
)rr�r�r)r~r
�
test_close+s

zTestExitStack.test_closecs�g�t��H}|j�fdd��}|j|�|j�}�jd�WdQRX�jd�|j�|j�dddg�dS)Ncs�jd�dS)Nr�)rr)r~rr
r�9sz)TestExitStack.test_pop_all.<locals>._exitrr�r�)r�r�r��pop_allrrUr
)rr�r��	new_stackr)r~r
�test_pop_all6s


zTestExitStack.test_pop_allcCsI|jt��3t��"}|jdd��ddWdQRXWdQRXdS)NcWsdS)NFr)r/rrr
�<lambda>Fsz/TestExitStack.test_exit_raise.<locals>.<lambda>rr)rrr�r�)rr�rrr
�test_exit_raiseCszTestExitStack.test_exit_raisec
Cs2t��"}|jdd��ddWdQRXdS)NcWsdS)NTr)r/rrr
r�Ksz2TestExitStack.test_exit_suppress.<locals>.<lambda>rr)r�r�)rr�rrr
�test_exit_suppressIsz TestExitStack.test_exit_suppressc%Cs=Gdd�d�}Gdd�d�}Gdd�d�}y\|t��I|tt��2|��!|t��
ddWdQRXWdQRXWdQRXWdQRXWndtk
r�}zD|j|jt�|j|jjt�|j|jjj�WYdd}~XnX|jd	�|jd}|j|t�|j|jt	�dS)
Nc@s4eZdZdd�Zdd�Zdd�ZdS)zFTestExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExccSs
||_dS)N)r/)rr/rrr
r�RszOTestExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__init__cSs|S)Nr)rrrr
rTszPTestExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__enter__cWs
|j�dS)N)r/)rr�rrr
rVszOTestExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__exit__N)rArMrNr�rrrrrr
�RaiseExcQsr�c@s4eZdZdd�Zdd�Zdd�ZdS)zQTestExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContextcSs||_||_dS)N)�outer�inner)rr�r�rrr
r�Zs	zZTestExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__init__cSs|S)Nr)rrrr
r]sz[TestExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__enter__c	Ws%y
|j�Wn|j�YnXdS)N)r�r�)rr�rrr
r_s
zZTestExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__exit__N)rArMrNr�rrrrrr
�RaiseExcWithContextYsr�c@s(eZdZdd�Zdd�ZdS)zITestExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExccSs|S)Nr)rrrr
rfszSTestExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExc.__enter__cWs|t|�_dS)NT)r1�
saved_details)rr�rrr
rhszRTestExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExc.__exit__N)rArMrNrrrrrr
�SuppressExcesr�rrz0Expected IndexError, but no exception was raised)
�
IndexError�KeyErrorr��
ValueErrorr2�__context__r4r'r�r)rr�r�r�r/�	inner_excrrr
�&test_exit_exception_chaining_referenceNs"


()

z4TestExitStack.test_exit_exception_chaining_referencecs5dd�}d��fdd�}ylt��\}|j|t�|j|t�|j|t�|j|�|j|t�ddWdQRXWndtk
r�}zD|j|jt�|j|jjt�|j	|jjj�WYdd}~XnX|j
d��d}|j|t�|j|jt�dS)NcSs
|�dS)Nr)r/rrr
�	raise_exc�sz=TestExitStack.test_exit_exception_chaining.<locals>.raise_exccs
|�dS)NTr)r�)r�rr
�suppress_exc�sz@TestExitStack.test_exit_exception_chaining.<locals>.suppress_excrrz0Expected IndexError, but no exception was raised)r�r�r�r�r�r�r�r2r�r4r'r)rr�r�r�r/r�r)r�r
�test_exit_exception_chaining~s&
)

z*TestExitStack.test_exit_exception_chainingcCs$dd�}dd�}y:t��*}|jdd��|j|t�WdQRXWn5tk
r�}z|j|t�WYdd}~XnX|jd�yDt��4}|j|t�|j|�|j|t�WdQRXWn5tk
r}z|j|t�WYdd}~XnX|jd�dS)	NcSs
|�dS)Nr)r/rrr
r��szDTestExitStack.test_exit_exception_non_suppressing.<locals>.raise_exccWsdS)NTr)r�rrr
r��szGTestExitStack.test_exit_exception_non_suppressing.<locals>.suppress_exccSsdS)Nrrrrr
r��szCTestExitStack.test_exit_exception_non_suppressing.<locals>.<lambda>z0Expected IndexError, but no exception was raisedz.Expected KeyError, but no exception was raised)r�r�r�r%r2r'r�r�)rr�r�r�r/rrr
�#test_exit_exception_non_suppressing�s"#

#z1TestExitStack.test_exit_exception_non_suppressingcCs/tdd��}td�}td�}td�}td�}yVt��F}|j||��|j||��|j||��|�WdQRXWn�tk
r*}zp|j||�|j|j|�|j|jj|�|j|jjj|�|j|jjjj�WYdd}~XnXdS)Nc
ssz	dVWd|�XdS)Nr)r/rrr
�gets_the_context_right�s	zVTestExitStack.test_exit_exception_with_correct_context.<locals>.gets_the_context_rightrr�r�r�)rr%r�r�r&r�r4)rr��exc1�exc2�exc3�exc4r�r/rrr
�(test_exit_exception_with_correct_context�s$z6TestExitStack.test_exit_exception_with_correct_contextc	CsAdd�}td�}td�}td�}td�}td�}yCt��3}|j|||�|j|||�|�WdQRXWn�tk
r<}z�|j||�|j|j|�|j|jj|�|j|jjj|�|j|jjjj|�|j|jjjjj�WYdd}~XnXdS)Nc
Ssz
|�Wd|�XdS)Nr)r�Z	outer_excrrr
�raise_nested�s
zMTestExitStack.test_exit_exception_with_existing_context.<locals>.raise_nestedrr�r�r��)r%r�r�r&r�r4)	rr�r�r�r�r�Zexc5r�r/rrr
�)test_exit_exception_with_existing_context�s(z7TestExitStack.test_exit_exception_with_existing_contextcCsqdd�}y,t��}|j|�ddWdQRXWn2tk
rl}z|jd�WYdd}~XnXdS)NcWsdS)NTr)r�rrr
r��sz@TestExitStack.test_body_exception_suppress.<locals>.suppress_excrrz%Expected no exception, got IndexError)r�r�r�r')rr�r�r/rrr
�test_body_exception_suppress�s
z*TestExitStack.test_body_exception_suppressc
CsPt��@}|jdd��|jdd��|jdd��WdQRXdS)NcWsdS)NTr)r/rrr
r�szETestExitStack.test_exit_exception_chaining_suppress.<locals>.<lambda>cWsddS)Nrrr)r/rrr
r�scWsidS)Nrr)r/rrr
r�s)r�r�)rr�rrr
�%test_exit_exception_chaining_suppress�sz3TestExitStack.test_exit_exception_chaining_suppressc
Cs;t��+}x!td�D]}|jt�qWWdQRXdS)Ni')r��ranger��int)rr��irrr
�test_excessive_nestingsz$TestExitStack.test_excessive_nestingcCsrGdd�dt�}|�}t�|_t�}|jt|j|�|j|�|j|jd|�dS)Nc@seZdZdS)z3TestExitStack.test_instance_bypass.<locals>.ExampleN)rArMrNrrrr
�Examplesr�rr�)	r�rr�rr�r�r�r&r�)rr�r�r�rrr
�test_instance_bypasss		
z"TestExitStack.test_instance_bypasscsGdd�dt��Gdd�dt�}t�fdd��}tdd��}|j���F}t��3}|j|��|j|��|d	��WdQRXWdQRX|j}|j|��|j|j|�|j	|jj�|j	|jj
�|j|j
|j�dS)
Nc@seZdZdS)zETestExitStack.test_dont_reraise_RuntimeError.<locals>.UniqueExceptionN)rArMrNrrrr
�UniqueExceptionsr�c@seZdZdS)zHTestExitStack.test_dont_reraise_RuntimeError.<locals>.UniqueRuntimeErrorN)rArMrNrrrr
�UniqueRuntimeErrorsr�c3sDy	dVWn4tk
r?}z�d�|�WYdd}~XnXdS)Nrz
new exception)r%)r/)r�rr
�seconds	z<TestExitStack.test_dont_reraise_RuntimeError.<locals>.secondcss;y	dVWn+tk
r6}z|�WYdd}~XnXdS)Nr)r%)r/rrr
�first s	z;TestExitStack.test_dont_reraise_RuntimeError.<locals>.firstzplease no infinite loop.)r%rrrr�r��	exceptionr2r�r4r3r&)rr�r�r�Zerr_ctxZes_ctxr/r)r�r
�test_dont_reraise_RuntimeErrors		z,TestExitStack.test_dont_reraise_RuntimeErrorN)rArMrNrrOrTr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrr
r��s&"
0
	r�c@smeZdZdZdZejdd��Zdd�Zdd�Z	dd	�Z
d
d�Zdd
�ZdS)�TestRedirectStreamNcCs2|jj}|jd�}|j|j|�dS)N)�redirect_streamrCr
)rrRrSrrr
rT<sz%TestRedirectStream.test_instance_docscCs?tt|j�}|jd�|jtt|j�|�dS)N)�getattr�sys�orig_streamr�r&)r�orig_stdoutrrr
�test_no_redirect_in_initCs
z+TestRedirectStream.test_no_redirect_in_initc
Cs�tj�}d}tt|j�}|j|��!t|dtt|j��WdQRX|jtt|j�|�|j�j	�}|j
||�dS)Nz<Consider an API like help(), which prints directly to stdout�file)�io�StringIOr�r�r�r��printr&�getvalue�stripr
)rre�msgr��srrr
�test_redirect_to_string_ioHs#z-TestRedirectStream.test_redirect_to_string_ioc
Cs9tj�}|j|��}|j||�WdQRXdS)N)r�r�r�r&)rre�enter_resultrrr
�test_enter_result_is_targetRsz.TestRedirectStream.test_enter_result_is_targetcCs�tj�}|j|�}tt|j�}|�'tddddtt|j��WdQRX|�!tddtt|j��WdQRX|jtt|j�|�|j�}|j	|d�dS)N�Hello�end� r�zWorld!z
Hello World!
)
r�r�r�r�r�r�r�r&r�r
)rre�
write_to_fr�r�rrr
�test_cm_is_reusableWs)#z&TestRedirectStream.test_cm_is_reusablecCs�tj�}|j|�}tt|j�}|�Qtddddtt|j��|�!tddtt|j��WdQRXWdQRX|jtt|j�|�|j�}|j	|d�dS)Nr�r�r�r�zWorld!z
Hello World!
)
r�r�r�r�r�r�r�r&r�r
)rrer�r�r�rrr
�test_cm_is_reentrantcs"*z'TestRedirectStream.test_cm_is_reentrant)
rArMrNr�r�rrOrTr�r�r�r�r�rrrr
r�7s
r�c@seZdZeZdZdS)�TestRedirectStdout�stdoutN)rArMrN�redirect_stdoutr�r�rrrr
r�psr�c@seZdZeZdZdS)�TestRedirectStderr�stderrN)rArMrN�redirect_stderrr�r�rrrr
rvsrc@s�eZdZejdd��Zdd�Zdd�Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dS)�TestSuppresscCs)tj}t�}|j|j|�dS)N)�suppressrCr
)rrRrSrrr
rT~s		zTestSuppress.test_instance_docsc	Cs'tt��}|j|�WdQRXdS)N)rr�r4)rr�rrr
�test_no_result_from_enter�sz&TestSuppress.test_no_result_from_entercCs1tt��|jtdd�d�WdQRXdS)Nr�r�� )rr�r
�pow)rrrr
�test_no_exception�s
zTestSuppress.test_no_exceptionc	Cs"tt��td�WdQRXdS)Nr�)rr�len)rrrr
�test_exact_exception�s
z!TestSuppress.test_exact_exceptionc	Cs tt��
ddWdQRXdS)Nr��2)r�LookupError)rrrr
�test_exception_hierarchy�s
z%TestSuppress.test_exception_hierarchycCs7|jt��!tt��
ddWdQRXWdQRXdS)Nrr)rrrr)rrrr
�test_other_exception�s
z!TestSuppress.test_other_exceptioncCs4|jt��t��
ddWdQRXWdQRXdS)Nrr)rrr)rrrr
�test_no_args�s
zTestSuppress.test_no_argscCsDttt��
ddWdQRXttt��td�WdQRXdS)Nrrr�)rrrr
)rrrr
�test_multiple_exception_args�sz)TestSuppress.test_multiple_exception_argscCswtt�}|�WdQRX|�td�WdQRX|�+|�td�WdQRXd}ddWdQRX|j|�dS)Nr�Trr)rr%r
rb)rZignore_exceptionsZouter_continuedrrr
r��sz!TestSuppress.test_cm_is_reentrantN)rArMrNrrOrTrr	rrrrrr�rrrr
r|sr�__main__)r�r�r]Zunittest�
contextlibr�rrk�ImportErrorZTestCaserrPrZZ
skipUnlessrgr�ryr|r�r�r�rrrA�mainrrrr
�<module>s0

�!0��m96