
    9i                     P   S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKJr  SSKJr  SSKJr   SSK r SSK"r"SSK"J#r#  SSK"J$r$  SSK"J%r%  SS	K"J&r&  SS
K"J'r'  SSK"J(r(  SSK"J)r)  SSK"J*r*  SSK+J,r,  SSK+J-r-  SSK+J.r.  SSK+J/r/  SSK+J0r0  \((       a  SSK1J2r2  / SQr3S\Rh                  ;   r5S\
Rl                  ;   =(       d    S\
Rl                  ;   r7\7r8S\
Rl                  ;   r9S\
Rl                  ;   r:\Rv                  S:  r<\Rz                  " 5       R}                  5       S;   r?\Rz                  " 5       S:H  r@\.S 5       rA\%(       a  \A" 5       S:  rB\A" 5       S:  rCOS rBS rCS!rDS"rES#rFS$rG\8(       a  \DS%-  rD\GS%-  rG\ES&-  rE\FS%-  rFS'\
R                  " 5        S(3rIS)rJS*R                  S+S,5      rL\R                  " 5       R}                  5       S-;   rN\
Rl                  R                  S.5      =(       dO    \
R                  R                  \
R                  R                  \
R                  R                  \T5      S/S/5      5      rU\
R                  R                  \US05      rV\
R                  R                  \
R                  R                  \T5      5      rW\X" \"R                  S15      rZ\X" \"R                  S25      r[\X" \"S35      r\\X" \"R                  S45      r]\X" \"R                  S55      r^\(=(       a    \)(       + r_\X" \"S65      r`\X" \"R                  S75      ra\X" \"R                  S85      rb\X" \"R                  S95      rc\X" \"S:5      rd\X" \"S;5      re\X" \"S<5      rf\X" \"R                  S=5      rg\%=(       d    \#=(       a    \
R                  " 5       S:g  ri \d=(       a    \j" \"R                  " 5       5      rl \X" \"S>5      =(       a    \j" \"R                  " 5       5      rsS? rt\t" 5       u  rurv\w" \
R                  S@5      ry\R                  " \yR                  5        \{" \"5       V s/ s H#  o R                  SA5      (       d  M  \}" \"U 5      PM%     sn r~\}" \SB\" 5       5      r\" 5       r\" 5       r " SC SD5      r\\GR                  lm        \ c#  \r \\GR                  SE'   \GR                  5          " SF SG\GR                  5      rSH r\SSI j5       r\SJ 5       rSK r\SL 5       r\SM 5       r\GR"                  \G4SN jrSSO jrSP rSQ r " SR SS5      r\" \"GR.                  S\GSTSU9SV 5       r\" \\4S\GSTSU9SSW j5       r\" \\ GR                  R                  4S\GSTSU9SX 5       rSY rSZ r\GR<                  S[ 5       rS\ rSS] jrSS^ jr " S_ S`\GRF                  5      r\ GRJ                  GRM                  \5SaSb9\ GRJ                  GRO                  ScSd9 " Se Sf\5      5       5       rSg rSh r " Si Sj5      r " Sk Sl5      r\D4Sm jrSSn jrSSo jrSSp jr\\S4Sq jr\R>                  4Sr jrSSs jrSt r\GR<                  Su 5       rSv rSw rSx rSy rSz rS{ rS| r\((       a  \GR<                  SS} j5       rO\GR<                  SS~ j5       r\R                  S 5       r\((       a   \R"                  " \GR"                  S 5        gg! \! a    Sr  GNf = f! \m a8    \R                  " \R                  " \p\R                  " 5       5      5        S rl GNef = f! \m a8    \R                  " \R                  " \p\R                  " 5       5      5        S rs GN~f = fs  sn f )zTest utilities.    N)AF_INET)AF_INET6)SOCK_STREAM)AIX)LINUX)MACOS)NETBSD)OPENBSD)POSIX)SUNOS)WINDOWS)bytes2human)debug)memoize)print_color)supports_ipv6)wait_pid)JDEVNULLGLOBAL_TIMEOUTTOLERANCE_SYS_MEM
NO_RETRIESPYPY
PYTHON_EXEPYTHON_EXE_ENVROOT_DIRSCRIPTS_DIRTESTFN_PREFIXUNICODE_SUFFIXINVALID_UNICODE_SUFFIX
CI_TESTINGVALID_PROC_STATUSESTOLERANCE_DISK_USAGEIS_64BITHAS_CPU_AFFINITYHAS_CPU_FREQHAS_ENVIRONHAS_PROC_IO_COUNTERS
HAS_IONICEHAS_MEMORY_MAPSHAS_PROC_CPU_NUM
HAS_RLIMITHAS_SENSORS_BATTERYHAS_BATTERYHAS_SENSORS_FANSHAS_SENSORS_TEMPERATURESHAS_NET_CONNECTIONS_UNIXMACOS_11PLUSMACOS_12PLUSCOVERAGEAARCH64PYTEST_PARALLELpyrun	terminatereap_childrenspawn_subprocspawn_zombiespawn_children_pair
ThreadTaskunittestskip_on_access_deniedskip_on_not_implementedretry_on_failureTestMemoryLeakPsutilTestCaseprocess_namespacesystem_namespaceis_win_secure_system_procfake_pytestchdirsafe_rmpathcreate_py_execreate_c_exe
get_testfn
get_winverkernel_version
call_untilwait_for_pidwait_for_filecheck_net_addressfilter_proc_net_connectionsget_free_portbind_socketbind_unix_sockettcp_socketpairunix_socketpaircreate_socketsreload_moduleimport_module_by_pathwarncopyload_shared_libis_namedtuple__pypy__GITHUB_ACTIONSCIBUILDWHEELCOVERAGE_RUNPYTEST_XDIST_WORKERl        >   arm64aarch64riscv64c                  B   [         R                  " 5       S   n [        [        [        U R                  S5      S S 5      5      nUS:X  aU  [        R                  " [        R                  SSS/SS	0S
S9n [        [        [        U R                  S5      S S 5      5      nU$ )Nr   .   )
      z-sS-cz-import platform; print(platform.mac_ver()[0])SYSTEM_VERSION_COMPAT0T)envuniversal_newlines)
platformmac_vertuplemapintsplit
subprocesscheck_outputsys
executable)version_strversions     U/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/psutil/tests/__init__.pymacos_versionr}   z   s    ""$Q'KC[..s3BQ789G( !--?	 )#.#	
 C!2!23!7!;<=N    )ri      )   r   Fri   i  P i            z@psutil--u   -ƒőős   futf8surrogateescape>   asciius-asciiPSUTIL_ROOT_DIRz..scriptscpu_affinityenviron
getloadavgionicememory_mapsnet_io_counterscpu_numio_countersrlimitsensors_batterysensors_fanssensors_temperaturesthreadscpu_freqc            	         S n [         R                  R                  5       n[        [        SS 5      n[
        (       a-  [        R                  S:  a  Ub  [        R                  US'   X!4$ [        (       a  [        R                  U4$ [        (       a  U " [        R                  5      =(       d    U " [         R                  R                  [        R                  5      5      =(       dj    U " [        R                  " SR                  " [        R                  S S 6 5      5      =(       d)    U " [        R                   " 5       R#                  5       5      nU(       d  [%        S5      eX14$ [         R                  R                  [        R                  5      n[         R                  R'                  U5      (       d   U5       eX14$ )Nc                      [         R                  " U S/[         R                  [         R                  S9  U $ ! [         R                   a     g f = f)Nz-V)stdoutstderr)rv   
check_callPIPECalledProcessError)exes    r|   attempt_get_py_exe.<locals>.attempt   sI    	!!dJOOJOO J ,, 		s   48 AA_base_executable)r      __PYVENV_LAUNCHER__zpython{}.{}rh   z"can't find python exe real abspath)osr   copygetattrrx   r   version_infory   r_   r   pathrealpathshutilwhichformatpsutilProcessr   
ValueErrorexists)r   rn   baser   s       r|   _get_py_exer      sI    **//
C 3*D1Dw3##v-$2B &)^^!"y	~~s""	CNN# /rww''78/]1133C3CBQ3GHI/
 v~~'++-. 	 ABBxggs~~.ww~~c""'C'"xr~   zr+STATUS_AF_UNIXc                       \ rS rSrSr\S 5       r\S 5       r\SS j5       r\SS j5       r	\SS j5       r
\SS	 j5       r " S
 S5      rSrg)rF   i  zA class that mimics some basic pytest APIs. This is meant for
when unit tests are run in production, where pytest may not be
installed. Still, the user can test psutil installation via:

    $ python3 -m psutil.tests
c                  6    S n [         R                  " U 5        g )Nc                  8    [         R                  " S[        SS9  g )Nz<Fake pytest module was used. Test results may be inaccurate.   
stacklevelwarningsr[   UserWarning r~   r|   _warn_on_exit0fake_pytest._warn_on_exit.<locals>._warn_on_exit!  s    MMNr~   )atexitregister)r   s    r|   r   fake_pytest._warn_on_exit  s    	 	&r~   c                      [         R                  " 5       R                  [        5      n[         R                  " SS9R                  U5        U$ )zrMimics pytest.main(). It has the same effect as running
`python3 -m unittest -v` from the project root directory.
rh   )	verbosity)r=   
TestLoaderdiscoverHERETextTestRunnerrun)argskwsuites      r|   mainfake_pytest.main*  s;    
 ##%..t4!,007r~   Nc                 Z   ^  " S S5      m[         R                  SU4S jj5       nU" XS9$ )zMimics `pytest.raises`.c                   (    \ rS rSrSr\S 5       rSrg))fake_pytest.raises.<locals>.ExceptionInfoi7  Nc                     U R                   $ N)_excselfs    r|   value/fake_pytest.raises.<locals>.ExceptionInfo.value:  s    yy r~   r   )__name__
__module____qualname____firstlineno__r   propertyr   __static_attributes__r   r~   r|   ExceptionInfor   7  s    D! !r~   r   c              3      >#    T" 5       n Uv   [        U < S35      e! U  aP  nU(       a9  [        R                  " U[        U5      5      (       d  SU SU S3n[        U5      eX2l         S nAg S nAff = f7f)Nz not raised"z" does not match ")AssertionErrorresearchstrr   )excmatcheinfoerrmsgr   s        r|   context#fake_pytest.raises.<locals>.context>  sz     !OE< %wk%:;;  !5#c(!;!;eW$6se1=C(-- 

	!s+   A8 A8A5AA0+A80A55A8)r   r   )
contextlibcontextmanager)r   r   r   r   s      @r|   raisesfake_pytest.raises3  s4    	! 	! 
	"	"
	< 
#
	< s((r~   c                     U(       a$  [         R                  " 5       R                  X5      $ [         R                  " 5       R                  U 5      $ )zMimics `pytest.warns`.)r=   TestCaseassertWarnsRegexassertWarns)warningr   s     r|   warnsfake_pytest.warnsM  s;     $$&77GG  "..w77r~   c                 .    [         R                  " U 5      e)zMimics `unittest.SkipTest`.)r=   SkipTestreasons    r|   skipfake_pytest.skipT  s     ''r~   c                 J    [         R                  " 5       R                  U 5      $ )zMimics `pytest.fail`.)r=   r   failr   s    r|   r  fake_pytest.failY  s       "''//r~   c                   <    \ rS rSr\SS j5       r " S S5      rSrg)fake_pytest.marki^  c                 .    [         R                  " X5      $ )z'Mimics `@pytest.mark.skipif` decorator.)r=   skipIf)	conditionr   s     r|   skipiffake_pytest.mark.skipif`  s     ??955r~   c                   (    \ rS rSrSrSS jrS rSrg)fake_pytest.mark.xdist_groupie  z4Mimics `@pytest.mark.xdist_group` decorator (no-op).Nc                     g r   r   r   names     r|   __init__%fake_pytest.mark.xdist_group.__init__h  s    r~   c                     U$ r   r   )r   cls_or_meths     r|   __call__%fake_pytest.mark.xdist_group.__call__k  s    ""r~   r   r   )r   r   r   r   __doc__r  r  r   r   r~   r|   xdist_groupr  e  s    F#r~   r  r   N )r   r   r   r   staticmethodr	  r  r   r   r~   r|   markr  ^  s    		6 
	6	# 	#r~   r  r   r   r  )r   r   r   r   r  r  r   r   r   r   r   r  r  r   r   r~   r|   rF   rF     s     ' '   ) )2 8 8 ( ( 0 0# #r~   rF   pytestc                   P   ^  \ rS rSrSrU 4S jrS rS rS rS r	S r
S	 rS
rU =r$ )r<   i  z6A thread task which does nothing expect staying alive.c                 r   > [         TU ]  5         SU l        SU l        [        R
                  " 5       U l        g )NFMbP?)superr  _running	_interval	threadingEvent_flag)r   	__class__s    r|   r  ThreadTask.__init__  s*    __&
r~   c                 n    U R                   R                  nSU SU R                   S[        U 5      S S3$ )N<z	 running=z at z#x>)r&  r   r!  idr  s     r|   __repr__ThreadTask.__repr__  s6    ~~&&4&	$--RXbMCCr~   c                 &    U R                  5         U $ r   )startr   s    r|   	__enter__ThreadTask.__enter__  s    

r~   c                 $    U R                  5         g r   )stop)r   r   kwargss      r|   __exit__ThreadTask.__exit__  s    		r~   c                     U R                   (       a  [        S5      e[        R                  R	                  U 5        U R
                  R                  5         g)zoStart thread and keep it running until an explicit
stop() request. Polls for shutdown every 'timeout' seconds.
zalready startedN)r!  r   r#  Threadr/  r%  waitr   s    r|   r/  ThreadTask.start  s9     ==.//t$

r~   c                     SU l         U R                  R                  5         U R                   (       a4  [        R                  " U R
                  5        U R                   (       a  M3  g g )NT)r!  r%  settimesleepr"  r   s    r|   r   ThreadTask.run  s9    

mmJJt~~& mmmr~   c                 j    U R                   (       d  [        S5      eSU l         U R                  5         g)z8Stop thread execution and and waits until it is stopped.zalready stoppedFN)r!  r   joinr   s    r|   r3  ThreadTask.stop  s%    }}.//		r~   )r%  r"  r!  )r   r   r   r   r  r  r,  r0  r5  r/  r   r3  r   __classcell__)r&  s   @r|   r<   r<     s/    @'D' r~   r<   c                 F   ^  [         R                  " T 5      U 4S j5       nU$ )Nc                  H   >  T" U 0 UD6$ ! [          a    [        5         e f = fr   )	Exceptionr8   )r   r4  funs     r|   wrapper&_reap_children_on_err.<locals>.wrapper  s/    	''' 	O	s    !	functoolswraps)rG  rH  s   ` r|   _reap_children_on_errrM    s%    __S  Nr~   c                    UR                  S[        5        UR                  S[        5        UR                  S[        R                  " 5       5        UR                  S[        5        [
        (       a  SnUR                  SU5        U c{  [        [        R                  " 5       S9n [        U5        SU S	3n[        S
U/n [        R                  " U 40 UD6n[        R                  U5        [        USSS9  [        U5        U$ [        R                  " U 40 UD6n[        R                  U5        [        UR                  5        U$ ! [        U5        f = f)ac  Create a python subprocess which does nothing for some secs and
return it as a subprocess.Popen instance.
If "cmd" is specified that is used instead of python.
By default stdin and stdout are redirected to /dev/null.
It also attempts to make sure the process is in a reasonably
initialized state.
The process is registered for cleanup on reap_children().
stdinr   cwdrn      creationflagsdirzimport time;open(r'z6', 'w').close();[time.sleep(0.1) for x in range(100)];rk   Tdeleteempty)
setdefaultr   r   getcwdr   r   rK   rH   r   rv   Popen_subprocesses_startedaddrP   rO   pid)cmdkwdsCREATE_NO_WINDOWtestfnpylinesprocs         r|   r9   r9     s'    	OOGW%OOHg&OOE299;'OOE>*w %)9:
{		,	   "99 
 tV,C$$S1D1E!%%e,&T:
 L   --!!%(UYYL s   'AE Ec                  <   Sn [        [        R                  " 5       S9n [        R                  " S[        R
                  R                  U5       S[         S35      n[        (       a  [        USS9u  p0O[        U5      u  p0[        R                  " UR                  5      n[        [        USS	S
95      n[        R!                  U5        [        R                  " U5      nXF4[#        U5        U b  [#        U 5        $ $ ! [#        U5        U b  [#        U 5        f f = f)zCreate a subprocess which creates another one as in:
A (us) -> B (child) -> C (grandchild).
Return a (child, grandchild) tuple.
The 2 processes are fully initialized and will live for 60 secs
and are registered for cleanup on reap_children().
NrS  zl            import subprocess, os, sys, time
            s = "import os, time;"
            s += "f = open('z', 'w');"
            s += "f.write(str(os.getpid()));"
            s += "f.close();"
            s += "[time.sleep(0.1) for x in range(100 * 6)];"
            p = subprocess.Popen([r'z.', '-c', s])
            p.wait()
            r   )rR  TFrU  )rK   r   rY  textwrapdedentr   basenamer   r   r6   r   r   r]  rt   rP   _pids_startedr\  rH   )tfilera  ssubpchildgrandchild_pid
grandchilds          r|   r;   r;     s    EBIIK(FOO !  WW--f56 7% &0L 1	 	 73KD%(KDtxx(]6$eLM.)^^N3
"F  	F s   CC? ?Dc                    ^ [         R                  (       d   e[        5       n [        R                  " SU  S35      nSn[        U 5      n UR                  [        5        [        U5      u  pBUR                  5       u  pV [        R                  " UR                  5       // / [        5        [        UR                  S5      5      n[        R                  U5        [         R                   " U5      m[#        U4S j5        UT4UR%                  5         UR%                  5         ['        U 5        Ub  ['        U5        $ $ ! UR%                  5         f = f! UR%                  5         ['        U 5        Ub  ['        U5        f f = f)zCreate a zombie process and return a (parent, zombie) process tuple.
In order to kill the zombie parent must be terminate()d first, then
zombie must be wait()ed on.
a          import os, sys, time, socket, contextlib
        child_pid = os.fork()
        if child_pid > 0:
            time.sleep(3000)
        else:
            # this is the zombie process
            with socket.socket(socket.AF_UNIX) as s:
                s.connect('za')
                pid = bytes(str(os.getpid()), 'ascii')
                s.sendall(pid)
        N   c                  F   > T R                  5       [        R                  :H  $ r   )statusr   STATUS_ZOMBIE)zombies   r|   <lambda>spawn_zombie.<locals>.<lambda>*  s    v}}&2F2FFr~   )r   r   rK   re  rf  rU   
settimeoutr   r6   acceptselectfilenort   recvrh  r\  r   rN   closerH   )		unix_filesrcri  sockparentconn_zpidrt  s	           @r|   r:   r:     s3   
 <<<I
//  &; '	 C EI&D'c
++-	MM4;;=/2r>Btyy'Dd#^^D)FFGF#JJL

I 	 JJL

I s%   4E  BD> E >EE ,E?c                 j   UR                  SS5        UR                  SS5        [        5       n [        US5       nUR                  U 5        SSS5        [	        [
        WR                  /40 UD6n[        UR                  5        XB4$ ! , (       d  f       NB= f! [         a    [        U5        e f = f)zRun python 'src' code string in a separate interpreter.
Returns a subprocess.Popen instance and the test file where the source
code was written.
r   Nr   w)rX  rK   openwriter9   r   r  rO   r]  rF  rH   )r~  r_  srcfilefrk  s        r|   r6   r6   5  s     	OOHd#OOHd#lG'31GGCL  j!&&1:T:TXX	  
  Gs"   B B
;B 

BB B2c                 X   [         (       a  SOSnUR                  S[        R                  5        UR                  S[        R                  5        UR                  SS5        UR                  SU5        [	        U [
        5      (       a  [        R                  " U 5      n [        R                  " U 40 UD6n[        R                  U5        UR                  [        S9u  pEUR                  S:w  a  [        XE-   5      eU(       a  [        U5        UR!                  S	5      (       a  US
S nU$ )zMRun cmd in a subprocess and return its output.
raises RuntimeError on error.
rQ  r   r   r   ro   TrR  timeout
N)r   rX  rv   r   
isinstancer   shlexru   rZ  r[  r\  communicater   
returncodeRuntimeErrorr[   endswith)r^  r_  flagspr   r   s         r|   shr  I  s     !IaEOOHjoo.OOHjoo.OO($/OOOU+#skk#%%Aa ]]>]:NF||q6?++VtMr~   c                 L  ^^^	^
 S m
S mUUU
4S jnUUU
4S jm	U	4S jnS nU n [        U[        5      (       a  U" Xb5      [        U[        R                  [        R                  45      (       a  U" U5        [        U[        5      (       a  UOUR
                  n[        R                  " U5      (       a   U5       e$ [        U[        R                  [        R                  45      (       a  T	" Xb5      [        U[        R                  [        R                  45      (       a  U" U5        [        U[        5      (       a  UOUR
                  n[        R                  " U5      (       a   U5       e$ [        U[        R                  5      (       a  U" Xb5      [        U[        R                  [        R                  45      (       a  U" U5        [        U[        5      (       a  UOUR
                  n[        R                  " U5      (       a   U5       e$ [        SU< 35      e! [        U[        R                  [        R                  45      (       a  U" U5        [        U[        5      (       a  UOUR
                  n[        R                  " U5      (       a   U5       ef = f)ax  Terminate a process and wait() for it.
Process can be a PID or an instance of psutil.Process(),
subprocess.Popen() or psutil.Popen().
If it's a subprocess.Popen() or psutil.Popen() instance also closes
its stdin / stdout / stderr fds.
PID is wait()ed even if the process is already gone (kills zombies).
Does nothing if the process does not exist.
Return process exit status.
c                    U R                  U5        [        (       aP  [        U [        R                  5      (       a0   [
        R                  " U R                  5      R                  U5      $ g g ! [
        R                   a     g f = fr   )	r9  r   r  rv   rZ  r   r   r]  NoSuchProcess)procr  s     r|   r9  terminate.<locals>.waitm  si    		'7z$
(8(899~~dhh/44W== :7 '' s   .A. .BBc                     [         (       a  [        (       a  [        R                  n[        (       a3  U[        R                  :w  a  U R                  [        R                  5        U R                  U5        g r   )r   r_   signalSIGKILLr   send_signalSIGCONT)r  sigs     r|   sendsigterminate.<locals>.sendsigv  sF    5^^..C 5SFNN*V^^,r~   c                    >  T" U T5        T" X5      $ ! [          a     N[         a"  n[        (       a  UR                  S:X  a   e S nAff = f)N   )ProcessLookupErrorOSErrorr   winerror)r  r  r   r  r  r9  s      r|   term_subprocess_proc'terminate.<locals>.term_subprocess_proc  sR    	D# D"" " 	 	w3<<1,	s   	 
A	AAAc                 \   >  T" U T5        T" X5      $ ! [         R                   a     Nf = fr   )r   r  )r  r  r  r  r9  s     r|   term_psutil_proc#terminate.<locals>.term_psutil_proc  s9    	D# D"" ## 		s   	 ++c                    >  [         R                  " U 5      nT" X!5      $ ! [         R                   a    [        (       a  [	        X5      s $  g f = fr   )r   r   r  r   r   )r]  r  r  r  s      r|   term_pidterminate.<locals>.term_pid  sO    	3>>#&D $D22 ## 	.u-- 	.s   ! *AAc                    U R                   (       a  U R                   R                  5         U R                  (       a  U R                  R                  5         U R                  (       a  U R                  R                  5         g g r   )r   r|  r   rO  )r  s    r|   flush_popenterminate.<locals>.flush_popen  sL    ;;KK;;KK::JJ r~   zwrong type )	r  rt   rv   rZ  r   r]  
pid_existsr   	TypeError)proc_or_pidr  wait_timeoutr  r  r  r  r]  r  r  r9  s    `      @@@r|   r7   r7   b  s   	##3 	A/aA, a***FLL9::Na%%a155$$S)).3.)) FNNFLL9::#A4 a***FLL9::Na%%a155$$S)).3.)) :++,,'8 a***FLL9::Na%%a155$$S)).3.)) k!/00a***FLL9::Na%%a155$$S)).3.))s   H%  6H% 3&H% H% %A>J#c                    [         R                  " 5       R                  U S9n[        (       a,  [        R	                  5       n[        U5        [        (       a  M,  [        (       a,  [        R	                  5       n[        U5        [        (       a  M,  U(       a`  U H  n[        USS9  M     [         R                  " U[        S9u  pVU H+  n[        SU< S35        [        U[        R                  S9  M-     gg)a  Terminate and wait() any subprocess started by this test suite
and any children currently running, ensuring that no processes stick
around to hog resources.
If recursive is True it also tries to terminate and wait()
all grandchildren started by this process.
	recursiveN)r  r  zcouldn't terminate process z; attempting kill())r  )r   r   childrenr[  popr7   rh  
wait_procsr   r[   r  r  )r  r  rk  r]  r  r  alives          r|   r8   r8     s     ~~((9(=H  
$((*$  

 -!# -
 Aad+ $$X~FA.qe3FGHaV^^, 	 r~   c                     [         (       d  [        S5      eSn [        R                  " 5       S   nU H$  nUR	                  5       (       d  US:X  a  X-  n M$    O   U (       d  [        SU< 35      eSnSnU R                  S5      n[        US   5      n[        U5      S:  a  [        US   5      n[        U5      S:  a  [        US   5      nXcU4$ )	z"Return a tuple such as (2, 6, 36).z	not POSIXr  rh   rg   zcan't parse r   r   r   )	r   NotImplementedErrorr   unameisdigitr   ru   rt   len)rj  r  cminormicronumsmajors          r|   rM   rM     s    5!+..
AHHJqME99;;!s(FA	 
 <y122EE773<DQLE
4yA~DG
4yA~DG%  r~   c                      [         (       d  [        S5      e[        R                  " 5       n U R                  =(       d    SnU S   U S   U4$ )Nznot WINDOWSr   r   )r   r  rx   getwindowsversionservice_pack_major)wvsps     r|   rL   rL     sE    7!-00				 B				#!BqE2a5"r~   c                   >    \ rS rSrSr\SSSS4S jrS rS rS r	S	r
g)
retryi   zA retry decorator.Nr  c                 r    U(       a  U(       a  [        S5      eXl        X l        X0l        X@l        XPl        g )Nz/timeout and retries args are mutually exclusive)r   	exceptionr  retriesintervallogfun)r   r  r  r  r  r  s         r|   r  retry.__init__  s.     wNOO" r~   c              #   V  #    U R                   (       a\  [        R                  " 5       U R                   -   n[        R                  " 5       U:  a   S v   [        R                  " 5       U:  a  M  g g U R                  (       a!  [        U R                  5       H  nS v   M	     g  S v   M  7fr   )r  r=  r  range)r   stop_atr  s      r|   __iter__retry.__iter__  sk     <<iikDLL0G))+' ))+'\\4<<( )  s   A*B).;B)c                 `    U R                   b!  [        R                  " U R                   5        g g r   )r  r=  r>  r   s    r|   r>  retry.sleep  s!    ==$JJt}}% %r~   c                 X   ^ ^ [         R                  " T5      UU 4S j5       nT Ul        U$ )Nc                     > S nT H  n T" U 0 UD6s  $    Ue! TR                    a;  nUnTR                  b  TR                  U5        TR                  5          S nAMX  S nAff = fr   )r  r  r>  )r   r4  r   r  rG  r   s       r|   rH  retry.__call__.<locals>.wrapper$  sk    C///  I ~~ C{{.C(JJLs   A#0AA#)rK  rL  	decorator)r   rG  rH  s   `` r|   r  retry.__call__#  s-    			 
	  !r~   )r  r  r  r  r  )r   r   r   r   r  rF  r  r  r>  r  r   r   r~   r|   r  r     s*      
&r~   r  r  )r  r  r  r  c                     U [         R                  " 5       ;  a  [         R                  " U 5      e[         R                  " U 5        g)z|Wait for pid to show up in the process list then return.
Used in the test suite to give time the sub process to initialize.
N)r   pidsr  r   )r]  s    r|   rO   rO   9  s/     &++-""3''
NN3r~   c                     [        U S5       nUR                  5       nSSS5        U(       d	  W(       d   eU(       a  [        U 5        W$ ! , (       d  f       N2= f)z8Wait for a file to be written on disk with some content.rbN)r  readrH   )fnamerV  rW  r  datas        r|   rP   rP   H  sC     
eT	avvx 
tEK 
	s   A


Ac                 &    U " 5       nU(       d   eU$ )z1Keep calling function until it evaluates to True.r   )rG  rets     r|   rN   rN   Y  s     %CJ3Jr~   c                 n   S n [         R                  " U 5      n[        R                  " UR                  5      (       a&  [        R
                  " [        R                  U 5      nO%[        R
                  " [         R                  U 5      n[        (       a  U" 5         gU" U5        g! [         a     gf = f)z?Convenience function for removing temporary test files or dirs.c                 H   [         R                   " 5       [        -   n[         R                   " 5       U:  a   U " 5       $ We! [         a     O&[         a  nUn[	        SU 35         S nAOS nAff = f[         R
                  " S5        [         R                   " 5       U:  a  Mm  Nf)Nz	ignoring g{Gz?)r=  r   FileNotFoundErrorr  r[   r>  )rG  r  r  r   s       r|   	retry_funsafe_rmpath.<locals>.retry_funn  s    
 ))+.iikG#(u 	 %  (y&''( JJt iikG#s   A   
A/	A/A**A/N)r   statS_ISDIRst_moderK  partialr   rmtreeremover   r  )r   r  strG  s       r|   rH   rH   k  s{    "WWT]<<

####FMM48C##BIIt4C5EcN s   BB' B' '
B43B4c                 R     [         R                  " U 5        g! [         a     gf = f)z.Convenience function for creating a directory.N)r   mkdirFileExistsErrorrS  s    r|   
safe_mkdirr    s$    
 s    
&&c              #      #    [         R                  " 5       n [         R                  " U 5        Sv   [         R                  " U5        g! [         R                  " U5        f = f7f)z@Context manager which temporarily changes the current directory.N)r   rY  rG   )dirnamecurdirs     r|   rG   rG     s>      YY[F

s   A%A
 A%
A""A%c                 l   [         R                  R                  U 5      (       a   U 5       e[        R                  " [
        U 5        [        R                  " [        U 5        [        (       aG  [         R                  " U 5      n[         R                  " XR                  [        R                  -  5        U $ )z6Create a Python executable file in the given location.)r   r   r   r   r   rH   r   copyfiler   r   r  chmodr  S_IEXEC)r   r  s     r|   rI   rI     sl    ww~~d##)T)#
OOK&
OOJ%uWWT]
zzDLL01Kr~   c                    [         R                  R                  U 5      (       a   U 5       e[        R                  " S5      (       d  [
        R                  " S5      $ Uc  [        R                  " S5      nO[        U[        5      (       d   U5       e[        R                  " [        U 5        [        [        SS9S5       nUR!                  U5        SSS5         ["        R$                  " SWR&                  SU /5        [        UR&                  5        U $ ! , (       d  f       NJ= f! [        WR&                  5        f = f)	z5Create a compiled C executable in the given location.gcczgcc is not installedNz
            #include <unistd.h>
            int main() {
                pause();
                return 1;
            }
            z.csuffixr  z-o)r   r   r   r   r   r  r   re  rf  r  r   r   r   rH   r  rK   r  rv   r   r  )r   c_coder  s      r|   rJ   rJ     s    ww~~d##)T)#<<{{122~ "  &#&&..&
OOK&	j%s	+q	 
,uaffdD9:AFFK 
,	+
 	AFFs   D$D( 
D%(D?c                      [         R                  " [        XS9n[        R                  R                  U5      (       d<  [        R                  R                  U5      n[        R                  " [        U5        U$ M{  )zReturn an absolute pathname of a file or dir that did not
exist at the time this call is made. Also schedule it for safe
deletion at interpreter exit. It's technically racy but probably
not really due to the time variant.
)prefixr  rT  )
tempfilemktempr   r   r   r   r   r   r   rH   )r  rT  r  r   s       r|   rK   rK     sV     mFLww~~d##77##D)DOOK.K r~   c                   ^    \ rS rSrSrS rSS jrS rS rS r	S	 r
S
 rS rS rS rS rSrg)rB   i  zTest class providing auto-cleanup wrappers on top of process
test utilities. All test classes should derive from this one, even
if we use pytest.
c                     U R                   R                  nUR                  S5      (       d  SU-   nSR                  UU R                   R                  U R
                  5      $ )Nzpsutil.zpsutil.tests.z{}.{}.{})r&  r   
startswithr   r   _testMethodName)r   fqmods     r|   __str__PsutilTestCase.__str__  sX    ))	**#e+E  NN##  
 	
r~   Nc                 D    [        XS9nU R                  [        U5        U$ )N)r  rT  )rK   
addCleanuprH   )r   r  rT  r  s       r|   rK   PsutilTestCase.get_testfn  s    &2U+r~   c                 H    [        U0 UD6nU R                  [        U5        U$ r   )r9   r  r7   )r   r   r_  rc  s       r|   r9   PsutilTestCase.spawn_subproc  s$    t,t,	5)r~   c                     U R                   " U0 UD6n [        R                  " UR                  5      $ ! [        R                   a    U R                  UR                  5        e f = fr   )r9   r   r   r]  r  assert_pid_gone)r   r   r4  rc  s       r|   spawn_psprocPsutilTestCase.spawn_psproc  sY    ""D3F3	>>%)),,## 	  +	s	   4 1A%c                 x    [        5       u  pU R                  [        U5        U R                  [        U5        X4$ r   )r;   r  r7   )r   child1child2s      r|   r;   "PsutilTestCase.spawn_children_pair  s1    ,.	6*	6*r~   c                 x    [        5       u  pU R                  [        U5        U R                  [        U5        X4$ r   )r:   r  r7   )r   r  rt  s      r|   r:   PsutilTestCase.spawn_zombie  s0    %	6*	6*r~   c                 x    [        U0 UD6u  p4U R                  [        U5        U R                  [        U5        U$ r   )r6   r  rH   r7   )r   r   r_  rc  r  s        r|   r6   PsutilTestCase.pyrun	  s4    --W-	5)r~   c                    [        U[        R                  5      (       d   eUR                  UR                  :X  d   eUR                  UR
                  :X  d   eUR                  (       a  UR                  (       d   e[        U[        R                  5      (       a;  UR                  UR                  :X  d   eUR                  b  UR                  S:  d   e[        U5        [        U5        g Nr   )r  r   Errorr]  r  _nameZombieProcessppid_ppidr   repr)r   r  r   s      r|   _check_proc_excPsutilTestCase._check_proc_exc  s    #v||,,,,ww$(("""xx4::%%%8888O8c6//0088tzz)))xx#xx1}$}CS	r~   c                      [         R                  " U5      n[        SU S35      e! [         R                   a    [        S5      e[         R                   a+  nUR
                  U:X  d   eUR                  b   e S nAOS nAff = f[         R                  " U5      (       a   U5       eU[         R                  " 5       ;  d   eU[         R                  " 5        Vs/ s H  oDR
                  PM     Os  snf sn;  d   eg )Nzdid not raise NoSuchProcess ()z&wasn't supposed to raise ZombieProcess)
r   r   r   r(  r  r]  r  r  r  process_iter)r   r]  r  r   xs        r|   r  PsutilTestCase.assert_pid_gone  s    	J>>#&D !#@a!HII ## 	K !IJJ## 	$77c>!>88###	$ $$S)).3.)&++-'''&*=*=*?@*?Q55*?@@@@s   ' 2B!A??BC2c                    U R                  UR                  5        [        U5      nUR                  UR                  SS9 H9  u  p4U R                  [        U5      US9    U" 5       nSU SU< 3n[        U5      e   UR                  SS9  g ! [        R                   a    e [        R                   a  nU R                  X5         S nAOS nAff = f S S S 5        M  ! , (       d  f       M  = f)NTclear_cacher  r  zProcess.z!() didn't raise NSP and returned r   r  )r  r]  rC   iterallsubTestr   r   r   r(  r  r,  r9  )r   r  nsrG  r  r  r   r   s           r|   assert_proc_gonePsutilTestCase.assert_proc_gone+  s    TXX&t$T:IC3t948.%C #4& )7$  )-- ; 			!	 ++ ++ 4((3343 9888s6   C!B(C(C6CCCC
C.	c                    S n[         R                  " UR                  5      nX:X  d   e[        (       d0  [        (       d%  [
        (       d  [        U5      [        U5      :X  d   eUR                  5       [         R                  :X  d   eUR                  5       (       d   e[         R                  " UR                  5      (       d   eUR                  5         U" U5        [        U5      nUR                  UR                  SS9 H/  u  pVU R                  [!        U5      US9    U" 5         S S S 5        M1     [(        (       Ga	  [*        R,                  " [         R"                  5       nUR/                  5         S S S 5        U R'                  UWR0                  5        [*        R,                  " [         R"                  5       nUR3                  5         S S S 5        U R'                  XR0                  5        [*        R,                  " [         R"                  5       nUR5                  5         S S S 5        U R'                  XR0                  5        UR7                  5         UR9                  5         UR;                  5         UR=                  5         UR                  5       (       d   e[         R                  " UR                  5      (       d   eU" U5        g ! [         R"                  [         R$                  4 a  nU R'                  X5         S nAGNS nAff = f! , (       d  f       GM"  = f! , (       d  f       GN= f! , (       d  f       GNq= f! , (       d  f       GN+= f)Nc                    [         (       a  g U R                  [        R                  " 5       ;   d   eU R                  [        R                  " 5        Vs/ s H  oR                  PM     sn;   d   e0 [        l        U R                  [        R                  " 5        Vs/ s H  oR                  PM     sn;   d   eg s  snf s  snf r   )r   r]  r   r  r0  _pmap)r  r1  s     r|   assert_in_pids9PsutilTestCase.assert_proc_zombie.<locals>.assert_in_pids?  s    u88v{{},,,88v/B/B/DE/D!/DEEEEFL88v/B/B/DE/D!/DEEEE  FEs   B<CTr4  r6  )r   r   r]  r
   r	   r   hashrr  rs  
is_runningr  as_dictrC   r7  r8  r9  r   r(  AccessDeniedr,  r   r  r   cmdliner   r   r   suspendresumer7   kill)	r   r  r@  cloner:  rG  r  r   cms	            r|   assert_proc_zombie!PsutilTestCase.assert_proc_zombie>  sL   	F txx(
 }}66UU:e,,,{{} 4 4444      ****tt$T:IC3t9484E 98 ; 5v334 5  rxx0v334
 5  xx0v334  " 5  xx0		      ****t) ,,f.A.AB 4((334 98 54 54 54sT   #L%K(L*L<M$L8L	LLL
L'	*
L9<
M
Mr   r  N)r   r   r   r   r  r  rK   r9   r  r;   r:   r6   r,  r  r;  rL  r   r   r~   r|   rB   rB     s?    


  A&6r~   rB   zunreliable on PYPYr   serial)r  c                       \ rS rSrSrSrSrSr\(       a  SOSr	Sr
\R                  " 5       r\" \R                   " S5      5      r\S	 5       r\S
 5       rS rS rS rS rS rS rS r SS jrS rSrg)rA   i  a  Test framework class for detecting function memory leaks,
typically functions implemented in C which forgot to free() memory
from the heap. It does so by checking whether the process memory
usage increased before and after calling the function many times.

Note that this is hard (probably impossible) to do reliably, due
to how the OS handles memory, the GC and so on (memory can even
decrease!). In order to avoid false positives, in case of failure
(mem > 0) we retry the test for up to 5 times, increasing call
repetitions each time. If the memory keeps increasing then it's a
failure.

If available (Linux, OSX, Windows), USS memory is used for comparison,
since it's supposed to be more precise, see:
https://gmpy.dev/blog/2016/real-process-memory-and-environ-in-python
If not, RSS memory is used. mallinfo() on Linux and _heapwalk() on
Windows may give even more precision, but at the moment are not
implemented.

PyPy appears to be completely unstable for this framework, probably
because of its JIT, so tests on PYPY are skipped.

Usage:

    class TestLeaks(psutil.tests.TestMemoryLeak):

        def test_fun(self):
            self.execute(some_function)
   ri   r   r   TPSUTIL_DEBUGc                 0    [         R                  " S5        g )NF)r   
_set_debugclss    r|   
setUpClassTestMemoryLeak.setUpClass  s    % r~   c                 D    [         R                  " U R                  5        g r   )r   rT  _psutil_debug_origrU  s    r|   tearDownClassTestMemoryLeak.tearDownClass  s    #001r~   c                 d    U R                   R                  5       n[        USUR                  5      $ )Nuss)	_thisprocmemory_full_infor   rss)r   mems     r|   _get_memTestMemoryLeak._get_mem  s)     nn--/sE377++r~   c                     [         (       a  U R                  R                  5       $ U R                  R                  5       $ r   )r   r_  num_fdsnum_handlesr   s    r|   _get_num_fdsTestMemoryLeak._get_num_fds  s-    5>>))++>>--//r~   c                 Z    U R                   (       a  [        US[        R                  S9  g g )Nyellow)colorfile)verboser   rx   r   )r   r   s     r|   _logTestMemoryLeak._log  s    <<8#**= r~   c                 6   U R                  5       nU R                  U5        U R                  5       nX2-
  nUS:  a  SU< S3n[        R                  " U5      $ US:  a;  [        (       a  SOSnUS:  a  US-  nU SU S	U< 3n[        R                  " U5      $ g
)zMakes sure num_fds() (POSIX) or num_handles() (Windows) does
not increase after calling a function.  Used to discover forgotten
close(2) and CloseHandle syscalls.
r   znegative diff z8 (gc probably collected a resource from a previous test)fdhandler   rj  z
 unclosed z after calling N)rh  callr  r  r   )r   rG  beforeafterdiffr   type_s          r|   
_check_fdsTestMemoryLeak._check_fds  s    
 ""$		#!!#~!8  )2 2  ;;s##!8!EDxEaxF*UG?3'BC;;s## r~   c                    [         R                  " SS9  U R                  5       n[        U5       H  nU R	                  U5      nAAM     [         R                  " SS9  U R                  5       n[         R
                  / :X  d   eXc-
  nU$ )zpGet 2 distinct memory samples, before and after having
called fun repeatedly, and return the memory difference.
r   )
generation)gccollectrc  r  rt  garbage)r   rG  timesmem1r1  r  mem2rw  s           r|   _call_ntimesTestMemoryLeak._call_ntimes  sr     	

a }}uA))C.C3  	

a }}zzR{r~   c           	         / nSnUn[        SUS-   5       H  nU R                  X5      n	SR                  U[        U	5      [        X-  5      U5      n
UR	                  U
5        X:*  =(       d    X:*  nU(       a  US:  a  U R                  U
5          g US:X  a
  [        5         U R                  U
5        X'-  nU	nM     [        R                  " SR                  U5      5      $ )Nr   r   z,Run #{}: extra-mem={}, per-call={}, calls={}z. )
r  r  r   r   appendro  printr  r  rA  )r   rG  r  r  	tolerancemessagesprev_memincreaseidxrb  r   successs               r|   
_check_memTestMemoryLeak._check_mem  s    GaK(C##C/C@GGC CK(	C OOC &9#/G7IIcN!8G		#!' )( {{499X.//r~   c                     U" 5       $ r   r   )r   rG  s     r|   rt  TestMemoryLeak.call  s	    ur~   Nc                    Ub  UOU R                   nUb  UOU R                  nUb  UOU R                  nUb  UOU R                  n US:  d   S5       eUS:  d   S5       eUS:  d   S5       eUS:  d   S5       e U R                  X5        U R                  U5        U R                  XXES9  g! [         a  n[        [        U5      5      eSnAff = f)	zTest a callable.Nr   ztimes must be >= 1r   zwarmup_times must be >= 0zretries must be >= 0ztolerance must be >= 0)r  r  r  )
r  warmup_timesr  r  r   r   r   r  ry  r  )r   rG  r  r  r  r  r   s          r|   executeTestMemoryLeak.execute  s     *

(4L$:K:K 	 %0'dll!*!6IDNN		'A:333:1$A&AA$a<7!77<>;#;;> 	#,'O  	'SX&&	's   4B. .
C8CCc                 <   ^^ UU4S jnU R                   " U40 UD6  g)z^Convenience method to test a callable while making sure it
raises an exception on every call.
c                  b   >  T" 5         [         R                  " T ST  35      $ ! T  a     g f = f)Nz did not raise )r  r  )r   rG  s   r|   rt  *TestMemoryLeak.execute_w_exc.<locals>.call&  s>    A {{cU/##?@@  s   % ..N)r  )r   r   rG  r4  rt  s    ``  r|   execute_w_excTestMemoryLeak.execute_w_exc!  s    
	A 	T$V$r~   r   )NNNN)r   r   r   r   r  r  r  r  r    r  rn  r   r   r_  boolr   getenvrZ  classmethodrW  r[  rc  rh  ro  ry  r  r  rt  r  r  r   r   r~   r|   rA   rA     s    > ELIbAGG Ibii78! ! 2 2,0>$,08 KOP,%r~   rA   c                 V    [         S 5       n U" 5       U    S:H  $ ! [         a     gf = f)Nc                      0 n [        S5      nUR                  5       SS   HI  nUR                  S5       Vs/ s H  o3R                  SS5      PM     nnUS   [	        US   5      peXPU'   MK     U $ s  snf )Nztasklist.exe /NH /FO csvr   ,r   r  r   )r  
splitlinesru   replacert   )r  outliner1  bitsr  r]  s          r|   	get_procs,is_win_secure_system_proc.<locals>.get_procs3  sv    +,NN$QR(D04

3@1IIc2&D@QT!W#H ) 
 As   A0zSecure SystemF)r   KeyError)r]  r  s     r|   rE   rE   1  s=     {3?22 s    
((c                      [         R                  " 5       n [        U S5      (       a  U R                  5       $ [        U S5      (       a$  [        R
                  " U R                  5       5      $ g)Nr   r   r   )r   r   hasattrr   randomchoicer   )r  s    r|   _get_eligible_cpur  C  sM    Aq)yy{	N	#	#}}Q^^-..r~   c                      \ rS rSrSrSS0 4SS0 4/rSS0 4SSSS	04S
S0 4SS0 4SS0 4SS0 4SS0 4SS0 4SS0 4/	rSS0 4SS0 4SS0 4SS0 4SS0 4SS0 4SS0 4SS0 4SSSS04SS0 4SS0 4SS0 4S S0 4S!S0 4S"S0 4S#S0 4S$S0 4/r\(       a$  \S%S0 4/-  r\S&S0 4/-  r\S'S0 4/-  r\S(S0 4/-  r\	(       a	  \S)S0 4/-  r\
(       a	  \S*S0 4/-  r\(       a  \S+\R                  40 4/-  r\(       a	  \S,S0 4/-  r\(       a	  \S-S0 4/-  r\(       a	  \S.S0 4/-  r\(       a	  \S/S0 4/-  r\(       a  \S0SS1S204/-  r/ r\(       a
  \SS0 4/-  rO\S\R(                  40 4/-  r\(       a  \S+\R                  S340 4/-  r\
(       a1  \(       a  \S*\R,                  S440 4/-  rO\S*\R.                  40 4/-  r\(       a  \S,\" 5       /40 4/-  rS5\R4                  40 4S6S0 4S7S0 4S8S0 4S9S0 4/r\(       a(  \S5\R8                  40 4/-  r\S5\R:                  40 4/-  r\\-   \-   \-   rS: rSAS; jr S< r!\"S= 5       r#\"S> 5       r$S?r%g@)BrC   iL  a  A container that lists all Process class method names + some
reasonable parameters to be called with. Utility methods (parent(),
children(), ...) are excluded.

>>> ns = process_namespace(psutil.Process())
>>> for fun, name in ns.iter(ns.getters):
...    fun()
cpu_percentr   memory_percentrD  r  r  TconnectionsrC  oneshotr  parentsr]  r9  )r   rF  	cpu_timescreate_timerP  r   r`  memory_infor  net_connectionskindr8  nicenum_ctx_switchesnum_threads
open_filesr)  rr  r   usernameuidsgidsterminalrf  r   r   r   r   r   r   rg  r   groupedF)rp  i   r   r  rG  rH  r7   rI  c                     Xl         g r   _proc)r   r  s     r|   r  process_namespace.__init__  s    
r~   c              #      #    [        U5      n[        R                  " U5        U HR  u  p4nU(       a  U R                  5         [	        U R
                  U5      n[        R                  " U/UQ70 UD6nXc4v   MT     g7fzOGiven a list of tuples yields a set of (fun, fun_name) tuples
in random order.
N)listr  shuffler5  r   r  rK  r  )r   lsr5  fun_namer   r_  rG  s          r|   r7  process_namespace.iter  sl      "Xr$& HD  "$**h/C##C7$7$7C/! %'s   A;A=c                 `    U R                   R                  U R                   R                  SS9  g)z&Clear the cache of a Process instance.T)_ignore_nspN)r  _initr]  r   s    r|   r5  process_namespace.clear_cache  s!    

T:r~   c                     U HE  u  n  nSU-   n[        X5      (       a  M  UR                  R                  < SU< S3n[        U5      e   g)zmGiven a TestCase instance and a list of tuples checks that
the class defines the required test method names.
test_z class should define a z methodN)r  r&  r   AttributeError)rV  
test_classr  r  r  	meth_namer   s          r|   test_class_coverage%process_namespace.test_class_coverage  s^    
 !NHa(*I:11!++447 8!}G-  %S)) !r~   c                 :   U R                    Vs1 s H  oS   iM	     nnU R                   Vs1 s H  oS   iM	     nn[        [        R                  5       Vs1 s H  oS   S:w  d  M  UiM     nnX#-  U-  nU(       a  [        SU< 35      eg s  snf s  snf s  snf )Nr   r  zuncovered Process class names: )r8  ignoredrT  r   r   r   )rV  r1  thisr  klassleftouts         r|   testprocess_namespace.test  s    !gg&g!g&!$-AQ4-/?/qQ43;/?>U*>wkJKK 	 '-?s   BBB(Br  N)T)&r   r   r   r   r  utilsr  gettersr   r'   r(   r+   r   RLIMIT_NOFILEr$   r*   r&   r   r)   settersNORMAL_PRIORITY_CLASSr   IOPRIO_CLASS_NONEIOPRIO_NORMALr  r  SIGTERMkillersCTRL_C_EVENTCTRL_BREAK_EVENTr8  r  r7  r5  r  r  r  r   r   r~   r|   rC   rC   L  s    R$'7R&@AE 
B	R+t,-	B	r2	B	2r	B	B	r
G 
B	b"	B	B	B	R$	B	R	B0	R	R$	B	r2	R	2r	B	R#G& VR$%%VR$%%ZR())YB'((]B+,,Xr2&''X 4 46;<<^R,--YB'((YB'((]B+,,]BE(:;<<GVT2&''Vf::<bABBX 4 4lCRHIIF$<$<a#@"EFFGF$8$8#:B?@@G^'8':&;%=rBCC 
)2.	B	2r	b"	RG ]V%8%8$:B?@@]V%<%<$>CDD
'/G
#g
-C"; * * L Lr~   rC   c                   r   \ rS rSrSrSS0 4SSSS04SSSS04S	S0 4S
SSS04S
SSS04SSSS04SSSS04S\R                  " 5       40 4SSSS04SS0 4SS0 4SSSS04S\R                  " 5       40 4SS0 4SS0 4SS0 4SS0 4/r\	(       a  \
(       a  \(       a  O\SSSS04/-  r\(       a	  \SS0 4/-  r\(       a	  \SS0 4/-  r\(       a	  \SS0 4/-  r\(       a	  \S S0 4/-  r\(       a  \S!S0 4/-  r\S"S#0 4/-  rS$S0 4S%\R$                  " 5       /40 4S&S0 4S'S0 4/r\r\S( 5       r\R0                  rSrg))*rD   i  zA container that lists all the module-level, system-related APIs.
Utilities such as cpu_percent() are excluded. Usage:

>>> ns = system_namespace
>>> for fun, name in ns.iter(ns.getters):
...    fun()
	boot_timer   	cpu_countlogicalFT	cpu_statsr  percpudisk_io_countersperdiskdisk_partitionsr8  
disk_usager  r  net_if_addrsnet_if_statsr   pernicr  r  swap_memoryusersvirtual_memoryr   r   r   r   r   win_service_iterwin_service_get)algr0  r  r  cpu_times_percentc              #      #    [        U 5      n [        R                  " U 5        U  H5  u  pn[        [        U5      n[
        R                  " U/UQ70 UD6nXA4v   M7     g7fr  )r  r  r  r   r   rK  r  )r  r  r   r_  rG  s        r|   r7  system_namespace.iter  sY     
 "Xr$& HD&(+C##C7$7$7C/! %'s   AA N)r   r   r   r   r  r   rY  getpidr  r%   r   r4   HAS_GETLOADAVGr/   r.   r,   r   r   r   r  r8  r  r7  rC   r  r   r   r~   r|   rD   rD     s    
b"	b9e,-	b9d+,	b"	b8U+,	b8T*+	R)T!23	B.			~r*	B0	R 	R 	B4 01			~r*	R	B	"b	2r"%G( WR(D)9:;;G\2r*+++R455^R,--&B/00'R011&"566 
R 	)*,b1	B	b"%	G C	" 	" ,??r~   rD   c                 \    S n[        [        [        R                  R                  4SU US9$ )zRDecorator which runs a test function and retries N times before
actually failing.
c                 <    [        U < S3[        R                  S9  g )Nz
, retrying)rm  )r  rx   r   )r   s    r|   r   retry_on_failure.<locals>.logfun   s    z"4r~   N)r  r  r  r  )r  r   r  r  rF  )r  r  s     r|   r@   r@     s1    
5 !6;;#8#89	 r~   c                    ^  U 4S jnU$ )z,Decorator to Ignore AccessDenied exceptions.c                 J   >^  [         R                  " T 5      U U4S j5       nU$ )Nc                     >  T" U 0 UD6$ ! [         R                   a$    Tb  T(       d  e [        R                  " S5      s $ f = f)Nzraises AccessDenied)r   rE  r  r   )r   r4  rG  only_ifs     r|   rH  9skip_on_access_denied.<locals>.decorator.<locals>.wrapper/  sI    :D+F++&& :&"{{#899	:s    5AArJ  rG  rH  r  s   ` r|   r  (skip_on_access_denied.<locals>.decorator.  s%    			: 
	: r~   r   r  r  s   ` r|   r>   r>   +  s     r~   c                    ^  U 4S jnU$ )z3Decorator to Ignore NotImplementedError exceptions.c                 J   >^  [         R                  " T 5      U U4S j5       nU$ )Nc                     >  T" U 0 UD6$ ! [          a4    Tb  T(       d  e TR                  < S3n[        R                  " U5      s $ f = f)Nz2 was skipped because it raised NotImplementedError)r  r   r  r   )r   r4  r   rG  r  s      r|   rH  ;skip_on_not_implemented.<locals>.decorator.<locals>.wrapperB  s]    
(D+F++& (&"||& '+ +  {{3''(s    ;A	A	rJ  r
  s   ` r|   r  *skip_on_not_implemented.<locals>.decoratorA  s%    			( 
	( r~   r   r  s   ` r|   r?   r?   >  s    " r~   c                     [         R                   " 5        nUR                  U S45        UR                  5       S   sSSS5        $ ! , (       d  f       g= f)z6Return an unused TCP port. Subject to race conditions.r   r   N)socketbindgetsockname)hostr  s     r|   rS   rS   [  s8    	D		4)!!$ 
s   &A
Ac                    Uc  U [         [        1;   a  Sn[        R                  " X5      n [        R                  S;  a/  UR                  [        R                  [        R                  S5        UR                  U5        U[        R                  :X  a  UR                  S5        U$ ! [         a    UR                  5         e f = f)zBinds a generic socket.r  r   >   ntcygwinr   r   )r   r   r  r   r  
setsockopt
SOL_SOCKETSO_REUSEADDRr  r   listenrF  r|  )familytypeaddrr  s       r|   rT   rT   b  s    |7H"55==&D	77**OOF--v/B/BAF		$6%%%KKN 

s   A:B( (Cc                    [         R                  (       d   e[        R                  R	                  U 5      (       a   U 5       e[
        R
                  " [
        R                  U5      n UR                  U 5        U[
        R                  :X  a  UR                  S5        U$ ! [         a    UR                  5         e f = f)zBind a UNIX socket.r   )r   r   r   r   r   r  r   r  r   r  rF  r|  )r  r   r  s      r|   rU   rU   s  s    <<<ww~~d##)T)#==.D		$6%%%KKN K  

s   )6B! !B=c                    [         R                   " U [        5       nUR                  U5        UR                  S5        UR	                  5       n[         R                   " U [        5      n UR                  U5        UR	                  5       n UR                  5       u  pQX:X  a  XS4sSSS5        $ UR                  5         M5  ! [         a    UR                  5         e f = f! , (       d  f       g= f)zVBuild a pair of TCP sockets connected to each other.
Return a (server, client) tuple.
r   N)	r  r   r  r  r  connectrx  r|  r  )r  r!  llr  caddras         r|   rV   rV     s     
v{	+r

		!~~MM&+.	IIdOMMOE))+=6 
,	+ 	   	GGI	 
,	+s$   AC!+;C0CCC!!
C/c                    [         R                  (       d   eS=p [        U [        R                  S9nUR                  S5        [        R                  " [        R                  [        R                  5      nUR                  S5        UR                  U 5        X4$ ! [         a(    Ub  UR                  5         Ub  UR                  5         e f = f)zwBuild a pair of UNIX sockets connected to each other through
the same UNIX file name.
Return a (server, client) tuple.
Nr   r   )
r   r   rU   r  r   setblockingr   r$  rF  r|  )r  serverclients      r|   rW   rW     s    
 <<<F!$V-?-?@1v~~v/A/AB1t   LLNLLNs   A>B 2Cc               #   f  #    / n S=p U R                  [        [        R                  [        R                  5      [        [        R                  [        R
                  5      45        [        5       (       a_  U R                  [        [        R                  [        R                  5      [        [        R                  [        R
                  5      45        [        (       a`  [        (       aU  [        5       n[        5       n[        U5      u  p4[        U[        R
                  S9nX4U4 H  nU R                  U5        M     U v   U  H  nUR                  5         M     X4 H  nUc  M  [        U5        M     g! U  H  nUR                  5         M     X4 H  nUc  M  [        U5        M     f = f7f)z1Open as many socket families / types as possible.Nr)  )extendrT   r  r   r   
SOCK_DGRAMr   r   r   r0   rK   rW   rU   r  r|  rH   )socksfname1fname2s1s2s3rj  r  s           r|   rX   rX     sJ     EF#(:(:;(9(9:
 	 ??LLFOOV-?-?@FOOV->->?  5--\F\F$V,FB!&v/@/@ABb\Q "AGGI %E E" & AGGI %E E" &s)   F1D<E9  F1)F19!F.F..F1c                    [        U[        R                  5      (       d   U5       eU[        R                  :X  a|  U R                  S5       Vs/ s H  n[        U5      PM     nn[        U5      S:X  d   U 5       eU H  nSUs=::  a  S::  a  M   U 5       e   U 5       e   [        R                  " U 5        gU[        R                  :X  a3  [        U [        5      (       d   U 5       e[        R                  " U 5        gU[        R                  :X  a   [        R                   " SU 5      c   U 5       eg[#        SU< 35      es  snf )zSCheck a net address validity. Supported families are IPv4,
IPv6 and MAC addresses.
rg   r   r      z([a-fA-F0-9]{2}[:|\-]?){6}Nzunknown family )r  enumIntEnumr  r   ru   rt   r  	ipaddressIPv4Addressr   r   IPv6Addressr   AF_LINKr   r   r   )r!  r  r1  octsnums        r|   rQ   rQ     s
    fdll++3V3+ $

301A04yA~#t#~C?s?(D(?(D(? d#	6??	"$$$*d*$d#	6>>	!xx5t<HN$NH?6*566 1s   Ec                 r    S nS nS nS nS nU" U 5        U" U 5        U" U 5        U" U 5        U" U 5        g)z*Check validity of a connection namedtuple.c                 f   [        U 5      S:H  n[        U 5      S;   d   [        U 5      5       eU S   U R                  :X  d   U R                  5       eU S   U R                  :X  d   U R                  5       eU S   U R                  :X  d   U R                  5       eU S   U R                  :X  d   U R                  5       eU S   U R
                  :X  d   U R
                  5       eU S   U R                  :X  d   U R                  5       eU(       a%  U S	   U R                  :X  d   U R                  5       eg g )
Nr   >   r  r   r   r   rh   r   r   r   r  )r  rr  r  r   laddrraddrrr  r]  )r  has_pids     r|   check_ntuple-check_connection_ntuple.<locals>.check_ntuple  s   d)q.4yF"-CI-"Aw$''!*477*!Aw$++%2t{{2%Aw$))#.TYY.#Aw$**$0djj0$Aw$**$0djj0$Aw$++%2t{{2%7dhh&00& r~   c                    U R                   [        [        [        1;   d   U R                   5       e[	        U R                   [
        R                  5      (       d   U 5       eU R                   [        :X  aV  [        R                  " U R                   U R                  5       n UR                  U R                  S   S45        S S S 5        g U R                   [        :X  a0  U R                  [        R                   :X  d   U R                  5       eg g ! [         a)  nUR                  [        R                  :w  a  e  S nAN|S nAff = f! , (       d  f       g = fr%  )r  r   r   r   r  r8  r9  r  r   r  rB  r  errnoEADDRNOTAVAILrr  r   	CONN_NONE)r  rj  r   s      r|   check_family-check_connection_ntuple.<locals>.check_family  s    {{w'::GDKKG:$++t||44:d:4;;'!
 t{{DII6!FFDJJqM1-. 76 [[G#;;&"2"22?DKK?2 $  yyE$7$77 8 76s0    E" D
ED>9E>EE
Ec                    [        [        S[        5       5      nU R                  [        R                  [        R
                  U1;   d   U R                  5       e[        U R                  [        R                  5      (       d   U 5       eU R                  [        R
                  :X  a0  U R                  [        R                  :X  d   U R                  5       eg g )NSOCK_SEQPACKET)r   r  objectr   r   r/  r  r8  r9  rr  r   rJ  )r  rN  s     r|   
check_type+check_connection_ntuple.<locals>.check_type  s     )968Dyy
 
 	 99		 

 $))T\\228D8299)));;&"2"22?DKK?2 *r~   c                 T   U R                   U R                  4 GH  nU R                  [        [        1;   a  [        U[        5      (       d   [        U5      5       eU(       d  ML  [        UR                  [        5      (       d   [        UR                  5      5       eSUR                  s=::  a  S::  d  O   UR                  5       e[        UR                  U R                  5        M  U R                  [        :X  d  M  [        U[        5      (       a  M   [        U5      5       e   g )Nr   i  )rB  rC  r  r   r   r  rr   r   portrt   rQ   ipr   r   )r  r!  s     r|   check_addrs,check_connection_ntuple.<locals>.check_addrs  s    ZZ,D{{w11!$..:T
:.!$))S11B4		?B1DII..9		9.!$''4;;7'!$,,8d4j8, -r~   c                 R   [        U R                  [        5      (       d   U R                  5       e[        [        5       Vs/ s H*  oR                  S5      (       d  M  [        [        U5      PM,     nnU R                  U;   d   U R                  5       eU R                  [        [        1;   aD  U R                  [        :X  a0  U R                  [        R                  :w  d   U R                  5       eg U R                  [        R                  :X  d   U R                  5       eg s  snf )NCONN_)r  rr  r   rT  r   r  r   r  r   r   r   r   rJ  )r  r1  validss      r|   check_status-check_connection_ntuple.<locals>.check_status  s    $++s++8T[[8+(+F
(31||G7LGFA 	 
 {{f$1dkk1$;;7H--$)){2J;;&"2"22?DKK?2;;&"2"22?DKK?2
s   D$D$Nr   )r  rE  rK  rP  rU  rZ  s         r|   check_connection_ntupler\    sH    
1@"
@9	@ tr~   c                     / nU  Hh  n[         (       aI  UR                  [        R                  :X  a+  [        (       a   SUR
                  ;   a  [        SU 35        MW  UR                  U5        Mj     U$ )zlOur process may start with some open UNIX sockets which are not
initialized by us, invalidating unit tests.
z/syslogz	skipping )r   r  r  r   r   rC  r   r  )consnewr  s      r|   rR   rR   .  s\     C5T[[FNN2udjj0	$()

4  Jr~   c                 .    [         R                  " U 5      $ r   )	importlibreload)modules    r|   rY   rY   A  s    F##r~   c                 6   [         R                  R                  [         R                  R                  U 5      5      S   n[        R
                  R                  X5      n[        R
                  R                  U5      nUR                  R                  U5        U$ r%  )
r   r   splitextrg  ra  utilspec_from_file_locationmodule_from_specloaderexec_module)r   r  specmods       r|   rZ   rZ   E  si    77BGG,,T23A6D>>11$=D
..
)
)$
/CKKC Jr~   c                 8    [         R                  " U [        SS9  g)zRaise a warning msg.rh   r   Nr   )r   s    r|   r[   r[   R  s    MM#{q1r~   c                     [        U 5      nUR                  n[        U5      S:w  d  US   [        La  g[	        USS5      n[        U[        5      (       d  g[        S U 5       5      $ )z-Check if object is an instance of namedtuple.r   r   F_fieldsNc              3   B   #    U  H  n[        U[        5      v   M     g 7fr   )r  r   ).0ns     r|   	<genexpr> is_namedtuple.<locals>.<genexpr>`  s     -1az!S!!1s   )r   	__bases__r  rr   r   r  r8  )r1  tbr  s       r|   r]   r]   W  s\    QA	A
1v{ad%'9d#Aa-1---r~   c              #   .  #    [         (       a  SOSnSn[        X-   S9n[        R                  " 5       R	                  5        Vs/ s H`  n[
        R                  R                  UR                  5      S   U:X  d  M5  XR                  R                  5       ;   d  MT  UR                  PMb     nn[        R                  " U5      n[        R                  " Xc5         [        R                  " U5        Uv   [        U5        gs  snf ! [        U5        f = f7f)zCtx manager which picks up a random shared CO lib used
by this process, copies it in another location and loads it
in memory via ctypes. Return the new absolutized path.
pypypythonz.sor  r   N)r   rK   r   r   r   r   r   re  lowerr  r  r   r   ctypesCDLLrH   )r  r   extdstr1  libsr~  s          r|   r\   r\   e  s      f(- ^^%113
3ww'*c1 69VV\\^6K AFF3 	 

 mmD!!	KKI
 s6   AD4D <D D +.DD 4DDDc              #   &  #    SSK Jn  SSK Jn  Sn[        X-   S9n[        R
                  " 5       R                  5        Vs/ s H  nUR                  R                  5       R                  U5      (       d  M3  S[        R                  R                  UR                  5      R                  5       ;   d  Mp  SUR                  R                  5       ;  d  M  UR                  PM     nn[        (       a  U(       dz  [        R
                  " 5       R                  5        Vs/ s HL  nS[        R                  R                  UR                  5      R                  5       ;   d  M@  UR                  PMN     nn[        R                  " U5      n[        R                   " Xt5        S	n [         R"                  " U5      nUv   UbU  [         R$                  R&                  R(                  n	UR*                  /U	l        U	" UR.                  5      n
U
S:X  a  U" 5       e[1        U5        g	s  snf s  snf ! UbU  [         R$                  R&                  R(                  n	UR*                  /U	l        U	" UR.                  5      n
U
S:X  a  U" 5       e[1        U5        f = f7f)
zCtx manager which picks up a random shared DLL lib used
by this process, copies it in another location and loads it
in memory via ctypes.
Return the new absolutized, normcased path.
r   )WinError)wintypesz.dllr  rz  wow64ry  N)r|  r  r  rK   r   r   r   r   r{  r  r   rg  r   r  r  r   r   WinDLLwindllkernel32FreeLibraryHMODULEargtypes_handlerH   )r  r  r  r~  r  r1  r  r~  cfiler  r  s              r|   r\   r\   }  s     	$#- ^^%113
3vv||~&&s+  BGG,,QVV4::<<  qvv||~-	 AFF3 	 
 4  )5577ARWW--aff5;;== 7  
 mmD!!	MM#&EI  $mm44@@(0(8(8'9$!%--0!8"*$?
"  $mm44@@(0(8(8'9$!%--0!8"*$sO   ?J2H79H4HH$9J?H$ H$00J!H) ;A.J)A%JJc                      [        SS9  g )NTr  )r8   r   r~   r|   cleanup_test_procsr    s
    D!r~   c                 .    [         R                  " U 5      $ r   )rx   exit)r  r  s     r|   ru  ru    s    #r~   r   )F)TFrN  )z	127.0.0.1)r  r  )r  r   r   r|  r8  rH  rK  r}  ra  r:  r   rp   r  r   ry  r  r   r  r  r  rv   rx   r
  re  r#  r=  	tracebackr=   r   r   r   r   r  ImportErrorr   r   r   r   r	   r
   r   r   r   psutil._commonr   r   r   r   r   psutil._psposixr   __all__builtin_module_namesr   r   r_   r    r3   r5   maxsizer#   machiner{  r4   RISCV64r}   r1   r2   r   r   r"   r   r   r   r   decoder   getfilesystemencodingASCII_FSgetr   r   rA  r  __file__r   r   r   r  r   r$   r&   r  r(   r)   r0   HAS_NET_IO_COUNTERSr*   r'   r+   r,   r.   r/   HAS_THREADSgetuidSKIP_SYSCONSr  r   r-   rF  r   r  r  
format_excr   r%   r   r   r   r  devnullr   r|  rT  r  r   r!   rO  r   r<  r[  rh  rF   r   r  modulesr   r8  r<   rM  r9   r;   r:   r6   r  r  r7   r8   rM   rL   r  r  rO   r  rP   rN   rH   r  r   rG   rI   rJ   rK   r   rB   r  r	  r  rA   rE   r  rC   rD   r@   r>   r?   rS   rT   rU   rV   rW   rX   rQ   r\  rR   rY   rZ   r[   r]   r\   r  )r1  s   0r|   <module>r     s  
        	   	   	        
                    &   " & ((#Z S---!RZZ/O>RZZ3O
RZZ''2::5;;




"
"
$(<
<



	
) 	 	( 	 ?X-L ?g-LLL 
# ' !OJaNA
 299;-q)%,,V5FG $$&,,.2GG ::>>+, 0@0@GGLL*D$71 ggll8Y/	ww12 6>>>: fnni0.V^^X.
&..-8 .Y f&78 6>>95 v~~}= V^^X.
f&78 6>2 "6+AB fnni02"))+"2%H$v/E/E/G*HK6:.J48I3JL)X )] 
N
rzz4
    !$F +1||I/FGFA  &)VX
. U# U#r ,    
>F'CKK(!! (`	 & &R # #L&R  &  0  &~~N Q/h-J!06 6r ""	  .1	 v{{445	D  4&\X&& \Z D!56h'g%^ g% ( 7g%T$FL FLRC@ C@L (  &:% [t " !' 2 2 .. # #<7(HV&$2
	. 	 . * *f " " 	
MM&..">? 	c<  FF  
OOI%%eY-A-A-CDEK
  
OOI%%eY-A-A-CDELpsB   \  \! :']" '^#^#\\!:]]":^ ^ 