
    ifK                        d 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Zddlm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZ d	Z eej                            d
d                    Z G d dej                  Zi ddddddddddddddddddddddddd dd!d"d#dd$dd%d&d'dd(dd)Zg d*Zddd+d,d-d.d.d/d0d1d2d2d2d3Zd4 Zd5 ZdTd6Zd7 Zd8 Zd9 Zd: Z d; Z!d< Z"h d=Z#h d>Z$dUd?Z%dVd@Z& G dA dBe'          Z(dC Z) ej*                    Z+dD Z,dE Z- G dF dGe.          Z/dH Z0dWdJZ1dK Z2dL Z3dM Z4 G dN dOe'          Z5 G dP dQe5ej6                  Z7 G dR dSej8                  Z9dS )Xz
.. caution::

    This module imports `subprocess` and `threading`; this can
    break monkey-patched unittests. Specifically,
    ``test_threading.ThreadTests.test_import_from_another_thread``.
    N)sleep   )six)validate_bool)perf_counter)get_originalFGEVENTTEST_QUIET0c                   "    e Zd ZdZdZd Zd ZdS )Popenz
    Depending on when we're imported and if the process has been monkey-patched,
    this could use cooperative or native Popen.
    Nc                     | S N selfs    >/usr/local/lib/python3.11/dist-packages/gevent/testing/util.py	__enter__zPopen.__enter__&   s        c                 $    t          |            d S r   )kill)r   argss     r   __exit__zPopen.__exit__)   s    T




r   )__name__
__module____qualname____doc__timerr   r   r   r   r   r   r      sC          E      r   r   normaldefaultzactual-outputredzcharacter-diffsmagentadebugcyanz
diff-chunkerror	brightredzerror-number	exceptionzexpected-outputgreenzfailed-examplefilename	lightblueinfolinenolightrednumberz	ok-numberskippedbrightyellowbrightmagenta	lightcyan)	slow-testsuboptimal-behaviourtestnamewarning))darkz0;)light1;)brightr8   )boldr8             !   "   #   $   %   )r   r   blackr    r'   brownyellowbluer!   r#   greygraywhitec                     d}t           D ]5\  }}|                     |          r| t          |          d          } |} n6t          |          }d||dS )N z[m)	_prefixes
startswithlen_colorcodes)colorprefix_codeprefixcode
color_codes        r   _color_coderV   ^   sr    K!  F## 	#f++,,'EKE	 U#JJ&;


33r   c                 6    t          t          |                    S r   )rV   _colorscheme)whats    r   _colorrZ   h   s    |D)***r   c                 F    t          |           |z   t          |          z   S r   )rZ   )rY   messager   s      r   	_colorizer]   k   s    $<<'!F6NN22r   c                     |                     dd          }|r| |z  }n| }t          ||          }t          5  t          j                            |dz              ddd           dS # 1 swxY w Y   dS )zV
    Log a *message*

    :keyword str color: One of the values from _colorscheme
    rQ   r   
N)popr]   output_locksysstderrwrite)r\   r   kwargsrQ   strings        r   logrg   n   s     JJw))E 4uf%%F	 ( (
$'''( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (s   #A((A,/A,c                 d    t           s(|                    dd           t          | g|R i | dS dS )z<
    Log the *message* only if we're not in quiet mode.
    rQ   r"   N)QUIET
setdefaultrg   )r\   r   re   s      r   r"   r"      sP      &'7+++G%d%%%f%%%%%& &r   c                 d   t          t          d          sd S 	 t          j        | d          S # t          $ r@}|j        dk    r*t          d| t          |          j        |           Y d }~d S Y d }~d S d }~wt          $ r/}t          d| t          |          j        |           Y d }~d S d }~ww xY w)Nkillpg	      zkillpg(%r, 9) failed: %s: %s)	hasattrosrl   OSErrorerrnorg   typer   	Exception)pidexs     r   rl   rl      s    2x   Hya    L L L8q==.T"XX5FKKKKKKKKK ===== H H H*Cb1BBGGGGGGGGGHs    . 
B//A33B/ $B**B/c                     d| z  }t          d| z  t          j                                                  d         }|r*|                                |dfvrt          d|           d S d S d S )Nz"ERROR: The process "%s" not found.ztaskkill /F /PID %s /T)rc   r   rK   z%r)r   
subprocessPIPEcommunicatestriprg   )ru   
ignore_msgerrs      r   kill_processtreer~      sx    5;J
(3.z
G
G
G
S
S
U
UVW
XC
 syy{{:r"222D# 22r   c                    t          | d          rL	 |                                  d S # t          $ r(}|j        dk    rY d }~d S |j        dk    rY d }~d S  d }~ww xY w	 t	          j        | j        d           d S # t          $ r Y d S w xY w)Nr   rn      rm   )ro   r   rq   rr   rp   ru   EnvironmentError)popenrv   s     r   _killr      s    uf 	JJLLLLL 	 	 	x1}}x2~~		GEIq!!!!! 	 	 	DD	s2   ( 
AAAAAA: :
BBc                 d   | j          | j                                          d | _         |                                 d S d| _        	 t	          | dd           rt          | j                   n3t          j        	                    d          rt          | j                   n## t          $ r t          j                     Y nw xY w	 t          |            n## t          $ r t          j                     Y nw xY w	 |                                  d S # t          $ r t          j                     Y d S w xY w)NTsetpgrp_enabledwin)r   cancelpoll
was_killedgetattrrl   ru   rb   platformrN   r~   rt   	traceback	print_excr   wait)r   s    r   r   r      sQ   {zz||E5+T22 	(59\$$U++ 	(UY'''   e   

   s7   AB   C ?C C C43C48D D/.D/>   GEVENT_DEBUGr	   GEVENTSETUP_EMBEDGEVENTSETUP_EV_VERIFY>   GEVENT_RESOLVERthreadGEVENTTEST_USE_RESOURCESallGEVENT_RESOLVER_NAMESERVERSz8.8.8.8c                    g }|pt           j                                        }|                    |pi            t	          |                                          D ]K\  }}|                    d          s|t          v r%||ft          v r1|	                    |d|           Lt          | t          j                  r|	                    |            n|                    |            d                    |          S )NGEVENT= )rp   environcopyupdatesorteditemsrN   IGNORED_GEVENT_ENV_KEYSIGNORED_GEVENT_ENV_ITEMSappend
isinstancer   string_typesextendjoin)commandenvsetenvresultkeyvalues         r   getnamer      s    F"*
"
"
$
$CJJv|SYY[[)) . .
U~~h'' 	)))<333ee,----'3+,, gg88Fr   c                    |                     dd           }d }t          j                            d          st	          t          dd           }|                     dd           }|                     dd           pi }t          | ||          }|d|d<   |rI|r|                                nt          j                                        }|                    |           |st          d|           t          | f||d	|}||_
        |d u|_        d
|_        |K t          dd          |t          |f          }	|	|_        d|	_        |	                                 |	|_        |S )NtimeoutDO_NOT_SETPGRPsetpgrpr   r   )r   r   1z+ %s)
preexec_fnr   F	threadingTimer)r   T)r`   rp   r   getr   r   r   r   rg   r   namer   r   r   r   r   daemonstart)
r   quietre   r   r   r   r   r   r   ts
             r   r   r      sm   jjD))GJ:>>*++ 2RD11

**UD
!
!CZZ$''-2F7F333D#&  6chhjjjRZ__%6%6

6 FD'DjcDDVDDEEJ&d2EE.Lg..wE9MMM				Lr   c                   P    e Zd ZdZ	 	 	 	 	 d	dZed             Zd ZeZd Z	d Z
dS )
	RunResultz
    The results of running an external command.

    If the command was successful, this has a boolean
    value of True; otherwise, a boolean value of false.

    The integer value of this object is the command's exit code.
    Nr   c
                     || _         || _        || _        || _        || _        || _        || _        || _        |	| _        d S r   )	r   
run_kwargsrT   outputr$   r   	run_countskipped_countrun_duration)
r   r   r   rT   r   r$   r   r   r   r   s
             r   __init__zRunResult.__init__  sK     $	
	"*(r   c                 4    | j                                         S r   )r   
splitlinesr   s    r   output_lineszRunResult.output_lines$  s    {%%'''r   c                 ,    t          | j                   S r   )boolrT   r   s    r   __bool__zRunResult.__bool__(  s    	??""r   c                     | j         S r   )rT   r   s    r   __int__zRunResult.__int__-  s
    yr   c                 X    d| j         d| j        d| j        d| j        d| j        dS )NzRunResult of: z
Code: z	
kwargs: z
Output:
----
z----
Error:
----
z----
)r   rT   r   r   r$   r   s    r   __repr__zRunResult.__repr__0  s=      LLLIIIOOOKKKJJJ#
 	
r   )NNNr   r   r   )r   r   r   r   r   propertyr   r   __nonzero__r   r   r   r   r   r   r     s          ,-) ) ) )( ( ( X(# # # K  
 
 
 
 
r   r   c                 F   d| v r|                      dd          } |                      dd          } |                      dd          } |                      dd          } |                      dd          } |                      dd          } |                      d	d          } d| v S )
NWarningzWarning -- reap_childrenNADAzWarning -- threading_cleanupz/DeprecationWarning: Using or importing the ABCsz UserWarning: libuv only supportsz&ImportWarning: Not importing directoryz*DeprecationWarning: 'U' mode is deprecatedz#DeprecationWarning: dns.hash module)replace)outs    r   _should_show_warning_outputr   F  s    C kk4f==kk8&AA kkKVTTkk<fEEkkBFKKkkFOOkk?HHr   c                    d}d}d}d}|r(t          j        d|          }|r||                                |                                         }|                    d|          }t          ||                    d          |                    d                             }t          j        d|          }|ryt          d||                                |                                                   }t          ||                    d          |                    d                             }|| |fz  }| d	k    rt          d
|          }|||fS )Nz[took %.1fs%s]rK   r   zRan (\d+) tests intookr   z \(skipped=(\d+)\)$r.   
   r2   )researchr   endr   intr]   )r   r   statusr.   r   r   rL   r   s           r   _find_test_statusr   _  s6   FGIM
 
:I+S11 	615577*+F^^FF33FC

15588 3455II,c22 	:	3qwwyy/@+ABBGAGGAJJquuQxx$7 899MtWo%Fbyy;//9m++r   c                    |                     dt                    }|                     dt                    }| }|                     dd          }|                     dd          }|r0d|vrd|vs
J |            t          j        |d<   t          j        |d<   t          | fd|i|}|j        }	 t                      }	|	                                \  }
}t                      |	z
  }|j
        s|                                d
}n|                                }t          |           |j        J n# t          |           |j        J w xY wt          |          o||v}|
rA|
                                }
t!          |
t"                    r|
n|
                    dd          }
|
rV|s|st'          |
          rCd|
                    dd          z   }
|
                                }
|
dz  }
t-          d||
           t/          ||
          \  }}}|rt-          d||||rdnd           n|st-          d||           t1          | |||v rdn||
|||||	  	        S )zv
    Execute *command*, returning a `RunResult`.

    This blocks until *command* finishes or until it times out.
    buffer_outputr   nestedFallowed_return_codesr   stdoutrc   NTIMEOUTzutf-8ignorez  r_   z
  z| %s
%sz! %s [code %s] %sr$   r3   )rQ   z- %s %sr   )r   r$   r   r   r   r   )r`   BUFFER_OUTPUTri   rx   STDOUTry   r   r   r   rz   r   r   r   r   r   r{   r   strdecoder   r   rstriprg   r   r   )r   re   r   r   verboser   r   r   r   
time_startr   r}   durationr   failedr   r   r   s                     r   runr   u  s    JJ>>MJJw&&EiGZZ%((F!::&<bAA +v%%(&*@*@*@&*@*@*@%,x%?x'111&11E:D
#!^^
$$&&S>>J. 	"uzz||3FFZZ\\FU{"""" 	U{"""""""
 &\\@f,@@F
 MiikkS))Lccszz'8/L/L
 # #' #%@%E%E #S[[v...jjlltJc"""'83'G'G$FI} %vv#?'')?	A 	A 	A 	A 	A %ItV$$$ +++##   s   /A(D0 0E
c                       e Zd ZdZdS )NoSetupPyFoundzRaised by find_setup_py_aboveN)r   r   r   r   r   r   r   r   r     s        ####r   r   c                    t           j                            t           j                            |                     }t           j                            t           j                            |d                    sv|t           j                            |          }}||k    rt          d|           t           j                            t           j                            |d                    v|S )zAReturn the directory containing setup.py somewhere above *a_file*zsetup.pyz!could not find my setup.py above )rp   pathdirnameabspathexistsr   r   )a_filerootprevs      r   find_setup_py_abover     s    7??27??62233DgnnRW\\$
;;<< T27??400d4<< .&&!RSSS	 gnnRW\\$
;;<< T
 Kr   Tc                    |  	 t          |           S # t          $ r Y nw xY w|:	 t          t          j        |j                 j                  S # t          $ r Y nw xY w|5	 t          t          j        |         j                  S # t          $ r Y nw xY w|rt          d          S t          dt                                )Nz./dnezAfter checking )r   r   rb   modulesr   __file__locals)r   a_module_namea_class	climb_cwds       r   search_for_setup_pyr    s    	&v... 	 	 	D	 	&s{73E'F'OPPP 	 	 	D	  	&s{='A'JKKK 	 	 	D	  ,"7+++
.:
;
;;s/    
  (A 
AA"#B 
BBc                      dt           j        d d         z  } dt           j        d d         z  }t          t           d          r
| dz  } |dz  }| |fS )N%s.%s   %s.%s.%srn   pypy_version_infopypy)rb   version_inforo   )	directoryfull_directorys     r   _version_dir_componentsr    s`    #*2A2..I#"22A2"66Ns'(( !V	& n$$r   c                     t          t                    } t          j                            | dd          }t                      \  }}dt          j        dd         z  }dt          j        dd         z  }t          t          d	          r
|d
z  }|d
z  }t          j                            ||          }t          j                            ||          }||fS )z
    Return a sequence of directories that could contain
    stdlib tests for the running version of Python.

    The most specific tests are at the end of the sequence.

    No checks are performed on existence of the directories.
    )r   src	greentestr  Nr	  r
  rn   r  r  )	r  r  rp   r   r   r  rb   r  ro   )setup_pyr  r  r  s       r   find_stdlib_testsr    s     #(333HXuk::I !8 9 9I~#*2A2..I#"22A2"66Ns'(( !V	& Y	22IW\\)^<<Nn$$r   c                      dt           j        vrdS t           j        d         } d |                     t           j        j                  D             } t           j        j                            |           S )z
    Return the PYTHONPATH environment variable (if set) with each
    entry being an absolute path. If not set, returns None.
    
PYTHONPATHNc                 L    g | ]!}t           j                            |          "S r   )rp   r   r   ).0ps     r   
<listcomp>z'absolute_pythonpath.<locals>.<listcomp>  s&    DDD1BGOOADDDr   )rp   r   splitr   pathsepr   )r   s    r   absolute_pythonpathr    s]    
 2:%%t:l#DDD

27?(C(CDDDD7?%%%r   c                   j    e Zd ZdZg ZdZdZdZdZd Z	e
d             Ze
d             Zd Zd	 Zd
 ZdS )ExampleMixinz
    Something that uses the ``examples/`` directory
    from the root of the gevent distribution.

    The `cwd` property is set to the root of the gevent distribution.
    rn   g      ?Nc                 H    t          t          t          |                     S )z(Return the directory containing setup.py)r   r  )r  r  rs   r   s    r   find_setup_pyzExampleMixin.find_setup_py  s%    "JJ
 
 
 	
r   c                     	 |                                  }n)# t          $ r}t          j        d|          d }~ww xY wt          j                            |d          S )N"Unable to locate file/dir to run: examples)r"  r   unittestSkipTestrp   r   r   )r   r   es      r   cwdzExampleMixin.cwd  sm    	S%%''DD 	S 	S 	S##aa$QRRR	Sw||D*---s    
=8=c                 .    t                      }|rd|indS )a"  
        Returns a dictionary of environment variables to set for the
        child in addition to (or replacing) the ones already in the
        environment.

        Since the child is run in `cwd`, relative paths in ``PYTHONPATH``
        need to be converted to absolute paths.
        r  N)r  )r   abs_pythonpaths     r   r   zExampleMixin.setenv&  s$     -..1?In--TIr   c                    t          | dd           rt          d| | j                  t          | dd           rt          d| | j                  	 | j        }| j        }n)# t          $ r}t          j        d|          d }~ww xY w| j	        pi }| j
        }|r%d|v r|d                             |           n||d<    |t          j        ddd|g| j        z   fd	|i|S )
Nr   zInvalid testserverr$  r   z-Wr   z-ur)  )r   AssertionErrorr   r-  exampler)  r   r&  r'  start_kwargsr   r   rb   
executableexample_args)r   methr-  
server_dirr(  re   r   s          r   _startzExampleMixin._start3  s8   4&& 	B tyAAA44(( 	D t{CCC	S\FJJ 	S 	S 	S##aa$QRRR	S "(b 	*6!!x ''////#)x t^T8T6:T=NN
 

 
 
 	
s   A 
B)B  Bc                 8    |                      t                    S N)r3  )r5  r   r   s    r   start_examplezExampleMixin.start_exampleM  s    {{{&&&r   c                 8    |                      t                    S r7  )r5  r   r   s    r   run_examplezExampleMixin.run_exampleP  s    {{{$$$r   )r   r   r   r   r2  before_delayafter_delayr/  r0  r"  r   r)  r   r5  r8  r:  r   r   r   r   r     s          LLKGL
 
 
 . . X. 
J 
J X
J
 
 
4' ' '% % % % %r   r   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )
TestServerNc                 >     ddl m} | fd            } |            S )Nr   )contextmanagerc               3      K                                    5 } | _                                         d V                                   d d d            d S # 1 swxY w Y   d S r   )r8  r   beforeafter)r   r   s    r   running_serverz1TestServer.running_server.<locals>.running_server[  s      ##%% "


	                 s   4AA A)
contextlibr@  )r   r@  rD  s   `  r   rD  zTestServer.running_serverX  sH    ------		 	 	 	 
	 ~r   c                     |                                  5  |                                  d d d            d S # 1 swxY w Y   d S r   )rD  _run_all_testsr   s    r   testzTestServer.testd  s      "" 	" 	"!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s   7;;c                     | j         t          | j                    |                     | j                                        | j        d| j                                                   d S Nz died with code )r;  r   assertIsNoner   r   r/  r   s    r   rB  zTestServer.beforeh  sk    ($#$$$$*//++"lllDJOO,=,=,=	 	 	 	 	r   c                     | j         t          | j                    |                     | j                                        | j        d| j                                                   d S rJ  )r<  r   rK  r   r   r/  r   s    r   rC  zTestServer.afterp  sk    '$"###$*//++"lllDJOO,=,=,=	 	 	 	 	r   c                     d}t          t          |                     D ]B}|                    d          r+t          | |          }t	          |          r |             d}C|sJ d S )NF_testT)r   dirrN   r   callable)r   ranmethodfunctions       r   rG  zTestServer._run_all_testsx  sw    SYY'' 	 	F  )) "400H%% HJJJC




r   )	r   r   r   r   rD  rH  rB  rC  rG  r   r   r   r>  r>  T  sd        E
  
  
 " " "        r   r>  c                       e Zd Zd Zd ZdS )alarmc                     t           j                            |            d| _        || _        |                                  d S )NT)r   Threadr   r   r   r   )r   r   s     r   r   zalarm.__init__  s8    !!$'''

r   c                     t          | j                   t          j                            d           t          j        d           d S )Nz	Timeout.
   )r   r   rb   rc   rd   rp   _exitr   s    r   r   z	alarm.run  s9    dl
&&&
r   N)r   r   r   r   r   r   r   r   rU  rU    s2              r   rU  )r   )NN)F)NNNT):r   r   rb   rp   r   r&  r   rx   timer   rK   r   gevent._configr   gevent._compatr   gevent.monkeyr   r   r   r   ri   r   rX   rM   rP   rV   rZ   r]   rg   r"   rl   r~   r   r   r   r   r   r   objectr   r   Lockra   r   r   rt   r   r   r  r  r  r  r   TestCaser>  rW  rU  r   r   r   <module>rb     s    
			 



 				                          ( ( ( ( ( ( ' ' ' ' ' ' & & & & & &  	bjnn%7==>>    J   $hy U	
 y V ) [ K  w f  H j  g!" #$ ~%& !%-  2  	 2R 4 4 4+ + +3 3 3 3( ( ("& & &	H 	H 	H    "  0         .   :=
 =
 =
 =
 =
 =
 =
 =
@  . in, , ,,; ; ;|$ $ $ $ $Y $ $ $  < < < <0% % %% % %4
& 
& 
&J% J% J% J% J%6 J% J% J%Z, , , , ,", , ,^    I     r   