o Eh@sddlZddlZddlZddlZddlZddlZddlmZddZddZ ddZ ej dd d Z dd d Z ej dd dZGdddZdS)N)supportcCsttjfSN)_thread_count threading _danglingcopyr r 4/usr/lib/python3.10/test/support/threading_helper.pythreading_setupsr c Gsd}t|D]L}ttjf}||krdS|sGdt_|d}td|d|dd|ddt|d|D] }td |q:d}d}t d t qdS) NdTz&threading_cleanup() failed to cleanup rz threads (count: z , dangling: )zDangling thread: {Gz?) rangerrrrrenvironment_altered print_warninglentimesleep gc_collect)original_values _MAX_COUNTcountvaluesdangling_threadsthreadr r r threading_cleanups,    rcstfdd}|S)zUse this function when threads are being used. This will ensure that the threads are cleaned up even when the test fails. cs$t}z |Wt|St|wr)r r)argskeyfuncr r decorator:szreap_threads..decorator) functoolswraps)r!r"r r r reap_threads6sr%ccs|durtj}t}zCdVWt}||} t}||kr$dSt|krFt|}d||d|dd|d|d }t|td tqt}||} t}||krc wt|krt|}d||d|dd|d|d }t|td tqY) 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_timedeadlinerdtmsgr r r wait_threads_exitDsZ        r0cCs:|durtj}|||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))rr*r/r r r join_threadis r3ccs,ddl}t|}g}zz|D] }|||qWntjr/tdt|t|fdVWzX|r:|t }t ddD]/}|d7}|D]}| t |t dqKdd|D}|sentjrrtd t||fqCWd d|D}|r| tjtd t|dSd d|D}|r| tjtd t|wzW|r|t }t ddD]/}|d7}|D]}| t |t dqdd|D}|sntjrtd t||fqWd d|D}|r| tjtd t|wd d|D}|r| tjtd t|w) Nrz/Can't start %d threads, only %d threads startedr <rcSg|]}|r|qSr r2.0tr r r z!start_threads..z7Unable to join %d threads during a period of %d minutescSr6r r7r8r r r r;r<zUnable to join %d threads) faulthandlerliststartappendrverboseprintrrr(rr1maxdump_tracebacksysstdoutr))threadsunlockr=startedr:endtimer*r r r start_threadsus       rKc@s0eZdZdZddZddZddZdd Zd S) 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) cCs"d|_d|_d|_d|_d|_dSr)exc_type exc_value exc_tracebackr _old_hookselfr r r __init__s  z"catch_threading_exception.__init__cCs$|j|_|j|_|j|_|j|_dSr)rMrNrOr)rRrr r r _hooks zcatch_threading_exception._hookcCstj|_|jt_|Sr)r excepthookrPrTrQr r r __enter__sz#catch_threading_exception.__enter__cGs|jt_|`|`|`|`dSr)rPrrUrMrNrOr)rRexc_infor r r __exit__s z"catch_threading_exception.__exit__N)__name__ __module__ __qualname____doc__rSrTrVrXr r r r rLs  rLr)r contextlibr#rErrtestrr rr%contextmanagerr0r3rKrLr r r r s    $ %