hHddlZddlZddlZddlZddlZddlZddlZddlmZdZ dZ dZ ej ddZ ddZej ddZGd d Zd efd ZeZd ddZdS)N)supportcdtjtjfSN)_thread_count threading _danglingcopy4/usr/lib/python3.11/test/support/threading_helper.pythreading_setuprs$ >  Y05577 77r c d}t|D]}tjtjf}||krdS|srdt _|d}t jd|d|dz d|ddt|d|D]}t jd |d}d}tj d t j dS) NdTz&threading_cleanup() failed to cleanup rz threads (count: z , dangling: )zDangling thread: {Gz?) rangerrrr renvironment_altered print_warninglentimesleep gc_collect)original_values _MAX_COUNTcountvaluesdangling_threadsthreads r threading_cleanupr!s6Jz""!!9#66 _ $ $ EE $*.G '%ay   !#H%+AY1C%C#H#H-3AY#H#H033C/D/D#H#H#H I I I+ F F%&D&&D&DEEEE $  4+r cFtjfd}|S)zUse this function when threads are being used. This will ensure that the threads are cleaned up even when the test fails. cZt} |t|S#t|wxYwr)rr!)argskeyfuncs r decoratorzreap_threads..decorator;s< $4; s # # # s # # #s *) functoolswraps)r&r's` r reap_threadsr*7s;_T$$$$$ r c #K| tj}tj} dVt j}||z} tj}||krdSt j|kr8t j|z }d||z d|dd|d|d }t |t jd tj#t j}||z} tj}||krnwt j|kr8t j|z }d||z d|dd|d|d }t |t jd tjwxYw) aH bpo-31234: Context manager to wait until all threads created in the with statement exit. Use _thread.count() to check if threads exited. Indirectly, wait until threads exit the internal t_bootstrap() C function of the _thread module. threading_setup() and threading_cleanup() are designed to emit a warning if a test leaves running threads in the background. This context manager is designed to cleanup threads started by the _thread.start_new_thread() which doesn't allow to wait for thread exit, whereas thread.Thread has a join() method. NTz!wait_threads() failed to cleanup z threads after .1fz seconds (count: z , old count: rr) r SHORT_TIMEOUTrrr monotonicAssertionErrorrr)timeout old_count start_timedeadlinerdtmsgs r wait_threads_exitr6Es '  I! ^%% ' !N$$E !!~(**^%% 2C59;LCC(*9CC"'CC6?CCC%S))) Ju       !^%% ' !N$$E !!~(**^%% 2C59;LCC(*9CC"'CC6?CCC%S))) Ju       ! ! ! ! !s CB,Fc| tj}|||rd|dd}t |dS)zcJoin a thread. Raise an AssertionError if the thread is still alive after timeout seconds. Nzfailed to join the thread in r,z seconds)rr-joinis_aliver/)r r0r5s r join_threadr:js`' KK "CgCCCCS!!!""r c #bKddl}t|}g} |D]+}|||,n@#tjr.t dt|t|fzxYwdV |r |tj }tddD]}|dz }|D]9}| t|tj z d:d|D}|sn.tjr!t dt||fzd |D}|r>| tjt!d t|zdS#d |D}|r>| tjt!d t|zwxYw# |r |tj }tddD]}|dz }|D]9}| t|tj z d:d|D}|sn.tjr!t dt||fzd |D}|r>| tjt!d t|znQ#d |D}|r>| tjt!d t|zwxYwwxYw) Nrz/Can't start %d threads, only %d threads startedr<rc:g|]}||Sr r9.0ts r z!start_threads..s%>>>>1>>>r z7Unable to join %d threads during a period of %d minutesc:g|]}||Sr r?r@s r rCz!start_threads..s%:::QQZZ\\:q:::r zUnable to join %d threads) faulthandlerliststartappendrverboseprintrrr.rr8maxdump_tracebacksysstdoutr/)threadsunlockrEstartedrBendtimer0s r start_threadsrSvs7mmGGQ  " " q!!!! "  4G7||S\\23444   Q n&&G B<< B B2  BBAFF3w)9)994@@AAAA>>g>>>E?B'*-g,,)@ABBB;:':::G Q++CJ777$%@3w<<%OPPP Q Q;:':::G Q++CJ777$%@3w<<%OPPPPPPP% Q n&&G B<< B B2  BBAFF3w)9)994@@AAAA>>g>>>E?B'*-g,,)@ABBB;:':::G Q++CJ777$%@3w<<%OPPP Q;:':::G Q++CJ777$%@3w<<%OPPPPPPPPPPPsF.A G =BGB0F AGL.B0KA L.AL**L.c*eZdZdZdZdZdZdZdS)catch_threading_exceptiona  Context manager catching threading.Thread exception using threading.excepthook. Attributes set when an exception is caught: * exc_type * exc_value * exc_traceback * thread See threading.excepthook() documentation for these attributes. These attributes are deleted at the context manager exit. Usage: with threading_helper.catch_threading_exception() as cm: # code spawning a thread which raises an exception ... # check the thread exception, use cm attributes: # exc_type, exc_value, exc_traceback, thread ... # exc_type, exc_value, exc_traceback, thread attributes of cm no longer # exists at this point # (to avoid reference cycles) cLd|_d|_d|_d|_d|_dSr)exc_type exc_value exc_tracebackr _old_hookselfs r __init__z"catch_threading_exception.__init__s* ! r cf|j|_|j|_|j|_|j|_dSr)rWrXrYr )r\r$s r _hookzcatch_threading_exception._hooks,  !/k r cJtj|_|jt_|Sr)r excepthookrZr_r[s r __enter__z#catch_threading_exception.__enter__s"-#z  r c8|jt_|`|`|`|`dSr)rZrrarWrXrYr )r\exc_infos r __exit__z"catch_threading_exception.__exit__s%#~  M N   KKKr N)__name__ __module__ __qualname____doc__r]r_rbrer r r rUrUsZ<"""  r rUreturncltjdkrtjjStjdkrdSdS)ayDetect whether Python can start new threads. Some WebAssembly platforms do not provide a working pthread implementation. Thread support is stubbed and any attempt to create a new thread fails. - wasm32-wasi does not have threading. - wasm32-emscripten can be compiled with or without pthread support (-s USE_PTHREADS / __EMSCRIPTEN_PTHREADS__). emscriptenwasiFT)rMplatform_emscripten_infopthreadsr r r _can_start_threadrqs6 ||###,,   utr F)modulecxd}|rtstj|dStjt|S)zSkip tests or modules that require working threading. Can be used as a function/class decorator or to skip an entire module. zrequires threading supportN)can_start_threadunittestSkipTest skipUnless)rrr5s r requires_working_threadingrxsJ 'C : )#C(( ( ) )"#3S999r r)r contextlibr(rMrrrutestrrr!r*contextmanagerr6r:rSrUboolrqrtrxr r r r}si  8886    !!!!!!!!H " " " " "Q"Q"Q"QJ66666666r4&%$&&). : : : : : : :r