a 4dg@sVdZddlZddlZddlZddlZddlZddlTddlmZddl Z Gdddej Z Gdddej Z Gd d d ej Z Gd d d ej ZGd ddej ZGdddej ZGdddeZGdddej ZGdddZGdddeej ZGdddZGdddeej ZGdddeej ZGdd d ej Zed!krRedS)"z9Unit tests for contextlib.py, and other context managers.N)*)supportc@s$eZdZddZddZddZdS)TestAbstractContextManagercCs*Gdddt}|}|||dS)NcseZdZfddZZS)z;TestAbstractContextManager.test_enter..DefaultEntercstj|dSNsuper__exit__selfargs __class__0/usr/local/lib/python3.9/test/test_contextlib.pyrszDTestAbstractContextManager.test_enter..DefaultEnter.__exit____name__ __module__ __qualname__r __classcell__rrr r DefaultEntersr)AbstractContextManagerassertIs __enter__)r rmanagerrrr test_entersz%TestAbstractContextManager.test_entercCsDGdddt}|t|Wdn1s60YdS)Nc@s eZdZdS)zETestAbstractContextManager.test_exit_is_abstract..MissingExitNrrrrrrr MissingExitsr)r assertRaises TypeError)r rrrrtest_exit_is_abstracts z0TestAbstractContextManager.test_exit_is_abstractcCsGddd}|t|tGdddt}|t|tGddd|}|t|tGddd|}|t|tdS) Nc@seZdZddZddZdS)zRTestAbstractContextManager.test_structural_subclassing..ManagerFromScratchcSs|Srrr rrrr sz\TestAbstractContextManager.test_structural_subclassing..ManagerFromScratch.__enter__cSsdSrr)r exc_type exc_value tracebackrrrr"sz[TestAbstractContextManager.test_structural_subclassing..ManagerFromScratch.__exit__NrrrrrrrrrManagerFromScratchsr%cseZdZfddZZS)zLTestAbstractContextManager.test_structural_subclassing..DefaultEntercstj|dSrrr r rrr(szUTestAbstractContextManager.test_structural_subclassing..DefaultEnter.__exit__rrrr rr'src@seZdZdZdS)zGTestAbstractContextManager.test_structural_subclassing..NoEnterN)rrrrrrrrNoEnter-sr&c@seZdZdZdS)zFTestAbstractContextManager.test_structural_subclassing..NoExitN)rrrrrrrrNoExit2sr') assertTrue issubclassr assertFalse)r r%rr&r'rrrtest_structural_subclassingsz6TestAbstractContextManager.test_structural_subclassingN)rrrrrr+rrrrr src@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ e jddZe jddZddZddZddZdd Zd!S)"ContextManagerTestCasecsrgtfdd}|4}|dg||d|Wdn1sT0Y|gddS)Nc3sddVddSN*appendrstaterrwoohoo<s z@ContextManagerTestCase.test_contextmanager_plain..woohoor.r/r.r/r0)contextmanager assertEqualr2r r5xrr3rtest_contextmanager_plain:s (z0ContextManagerTestCase.test_contextmanager_plainc sgtfdd}|t`|:}|dg||d|tWdn1sf0YWdn1s0Y|gddS)Nc 3s0dzdVWdn d0dSr-r1rr3rrr5Is zBContextManagerTestCase.test_contextmanager_finally..woohoor.r/r6)r7rZeroDivisionErrorr8r2r9rr3rtest_contextmanager_finallyGs   Bz2ContextManagerTestCase.test_contextmanager_finallycCs6tdd}|}|||ttdddS)Ncss dVdSrrrrrrwheeYszCContextManagerTestCase.test_contextmanager_no_reraise..wheefoo)r7rr*rr)r r>ctxrrrtest_contextmanager_no_reraiseXs  z5ContextManagerTestCase.test_contextmanager_no_reraisecCs6tdd}|}||t|jttdddS)Ncss"z dVWndVYn0dSrrrrrrwhoobs zOContextManagerTestCase.test_contextmanager_trap_yield_after_throw..whoor?)r7rr RuntimeErrorrr)r rBr@rrr*test_contextmanager_trap_yield_after_throwas zAContextManagerTestCase.test_contextmanager_trap_yield_after_throwcs|gtfdd}|<}dg|d|tdWdn1s^0YgddS)Nc 3s^dz dVWnDtyX}z,|jdgdWYd}~n d}~00dS)Nr.r/rr6)r2r<r r8)er r4rrr5ps   zAContextManagerTestCase.test_contextmanager_except..woohoor.r/r0r6)r7r8r2r<r9rrFrtest_contextmanager_exceptns  &z1ContextManagerTestCase.test_contextmanager_exceptc Cstd}tdd}zZ|td:||Wdn1sD0YWdn1sb0YWn0ty}z|||WYd}~nd}~00|ddS)Nspamcss dVdSrrrrrrr5szJContextManagerTestCase.test_contextmanager_except_stopiter..woohoo StopIterationStopIteration was suppressed)rIr7ZassertWarnsRegexDeprecationWarning Exceptionrfail)r stop_excr5exrrr#test_contextmanager_except_stopiters D"z:ContextManagerTestCase.test_contextmanager_except_stopiterc Csd}i}t||||d}td}z.||Wdn1sF0YWn0ty}z|||WYd}~nd}~00|ddS)Nztfrom __future__ import generator_stop from contextlib import contextmanager @contextmanager def woohoo(): yield r5rHrJ)execrIrLrrM)r codelocalsr5rNrOrrr!test_contextmanager_except_pep479s &"z8ContextManagerTestCase.test_contextmanager_except_pep479c Cstdd}z.|tWdn1s.0YWnTty}z<|t|t||jdd||j tWYd}~n d}~00z2|t dWdn1s0YWnTty}z:|t|t ||jdd| |j WYd}~n d}~00dS)Nc ss>z dVWn.ty8}ztd|WYd}~n d}~00dS)Nissue29692:Chained)rLrCexcrrrtest_issue29692s zoContextManagerTestCase.test_contextmanager_do_not_unchain_non_stopiteration_exceptions..test_issue29692rrUzissue29692:Unchained) r7r<rLrtyperCr8r assertIsInstance __cause__rI assertIsNone)r rXrOrrr?test_contextmanager_do_not_unchain_non_stopiteration_exceptionss  &$*zVContextManagerTestCase.test_contextmanager_do_not_unchain_non_stopiteration_exceptionscCs"dd}t|dddd}|S)Ncsfdd}|S)Ncs"D]\}}t|||q|Sr)itemssetattr)funckvkwrrdecorateszXContextManagerTestCase._create_contextmanager_attribs..attribs..decorater)rdrerrcrattribss zFContextManagerTestCase._create_contextmanager_attribs..attribsbar)r?cSsdS)Whee!Nr)rHrrrbazszBContextManagerTestCase._create_contextmanager_attribs..bazr7)r rfrirrr_create_contextmanager_attribss  z5ContextManagerTestCase._create_contextmanager_attribscCs(|}||jd||jddS)Nrirg)rkr8rr?r rirrrtest_contextmanager_attribssz2ContextManagerTestCase.test_contextmanager_attribscCs|}||jddSNrhrkr8__doc__rlrrrtest_contextmanager_doc_attribsz5ContextManagerTestCase.test_contextmanager_doc_attribcCs|d}||jddSrnrorlrrr*test_instance_docstring_given_cm_docstrings zAContextManagerTestCase.test_instance_docstring_given_cm_docstringcCsLtdd}|ddddd}||dWdn1s>0YdS) Ncss||||fVdSrrr r`r kwdsrrrr5sz4ContextManagerTestCase.test_keywords..woohoo !,rs)rurvrwrxr7r8)r r5targetrrr test_keywordss z$ContextManagerTestCase.test_keywordscsRGddd}tfdd}|||dWdn1sD0YdS)Nc@s eZdZdS)z0ContextManagerTestCase.test_nokeepref..ANrrrrrAsr|c3s6t|}t|}||dVdSr)weakrefrefr\abr rrr5s     z5ContextManagerTestCase.test_nokeepref..woohoorrj)r r|r5rr rtest_nokeeprefs z%ContextManagerTestCase.test_nokeeprefcCstdd}|t|Wdn1s20Y|t|ddWdn1sf0Y|t|ddWdn1s0YdS)Ncss dVdSrrrrrrr5sz8ContextManagerTestCase.test_param_errors..woohoor)r7rrr r5rrrtest_param_errorss  $ ( z(ContextManagerTestCase.test_param_errorscs@dtfdd}|fddddS)Nrc3s*}d7dVd8|dSNr.)r8)before)depthr rrr5s z5ContextManagerTestCase.test_recursive..woohoocsdkrdS)N rr)r recursiverrrsz8ContextManagerTestCase.test_recursive..recursiveryrr)rrr rtest_recursivesz%ContextManagerTestCase.test_recursiveN)rrrr;r=rArDrGrPrTr]rkrmrrequires_docstringsrqrrr{rrrrrrrr,8s$       r,c@s*eZdZejddZddZddZdS)ClosingTestCasecCs tj}td}||j|dSr)closingrpr8r Z cm_docstringobjrrrtest_instance_docssz"ClosingTestCase.test_instance_docscsngGfddd}|}|gt|}|||Wdn1sR0Y|dgdS)NcseZdZfddZdS)z'ClosingTestCase.test_closing..CcsddSrr1r r3rrclosesz-ClosingTestCase.test_closing..C.closeNrrrrrr3rrCsrr.)r8rr rr:yrr3r test_closings  *zClosingTestCase.test_closingc sgGfddd}|}|g|tLt|$}|||ddWdn1sf0YWdn1s0Y|dgdS)NcseZdZfddZdS)z-ClosingTestCase.test_closing_error..CcsddSrr1r r3rrr$sz3ClosingTestCase.test_closing_error..C.closeNrrr3rrr#srr.r)r8rr<rrrr3rtest_closing_error!s    Dz"ClosingTestCase.test_closing_errorN)rrrrrrrrrrrrr s  rc@seZdZddZdS)NullcontextTestCasecCsLGddd}|}t|}|||Wdn1s>0YdS)Nc@s eZdZdS)z/NullcontextTestCase.test_nullcontext..CNrrrrrr1sr) nullcontextr)r rcZc_inrrrtest_nullcontext0s z$NullcontextTestCase.test_nullcontextN)rrrrrrrrr/src@seZdZddZdS)FileContextTestCasec Cst}zd}t|d&}||j|dWdn1sD0Y||jd}|t^t|d4}||j| | dddWdn1s0YWdn1s0Y||jWt |n t |0dS)NwzBooh rr.r) tempfilemktempopenr*closedwriter(rr<r8readrunlink)r Ztfnfrrr testWithOpen:s  (    Dz FileContextTestCase.testWithOpenN)rrrrrrrrr8src@s<eZdZddZddZddZddZd d Zd d Zd S)LockContextTestCasec Cs|||||Wdn1s20Y|||tH|$||ddWdn1s0YWdn1s0Y||dSNr.r)r*r(rr<)r locklockedrrr boilerPlateNs *   DzLockContextTestCase.boilerPlatecCst}|||jdSr) threadingLockrrr rrrr testWithLockYsz LockContextTestCase.testWithLockcCst}|||jdSr)rRLockr _is_ownedrrrr testWithRLock]sz!LockContextTestCase.testWithRLockcs$tfdd}||dS)NcsSr)rrrrrrcsz5LockContextTestCase.testWithCondition..locked)r Conditionrr rrrrtestWithConditionas z%LockContextTestCase.testWithConditioncs$tfdd}||dS)NcsdrdSdSdSNFTacquirereleaserrrrris z5LockContextTestCase.testWithSemaphore..locked)r SemaphorerrrrrtestWithSemaphoregs z%LockContextTestCase.testWithSemaphorecs$tfdd}||dS)NcsdrdSdSdSrrrrrrrss z.locked)rBoundedSemaphorerrrrrtestWithBoundedSemaphoreqs z,LockContextTestCase.testWithBoundedSemaphoreN) rrrrrrrrrrrrrrLs   rc@s,eZdZdZdZdZdZddZddZdS) mycontextz9Example decoration-compatible context manager for testingFNcCs d|_|SNTstartedr rrrrszmycontext.__enter__cGs ||_|jSr)rWcatchr rWrrrrszmycontext.__exit__) rrrrprrWrrrrrrrr|s rc@sbeZdZejddZddZddZddZd d Z d d Z d dZ ddZ ddZ ddZdS)TestContextDecoratorcCstj}t}||j|dSr)rrpr8rrrrrsz'TestContextDecorator.test_instance_docscCsTt}|(}|||||jWdn1s80Y||jddS)NNNN)rrr(rr8rW)r contextresultrrrtest_contextdecorators  *z*TestContextDecorator.test_contextdecoratorc Cst}|td<|tdWdn1s60YWdn1sT0Y||j||jdtt}d|_|tdWdn1s0Y||j||jdtdS)Nr?rT)rassertRaisesRegex NameErrorassertIsNotNonerWrr)r rrrr$test_contextdecorator_with_exceptionsD & z9TestContextDecorator.test_contextdecorator_with_exceptioncs0tfdd}|jddS)NcsjjdSrr\rWr(rrrr rrtests z1TestContextDecorator.test_decorator..testr)rr8rWr rrrrtest_decorators z#TestContextDecorator.test_decoratorcsltfdd}td|Wdn1s@0YjjdtdS)Ncs$jjtddS)Nr?)r\rWr(rrrrrrrs  z@TestContextDecorator.test_decorator_with_exception..testr?r)rrrrrWrrrrrtest_decorator_with_exceptions$ z2TestContextDecorator.test_decorator_with_exceptioncstGfdddt}|}|dd||jd||jd||jd|}|ddd||jd||jd||jd|}|jddd||jd||jddS) NcseZdZdddZdS)z9TestContextDecorator.test_decorating_method..TestNcSs||_||_||_dSr)rrr)r rrrrrrmethodsz@TestContextDecorator.test_decorating_method..Test.method)N)rrrrrrrrTestsrr.rrrr)robjectrr8rrr)r rrrrrtest_decorating_methods   z+TestContextDecorator.test_decorating_methodc CsdGdddt}|t6|Wdn1s80YWdn1sV0YdS)Nc@seZdZddZddZdS)z7TestContextDecorator.test_typo_enter..mycontextcSsdSrrr rrr __unter__szATestContextDecorator.test_typo_enter..mycontext.__unter__cWsdSrrrrrrrsz@TestContextDecorator.test_typo_enter..mycontext.__exit__N)rrrrrrrrrrsrContextDecoratorrAttributeErrorr rrrrtest_typo_enters z$TestContextDecorator.test_typo_enterc CsdGdddt}|t6|Wdn1s80YWdn1sV0YdS)Nc@seZdZddZddZdS)z6TestContextDecorator.test_typo_exit..mycontextcSsdSrrr rrrrsz@TestContextDecorator.test_typo_exit..mycontext.__enter__cWsdSrrrrrr__uxit__sz?TestContextDecorator.test_typo_exit..mycontext.__uxit__N)rrrrrrrrrrsrrrrrrtest_typo_exits z#TestContextDecorator.test_typo_exitcsRGdddt}Gddd|t}|fdd}|jddS)Nc@s$eZdZdZdZddZddZdS)zHTestContextDecorator.test_contextdecorator_as_mixin..somecontextFNcSs d|_|Srrr rrrrszRTestContextDecorator.test_contextdecorator_as_mixin..somecontext.__enter__cWs ||_dSrrVrrrrrszQTestContextDecorator.test_contextdecorator_as_mixin..somecontext.__exit__)rrrrrWrrrrrr somecontextsrc@s eZdZdS)zFTestContextDecorator.test_contextdecorator_as_mixin..mycontextNrrrrrr srcsjjdSrrrrrrrs zATestContextDecorator.test_contextdecorator_as_mixin..testr)rrr8rW)r rrrrrrtest_contextdecorator_as_mixins z3TestContextDecorator.test_contextdecorator_as_mixincsbtfdd}g|dfdd}|dgdg|dgd dS) Nc3s|dVddS)Nr0r1)rr3rrr5s zETestContextDecorator.test_contextmanager_as_decorator..woohoor.csdg|dSr)r8r2)r:rFrrrszCTestContextDecorator.test_contextmanager_as_decorator..test something)r.rr0something else)r.rr0ry)r r5rrrFr test_contextmanager_as_decoratorsz5TestContextDecorator.test_contextmanager_as_decoratorN)rrrrrrrrrrrrrrrrrrrrs     rc@seZdZdZejddZddZddZdd Z d d Z d d Z ddZ ddZ ddZddZddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'ZdS)(TestBaseExitStackNcCs"|jj}|}||j|dSr) exit_stackrpr8rrrrr.sz$TestBaseExitStack.test_instance_docscCs,|Wdn1s0YdSr)rr rrrtest_no_resources5s z#TestBaseExitStack.test_no_resourcesc s8difdifdifdtddfdtddfdtddfdtdddfg}gfd 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 qWdn1s80Y| |g|}| t |jdd Wdn1s0Y| t |jjd d Wdn1s0Y| t |j|dd Wdn1s0YWdn1s0Y| gdS)Nr)r.)r.rr.)Zexampler)r callbackcs||fdS)zTest metadata propagationNr1)r rtrrr_exitDsz.TestBaseExitStack.test_callback.._exit)argr)rr)dictrreversedrr_exit_callbacks __wrapped__ZassertNotEqualrr\rpr8rr)r Zexpectedrstackr rtrwrapperrrr test_callback9sD        6   , . NzTestBaseExitStack.test_callbackcs@tfdd}dd}fdd}Gdddt}}|||jd d |||}|||jd d j||||jd d |||}|||jd d j||||jd d ||||jd d |d d Wdn1s20YdS) Ncs|dSr)rr!rWexc_tbZ exc_raisedr rr _expect_excdsz0TestBaseExitStack.test_push.._expect_exccWsdSrr exc_detailsrrr _suppress_excfsz2TestBaseExitStack.test_push.._suppress_exccs"|||dSr)r\rr rr _expect_okhs  z/TestBaseExitStack.test_push.._expect_okc@s$eZdZddZddZddZdS)z+TestBaseExitStack.test_push..ExitCMcSs ||_dSr check_exc)r rrrr__init__msz4TestBaseExitStack.test_push..ExitCM.__init__cSs|ddS)NzShould not be called!)rMr rrrrosz5TestBaseExitStack.test_push..ExitCM.__enter__cWs|j|dSrrr rrrrrqsz4TestBaseExitStack.test_push..ExitCM.__exit__NrrrrrrrrrrExitCMlsrr.r)r<rrpushrr__self__)r rrrrrcmrrr test_pushbs*        zTestBaseExitStack.test_pushcsGfdddt}g|}|X}|jfdd}||||||jddj|dWdn1s0Y| gddS) Ncs$eZdZfddZfddZdS)z4TestBaseExitStack.test_enter_context..TestCMcsddSrr1r rrrrsz>TestBaseExitStack.test_enter_context..TestCM.__enter__csddSNrr1rrrrrsz=TestBaseExitStack.test_enter_context..TestCM.__exit__Nr$rrrrTestCMs rcsddS)Nrr1rrrrrsz3TestBaseExitStack.test_enter_context.._exitrr.r)r.rrr) rrrr enter_contextrrr r2r8)r rr rrrrrtest_enter_contexts   (z$TestBaseExitStack.test_enter_contextcsng|>}|jfdd}|||dWdn1sP0Y|ddgdS)NcsddSrr1rrrrrsz+TestBaseExitStack.test_close.._exitrr.)rrrrr2r8)r rrrrr test_closes  (zTestBaseExitStack.test_closecsg|>}|jfdd}|||}dWdn1sP0Yd||gddS)NcsddSr r1rrrrrsz-TestBaseExitStack.test_pop_all.._exitr.r)r.rr)rrrpop_allr2rr8)r rr new_stackrrr test_pop_alls  ( zTestBaseExitStack.test_pop_allc Csl|tN|&}|ddddWdn1s@0YWdn1s^0YdS)NcWsdS)NFrrVrrrz3TestBaseExitStack.test_exit_raise..r.r)rr<rr r rrrrtest_exit_raises  z!TestBaseExitStack.test_exit_raisecCsB|&}|ddddWdn1s40YdS)NcWsdSrrrVrrrrrz6TestBaseExitStack.test_exit_suppress..r.rrr rrrrtest_exit_suppresss z$TestBaseExitStack.test_exit_suppressc Cs^Gddd}Gddd}Gddd}z|t|ttf|@|tddWdn1sp0YWdn1s0YWdn1s0YWdn1s0YWnTty*}z:||jt||jjt||jjjWYd}~nd}~00|d |jd}||t||jt dS) Nc@s$eZdZddZddZddZdS)zJTestBaseExitStack.test_exit_exception_chaining_reference..RaiseExccSs ||_dSrrVrrrrrszSTestBaseExitStack.test_exit_exception_chaining_reference..RaiseExc.__init__cSs|Srrr rrrrszTTestBaseExitStack.test_exit_exception_chaining_reference..RaiseExc.__enter__cWs |jdSrrVrrrrrszSTestBaseExitStack.test_exit_exception_chaining_reference..RaiseExc.__exit__NrrrrrRaiseExcsrc@s$eZdZddZddZddZdS)zUTestBaseExitStack.test_exit_exception_chaining_reference..RaiseExcWithContextcSs||_||_dSr)outerinner)r rrrrrrsz^TestBaseExitStack.test_exit_exception_chaining_reference..RaiseExcWithContext.__init__cSs|Srrr rrrrsz_TestBaseExitStack.test_exit_exception_chaining_reference..RaiseExcWithContext.__enter__cWs"z |jWn|jYn0dSr)rrrrrrrs z^TestBaseExitStack.test_exit_exception_chaining_reference..RaiseExcWithContext.__exit__NrrrrrRaiseExcWithContextsrc@seZdZddZddZdS)zMTestBaseExitStack.test_exit_exception_chaining_reference..SuppressExccSs|Srrr rrrrszWTestBaseExitStack.test_exit_exception_chaining_reference..SuppressExc.__enter__cWs|t|_dSr)rY saved_detailsrrrrrs zVTestBaseExitStack.test_exit_exception_chaining_reference..SuppressExc.__exit__Nr$rrrr SuppressExcsr r.r0Expected IndexError, but no exception was raised) IndexErrorKeyErrorr ValueErrorrZ __context__r\rMrr<)r rrr rW inner_excrrr&test_exit_exception_chaining_references"    &   z8TestBaseExitStack.test_exit_exception_chaining_referencec s dd}dfdd}zn|R}||t||t||t||||tddWdn1sz0YWnRty}z:||jt||jjt| |jjjWYd}~nd}~00| dd}||t||jt dS)NcSs|dSrrrVrrr raise_excszATestBaseExitStack.test_exit_exception_chaining..raise_exccs|dSrrrrrr suppress_excszDTestBaseExitStack.test_exit_exception_chaining..suppress_excr.rr!) rrr"r#rr r$rZr%r\rMr<)r r(r*rrWr&rr)rtest_exit_exception_chainings&       *&  z.TestBaseExitStack.test_exit_exception_chainingc Cs"dd}dd}zF|*}|dd||tWdn1sJ0YWn0ty}z||tWYd}~nd}~00|dzN|2}||t||||tWdn1s0YWn2ty}z||tWYd}~nd}~00|ddS) NcSs|dSrrrVrrrr( szHTestBaseExitStack.test_exit_exception_non_suppressing..raise_exccWsdSrrrrrrr*szKTestBaseExitStack.test_exit_exception_non_suppressing..suppress_exccSsdSrrrrrrrrzGTestBaseExitStack.test_exit_exception_non_suppressing..r!z.Expected KeyError, but no exception was raised)rrr"rLrZrMr#r )r r(r*rrWrrr#test_exit_exception_non_suppressing s" ."    ."z5TestBaseExitStack.test_exit_exception_non_suppressingc Cstdd}td}td}td}td}zZ|>}||||||||||Wdn1sz0YWnrty}zZ|||||j|||jj|||jjj|||jjjjWYd}~n d}~00dS)NcsszdVW|n|0dSrrrVrrrgets_the_context_right&szZTestBaseExitStack.test_exit_exception_with_correct_context..gets_the_context_rightr.rrr)r7rLrrrr%r\)r r-exc1exc2exc3exc4rrWrrr(test_exit_exception_with_correct_context$s(  &  z:TestBaseExitStack.test_exit_exception_with_correct_contextc Cs dd}td}td}td}td}td}zL|0}|||||||||Wdn1sp0YWnty}zp|||||j|||jj|||jjj|||jjjj|||jjjjjWYd}~n d}~00dS)NcSsz |W|n|0dSrr)r&Z outer_excrrr raise_nestedFszQTestBaseExitStack.test_exit_exception_with_existing_context..raise_nestedr.rrrr)rLrrrr%r\) r r3r.r/r0r1Zexc5rrWrrr)test_exit_exception_with_existing_contextCs, &   z;TestBaseExitStack.test_exit_exception_with_existing_contextc Cszdd}z>|"}||ddWdn1s:0YWn.tyt}z|dWYd}~n d}~00dS)NcWsdSrrrrrrr*`szDTestBaseExitStack.test_body_exception_suppress..suppress_excr.rz%Expected no exception, got IndexError)rr r"rM)r r*rrWrrrtest_body_exception_suppress_s  *z.TestBaseExitStack.test_body_exception_suppresscCsV|:}|dd|dd|ddWdn1sH0YdS)NcWsdSrrrVrrrrkrzITestBaseExitStack.test_exit_exception_chaining_suppress..cWsddSrrrVrrrrlrcWsidSrrrVrrrrmrrrrrr%test_exit_exception_chaining_suppressis z7TestBaseExitStack.test_exit_exception_chaining_suppresscCsD|(}tdD]}|tqWdn1s60YdS)Ni')rrangerint)r rirrrtest_excessive_nestingos  z(TestBaseExitStack.test_excessive_nestingcCsZGdddt}|}t|_|}|t|j|||||jdd|dS)Nc@s eZdZdS)z7TestBaseExitStack.test_instance_bypass..ExampleNrrrrrExamplevrr;rr.) rrrrrrr rr)r r;r rrrrtest_instance_bypassus z&TestBaseExitStack.test_instance_bypassc sGdddtGdddt}tfdd}tdd}|X}|0}|||||d Wdn1s0YWdn1s0Y|j}||||j|| |jj| |jj | |j |jdS) Nc@s eZdZdS)zITestBaseExitStack.test_dont_reraise_RuntimeError..UniqueExceptionNrrrrrUniqueExceptionrr=c@s eZdZdS)zLTestBaseExitStack.test_dont_reraise_RuntimeError..UniqueRuntimeErrorNrrrrrUniqueRuntimeErrorrr>c 3s>z dVWn.ty8}zd|WYd}~n d}~00dS)Nr.z new exceptionrLrVr=rrseconds z@TestBaseExitStack.test_dont_reraise_RuntimeError..secondc ss8z dVWn(ty2}z|WYd}~n d}~00dSrr?rVrrrfirsts z?TestBaseExitStack.test_dont_reraise_RuntimeError..firstzplease no infinite loop.) rLrCr7rrr exceptionrZr%r\r[r)r r>rArBZerr_ctxZes_ctxrWrr@rtest_dont_reraise_RuntimeError~s"     D z0TestBaseExitStack.test_dont_reraise_RuntimeError)rrrrrrrrrr rrrrrr'r+r,r2r4r5r6r:r<rDrrrrr+s* )"  0  rc@seZdZeZdS) TestExitStackN)rrr ExitStackrrrrrrEsrEc@sJeZdZdZdZejddZddZddZ dd Z d d Z d d Z dS)TestRedirectStreamNcCs$|jj}|d}||j|dSr)redirect_streamrpr8rrrrrs z%TestRedirectStream.test_instance_docscCs.tt|j}|d|tt|j|dSr)getattrsys orig_streamrHr)r orig_stdoutrrrtest_no_redirect_in_inits  z+TestRedirectStream.test_no_redirect_in_initcCst}d}tt|j}||$t|tt|jdWdn1sL0Y|tt|j|| }| ||dS)Nztt"|tdddWdn1s00YdS)Nrr )rir$r8powr rrrtest_no_exceptions zTestSuppress.test_no_exceptioncCs4tttdWdn1s&0YdS)Nr)rirlenr rrrtest_exact_exceptions z!TestSuppress.test_exact_exceptioncCs4ttddWdn1s&0YdS)Nr[2)ri LookupErrorr rrrtest_exception_hierarchys z%TestSuppress.test_exception_hierarchyc Cs^|t@ttddWdn1s20YWdn1sP0YdSr)rr<rirr rrrtest_other_exceptions  z!TestSuppress.test_other_exceptionc Cs\|t>tddWdn1s00YWdn1sN0YdSr)rr<rir rrr test_no_argss zTestSuppress.test_no_argscCshtttddWdn1s(0YttttdWdn1sZ0YdS)Nr.rr)rir<rrnr rrrtest_multiple_exception_args s & z)TestSuppress.test_multiple_exception_argsc Cstt}|Wdn1s"0Y|tdWdn1sN0Y|H|tdWdn1s0Yd}ddWdn1s0Y||dS)NrTr.r)rirLrnr()r Zignore_exceptionsZouter_continuedrrrras&&&z!TestSuppress.test_cm_is_reentrantN)rrrrrrrjrmrorrrsrtrurarrrrrhs rh__main__)rprPrJrrZunittest contextlibrrr}ZTestCaserr,rrrrrrrrrErGrbrerhrmainrrrrs8 +V" 0!x96