U
    ;gh                  K   @   s  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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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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 zddlZW n e k
r&   dZY nX 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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dl*m-Z- ddl*m.Z. ddl*m/Z/ e'rddl0m1Z1 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/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^gKZ2d_ej3kZ4d`e
j5kpdae
j5kZ6e6Z7dbe
j5kZ8dce
j5kZ9ej:ddkZ;e< dekZ=e-dfdg Z>e$re> dhkZ?e> dikZ@ndjZ?djZ@dkZAdlZBdmZCdnZDe7r,eAdo9 ZAeDdo9 ZDeBdp9 ZBeCdo9 ZCe
jEdqkrJdre
F  dsZGndte
F  dsZGduZHdvIdwdxZJeK L dykZMe
jNOe
jNPe
jNQeRdzdzZSe
j5Td{e
jNPeSd|ZUe
jNOe
jNQeRZVeWe!jXd}ZYeWe!d~ZZeWe!jXdZ[eWe!dZ\eWe!jXdZ]eWe!jXdZ^e'oe( Z_eWe!dZ`eWe!jXdZaeWe!jXdZbeWe!jXdZceWe!dZdzedo`eee!f ZgW n ehk
r~   djZgY nX eWe!dZieWe!dZjeWe!jXdZke$se"oe
l dkZmdd Znen \ZoZpeqe
jrdZsetesju dd eve!D Zwexedey Zze{ Z|e{ Z}G dd< d<ej~Zdd Zeddd9Zedd; Zdd: Zedd6 Zedd ZejeDfdd7Zddd8ZddN ZddM ZG dd dZee!jdeDddddP ZeeefdeDdddddQZeedeDddddO ZddI Zdd ZejddH ZddJ ZdddKZdddLZG ddG dGZedkr@eZG ddB dBejZejje4ddG ddA dAeZddE ZddF Zdd ZG ddC dCZG ddD dDZeAfdd@Zddd>Zddd?ZdddTZeedfddUZejfddVZdddWZddX ZejddY ZddR ZddƄ ZddS ZddZ Zdd[ Zdd\ Zdd^ Ze'r^ejddd]Znejddd]Zejtddτ Ze'reejddф  dS )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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_testprocspawn_zombiespawn_children_pair
ThreadTaskunittestskip_on_access_deniedskip_on_not_implementedretry_on_failureTestMemoryLeakPsutilTestCaseprocess_namespacesystem_namespaceprint_sysinfo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_namedtupleZ__pypy__GITHUB_ACTIONSZCIBUILDWHEELZCOVERAGE_RUNZPYTEST_XDIST_WORKERl        aarch64c                  C   sn   t  d } ttt| dd d }|dkrjtjtj	dddgdd	id
d} ttt| dd d }|S )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version ru   U/opt/bot/vods_filmes/script/venv/lib/python3.8/site-packages/psutil/tests/__init__.pymacos_versionz   s    
rw   )rb      )   r   Frb   i  P i            javaz$psutil--z@psutil-u   -ƒőős   futf8surrogateescape>   us-asciiasciiz..ZPSUTIL_SCRIPTS_DIRscriptscpu_affinitycpu_freqenviron
getloadavgionicememory_mapsnet_io_counterscpu_numio_countersrlimitsensors_batterysensors_fanssensors_temperaturesthreadsc                  C   s   dd } t j }ttdd }trFtjdkrF|d k	rFtj|d< ||fS trTtj|fS t	r| tjp| t j
tjp| tdjtjd d  p| t  }|std||fS t j
tj}t j
|st|||fS d S )	Nc                 S   s>   zt j| dgt jt jd W n t jk
r4   Y d S X | S d S )Nz-V)stdoutstderr)ro   
check_callPIPECalledProcessError)exeru   ru   rv   attempt   s      
z_get_py_exe.<locals>.attempt_base_executable)r{      __PYVENV_LAUNCHER__zpython{}.{}ra   z"can't find python exe real abspath)osr   copygetattrrq   r   version_inforr   r^   r   pathrealpathshutilwhichformatpsutilProcessr   
ValueErrorexistsAssertionError)r   rg   baser   ru   ru   rv   _get_py_exe   s0    




r   zr+c                 C   s    g | ]}| d rtt|qS )ZSTATUS_
startswithr   r   .0xru   ru   rv   
<listcomp>  s    
 r   AF_UNIXc                       sP   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
  ZS )r;   z6A thread task which does nothing expect staying alive.c                    s$   t    d| _d| _t | _d S )NFMbP?)super__init___running	_interval	threadingEvent_flagself	__class__ru   rv   r     s    
zThreadTask.__init__c                 C   s(   | j j}d| d| j dt| ddS )N<z	 running=z at z#x>)r   __name__r   idr   nameru   ru   rv   __repr__  s    zThreadTask.__repr__c                 C   s   |    | S N)startr   ru   ru   rv   	__enter__#  s    zThreadTask.__enter__c                 O   s   |    d S r   )stop)r   argskwargsru   ru   rv   __exit__'  s    zThreadTask.__exit__c                 C   s(   | j rtdtj|  | j  dS )zStart 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   ru   ru   rv   r   *  s    zThreadTask.startc                 C   s(   d| _ | j  | j r$t| j qd S )NT)r   r   settimesleepr   r   ru   ru   rv   run3  s    
zThreadTask.runc                 C   s    | j stdd| _ |   dS )z8Stop thread execution and and waits until it is stopped.zalready stoppedFN)r   r   joinr   ru   ru   rv   r   9  s    zThreadTask.stop)r   
__module____qualname____doc__r   r   r   r   r   r   r   __classcell__ru   ru   r   rv   r;     s   	c                    s   t   fdd}|S )Nc                     s.   z | |W S  t k
r(   t   Y nX d S r   )	Exceptionr7   r   r   funru   rv   wrapperG  s
    z&_reap_children_on_err.<locals>.wrapper	functoolswrapsr   r   ru   r   rv   _reap_children_on_errF  s    r   c                 K   s   | dt | dt | dt  | dt trHd}| d| | dkrtt d}zHt| d	| d
}td|g} t	j
| f|}t| t|ddd W 5 t| X n"t	j
| f|}t| t|j |S )a  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   cwdrg      creationflagsNdirzimport time;open(r'z6', 'w').close();[time.sleep(0.1) for x in range(100)];rd   Tdeleteempty)
setdefaultr   r   getcwdr   r   rK   rH   r   ro   Popen_subprocesses_startedaddrP   rO   pid)cmdkwdsZCREATE_NO_WINDOWtestfnZpylinesprocru   ru   rv   r8   R  s,    





c                  C   s   d} t t d}ztdtj| dt d}t	rJt
|dd\}} nt
|\}} t|j}tt|dd	d
}t| t|}||fW S t| | dk	rt|  X dS )a  Create 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().
    Nr   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   )r   TFr   )rK   r   r   rH   textwrapdedentr   basenamer   r   r5   r   r   r   rm   rP   _pids_startedr   )tfiler   ssubpchildZgrandchild_pidZ
grandchildru   ru   rv   r:   |  s&    



c                     s   t js
tt } td|  d}d}t| }z|	t
 t|\}}| \}}zXt| gg g t
 t|d}t| t | t fdd | fW W S |  X W 5 |  t|  |dk	rt| X dS )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                      s      tjkS r   )statusr   STATUS_ZOMBIEru   zombieru   rv   <lambda>      zspawn_zombie.<locals>.<lambda>)r   r
   r   rK   r   r   rU   closerH   
settimeoutr   r5   acceptselectfilenorm   recvr   r   r   rN   )Z	unix_filesrcr   sockparentconn_Zzpidru   r   rv   r9     s.    




c              	   K   s   | dd | dd t }zFt|d}||  W 5 Q R X tt|jgf|}t|j ||fW S  t	k
r   t
|  Y nX dS )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)r   rK   openwriter8   r   r   rO   r   r   rH   )r
  r   srcfilefr   ru   ru   rv   r5     s    

c                 K   s   t rdnd}|dtj |dtj |dd |d| t| trTt| } tj| f|}t	
| |jtd\}}|jdkrt|| |rt| |d	r|d
d }|S )zURun cmd in a subprocess and return its output.
    raises RuntimeError on error.
    r   r   r   r   rh   Tr   timeout
N)r   r   ro   r   
isinstancestrshlexrn   r   r   r   communicater   
returncodeRuntimeErrorr[   endswith)r   r   flagspr   r   ru   ru   rv   sh  s"    




r!  c              
      s   dd dd   fdd} fddfd	d
}dd }| }zdt |trb|||W NS t |tjtjfr||W .S t |tjr|||W S t	d|W 5 t |tjtjfr|| t |tr|n|j}t|rt|X dS )a  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                 S   sJ   |  | trFt| tjrFzt| j |W S  tjk
rD   Y nX d S r   )	r   r   r  ro   r   r   r   r   NoSuchProcessprocr  ru   ru   rv   r     s    
zterminate.<locals>.waitc                 S   s6   t rtrtj}tr(|tjkr(| tj | | d S r   )r   r^   signalSIGKILLr
   send_signalSIGCONT)r$  sigru   ru   rv   sendsig  s
    zterminate.<locals>.sendsigc              
      s^   z |  W nD t k
r"   Y n2 tk
rR } ztr@|jdkr@ W 5 d }~X Y nX | |S )N   )ProcessLookupErrorOSErrorr   winerror)r$  r  errr*  r)  r   ru   rv   term_subprocess_proc  s    z'terminate.<locals>.term_subprocess_procc                    s0   z |  W n t jk
r$   Y nX | |S r   )r   r"  r#  r0  ru   rv   term_psutil_proc"  s
    z#terminate.<locals>.term_psutil_procc                    sF   zt | }W n( t jk
r6   tr2t| | Y S Y nX  ||S d S r   )r   r   r"  r
   r   )r   r  r$  )r2  ru   rv   term_pid)  s    zterminate.<locals>.term_pidc                 S   s4   | j r| j   | jr | j  | jr0| j  d S r   )r   r  r   r   )r$  ru   ru   rv   flush_popen3  s    

zterminate.<locals>.flush_popenzwrong type N)
r  ro   r   r   rm   r   
pid_existsr   r   	TypeError)Zproc_or_pidr)  wait_timeoutr1  r3  r4  r   r   ru   )r*  r)  r2  r   rv   r6     s&    	

	
c                 C   s   t  j| d}tr&t }t| qtr<t }t| q&|r|D ]}t|dd qDt j|td\}}|D ]"}t	d|d t|t
jd qldS )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)r7  r  zcouldn't terminate process z; attempting kill())r)  )r   r   childrenr   popr6   r   
wait_procsr   r[   r%  r&  )r9  r:  r   r   r   r  aliveru   ru   rv   r7   M  s    


c                  C   s   t stdd} t d }|D ]"}| s4|dkr>| |7 } q  qDq | sVtd|d}d}| d}t|d }t|dkrt|d }t|dkrt|d }|||fS )	z"Return a tuple such as (2, 6, 36).z	not POSIX ra   r`   zcan't parse r      r{   )	r
   NotImplementedErrorr   unameisdigitr   rn   rm   len)r   rA  cminormicroZnumsmajorru   ru   rv   rM   r  s&    

c                  C   s0   t stdt } | jpd}| d | d |fS )Nznot WINDOWSr   r?  )r   r@  rq   getwindowsversionservice_pack_major)Zwvspru   ru   rv   rL     s
    
c                   @   s<   e Zd ZdZeddddfddZdd Zdd	 Zd
d ZdS )retryzA retry decorator.Nr   c                 C   s2   |r|rt d|| _|| _|| _|| _|| _d S )Nz/timeout and retries args are mutually exclusive)r   	exceptionr  retriesintervallogfun)r   rL  r  rM  rN  rO  ru   ru   rv   r     s    zretry.__init__c                 c   sT   | j r*t | j  }t |k rPd V  qn&| jrHt| jD ]
}d V  q:nd V  qHd S r   )r  r   rM  range)r   stop_atr  ru   ru   rv   __iter__  s    

zretry.__iter__c                 C   s   | j d k	rt| j  d S r   )rN  r   r   r   ru   ru   rv   r     s    
zretry.sleepc                    s"   t   fdd}|_|S )Nc                     st   d }D ]b}z | |W   S  j k
rh } z,|}jd k	rH|   W Y qW 5 d }~X Y qX q|d S r   )rL  rO  r   )r   r   excr  r   r   ru   rv   r     s    

zretry.__call__.<locals>.wrapper)r   r   	decorator)r   r   r   ru   rT  rv   __call__  s    zretry.__call__)	r   r   r   r   r   r   rR  r   rV  ru   ru   ru   rv   rK    s   
rK  r   )rL  rO  r  rN  c                 C   s$   | t  krt | t |  dS )zWait 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   ru   ru   rv   rO     s    

Tc              	   C   s:   t | d}| }W 5 Q R X |s*|s*t|r6t|  |S )z8Wait for a file to be written on disk with some content.rb)r  readr   rH   )fnamer   r   r  dataru   ru   rv   rP     s    c                 C   s   |  }|st |S )z1Keep calling function until it evaluates to True.)r   )r   retru   ru   rv   rN     s    c                 C   sn   dd }zLt | }t|jr0ttj| }ntt j| }t	rJ|  n|| W n t
k
rh   Y nX dS )z?Convenience function for removing temporary test files or dirs.c              
   S   s|   t   t }t   |k rtz|  W S  tk
r4   Y n4 tk
rf } z|}td|  W 5 d }~X Y nX t d q|d S )Nz	ignoring g{Gz?)r   r   FileNotFoundErrorr-  r[   r   )r   rQ  r  r/  ru   ru   rv   	retry_fun  s     zsafe_rmpath.<locals>.retry_funN)r   statS_ISDIRst_moder   partialr   rmtreeremover
   r^  )r   r_  str   ru   ru   rv   rH     s    
c                 C   s(   zt |  W n tk
r"   Y nX dS )z.Convenience function for creating a directory.N)r   mkdirFileExistsErrorr   ru   ru   rv   
safe_mkdir$  s    ri  c              	   c   s.   t  }zt |  dV  W 5 t | X dS )z@Context manager which temporarily changes the current directory.N)r   r   rG   )dirnamecurdirru   ru   rv   rG   ,  s
    

c                 C   sR   t j| rt| tt|  tt	|  t
rNt | }t | |jtjB  | S )z6Create a Python executable file in the given location.)r   r   r   r   atexitregisterrH   r   copyfiler   r
   r`  chmodrb  S_IEXEC)r   rf  ru   ru   rv   rI   7  s    
c              	   C   s   t j| rt| tds(td|dkr<t	d}nt
|tsNt|tt|  ttddd}|| W 5 Q R X ztd|jd| g W 5 t|j X | S )	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   pytestskipr   r   r  r  rl  rm  rH   r  rK   r  r   ro   r   )r   Zc_coder  ru   ru   rv   rJ   B  s    

r>  c                 C   s>   t jt| |d}tj|s tj|}tt	| |S q dS )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.
    )prefixrs  r   N)
tempfilemktempr   r   r   r   r   rl  rm  rH   )rs  r   r   r   ru   ru   rv   rK   \  s
    c                   @   sT   e Zd ZdZedd ZedddZedddZedd
dZG dd dZ	dS )rF   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                  O   s4   t  t}t jdd| tjdtdd |S )zMimics pytest.main(). It has the same effect as running
        `python3 -m unittest -v` from the project root directory.
        ra   )	verbosityz<Fake pytest module was used. Test results may be inaccurate.r?  
stacklevel)	r<   Z
TestLoaderdiscoverHEREZTextTestRunnerr   warningsr[   UserWarning)r   kwZsuiteru   ru   rv   mainw  s    zfake_pytest.mainNc                    s.   G dd d t jd fdd	}|| |dS )zMimics `pytest.raises`.c                   @   s   e Zd ZdZedd ZdS )z)fake_pytest.raises.<locals>.ExceptionInfoNc                 S   s   | j S r   )_excr   ru   ru   rv   value  s    z/fake_pytest.raises.<locals>.ExceptionInfo.value)r   r   r   r  propertyr  ru   ru   ru   rv   ExceptionInfo  s   r  Nc              
   3   sz     }z
|V  W nV | k
rf } z8|rPt |t|sPd| d| d}t|||_W 5 d }~X Y nX t| dd S )N"z" does not match "z not raised)researchr  r   r  )rS  matchZeinfor/  msgr  ru   rv   context  s    
z#fake_pytest.raises.<locals>.context)r  )N)
contextlibcontextmanager)rS  r  r  ru   r  rv   raises  s    zfake_pytest.raisesc                 C   s"   |rt  | |S t  | S )zMimics `pytest.warns`.)r<   TestCaseZassertWarnsRegexZassertWarns)warningr  ru   ru   rv   warns  s    zfake_pytest.warnsr>  c                 C   s   t | dS )zMimics `unittest.SkipTest`.N)r<   ZSkipTestreasonru   ru   rv   ru    s    zfake_pytest.skipc                   @   s(   e Zd ZedddZG dd dZdS )zfake_pytest.markr>  c                 C   s   t | |S )z'Mimics `@pytest.mark.skipif` decorator.)r<   ZskipIf)	conditionr  ru   ru   rv   skipif  s    zfake_pytest.mark.skipifc                   @   s"   e Zd ZdZdddZdd ZdS )zfake_pytest.mark.xdist_groupz4Mimics `@pytest.mark.xdist_group` decorator (no-op).Nc                 C   s   d S r   ru   r   ru   ru   rv   r     s    z%fake_pytest.mark.xdist_group.__init__c                 C   s   |S r   ru   )r   Zcls_or_methru   ru   rv   rV    s    z%fake_pytest.mark.xdist_group.__call__)N)r   r   r   r   r   rV  ru   ru   ru   rv   xdist_group  s   
r  N)r>  )r   r   r   staticmethodr  r  ru   ru   ru   rv   mark  s   r  )N)N)r>  )
r   r   r   r   r  r  r  r  ru  r  ru   ru   ru   rv   rF   o  s   
c                   @   sZ   e Zd ZdZdddZdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd ZdS )rA   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.
    r>  Nc                 C   s   t ||d}| t| |S )N)rs  r   )rK   
addCleanuprH   )r   rs  r   r[  ru   ru   rv   rK     s    zPsutilTestCase.get_testfnc                 O   s   t ||}| t| |S r   )r8   r  r6   )r   r   r   r   ru   ru   rv   r8     s    
zPsutilTestCase.spawn_testprocc                 C   s*   t  \}}| t| | t| ||fS r   )r:   r  r6   )r   Zchild1Zchild2ru   ru   rv   r:     s    
z"PsutilTestCase.spawn_children_pairc                 C   s*   t  \}}| t| | t| ||fS r   )r9   r  r6   )r   r  r  ru   ru   rv   r9     s    
zPsutilTestCase.spawn_zombiec                 O   s*   t ||\}}| t| | t| |S r   )r5   r  rH   r6   )r   r   r   r   r  ru   ru   rv   r5     s    zPsutilTestCase.pyrunc                 C   s   t |tjst|j|jks t|j|jks0t|jr@|js@tt |tjrt|j|j	ks\t|jd k	rt|jdksttt
| t| d S Nr   )r  r   Errorr   r   r   _nameZombieProcessppidZ_ppidr  repr)r   r$  rS  ru   ru   rv   _check_proc_exc  s    

zPsutilTestCase._check_proc_excc              	   C   s   t tj4}zt| W n tjk
r:   tdY nX W 5 Q R X |jj|ksVt|jj	d ksftt
|rxt||t kst|dd t D kstd S )Nz&wasn't supposed to raise ZombieProcessc                 S   s   g | ]
}|j qS ru   rX  r   ru   ru   rv   r     s     z0PsutilTestCase.assertPidGone.<locals>.<listcomp>)rt  r  r   r"  r   r  r   r  r   r   r5  rW  process_iter)r   r   cmru   ru   rv   assertPidGone  s    zPsutilTestCase.assertPidGonec                 C   s   |  |j t|}|j|jddD ]\}}| j||dp z
| }W nF tjk
r^    Y nH tjk
r } z| 	|| W 5 d }~X Y nX d| d|}t
|W 5 Q R X q$|jdd d S )NTclear_cacher$  r   zProcess.z!() didn't raise NSP and returned r   r  )r  r   rB   iterallsubTestr   r  r"  r  r   r   )r   r$  nsr   r   r]  rS  r  ru   ru   rv   assertProcessGone  s    
z PsutilTestCase.assertProcessGonec                 C   sd  t |j}||kstts4ts4t|t|ks4t| t jksFt|	 sRtt 
|jsbt|  |jt  ks|t|jdd t  D ksti t _|jdd t  D kstt|}|j|jddD ]f\}}| j||dJ z
|  W n8 t jt jfk
r, } z| || W 5 d }~X Y nX W 5 Q R X qtrtt j}|  W 5 Q R X | ||j tt j}|  W 5 Q R X | ||j tt j}|  W 5 Q R X | ||j |  |  |  |   |	 stt 
|js
t|jt  kst|jdd t  D ks<ti t _|jdd t  D ks`td S )	Nc                 S   s   g | ]
}|j qS ru   rX  r   ru   ru   rv   r     s     z6PsutilTestCase.assertProcessZombie.<locals>.<listcomp>c                 S   s   g | ]
}|j qS ru   rX  r   ru   ru   rv   r   !  s     Tr  r  c                 S   s   g | ]
}|j qS ru   rX  r   ru   ru   rv   r   =  s     c                 S   s   g | ]
}|j qS ru   rX  r   ru   ru   rv   r   ?  s     )!r   r   r   r   r	   r   hashr   r   
is_runningr5  as_dictrW  r  Z_pmaprB   r  r  r  r  AccessDeniedr  r   rt  r  cmdliner  r   r   suspendresumer6   kill)r   r$  cloner  r   r   rS  r  ru   ru   rv   assertProcessZombie  sN    
*z"PsutilTestCase.assertProcessZombie)r>  N)r   r   r   r   rK   r8   r:   r9   r5   r  r  r  r  ru   ru   ru   rv   rA     s   
zunreliable on PYPYr  c                   @   s   e Zd ZdZdZdZdZer dndZdZ	e
 ZeedZedd	 Zed
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdddZdd ZdS ) r@   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)
       rb   r   rz   TZPSUTIL_DEBUGc                 C   s   t d d S )NF)r   
_set_debugclsru   ru   rv   
setUpClassy  s    zTestMemoryLeak.setUpClassc                 C   s   t | j d S r   )r   r  _psutil_debug_origr  ru   ru   rv   tearDownClass}  s    zTestMemoryLeak.tearDownClassc                 C   s   | j  }t|d|jS )NZuss)	_thisprocmemory_full_infor   Zrss)r   memru   ru   rv   _get_mem  s    
zTestMemoryLeak._get_memc                 C   s   t r| j S | j S d S r   )r
   r  num_fdsnum_handlesr   ru   ru   rv   _get_num_fds  s    
zTestMemoryLeak._get_num_fdsc                 C   s   | j rt|dtjd d S )Nyellow)colorfile)verboser   rq   r   )r   r  ru   ru   rv   _log  s    zTestMemoryLeak._logc                 C   s   |   }| | |   }|| }|dk r@d|d}| ||dkrtrPdnd}|dkrd|d7 }| d| d	|}| |d
S )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?  r   z
 unclosed z after calling N)r  callfailr
   )r   r   beforeafterdiffr  type_ru   ru   rv   
_check_fds  s    


zTestMemoryLeak._check_fdsc                 C   s^   t jdd |  }t|D ]}| |}~~qt jdd |  }t jg ksRt|| }|S )zGet 2 distinct memory samples, before and after having
        called fun repeatedly, and return the memory difference.
        r?  )Z
generation)gcZcollectr  rP  r  garbager   )r   r   timesZmem1r   r]  Zmem2r  ru   ru   rv   _call_ntimes  s    
zTestMemoryLeak._call_ntimesc                 C   s   g }d}|}t d|d D ]}| ||}	d|t|	t|	| |}
||
 |	|kp^|	|k}|r||dkrv| |
  d S |dkrt  | |
 ||7 }|	}q| d|d S )Nr   r?  z,Run #{}: extra-mem={}, per-call={}, calls={}z. )	rP  r  r   r   appendr  printr  r   )r   r   r  rM  	tolerancemessagesZprev_memZincreaseidxr  r  successru   ru   rv   
_check_mem  s.    



zTestMemoryLeak._check_memc                 C   s   | S r   ru   )r   r   ru   ru   rv   r    s    zTestMemoryLeak.callNc              
   C   s   |dk	r|n| j }|dk	r|n| j}|dk	r0|n| j}|dk	rB|n| j}zD|dksZtd|dksjtd|dksztd|dkstdW n. tk
r } ztt|W 5 d}~X Y nX | || | | | j	||||d dS )	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  rM  r  )
r  warmup_timesrM  r  r   r   r  r  r  r  )r   r   r  r  rM  r  r/  ru   ru   rv   execute  s    
zTestMemoryLeak.executec                    s"    fdd}j |f| dS )znConvenience method to test a callable while making sure it
        raises an exception on every call.
        c                      s      d S r   )ZassertRaisesru   rS  r   r   ru   rv   r    s    z*TestMemoryLeak.execute_w_exc.<locals>.callN)r  )r   rS  r   r   r  ru   r  rv   execute_w_exc  s    zTestMemoryLeak.execute_w_exc)NNNN)r   r   r   r   r  r  r  r   rM  r  r   r   r  boolr   getenvr  classmethodr  r  r  r  r  r  r  r  r  r  r  ru   ru   ru   rv   r@   P  s2   

       
c                  C   s  dd l } dd l}dd l}dd l}dd l}zdd l}W n tk
rL   d }Y nX zdd l}W n tk
rr   d }Y nX |  }t	j
rtdrtd|d< n~t	jrdt d  |d< n`t	jrddttt  |d< ttdr|d  d	t  7  < nt  dt  |d< d	tt t g |d
< t	jrPt d |d< d	t t  t! g|d< t"|dd|d< |d k	r|d  d|j# d7  < t	jrtdrtddg}t|$dd |d< nd|d< t% d }	|	r|	|d< t&' |d< |( }
|
d  d	|
d  |d< |j)t	* +d|d< |j, +d|d< |- |d< t.j/0d|d< t.1 |d < t2|d!< t3 |d"< t.4 |d#< t	5 |d$< d%j6t7d&d' t	8 D  |d(< t	9 }d)6t:|j;t<|j=t<|j>|d*< t	? }d)6t:|j;t<|j=t<|j>|d+< t@t	A |d,< t	B C }|Dd-d  |E||d.< tFd/t&jGd0 |H D ]$\}}tFd16|d2 |t&jGd0 qrtFd/t&jGd0 t&jIJ  t&jIJ  d S )3Nr   lsb_releasezlsb_release -d -sZOSzDarwin zWindows  win32_editionz, archra   Zkernelpython__version__znot installedpipz (wheel=)rq  z	--versionr  r?  glibczfs-encodinglangz%Y-%m-%d %H:%M:%Sz	boot-timer   user~homer   ZpyexehostnameZPIDZcpusz{:.1f}%, {:.1f}%, {:.1f}%c                 s   s   | ]}|t   d  V  qdS )d   N)r   	cpu_countr   ru   ru   rv   	<genexpr>@  s     z print_sysinfo.<locals>.<genexpr>Zloadavgz{}%%, used={}, total={}ZmemoryswaprW  r   r$  zF======================================================================r  z	{:<17} {}:)Kcollectionsdatetimegetpasslocalepprintr  ImportErrorwheelOrderedDictr   r   r   r   r!  ZOSXri   rj   r   r   rl   r  	win32_verhasattrr  systemrt   listarchitecturemachiner
   rA  python_implementationpython_versionpython_compilerr   r  rn   libc_verrq   getfilesystemencoding	getlocalefromtimestamp	boot_timestrftimenowgetuserr   r   
expanduserr   r   nodegetpidr  r   rk   r   virtual_memoryrm   percentr   usedtotalswap_memoryrC  rW  r   r  r;  pformatr  r   itemsr   flush)r  r  r  r  r  r  r  infooutr   r  r  r  Zpinfokvru   ru   rv   rD     s    





c                 C   s8   t dd }z| |  dkW S  tk
r2   Y dS X d S )Nc                  S   sX   i } t d}| dd  D ]6}dd |dD }|d t|d  }}|| |< q| S )Nztasklist.exe /NH /FO csvr?  c                 S   s   g | ]}| d dqS )r  r>  )replacer   ru   ru   rv   r   i  s     z@is_win_secure_system_proc.<locals>.get_procs.<locals>.<listcomp>,r   )r!  
splitlinesrn   rm   )r]  r  linebitsr   r   ru   ru   rv   	get_procsd  s    
z,is_win_secure_system_proc.<locals>.get_procszSecure SystemF)r   KeyError)r   r&  ru   ru   rv   rE   b  s    
	c                  C   s6   t  } t| dr|  S t| dr2t|  S dS )Nr   r   r   )r   r   r  r   randomchoicer   )r   ru   ru   rv   _get_eligible_cput  s    

r*  c                   @   sB  e Zd ZdZddi fddi fgZddi fddddifd	di fd
di fddi fddi fddi fddi fddi fg	Zddi fddi fddi fddi fddi fddi fddi fddi fddddifddi fddi fddi fddi fd di fd!di fd"di fd#di fgZerFed$di fg7 Zed%di fg7 Zed&di fg7 Zed'di fg7 Zer\ed(di fg7 Ze	rred)di fg7 Ze
red*ejfi fg7 Zered+di fg7 Zered,di fg7 Zered-di fg7 Zered.di fg7 Zered/dd0d1ifg7 Zg Zereddi fg7 Znedejfi fg7 Ze
rJed*ejd2fi fg7 Ze	rerned)ejd3fi fg7 Zned)ejfi fg7 Zered+e gfi fg7 Zd4ejfi fd5di fd6di fd7di fd8di fgZered4ejfi fg7 Zed4ejfi fg7 Zee e e Zd9d: ZdDd;d<Zd=d> Z e!d?d@ Z"e!dAdB Z#dCS )ErB   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_percentru   Zmemory_percentr  r:  r9  Tconnectionsr  Zoneshotr  parentsr   r   r   r  	cpu_timesZcreate_timer   r   r  Zmemory_infor   net_connectionskindr  niceZnum_ctx_switchesZnum_threadsZ
open_filesr  r   r   usernameZuidsZgidsZterminalr  r   r   r   r   r   r   r  r   groupedF)r   i   r   r'  r  r  r6   r  c                 C   s
   || _ d S r   )_proc)r   r$  ru   ru   rv   r     s    zprocess_namespace.__init__c                 c   sZ   t |}t| |D ]>\}}}|r,|   t| j|}tj|f||}||fV  qdS z_Given a list of tuples yields a set of (fun, fun_name) tuples
        in random order.
        N)r  r(  shuffler  r   r5  r   rc  )r   lsr  fun_namer   r   r   ru   ru   rv   r    s    
zprocess_namespace.iterc                 C   s   | j j| j jdd dS )z&Clear the cache of a Process instance.T)Z_ignore_nspN)r5  _initr   r   ru   ru   rv   r    s    zprocess_namespace.clear_cachec                 C   sB   |D ]8\}}}d| }t ||s|jjd|d}t|qdS )z}Given a TestCase instance and a list of tuples checks that
        the class defines the required test method names.
        Ztest_z class should define a z methodN)r  r   r   AttributeError)r  Z
test_classr8  r9  r  	meth_namer  ru   ru   rv   test_class_coverage  s    
z%process_namespace.test_class_coveragec                 C   sV   dd | j D }dd | jD }dd ttjD }||B |A }|rRtd|d S )Nc                 S   s   h | ]}|d  qS r.  ru   r   ru   ru   rv   	<setcomp>  s     z)process_namespace.test.<locals>.<setcomp>c                 S   s   h | ]}|d  qS r.  ru   r   ru   ru   rv   r>    s     c                 S   s   h | ]}|d  dkr|qS )r   r  ru   r   ru   ru   rv   r>     s      zuncovered Process class names: )r  ignoredr   r   r   r   )r  thisr?  klassZleftoutru   ru   rv   test  s    zprocess_namespace.testN)T)$r   r   r   r   utilsr?  gettersr
   r&   r'   r*   r   ZRLIMIT_NOFILEr#   r)   r%   r   r(   ZsettersZNORMAL_PRIORITY_CLASSr   ZIOPRIO_CLASS_NONEZIOPRIO_NORMALr*  r%  SIGTERMZkillersZCTRL_C_EVENTZCTRL_BREAK_EVENTr  r   r  r  r  r=  rB  ru   ru   ru   rv   rB   }  s   	

c                   @   s  e Zd ZdZddi fddddifddddifddi fd	dd
difd	dd
difddddifddddifde fi fddddifddi fddi fddddifde fi fddi fddi fddi fddi fgZere	re
 dkrneddd
difg7 Zereddi fg7 Zer"eddi fg7 Zer8eddi fg7 ZerNed di fg7 Zerted!di fg7 Zed"d#i fg7 Zd$di fd%e gfi fd&di fd'di fgZeZed(d) ZejZd*S )+rC   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()
    r  ru   r  ZlogicalFTZ	cpu_statsr/  ZpercpuZdisk_io_countersZperdiskZdisk_partitionsr  
disk_usager0  r1  Znet_if_addrsZnet_if_statsr   Zpernicr5  rW  r  Zusersr  arm64r   r   r   r   r   Zwin_service_iterZwin_service_get)algr  r<  r+  Zcpu_times_percentc                 c   sL   t | } t|  | D ]0\}}}tt|}tj|f||}||fV  qdS r6  )r  r(  r7  r   r   r   rc  )r8  r9  r   r   r   ru   ru   rv   r  =  s    

zsystem_namespace.iterN)r   r   r   r   r   r   r  rD  r$   r   ri   r  HAS_GETLOADAVGr.   r-   r+   r   r   r   r?  r  r  r  rB   r=  ru   ru   ru   rv   rC     sX   	
c                 C   s   dd }t td| |dS )zZDecorator which runs a test function and retries N times before
    actually failing.
    c                 S   s   t | dtjd d S )Nz
, retryingr  )r  rq   r   )rS  ru   ru   rv   rO  Q  s    z retry_on_failure.<locals>.logfunN)rL  r  rM  rO  )rK  r   )rM  rO  ru   ru   rv   r?   L  s       c                    s    fdd}|S )z,Decorator to Ignore AccessDenied exceptions.c                    s   t   fdd}|S )Nc                     s@   z | |W S  t jk
r:   d k	r,s, tdY nX d S )Nzraises AccessDenied)r   r  rt  ru  r   r   only_ifru   rv   r   ]  s    z9skip_on_access_denied.<locals>.decorator.<locals>.wrapperr   r   rK  r   rv   rU  \  s    	z(skip_on_access_denied.<locals>.decoratorru   rK  rU  ru   rL  rv   r=   Y  s    c                    s    fdd}|S )z3Decorator to Ignore NotImplementedError exceptions.c                    s   t   fdd}|S )Nc                     sJ   z | |W S  t k
rD   d k	r*s*  jd}t|Y nX d S )Nz2 was skipped because it raised NotImplementedError)r@  r   rt  ru  )r   r   r  rJ  ru   rv   r   p  s    
z;skip_on_not_implemented.<locals>.decorator.<locals>.wrapperr   r   rL  r   rv   rU  o  s    z*skip_on_not_implemented.<locals>.decoratorru   rM  ru   rL  rv   r>   l  s    	127.0.0.1c              
   C   s:   t   (}|| df | d W  5 Q R  S Q R X dS )z6Return an unused TCP port. Subject to race conditions.r   r?  N)socketbindgetsockname)hostr  ru   ru   rv   rS     s    
c                 C   s   |dkr| t thkrd}t| |}z@tjdkrB|tjtjd || |tj	kr`|
d |W S  tk
r   |   Y nX dS )zBinds a generic socket.Nr>  r   >   cygwinntr?  rz   )r   r   rO  r   r   
setsockopt
SOL_SOCKETSO_REUSEADDRrP  r   listenr   r  )familytypeaddrr  ru   ru   rv   rT     s    



c                 C   sr   t js
ttj| rt| ttj|}z"||  |tj	krL|
d W n tk
rl   |   Y nX |S )zBind a UNIX socket.rz   )r   r
   r   r   r   r   rO  r   rP  r   rY  r   r  )r   r[  r  ru   ru   rv   rU     s    


rS  c              
   C   s   t  | t}|| |d | }t  | t}zJ|| | }| \}}||krt||fW W  5 Q R  S |  qJW n tk
r   |   Y nX W 5 Q R X dS )z^Build a pair of TCP sockets connected to each other.
    Return a (server, client) tuple.
    rz   N)	rO  r   rP  rY  rQ  connectr  r  r-  )rZ  r\  ZllrD  Zcaddraru   ru   rv   rV     s    


c                 C   s   t js
td }}z@t| tjd}|d ttjtj}|d ||  W n6 t	k
r   |dk	rr|
  |dk	r|
   Y nX ||fS )zBuild 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
   r   rU   rO  r   setblockingr   r]  r   r  )r   serverclientru   ru   rv   rW     s    


c               	   c   s   g } d }}z| ttjtjttjtjf t rX| ttj	tjttj	tjf t
rtrt }t }t|\}}t|tjd}|||fD ]}| | q| V  W 5 | D ]}|   q||fD ]}|dk	rt| qX dS )z1Open as many socket families / types as possible.Nr_  )r  rH   extendrT   rO  r   r   
SOCK_DGRAMr   r   r
   r/   rK   rW   rU   r  )socksZfname1Zfname2r   r[  s1s2Zs3ru   ru   rv   rX     s2    

c                 C   s   t |tjst||tjkrzdd | dD }t|dksFt| |D ]"}d|  krbdksJn t| qJt	|  nZ|tj
krt | tst| t|  n2|tjkrtd| dk	st| ntd	|dS )
z[Check a net address validity. Supported families are IPv4,
    IPv6 and MAC addresses.
    c                 S   s   g | ]}t |qS ru   )rm   r   ru   ru   rv   r     s     z%check_net_address.<locals>.<listcomp>r`   r|   r      z([a-fA-F0-9]{2}[:|\-]?){6}Nzunknown family )r  enumIntEnumr   rO  r   rn   rC  	ipaddressIPv4Addressr   r  IPv6Addressr   ZAF_LINKr  r  r   )r\  rZ  Zoctsnumru   ru   rv   rQ     s    
 

c                 C   sT   dd }dd }dd }dd }d	d
 }||  ||  ||  ||  ||  dS )z*Check validity of a connection namedtuple.c                 S   s   t | dk}t | dks$tt | | d | jks<t| j| d | jksTt| j| d | jkslt| j| d | jkst| j| d | jkst| j| d | jkst| j|r| d	 | jkst| jd S )
Nr   >   r+  r   r   r?  ra   r{   r|   rz   r+  )	rC  r   r  rZ  r[  laddrraddrr   r   )r  Zhas_pidru   ru   rv   check_ntuple  s    z-check_connection_ntuple.<locals>.check_ntuplec                 S   s   | j ttthkst| j t| j tjs0t| | j tkrt| j | j	P}z|
| jd df W n0 tk
r } z|jtjkr W 5 d }~X Y nX W 5 Q R X n | j tkr| jtjkst| jd S r  )rZ  r   r   r   r   r  ri  rj  rO  r[  rP  ro  r-  errnoZEADDRNOTAVAILr   r   	CONN_NONE)r  r   r/  ru   ru   rv   check_family   s    
 
z-check_connection_ntuple.<locals>.check_familyc                 S   sh   t tdt }| jtjtj|hks,t| jt| jtj	sBt| | jtjkrd| j
tjksdt| j
d S )NSOCK_SEQPACKET)r   rO  objectr[  r   rd  r   r  ri  rj  r   r   rs  )r  ru  ru   ru   rv   
check_type1  s    z+check_connection_ntuple.<locals>.check_typec                 S   s   | j | jfD ]}| jtthkrt|ts4tt||s:qt|j	t
sTtt|j	d|j	  krjdksvn t|j	t|j| j q| jtkrt|tstt|qd S )Nr   i  )ro  rp  rZ  r   r   r  rk   r   r[  portrm   rQ   ipr   r  )r  r\  ru   ru   rv   check_addrs=  s    "
z,check_connection_ntuple.<locals>.check_addrsc                 S   s   t | jtst| jdd ttD }| j|ks<t| j| jtthkrl| j	t
krl| jtjkst| jn| jtjkst| jd S )Nc                 S   s    g | ]}| d rtt|qS )ZCONN_r   r   ru   ru   rv   r   L  s    
 zAcheck_connection_ntuple.<locals>.check_status.<locals>.<listcomp>)r  r   r  r   r   r   rZ  r   r   r[  r   rs  )r  Zvalidsru   ru   rv   check_statusJ  s    z-check_connection_ntuple.<locals>.check_statusNru   )r  rq  rt  rw  rz  r{  ru   ru   rv   check_connection_ntuple  s    r|  c                 C   sJ   g }| D ]<}t r:|jtjkr:tr:d|jkr:td|  q|| q|S )ztOur process may start with some open UNIX sockets which are not
    initialized by us, invalidating unit tests.
    z/syslogz	skipping )r
   rZ  rO  r   r   rp  r   r  )Zconsnewr  ru   ru   rv   rR   \  s    c                 C   s
   t | S r   )	importlibreload)moduleru   ru   rv   rY   o  s    c                 C   sB   t jt j| d }tj|| }tj|}|j	| |S r  )
r   r   splitextr   r~  utilspec_from_file_locationmodule_from_specloaderexec_module)r   r   specmodru   ru   rv   rZ   s  s
    c                 C   s   t j| tdd dS )zRaise a warning msg.ra   rz  N)r~  r[   r  )r  ru   ru   rv   r[     s    c                 C   sV   t | }|j}t|dks&|d tk	r*dS t|dd}t|tsDdS tdd |D S )z-Check if object is an instance of namedtuple.r?  r   F_fieldsNc                 s   s   | ]}t |tV  qd S r   )r  r  )r   nru   ru   rv   r    s     z is_namedtuple.<locals>.<genexpr>)r[  	__bases__rC  rk   r   r  r  )r   tbr  ru   ru   rv   r]     s    
c                 #   st   t rdnd dt|  d} fddt  D }t|}t|| zt
| |V  W 5 t	| X dS )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.
        pypyr  z.sorr  c                    s6   g | ].}t j|jd  kr |j kr|jqS )r?  )r   r   r  lowerr   r   extru   rv   r     s    'copyload_shared_lib.<locals>.<listcomp>N)r   rK   r   r   r   r(  r)  r   rn  rH   ctypesCDLL)rs  dstlibsr
  ru   r  rv   r\     s    



c           	      #   s   ddl m} ddl m} d t|   d} fddt  D }trb|sbdd t  D }t	|}t
|| d	}zt |}|V  W 5 |d	k	rt jjj}|jg|_||j}|dkr| t| X d	S )
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.dllrr  c                    sF   g | ]>}|j   rd tj |j  krd|j  kr|j qS )r  Zwow64)r   r  r  r   r   r   r  ru   rv   r     s
   r  c                 S   s(   g | ] }d t j|j kr|jqS )r  )r   r   r   r  r   ru   ru   rv   r     s   N)r  r  r  rK   r   r   r   r   r(  r)  r   rn  windllkernel32FreeLibraryZHMODULEZargtypesZ_handlerH   ZWinDLL)	rs  r  r  r  r  r
  cfiler  r]  ru   r  rv   r\     s0    








c                   C   s   t dd d S )NTr8  )r7   ru   ru   ru   rv   cleanup_test_procs  s    r  c                 C   s
   t | S r   )rq   exit)r)  r  ru   ru   rv   r    r  r  )N)F)TF)N)r>  N)N)N)rN  )rS  )r>  )r>  )r   rl  r  r  ri  rr  r   r  r~  rk  r   ri   r(  r  r  r  r   r%  rO  r`  ro   rq   rw  r   r   r   r<   r~  r   r   r   rt  r  r   r   r   r   r   r	   r
   r   r   Zpsutil._commonr   r   r   r   r   Zpsutil._psposixr   __all__builtin_module_namesr   r   r^   r   r2   r4   maxsizer"   r  r3   rw   r0   r1   r   r   r!   r   r   r  r   r   decoder   r  r  ZASCII_FSr   r   r   rj  __file__r   getr   r}  r  r   r#   r$   r%   rI  r'   r(   r/   ZHAS_NET_IO_COUNTERSr)   r&   r*   r+   r  r   r,   r   r-   r.   ZHAS_THREADSgetuidZSKIP_SYSCONSr   r   r   r  devnullr   rm  r  r   r    r   rv  r   r   r   r   r   r;   r   r8   r:   r9   r5   r!  rE  r6   r7   rM   rL   rK  r"  rO   r^  r   rP   rN   rH   ri  r  rG   rI   rJ   rK   rF   r  rA   r  r  r@   rD   rE   r*  rB   rC   r?   r=   r>   rS   rT   rU   rV   rW   rX   rQ   r|  rR   rY   rZ   r[   r]   r\   r  ru   ru   ru   rv   <module>   s*  
                                                   -





 






,
0)
&)

T
%9
	
"



M
  &l	 
F




K3
