U Wgl(@s$ddlmZddlZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z dd l m Z zDdd l mZdd l mZdd l mZed ddZedddZdZWn ek rdZZZYnXeedk dGddde Zeedk dGddde Zedkr edS))print_functionN)partial) skipUnless)skipIf)greenlet) getcurrent)TestCase)Context) ContextVar) copy_contextid)defaultvarzContextVar not supportedc@sleZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZdS)ContextVarsTestscOstj||SN)r run)selfargskwargsrO/opt/hc_python/lib64/python3.8/site-packages/greenlet/tests/test_contextvars.py _new_ctx_run#szContextVarsTests._new_ctx_runcCsbt}|dkr||n|||||tdD]}||d7<|q>dS)Nr)ID_VAR assertIsNoneget assertEqualsetrange)r greenlet_idcallbackcountsexpectZctx_var_rrr _increment&s  zContextVarsTests._incrementc sVtdtjtddtdDfddtddD}|D](}dkr`t|_qJd krJtj|_qJtd D]*}td7<|D] }|qq|d krt |D] }|qn|D] }|q|D]4} |j d kr |jqވ |jqވd kr8ddddd d nttd gdS)Nrcss|]}|dfVqdS)rNr.0irrr 6sz1ContextVarsTests._test_context..c sXg|]P}ttdkr$ttjjnj|dkr>|dndkrJdnddqS)rsharer)rrrN)r r!r"r#)rrr rr%r&r!r" propagate_byrrr 8sz2ContextVarsTests._test_context..rrr+rr)rrr)rrrswitchdictrr gr_contextrreversedZ assertTrueZdeadrZassertIsNotNonervalues)rr-ZletsZletr(rr,r _test_context1s8           zContextVarsTests._test_contextcCs||jddS)Nrrr7rrrr&test_context_propagated_by_context_runnsz7ContextVarsTests.test_context_propagated_by_context_runcCs||jddS)Nrr8r9rrr,test_context_propagated_by_setting_attributeqsz=ContextVarsTests.test_context_propagated_by_setting_attributecCs||jddSrr8r9rrrtest_context_not_propagatedtsz,ContextVarsTests.test_context_not_propagatedcCs||jddS)Nr+r8r9rrrtest_context_sharedwsz$ContextVarsTests.test_context_sharedcCs@ttj}ttj}|tj|tj|dSr)rr rr2rrlet1Zlet2rrrtest_break_ctxvarszs   z#ContextVarsTests.test_break_ctxvarscCsLttj}ttj}t|_t|_||||dSr)rrr2r r4r>rrr9test_not_broken_if_using_attribute_instead_of_context_runs  zJContextVarsTests.test_not_broken_if_using_attribute_instead_of_context_runc stdfdd}t|td `W5QRXj\}}|j |td |td | tj d|_j|d_j|d_jdt  t|d t|ddS)NcsXtjtdjttdjtdj}d_tjtdjttdjtdj}tj ||ftd|_tdtj tj|_tdtj tjdS)Nrr) rrrr4rZassertIsInstancer rrparentr2) old_context new_contextgrrrrtargets6       zFContextVarsTests.test_context_assignment_while_running..targetzcan't delete context attributerr)rrrassertRaisesRegexAttributeErrorr4rr2assertIsrrrgcZcollectsys getrefcount)rrGrCrDrrEr%test_context_assignment_while_runnings0 )   z6ContextVarsTests.test_context_assignment_while_runningc sXddl}tdt||||gfddfdd}|j|dd}|dd}|t d t |d W5QRX|t d  d|_ W5QRXd| |j | |j td d|_ |d| |dg||j |_ | |j dd=d}d}dS) NrcsDtddtdtjtdS)Nr r)VAR_VARrwaitrrBr2appendrr)holder is_runningshould_suspendrrgreenlet_in_thread_fns     zXContextVarsTests.test_context_assignment_different_thread..greenlet_in_thread_fncsFt}|_||d|~tdS)NrO)rr4rRr2rrQ)rF)ctx did_suspendrVrS should_exitrr thread_fns  zLContextVarsTests.test_context_assignment_different_thread..thread_fnT)rGdaemonrOzrunning in a differentr4r) threadingrPrr EventThreadstartrQrH ValueErrorgetattrr4rJrjoinr)rr\rZthreadrFr)rWrXrVrSrTrYrUr(test_context_assignment_different_threads@       z9ContextVarsTests.test_context_assignment_different_threadc Cs(t}|td ||_W5QRXdS)Nz6greenlet context must be a contextvars.Context or None)rrH TypeErrorr4)rgrrr"test_context_assignment_wrong_types z3ContextVarsTests.test_context_assignment_wrong_typeN)__name__ __module__ __qualname__rr%r7r:r;r<r=r@rArNrdrgrrrrr!s =  HBrzContextVar supportedc@seZdZddZdS)NoContextVarsTestsc Csttj}|t|d|tt|dW5QRX|t d|_W5QRX||tt|dW5QRX|t d|_W5QRX~dS)Nr4) rrr2Z assertFalsehasattrZ assertRaisesrIrar4)rr?rrrtest_contextvars_errors!s     z*NoContextVarsTests.test_contextvars_errorsN)rhrirjrmrrrrrksrk__main__) __future__rrKrLZunittest functoolsrrrrrr Z contextvarsr r r rrP ImportErrorrrkrhmainrrrrs2             ~