a ze@@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)argskwargscoroloopfuncN/opt/bitninja-python-dojo/embedded/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..MissingAexitNr"r#r$rrrr MissingAexit>sr:)rr2 TypeError)rr:rrrtest_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(rrrrrManagerFromScratchEsrBcseZdZfddZZS)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 NoneAenterSsrCc@seZdZdZdS)zNTestAbstractAsyncContextManager.test_structural_subclassing..NoneAexitN)r"r#r$rrrrr NoneAexitXsrD)Z assertTrue issubclassr assertFalse)rrBr&rCrDrrrtest_structural_subclassingDsz;TestAbstractAsyncContextManager.test_structural_subclassingN)r"r#r$rr+r8r<rGrrrrrs   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..woohoorJrKrJrKrL)rr4r3rrPxrrNrtest_contextmanager_plain`s 4z5AsyncContextManagerTestCase.test_contextmanager_plainc sgtfdd}|tt|4IdH@}|dg||d|tWdIdHq1IdHsz0YWdn1s0Y|gddS)Nc s0dzdVWdn d0dSrIrMrrNrrrPqs zGAsyncContextManagerTestCase.test_contextmanager_finally..woohoorJrKrQ)rr2ZeroDivisionErrorr4r3rRrrNrtest_contextmanager_finallyns   Nz7AsyncContextManagerTestCase.test_contextmanager_finallycsBtdd}|}|IdH||ttddIdHdS)NcSs dVdSrrrrrrwheeszHAsyncContextManagerTestCase.test_contextmanager_no_reraise..wheefoo)rr(rFrr;)rrWr,rrrtest_contextmanager_no_reraises  z:AsyncContextManagerTestCase.test_contextmanager_no_reraisecsftdd}|}|IdH|t(|ttddIdHWdn1sX0YdS)NcSs"z dVWndVYn0dSrrrrrrwhoos zTAsyncContextManagerTestCase.test_contextmanager_trap_yield_after_throw..whoorX)rr(r2 RuntimeErrorrr;rrZr,rrr*test_contextmanager_trap_yield_after_throws   zFAsyncContextManagerTestCase.test_contextmanager_trap_yield_after_throwcsNtdd}|}|t|IdHWdn1s@0YdS)NcSsdSrrrrrrrZszKAsyncContextManagerTestCase.test_contextmanager_trap_no_yield..whoo)rr2r[r(r\rrr!test_contextmanager_trap_no_yields   z=AsyncContextManagerTestCase.test_contextmanager_trap_no_yieldcsbtdd}|}|IdH|t$|dddIdHWdn1sT0YdS)NcSsdVdVdSrrrrrrrZszOAsyncContextManagerTestCase.test_contextmanager_trap_second_yield..whoo)rr(r2r[rr\rrr%test_contextmanager_trap_second_yields   zAAsyncContextManagerTestCase.test_contextmanager_trap_second_yieldcsbtdd}|}|IdH|t$|tddIdHWdn1sT0YdS)NcSs&z dVWnty tYn0dSr)r[ SyntaxErrorrrrrrZs  zLAsyncContextManagerTestCase.test_contextmanager_non_normalised..whoo)rr(r2r`rr[r\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)NrJrKrrQ)r3rUr r4)errOrrrPs   zFAsyncContextManagerTestCase.test_contextmanager_except..woohoorJrKrLrQ)rr4r3rUrRrrcrtest_contextmanager_excepts  2z6AsyncContextManagerTestCase.test_contextmanager_exceptc stdd}Gdddt}Gdddt}tdtd|d|dfD]}|jt|d zB|4IdH|WdIdHq1IdHs0YWn0ty}z|||WYd}~nd}~00||d WdqH1s0YqHdS) NcSs dVdSrrrrrrrPszOAsyncContextManagerTestCase.test_contextmanager_except_stopiter..woohooc@s eZdZdS)z^AsyncContextManagerTestCase.test_contextmanager_except_stopiter..StopIterationSubclassNr9rrrrStopIterationSubclasssrec@s eZdZdS)zcAsyncContextManagerTestCase.test_contextmanager_except_stopiter..StopAsyncIterationSubclassNr9rrrrStopAsyncIterationSubclasssrfspamZham)typez was suppressed)r StopIterationStopAsyncIterationsubTestrh Exceptionr'fail)rrPrerfZstop_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 )rlr[r6rrrrPs zQAsyncContextManagerTestCase.test_contextmanager_wrap_runtimeerror..woohoorJr)rr2r[rj)rrPrrr%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)rwrxrrvrattribss zKAsyncContextManagerTestCase._create_contextmanager_attribs..attribsbar)rXcSs dVdSrr)rgrrrbazszGAsyncContextManagerTestCase._create_contextmanager_attribs..baz)r)rryr{rrr_create_contextmanager_attribss  z:AsyncContextManagerTestCase._create_contextmanager_attribscCs(|}||jd||jddS)Nr{rz)r|r4r"rXrr{rrrtest_contextmanager_attribs sz7AsyncContextManagerTestCase.test_contextmanager_attribscCs|}||jddSNzWhee!r|r4__doc__r}rrrtest_contextmanager_doc_attribsz:AsyncContextManagerTestCase.test_contextmanager_doc_attribc sV|d}||jd|4IdHWdIdHqR1IdHsH0YdSrrr}rrr*test_instance_docstring_given_cm_docstrings zFAsyncContextManagerTestCase.test_instance_docstring_given_cm_docstringc s`tdd}|ddddd4IdH"}||dWdIdHq\1IdHsR0YdS) NcSs||||fVdSrrrrr kwdsrrrrPsz9AsyncContextManagerTestCase.test_keywords..woohoor-r0!,r)r-r0rr)rr4)rrPtargetrrr test_keywordss z)AsyncContextManagerTestCase.test_keywordsN)r"r#r$rrTrVrYr]r^r_rardrorqr|r~rZrequires_docstringsrrrrrrrrH^s:            rHc@sdeZdZGdddeZeZddZeddZeddZ ed d Z ed d Z ed dZ dS)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_coroutine's z3TestAsyncExitStack.SyncAsyncExitStack.run_coroutinecCs||Sr)racloser=rrrr <sz+TestAsyncExitStack.SyncAsyncExitStack.closecCs||Sr)rr(r=rrr __enter__?sz/TestAsyncExitStack.SyncAsyncExitStack.__enter__cGs||j|Sr)rrr exc_detailsrrr__exit__Bsz.TestAsyncExitStack.SyncAsyncExitStack.__exit__N)r"r#r$ staticmethodrr rrrrrrSyncAsyncExitStack&s  rcCs6t|_t|j||jj|tjddSr)rr rr Z addCleanupr r r=rrrsetUpGs  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)rJ)rJrJ)Zexamplecs||fdSrrM)r rrrr_exitXsz5TestAsyncExitStack.test_async_callback.._exit)argr)callbackr)dictrreversedpush_async_callbackr'_exit_callbacks __wrapped__ZassertNotEqualr" assertIsNonerr4r2r; exit_stack)rZexpectedrstackr rrrrrrtest_async_callbackMsB      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_exc{sz7TestAsyncExitStack.test_async_push.._expect_exccsdSNTrrrrr _suppress_exc}sz9TestAsyncExitStack.test_async_push.._suppress_exccs"|||dSr)rrr=rr _expect_oks  z6TestAsyncExitStack.test_async_push.._expect_okc@s$eZdZddZddZddZdS)z2TestAsyncExitStack.test_async_push..ExitCMcSs ||_dSr check_exc)rrrrr__init__sz;TestAsyncExitStack.test_async_push..ExitCM.__init__cs|ddS)NzShould not be called!)rmr=rrrr(sz=TestAsyncExitStack.test_async_push..ExitCM.__aenter__cs|j|IdHdSrrrrrrrsz.ExitCM.__aexit__N)r"r#r$rr(rrrrrExitCMsrrJr)rUobjectrpush_async_exitr'r__self__)rrrrrrcmrrrtest_async_pushxs*       z"TestAsyncExitStack.test_async_pushc sGfdddt}g|}t4IdHd}|jfdd}||||IdH||jddj|dWdIdHq1IdHs0Y| gddS) Ncs$eZdZfddZfddZdS)z;TestAsyncExitStack.test_async_enter_context..TestCMcsddS)NrJrMr=rrrr(szFTestAsyncExitStack.test_async_enter_context..TestCM.__aenter__csddS)NrrMrrrrrszETestAsyncExitStack.test_async_enter_context..TestCM.__aexit__NrArrrrTestCMs rcsddS)NrMrrrrrsz:TestAsyncExitStack.test_async_enter_context.._exitrrJr)rJrrr) 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|dSrrrprrr raise_excszHTestAsyncExitStack.test_async_exit_exception_chaining..raise_exccs|dSrrrZ saved_detailsrr suppress_excszKTestAsyncExitStack.test_async_exit_exception_chaining..suppress_excrJr0Expected IndexError, but no exception was raised) rr IndexErrorKeyErrorAttributeErrorrr1ZassertIsInstancerrrmrU)rrrrr6Z inner_excrrr"test_async_exit_exception_chainings&      6&  z5TestAsyncExitStack.test_async_exit_exception_chainingc sGdddttfddtfdd}|fD]}zD|4IdHtWdIdHq1IdHs0YWn0y}z|jWYd}~nd}~00dWdq:1s0Yq:dS)Nc@s eZdZdS)zWTestAsyncExitStack.test_async_exit_exception_explicit_none_context..MyExceptionNr9rrrr MyExceptionsrc s@z dVWn0ty:}z|Wd|_nd|_0Yn0dSr) BaseExceptionrrp)rrrmy_cms  zQTestAsyncExitStack.test_async_exit_exception_explicit_none_context..my_cmc sX4IdH.}|IdH|VWdIdHqT1IdHsJ0YdSr)rr)r)rrrrmy_cm_with_exit_stackszaTestAsyncExitStack.test_async_exit_exception_explicit_none_context..my_cm_with_exit_stackr)rlrrkrrrrm)rrrr6r)rrrr/test_async_exit_exception_explicit_none_contexts   4"zBTestAsyncExitStack.test_async_exit_exception_explicit_none_contextN) r"r#r$rrrrrrrrrrrrrrr%s * #  r__main__)r contextlibrrrrtestrZunittestZtest.test_contextlibrrZTestCaserrHrr"mainrrrrs  EHR