a 4dg/;@sddlZddlmZmZmZddlZddlmZddlZddl m Z ddZ Gdddej Z Gd d d ej ZGd d d e ej Zed kredS)N)asynccontextmanagerAbstractAsyncContextManagerAsyncExitStack)support)TestBaseExitStackcstfdd}|S)Nc sX|i|}t}t|z||W|tdS|td0dSN)asyncionew_event_loopset_event_loopZrun_until_completecloseset_event_loop_policy)argskwargscoroloopfunc6/usr/local/lib/python3.9/test/test_contextlib_async.pywrapper s   z_async_test..wrapper) functoolswraps)rrrrr _async_test s rc@s4eZdZeddZeddZddZddZd S) TestAbstractAsyncContextManagerc stGdddt}|}||IdH||4IdH"}|||WdIdHqp1IdHsf0YdS)NcseZdZfddZZS)z@TestAbstractAsyncContextManager.test_enter..DefaultEntercstj|IdHdSrsuper __aexit__selfr  __class__rrrszJTestAbstractAsyncContextManager.test_enter..DefaultEnter.__aexit____name__ __module__ __qualname__r __classcell__rrrr DefaultEntersr&)rassertIs __aenter__)rr&managercontextrrr test_enters z*TestAbstractAsyncContextManager.test_enterc stddfdd}g}td}|tn4IdH:|2z3dHW}|||qF6WdIdHq1IdHs0YWdn1s0Y||dgdS)NcSs dVdSrrrrrrctx+szUTestAbstractAsyncContextManager.test_async_gen_propagates_generator_exit..ctxc sD4IdHdVWdIdHq@1IdHs60YdS)N rrr,rrgen/szUTestAbstractAsyncContextManager.test_async_gen_propagates_generator_exit..genr-)r ValueError assertRaisesappend assertEqual)rr/retexcvalrr.r(test_async_gen_propagates_generator_exit's    PzHTestAbstractAsyncContextManager.test_async_gen_propagates_generator_exitcCsDGdddt}|t|Wdn1s60YdS)Nc@s eZdZdS)zKTestAbstractAsyncContextManager.test_exit_is_abstract..MissingAexitN)r"r#r$rrrr MissingAexit>sr9)rr2 TypeError)rr9rrrtest_exit_is_abstract=s z5TestAbstractAsyncContextManager.test_exit_is_abstractcCsGddd}|t|tGdddt}|t|tGddd|}|t|tGddd|}|t|tdS) Nc@seZdZddZddZdS)zWTestAbstractAsyncContextManager.test_structural_subclassing..ManagerFromScratchcs|Srrrrrrr(FszbTestAbstractAsyncContextManager.test_structural_subclassing..ManagerFromScratch.__aenter__csdSrr)rexc_type exc_value tracebackrrrrHszaTestAbstractAsyncContextManager.test_structural_subclassing..ManagerFromScratch.__aexit__Nr"r#r$r(rrrrrManagerFromScratchEsrAcseZdZfddZZS)zQTestAbstractAsyncContextManager.test_structural_subclassing..DefaultEntercstj|IdHdSrrrrrrrNsz[TestAbstractAsyncContextManager.test_structural_subclassing..DefaultEnter.__aexit__r!rrrrr&Msr&c@seZdZdZdS)zOTestAbstractAsyncContextManager.test_structural_subclassing..NoneAenterN)r"r#r$r(rrrr NoneAenterSsrBc@seZdZdZdS)zNTestAbstractAsyncContextManager.test_structural_subclassing..NoneAexitN)r"r#r$rrrrr NoneAexitXsrC)Z assertTrue issubclassr assertFalse)rrAr&rBrCrrrtest_structural_subclassingDsz;TestAbstractAsyncContextManager.test_structural_subclassingN)r"r#r$rr+r8r;rFrrrrrs   rc@seZdZeddZeddZeddZeddZed d Zed d Z ed dZ eddZ eddZ eddZ ddZddZejddZejeddZeddZdS) AsyncContextManagerTestCasec sgtfdd}|4IdH:}|dg||d|WdIdHqr1IdHsh0Y|gddS)NcsddVddSN*r3rstaterrwoohoocs zEAsyncContextManagerTestCase.test_contextmanager_plain..woohoorIrJrIrJrK)rr4r3rrOxrrMrtest_contextmanager_plain`s 4z5AsyncContextManagerTestCase.test_contextmanager_plainc sgtfdd}|tt|4IdH@}|dg||d|tWdIdHq1IdHsz0YWdn1s0Y|gddS)Nc s0dzdVWdn d0dSrHrLrrMrrrOqs zGAsyncContextManagerTestCase.test_contextmanager_finally..woohoorIrJrP)rr2ZeroDivisionErrorr4r3rQrrMrtest_contextmanager_finallyns   Nz7AsyncContextManagerTestCase.test_contextmanager_finallycsBtdd}|}|IdH||ttddIdHdS)NcSs dVdSrrrrrrwheeszHAsyncContextManagerTestCase.test_contextmanager_no_reraise..wheefoo)rr(rErr:)rrVr,rrrtest_contextmanager_no_reraises  z:AsyncContextManagerTestCase.test_contextmanager_no_reraisecsftdd}|}|IdH|t(|ttddIdHWdn1sX0YdS)NcSs"z dVWndVYn0dSrrrrrrwhoos zTAsyncContextManagerTestCase.test_contextmanager_trap_yield_after_throw..whoorW)rr(r2 RuntimeErrorrr:rrYr,rrr*test_contextmanager_trap_yield_after_throws   zFAsyncContextManagerTestCase.test_contextmanager_trap_yield_after_throwcsNtdd}|}|t|IdHWdn1s@0YdS)NcSsdSrrrrrrrYszKAsyncContextManagerTestCase.test_contextmanager_trap_no_yield..whoo)rr2rZr(r[rrr!test_contextmanager_trap_no_yields   z=AsyncContextManagerTestCase.test_contextmanager_trap_no_yieldcsbtdd}|}|IdH|t$|dddIdHWdn1sT0YdS)NcSsdVdVdSrrrrrrrYszOAsyncContextManagerTestCase.test_contextmanager_trap_second_yield..whoo)rr(r2rZrr[rrr%test_contextmanager_trap_second_yields   zAAsyncContextManagerTestCase.test_contextmanager_trap_second_yieldcsbtdd}|}|IdH|t$|tddIdHWdn1sT0YdS)NcSs&z dVWnty tYn0dSr)rZ SyntaxErrorrrrrrYs  zLAsyncContextManagerTestCase.test_contextmanager_non_normalised..whoo)rr(r2r_rrZr[rrr"test_contextmanager_non_normaliseds   z>AsyncContextManagerTestCase.test_contextmanager_non_normalisedc sgtfdd}|4IdHB}dg|d|tdWdIdHq|1IdHsr0YgddS)Nc s^dz dVWnDtyX}z,|jdgdWYd}~n d}~00dS)NrIrJrrP)r3rTr r4)errNrrrOs   zFAsyncContextManagerTestCase.test_contextmanager_except..woohoorIrJrKrP)rr4r3rTrQrrbrtest_contextmanager_excepts  2z6AsyncContextManagerTestCase.test_contextmanager_exceptc stdd}tdtdfD]}|jt|dzB|4IdH|WdIdHqr1IdHsh0YWn0ty}z|||WYd}~nd}~00||dWdq1s0YqdS)NcSs dVdSrrrrrrrOszOAsyncContextManagerTestCase.test_contextmanager_except_stopiter..woohoospamZham)typez was suppressed)r StopIterationStopAsyncIterationZsubTestre Exceptionr'fail)rrOZstop_excexrrr#test_contextmanager_except_stopiters 2"z?AsyncContextManagerTestCase.test_contextmanager_except_stopiterc stdd}|tR|4IdHddWdIdHqZ1IdHsP0YWdn1sn0Y|tN|4IdHtWdIdHq1IdHs0YWdn1s0YdS)Nc SsDz dVWn4ty>}ztd||WYd}~n d}~00dS)Nzcaught )rhrZr6rrrrOs zQAsyncContextManagerTestCase.test_contextmanager_wrap_runtimeerror..woohoorIr)rr2rZrg)rrOrrr%test_contextmanager_wrap_runtimeerrors  P zAAsyncContextManagerTestCase.test_contextmanager_wrap_runtimeerrorcCs"dd}t|dddd}|S)Ncsfdd}|S)Ncs"D]\}}t|||q|Sr)itemssetattr)rkvkwrrdecoratesz]AsyncContextManagerTestCase._create_contextmanager_attribs..attribs..decorater)rsrtrrrrattribss zKAsyncContextManagerTestCase._create_contextmanager_attribs..attribsbar)rWcSs dVdSrr)rdrrrbazszGAsyncContextManagerTestCase._create_contextmanager_attribs..baz)r)rrurwrrr_create_contextmanager_attribss  z:AsyncContextManagerTestCase._create_contextmanager_attribscCs(|}||jd||jddS)Nrwrv)rxr4r"rWrrwrrrtest_contextmanager_attribssz7AsyncContextManagerTestCase.test_contextmanager_attribscCs|}||jddSNzWhee!rxr4__doc__ryrrrtest_contextmanager_doc_attribsz:AsyncContextManagerTestCase.test_contextmanager_doc_attribc sV|d}||jd|4IdHWdIdHqR1IdHsH0YdSr{r|ryrrr*test_instance_docstring_given_cm_docstrings zFAsyncContextManagerTestCase.test_instance_docstring_given_cm_docstringc s`tdd}|ddddd4IdH"}||dWdIdHq\1IdHsR0YdS) NcSs||||fVdSrrrrr kwdsrrrrOsz9AsyncContextManagerTestCase.test_keywords..woohoor-r0!,r)r-r0rr)rr4)rrOtargetrrr test_keywordss z)AsyncContextManagerTestCase.test_keywordsN)r"r#r$rrSrUrXr\r]r^r`rcrkrmrxrzrZrequires_docstringsr~rrrrrrrG^s:            rGc@sXeZdZGdddeZeZddZeddZeddZ ed d Z ed d Z d S)TestAsyncExitStackc@s0eZdZeddZddZddZddZd S) z%TestAsyncExitStack.SyncAsyncExitStackcsjtt|}|fdd|}|s@|S|j}z|Wn||_|Yn0dS)NcsSr)stop)frrr!zETestAsyncExitStack.SyncAsyncExitStack.run_coroutine..)rZget_event_loopZ ensure_futureadd_done_callbackZ run_forever exceptionresult __context__)rrr6r*rrr run_coroutines z3TestAsyncExitStack.SyncAsyncExitStack.run_coroutinecCs||Sr)racloser<rrrr 1sz+TestAsyncExitStack.SyncAsyncExitStack.closecCs||Sr)rr(r<rrr __enter__4sz/TestAsyncExitStack.SyncAsyncExitStack.__enter__cGs||j|Sr)rrr exc_detailsrrr__exit__7sz.TestAsyncExitStack.SyncAsyncExitStack.__exit__N)r"r#r$ staticmethodrr rrrrrrSyncAsyncExitStacks  rcCs6t|_t|j||jj|tjddSr)rr rr Z addCleanupr r r<rrrsetUp<s  zTestAsyncExitStack.setUpc sRdifdifdifdtddfdtddfdtddfg}gfdd}t4IdH}t|D]p\}}|r|r|j|g|Ri|}n:|r|j|g|R}n"|r|j|fi|}n ||}|||qb|jD]>}||dj|||dj|j| |dj |j qWdIdHqH1IdHs>0Y| |gt4IdH}| t |jddWdn1s0Y| t |jjd dWdn1s0Y| t |j|d d Wdn1s 0YWdIdHqB1IdHs80Y| gdS) Nr)rI)rIrI)Zexamplecs||fdSrrL)r rrrr_exitMsz5TestAsyncExitStack.test_async_callback.._exit)argr)callbackr)dictrreversedpush_async_callbackr'_exit_callbacks __wrapped__ZassertNotEqualr" assertIsNoner}r4r2r: exit_stack)rZexpectedrstackr rrrrrrtest_async_callbackBsB      D  , . \z&TestAsyncExitStack.test_async_callbackc sVtfdd}dd}fdd}Gdddt}4IdH}|||jd d |||}|||jd d j||||jd d |||}|||jd d j||||jd d ||||jd d |d d WdIdHqR1IdHsH0YdS) Ncs|dSr)r'r=r6exc_tbZ exc_raisedrrr _expect_excpsz7TestAsyncExitStack.test_async_push.._expect_exccsdSNTrrrrr _suppress_excrsz9TestAsyncExitStack.test_async_push.._suppress_exccs"|||dSr)rrr<rr _expect_okts  z6TestAsyncExitStack.test_async_push.._expect_okc@s$eZdZddZddZddZdS)z2TestAsyncExitStack.test_async_push..ExitCMcSs ||_dSr check_exc)rrrrr__init__ysz;TestAsyncExitStack.test_async_push..ExitCM.__init__cs|ddS)NzShould not be called!)rir<rrrr({sz=TestAsyncExitStack.test_async_push..ExitCM.__aenter__cs|j|IdHdSrrrrrrr}sz.ExitCM.__aexit__N)r"r#r$rr(rrrrrExitCMxsrrIr)rTobjectrpush_async_exitr'r__self__)rrrrrrcmrrrtest_async_pushms*       z"TestAsyncExitStack.test_async_pushc sGfdddt}g|}t4IdHd}|jfdd}||||IdH||jddj|dWdIdHq1IdHs0Y| gddS) Ncs$eZdZfddZfddZdS)z;TestAsyncExitStack.test_async_enter_context..TestCMcsddS)NrIrLr<rrrr(szFTestAsyncExitStack.test_async_enter_context..TestCM.__aenter__csddS)NrrLrrrrrszETestAsyncExitStack.test_async_enter_context..TestCM.__aexit__Nr@rrrrTestCMs rcsddS)NrLrrrrrsz:TestAsyncExitStack.test_async_enter_context.._exitrrIr)rIrrr) rrrZassertIsNotNoneenter_async_contextr'rrr3r4)rrrrrrrrtest_async_enter_contexts 4z+TestAsyncExitStack.test_async_enter_contextc sdd}dfdd}z|4IdHX}||t||t||t||||tddWdIdHq1IdHs0YWnRty}z:||jt||jjt| |jjjWYd}~nd}~00| dd}||t||jt dS)Ncs|dSrrrlrrr raise_excszHTestAsyncExitStack.test_async_exit_exception_chaining..raise_exccs|dSrrrZ saved_detailsrr suppress_excszKTestAsyncExitStack.test_async_exit_exception_chaining..suppress_excrIrz0Expected IndexError, but no exception was raised) rr IndexErrorKeyErrorAttributeErrorrr1ZassertIsInstancerrrirT)rrrrr6Z inner_excrrr"test_async_exit_exception_chainings&      6&  z5TestAsyncExitStack.test_async_exit_exception_chainingN) r"r#r$rrrrrrrrrrrrrrs * # r__main__)r contextlibrrrrtestrZunittestZtest.test_contextlibrrZTestCaserrGrr"mainrrrrs  E=/