
    dafq                         d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d Z	d Z
d Zej                  dd       ZddZej                  dd       Z G d	 d
      ZdefdZ e       ZdddZy)    N)supportc                  \    t        j                         t        t        j                        fS N)_thread_countlen	threading	_dangling     8/root/Python-3.12.4/Lib/test/support/threading_helper.pythreading_setupr      s    >>S!4!4555r   c                  b   | \  }}d}t        j                  |d      D ]6  }t        t        j                        }t        j                         }||k  s6 y  dt         _        t        j                  d|dd| d| d	 dt               
       |D ]  }t        j                  d
|        y )Ng      ?FerrorTz2threading_cleanup() failed to clean up threads in .1fz  seconds
  before: thread count=z, dangling=z
  after: thread count=zDangling thread: )
r   sleeping_retrylistr	   r
   r   r   environment_alteredprint_warningr   )original_values
orig_countorig_ndanglingtimeout_dangling_threadscountthreads           r   threading_cleanupr      s    !0JG##G59   	 3 34 J #'Gc] "",[8H I!!&{37G3H2I	KL
 # > 1&<=>r   c                 B     t        j                          fd       }|S )zUse this function when threads are being used.  This will
    ensure that the threads are cleaned up even when the test fails.
    c                  N    t               }	  |  t        |  S # t        |  w xY wr   )r   r   )argskeyfuncs     r   	decoratorzreap_threads.<locals>.decorator>   s)    	$;s#s#s    
$)	functoolswraps)r$   r%   s   ` r   reap_threadsr(   :   s(     __T$ $ r   c              #     K   | t         j                  } t        j                         }	 d t	        j
                         }t        j                  | d      D ]1  }t        j                          t        j                         }||k  s1 y t	        j
                         |z
  }d|z
   d|dd| d| d		}t        |      # t	        j
                         }t        j                  | d      D ]1  }t        j                          t        j                         }||k  s1 w  t	        j
                         |z
  }d|z
   d|dd| d| d		}t        |      xY ww)
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.
    NFr   z!wait_threads() failed to cleanup z threads after r   z seconds (count: z, old count: ))	r   SHORT_TIMEOUTr   r   time	monotonicr   
gc_collectAssertionError)r   	old_count
start_timer   r   dtmsgs          r   wait_threads_exitr4   H   sk     '' I&^^%
''u= 
	&A NN$E	!	
	& !J.B6uy7H6I J$$&s8 ,#WM)A?C !%% ^^%
''u= 
	&A NN$E	!	
	& !J.B6uy7H6I J$$&s8 ,#WM)A?C !%%s)   'EC AE
8EAE8EEc                     |t         j                  }| j                  |       | j                         rd|dd}t	        |      y)zcJoin a thread. Raise an AssertionError if the thread is still alive
    after timeout seconds.
    Nzfailed to join the thread in r   z seconds)r   r+   joinis_aliver/   )r   r   r3   s      r   join_threadr8   k   sJ     ''
KK-gc](CS!! r   c              #   8  K   	 dd l }t        |       } g }	 	 | D ]#  }|j                          |j	                  |       % 	 d  	 |r |        t        j                         }t        dd      D ]  }|dz  }|D ]2  }|j                  t        |t        j                         z
  d             4 |D cg c]  }|j                         s| }}|s n,t
        j                  stt        dt        |      |fz          |D cg c]  }|j                         s| }}|r8||j                  t         j"                         t%        dt        |      z        y # t        $ r d }Y ^w xY w#  t
        j                  r"t        dt        |       t        |      fz          xY wc c}w c c}w # |D cg c]  }|j                         s| nc c}w }}|r8||j                  t         j"                         t%        dt        |      z        w xY w# 	 |r |        t        j                         }t        dd      D ]  }|dz  }|D ]2  }|j                  t        |t        j                         z
  d             4 |D cg c]  }|j                         s| nc c}w }}|s n,t
        j                  szt        dt        |      |fz          |D cg c]  }|j                         s| nc c}w }}|r8||j                  t         j"                         t%        dt        |      z        w # |D cg c]  }|j                         s| nc c}w }}|r8||j                  t         j"                         t%        dt        |      z        w xY wxY ww)	Nr   z/Can't start %d threads, only %d threads started      <   g{Gz?z7Unable to join %d threads during a period of %d minuteszUnable to join %d threads)faulthandlerImportErrorr   startappendr   verboseprintr   r,   r-   ranger6   maxr7   dump_tracebacksysstdoutr/   )threadsunlockr=   startedtendtimer   s          r   start_threadsrM   w   sZ     7mGGQ	 "	q!" 		Qnn&G B< 	B2  BAFF3w)994@AB&->1>>?? '*-g,)@A B	B #*:QQZZ\q:G:+ //

;$%@3w<%OPP C  	G7|S\23 4 ? ;':QQZZ\q::G:+ //

;$%@3w<%OPP !	Qnn&G B< 	B2  BAFF3w)994@AB&->1>>>?? '*-g,)@A B	B #*:QQZZ\q::G:+ //

;$%@3w<%OPP  #*:QQZZ\q::G:+ //

;$%@3w<%OPP s  NE N(E* H A,F, 1F"F"F, "F, =NF'F'=NE'#N&E''N*5FH "F, 'N,H1GGAHNNA,L2?J
J
L26L2NK0+K0/AN2N7MMANNNc                   (    e Zd ZdZd Zd Zd Zd Zy)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)
    c                 J    d | _         d | _        d | _        d | _        d | _        y r   )exc_type	exc_valueexc_tracebackr   	_old_hookselfs    r   __init__z"catch_threading_exception.__init__   s&    !r   c                     |j                   | _         |j                  | _        |j                  | _        |j                  | _        y r   )rQ   rR   rS   r   )rV   r"   s     r   _hookzcatch_threading_exception._hook   s1    !//kkr   c                 Z    t         j                  | _        | j                  t         _        | S r   )r	   
excepthookrT   rY   rU   s    r   	__enter__z#catch_threading_exception.__enter__   s     "--#zz	r   c                 >    | j                   t        _        | `| `| `| `y r   )rT   r	   r[   rQ   rR   rS   r   )rV   exc_infos     r   __exit__z"catch_threading_exception.__exit__   s#    #~~	MNKr   N)__name__
__module____qualname____doc__rW   rY   r\   r_   r   r   r   rO   rO      s    <"
r   rO   returnc                      t         j                  dk(  rt         j                  j                  S t         j                  dk(  ryy)ay  Detect 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__).
    
emscriptenwasiFT)rF   platform_emscripten_infopthreadsr   r   r   _can_start_threadrk      s6     |||###,,,		 r   F)modulec                 v    d}| rt         st        j                  |      yt        j                  t         |      S )zSkip tests or modules that require working threading.

    Can be used as a function/class decorator or to skip an entire module.
    zrequires threading supportN)can_start_threadunittestSkipTest
skipUnless)rl   r3   s     r   requires_working_threadingrr      s;    
 'C##C((   ""#3S99r   r   )r   
contextlibr&   rF   r	   r,   ro   testr   r   r   r(   contextmanagerr4   r8   rM   rO   boolrk   rn   rr   r   r   r   <module>rw      s       
    6>< & &D	" 'Q 'QT6 6r4 & %& ). 
:r   