a
    QIh                     @   s  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlZddlmZ ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! ddlm"Z" ddlm#Z# ddlm$Z$ ddlm%Z% ddlm&Z& ddlm'Z' ddlm(Z( ddlm)Z) ddlm*Z* ddlm+Z+ ddlm,Z, ddlm-Z- ddlm.Z. ddlm/Z/ ddlm0Z0 dd lm1Z1 dd!lm2Z2 dd"lm3Z3 dd#lm4Z4 dd$lm5Z5 dd%lm6Z6 dd&lm7Z7 dd'lm8Z8 dd(lm9Z9 dd)lm:Z: dd*lm;Z; G d+d, d,e.Z<G d-d. d.e.Z=dS )/Tests for psutil.Process class.    N)mock)AIX)BSD)LINUX)MACOS)NETBSD)OPENBSD)OSX)POSIX)WINDOWS)	open_text)
CI_TESTING)GITHUB_ACTIONS)GLOBAL_TIMEOUT)HAS_CPU_AFFINITY)HAS_ENVIRON)
HAS_IONICE)HAS_MEMORY_MAPS)HAS_PROC_CPU_NUM)HAS_PROC_IO_COUNTERS)
HAS_RLIMIT)HAS_THREADS)MACOS_11PLUS)PYPY)
PYTHON_EXE)PYTHON_EXE_ENV)PsutilTestCase)
ThreadTask)
call_until)copyload_shared_lib)create_c_exe)create_py_exe)process_namespace)pytestreap_children)retry_on_failure)sh)skip_on_access_denied)skip_on_not_implemented)wait_for_pidc                   @   sJ  e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	j
je dddd Zdd Ze	j
jedddd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Ze	j
je d#dd$d% Zd&d' Ze	j
je d(dd)d* Ze	j
je d#deed+d,d- Ze	j
je  d#de	j
je d.dd/d0 Z!e	j
je  d#de	j
je" d1dd2d3 Z#e	j
je$ d#dd4d5 Z%e	j
je$ d#dd6d7 Z&e	j
je$ d#dd8d9 Z'e	j
je$ d#dd:d; Z(e	j
je$ d#dd<d= Z)d>d? Z*e	j
je" d@ddAdB Z+e	j
je, d#ddCdD Z-e. e/e0d+e	j
je, d#ddEdF Z1e. dGdH Z2dIdJ Z3e	j
je4 d#ddKdL Z5e	j
je4 d#ddMdN Z6dOdP Z7dQdR Z8dSdT Z9dUdV Z:e	j
je;dWddXdY Z<dZd[ Z=e	j
je;d\dd]d^ Z>e	j
je d(dd_d` Z?e	j
je d(ddadb Z@dcdd ZAdedf ZBdgdh ZCdidj ZDdkdl ZEe	j
jeF d#ddmdn ZGe	j
jeF d#ddodp ZHe	j
jeF d#ddqdr ZIe	j
jeJdsddtdu ZKe	j
jeJdsddvdw ZLe	j
je d(ddxdy ZMeed+e	j
jeNpedzdd{d| ZOd}d~ ZPdd ZQdd ZRe. dd ZSdd ZTdd ZUdd ZVdd ZWdd ZXdd ZYdd ZZdd Z[dd Z\dd Z]dd Z^e	j
je d(ddd Z_e	j
je d(ddd Z`e	j
je d(ddd Zadd Zbdd Zce	j
jed d#ddd Zee	j
jed d#de	j
je d(de	j
jefdde	j
jedddd ZgdS )TestProcessr   c                 O   sF   | j |i |}zt|jW S  tjy@   | |j  Y n0 d S N)spawn_testprocpsutilProcesspidNoSuchProcessZassertPidGone)selfargskwargssproc r7   S/var/www/html/venv_bot_3.9/lib/python3.9/site-packages/psutil/tests/test_process.pyspawn_psprocL   s    zTestProcess.spawn_psprocc                 C   sN   t  }|jt ksJ tt d|_W d    n1 s@0    Y  d S )N!   )r/   r0   r1   osgetpidr$   raisesAttributeErrorr3   pr7   r7   r8   test_pidV   s    zTestProcess.test_pidc                 C   sJ   |   }|  | }tr,|tjks<J n|tj ks<J | | d S r-   )r9   killwaitr   signalSIGTERMSIGKILLassertProcessGoner3   r@   coder7   r7   r8   	test_kill\   s    zTestProcess.test_killc                 C   sJ   |   }|  | }tr,|tjks<J n|tj ks<J | | d S r-   )r9   	terminaterC   r   rD   rE   rG   rH   r7   r7   r8   test_terminatef   s    zTestProcess.test_terminatec                 C   sX   t r
tjntj}|  }|| | }tr<||ksJJ n|| ksJJ | | d S r-   )	r   rD   rF   rE   r9   send_signalrC   r   rG   )r3   sigr@   rI   r7   r7   r8   test_send_signalp   s    
zTestProcess.test_send_signalz	not POSIX)reasonc              	   C   s   t j}|  }tjdtdF ttj	 |
| W d    n1 sJ0    Y  W d    n1 sh0    Y  |  }tjdtdF ttj |
| W d    n1 s0    Y  W d    n1 s0    Y  d S )Nzpsutil.os.killside_effect)rD   rE   r9   r   patchProcessLookupErrorr$   r=   r/   r2   rM   PermissionErrorAccessDenied)r3   rN   r@   r7   r7   r8   test_send_signal_mocked{   s    Fz#TestProcess.test_send_signal_mockedc                 C   s   t ddg}| |}| }|dks(J | | t ddg}| j|tjd}| }|dks`J | | t ddg}| |}| }|dksJ | | t dd	g}| |}| }|dksJ | | d S )
N-cpassr   z1 / 0)stderr   zimport sys; sys.exit(5);   zimport os; os._exit(5);)r   r9   rC   rG   
subprocessPIPE)r3   cmdr@   rI   r7   r7   r8   test_wait_exited   s(    









zTestProcess.test_wait_exitedzfails on NETBSDc                 C   s  |   }tr|tj ttj |j	dd W d    n1 sF0    Y  |tj
 ttj |j	dd W d    n1 s0    Y  |tj |	 tj ksJ |	 tj ksJ n|  ttj |j	dd W d    n1 s0    Y  |  ttj |j	dd W d    n1 sD0    Y  |  |	 tjksjJ |	 tjks~J d S )NMbP?)timeout)r9   r   rM   rD   SIGSTOPr$   r=   r/   TimeoutExpiredrC   SIGCONTrE   suspendresumerK   r?   r7   r7   r8   test_wait_stopped   s(    **,,zTestProcess.test_wait_stoppedc                 C   s   |   \}}ttj |d W d    n1 s80    Y  ttj |d W d    n1 sn0    Y  |  |  | }| }tr|tj	 ksJ |d u sJ n|tj	ksJ |tj	ksJ d S )N{Gz?)
spawn_children_pairr$   r=   r/   rd   rC   rK   r   rD   rE   )r3   child
grandchildZ	child_retZgrandchild_retr7   r7   r8   test_wait_non_children   s    ((z"TestProcess.test_wait_non_childrenc                 C   s   |   }|  ttj |d W d    n1 s<0    Y  ttj |d W d    n1 sr0    Y  tt |d W d    n1 s0    Y  d S )Nri   r   )r9   namer$   r=   r/   rd   rC   
ValueErrorr?   r7   r7   r8   test_wait_timeout   s    ((zTestProcess.test_wait_timeoutc                 C   s   |   }ttj |d W d    n1 s40    Y  |  t t }t |k rz|d}W qW qR tjy   Y qR0 qR| 	dt
r|tj ksJ n|tjksJ | | d S )Nr   rb   )r9   r$   r=   r/   rd   rC   rB   timer   failr   rD   rF   rE   rG   )r3   r@   stop_atrI   r7   r7   r8   test_wait_timeout_nonblocking   s     (

z)TestProcess.test_wait_timeout_nonblockingc                 C   s   t  }|jdd |jdd tdD ]*}|jd d}t|tsFJ |dks(J q(tt |jdd W d    n1 s0    Y  d S )Nra   )intervald           rn   )	r/   r0   cpu_percentrange
isinstancefloatr$   r=   rp   )r3   r@   _percentr7   r7   r8   test_cpu_percent   s    zTestProcess.test_cpu_percentc                 C   sH   t jdd d&}t   |js&J W d    n1 s:0    Y  d S )Nzpsutil.cpu_count)Zreturn_value)r   rS   r/   r0   ry   called)r3   mr7   r7   r8   test_cpu_percent_numcpus_none   s    z)TestProcess.test_cpu_percent_numcpus_nonec              	   C   s   t   }|jdksJ ||jdks0J ||jdksBJ ||jdksTJ |trj|jdksjJ ||j	D ]}t
dt
t|| qpd S )Nrx   z%H:%M:%S)r/   r0   	cpu_timesusersystemchildren_userchildren_systemr   Ziowait_fieldsrr   strftime	localtimegetattr)r3   timesro   r7   r7   r8   test_cpu_times  s    
zTestProcess.test_cpu_timesc                 C   sN   dd }|  t   }t }| j|j|jdd | j|j|jdd d S )Nc                  S   s8   t  jd } t  j| k r4tdD ]}|dC }q$qd S )Ng?i    )r;   r   r   rz   )rt   xr7   r7   r8   	waste_cpu  s    z/TestProcess.test_cpu_times_2.<locals>.waste_cpu皙?)delta)r/   r0   r   r;   r   ZassertAlmostEqualr   r   )r3   r   abr7   r7   r8   test_cpu_times_2  s    zTestProcess.test_cpu_times_2znot supportedc                 C   sP   t  }| }|dksJ t  dkr4|dks4J | tt  v sLJ d S Nr   r[   )r/   r0   Zcpu_num	cpu_countrz   )r3   r@   numr7   r7   r8   test_cpu_num  s    zTestProcess.test_cpu_numc                 C   sb   |   }t }| }t|| }|dkrH| d| d| d| tdt|  d S )Nr   z
expected: z	, found: z, difference: z%Y %m %d %H:%M:%S)r9   rr   create_timeabsrs   r   r   )r3   r@   nowr   
differencer7   r7   r8   test_create_time%  s    zTestProcess.test_create_timez
POSIX onlyc                 C   sV   t   }|d urRztjtd}W n tyD   t	dY n0 ||ksRJ d S )Nttyzcan't rely on `tty` CLI)
r/   r0   terminalr;   pathrealpathr(   RuntimeErrorr$   skip)r3   r   r   r7   r7   r8   test_terminal7  s    zTestProcess.test_terminal)Zonly_ifc                 C   s  t  }| }ttd}|  W d    n1 s80    Y  | }tsts|j|jksbJ |j	|j	ksrJ t
r|j|jksJ |j|jksJ n |j|jksJ |j|jksJ | }t|  d$}|tdd d W d    n1 s0    Y  | }|j	|j	ksJ |j|jks.J |j|jks@J |j|jksRJ t
r||j|jksjJ |j|jks|J tt|D ]>}tr|dkrq|| dksJ || dksJ qd S )Nrbwbr   i@B asciir   r   )r/   r0   Zio_countersopenr   readr   r   Z
read_countZwrite_countr   Z
read_charsZwrite_chars
read_byteswrite_bytes
get_testfnwritebytesrz   len)r3   r@   Zio1fZio2ir7   r7   r8   test_io_countersC  s:    &2zTestProcess.test_io_countersz
linux onlyc                    sR   fdd}t   ts.  d t jks.J t jdks<J t jdksJJ t jdksXJ t jdksfJ   }| ||  t j t	  t jdfksJ t
t   jt jdd W d    n1 s0    Y   t j t	  t jdfksJ  jt jdd t	  t jdfks.J t
t   jt jd	d W d    n1 s`0    Y  z jt jdd W n t jy   Y n0 t
jtd
d  t jd W d    n1 s0    Y  t
jtd
d  t jd W d    n1 s0    Y  t
jtdd  jdd W d    n1 sD0    Y  d S )Nc                    s&   | \}}|t jkrd} || d S Nr   )r/   IOPRIO_CLASS_NONEionice)initZioclassvaluer@   r7   r8   cleanupn  s    
z.TestProcess.test_ionice_linux.<locals>.cleanupr   r[   r         r      zioclass accepts no valuematchz$'ioclass' argument must be specified)r/   r0   r   r   r   ZIOPRIO_CLASS_RTZIOPRIO_CLASS_BEZIOPRIO_CLASS_IDLE
addCleanuptupler$   r=   rp   rV   )r3   r   r   r7   r   r8   test_ionice_linuxk  s@    .0..zTestProcess.test_ionice_linuxz!not supported on this win versionc                 C   s*  t  }ts| t jksJ | }| |j| |t j | t jksRJ |t j | t jkspJ z|t j W n t j	y   Y n0 | t jksJ t
jtdd  |jt jdd W d    n1 s0    Y  t
jtdd  |t jd  W d    n1 s0    Y  d S )Nz&value argument not accepted on Windowsr   r[   r   zis not a valid priority)r/   r0   r   r   ZIOPRIO_NORMALr   ZIOPRIO_VERYLOWZ
IOPRIO_LOWZIOPRIO_HIGHrV   r$   r=   	TypeErrorrp   )r3   r@   r   r7   r7   r8   test_ionice_win  s(    .zTestProcess.test_ionice_winc                 C   s   dd l }tt }dd ttD }|s4J ||D ]}tt|}|dksRJ |t|v r|t||kspJ trvq8|||	|ksJ q8||}t
|dksJ |d dksJ |d dks8J q8d S )Nr   c                 S   s   g | ]}| d r|qS )ZRLIMIT)
startswith.0r   r7   r7   r8   
<listcomp>      z/TestProcess.test_rlimit_get.<locals>.<listcomp>r   rn   r[   )resourcer/   r0   r;   r<   dirr   r   rlimitZ	getrlimitr   )r3   r   r@   namesro   r   retr7   r7   r8   test_rlimit_get  s     

zTestProcess.test_rlimit_getc                 C   s   |   }|tjd |tjdks*J trntjtdd" tj	dd W d    n1 sd0    Y  tt |tjd W d    n1 s0    Y  d S )N)r\   r\   zcan't use prlimitr   r   )r\   r\   r\   )
r9   r   r/   ZRLIMIT_NOFILEr   r$   r=   rp   Z_psplatformr0   r?   r7   r7   r8   test_rlimit_set  s    0zTestProcess.test_rlimit_setc                 C   s:  t  }|  }|t j\}}z|t jd|f t|d}|d W d    n1 s^0    Y  tt	D}t|d}|d W d    n1 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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)r/   r0   r   r   RLIMIT_FSIZEr   r   r$   r=   OSErrorr   errnoZEFBIG)r3   r@   testfnsofthardr   excr7   r7   r8   test_rlimit  s    (FzTestProcess.test_rlimitc                 C   s   t  }|t j\}}z|t jd|f |t jt j|f t|  d}|d W d    n1 sn0    Y  W |t j||f |t j||fksJ n,|t j||f |t j||fksJ 0 d S )Nr   r   s   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)r/   r0   r   r   RLIM_INFINITYr   r   r   )r3   r@   r   r   r   r7   r7   r8   test_rlimit_infinity  s    *z TestProcess.test_rlimit_infinityc                 C   s<   t  }|t j\}}|t jks&J |t j||f d S r-   )r/   r0   r   r   r   )r3   r@   r   r   r7   r7   r8   test_rlimit_infinity_value  s    z&TestProcess.test_rlimit_infinity_valuec                 C   s   t  }tr:z| }W qB t jy6   tdY qB0 n| }t ( | }||d ksbJ W d    n1 sv0    Y  d S )N$on OpenBSD this requires root accessr[   )r/   r0   r	   num_threadsrV   r$   r   r   )r3   r@   step1step2r7   r7   r8   test_num_threads	  s    zTestProcess.test_num_threadszWINDOWS onlyc                 C   s   t  }| dksJ d S r   )r/   r0   Znum_handlesr?   r7   r7   r8   test_num_handles  s    zTestProcess.test_num_handlesc                 C   s   t  }tr:z| }W qB t jy6   tdY qB0 n| }t n | }t|t|d ksjJ |d }|j	|d ksJ |j
|d ksJ |j|d ksJ W d    n1 s0    Y  d S )Nr   r[   r   r   )r/   r0   r	   threadsrV   r$   r   r   r   id	user_timesystem_time)r3   r@   r   r   Zathreadr7   r7   r8   test_threads   s    zTestProcess.test_threadsc                 C   s   |   }tr8z|  W n tjy6   tdY n0 t| j	t
dd | D  dk sdJ t| jt
dd | D  dk sJ d S )Nr   c                 s   s   | ]}|j V  qd S r-   )r   r   r7   r7   r8   	<genexpr>?  r   z-TestProcess.test_threads_2.<locals>.<genexpr>r   c                 s   s   | ]}|j V  qd S r-   )r   r   r7   r7   r8   r   C  r   )r9   r	   r   r/   rV   r$   r   r   r   r   sumr   r?   r7   r7   r8   test_threads_24  s    ""zTestProcess.test_threads_2c                 C   s   t  }| d d \}}| }|dks0J |dks<J d gd }| d d \}}| }||ksnJ ||kszJ ||ksJ ~tr| }	|	j|	jksJ |	j|	jksJ | }	|	j	D ]}
t
|	|
dksJ qd S )Nr   r   i` )r/   r0   Zmemory_infomemory_percentr   ZrssZwsetvmsZpagefiler   r   )r3   r@   Zrss1Zvms1Zpercent1ZmemarrZrss2Zvms2Zpercent2memro   r7   r7   r8   test_memory_infoG  s&    

zTestProcess.test_memory_infoc                 C   s   t  }t  j}| }|jD ]8}t||}|dks:J |dkrFts trLq ||ks J q tsft	sft
rt|jdkstJ tr|jdksJ |jdksJ d S )Nr   r   )r/   r0   Zvirtual_memorytotalZmemory_full_infor   r   r
   r   r   r   ussZpssZswap)r3   r@   r   r   ro   r   r7   r7   r8   test_memory_full_infof  s    


z!TestProcess.test_memory_full_infoc           
      C   s  t  }| }t|tt|ks(J |jdd}|D ]}|jdrLq8tr\|jdkr\q8tj	|jstJ |jt
rz*tj|jstj|jsJ |jW nZ ty   ts td}| }W d    n1 s0    Y  |j d|vr Y n0 q8dtj|jvr8zt|j}W n ty8   Y q80 t|js8J |jq8|D ]h}|jD ]Z}t||}	|dkr|q`|d	v r|	sJ |	n"t|	tsJ |	d
ks`J |	q`qVd S )NF)grouped[Zpvclockz/proc/self/smapsz
 (deleted)Z64r   >   addrZpermsr   )r/   r0   memory_mapsr   setr   r   r   r;   isabsr   existsislinkAssertionErrorr   r   r   basenamestatFileNotFoundErrorS_ISREGst_moder   r   r{   int)
r3   r@   mapsZext_mapsntr   datastfnamer   r7   r7   r8   test_memory_mapsv  sN    

&




zTestProcess.test_memory_mapsc                    s`   t  }t >}dd   fdd| D } ||v s>J W d    n1 sR0    Y  d S )Nc                 S   s   t jt j| S r-   )r;   r   r   normcaser   r7   r7   r8   normpath  s    z8TestProcess.test_memory_maps_lists_lib.<locals>.normpathc                    s   g | ]} |j qS r7   )r   r   r	  r7   r8   r     r   z:TestProcess.test_memory_maps_lists_lib.<locals>.<listcomp>)r/   r0   r    r   )r3   r@   r   Zlibpathsr7   r
  r8   test_memory_maps_lists_lib  s
    z&TestProcess.test_memory_maps_lists_libc                 C   sb   t  }|  tt |jdd W d    n1 s<0    Y  tsRtsRtr^|jdd d S )Nz?!?)Zmemtyper   )	r/   r0   r   r$   r=   rp   r   r   r   r?   r7   r7   r8   test_memory_percent  s    *zTestProcess.test_memory_percentc                 C   sL   |   }| sJ | s J |  |  | r<J | rHJ d S r-   )r9   
is_runningrB   rC   r?   r7   r7   r8   test_is_running  s    zTestProcess.test_is_runningc                 C   s   |   }| }z|tksJ W n ty   tr`t|ttkr`tjj}|||tksJ nNt	j
d  dt	j
d  }z ||dt|dksJ W n ty   Y n0 Y n0 t|ddg}|dksJ d S )Nr   .r[    rX   zimport os; print('hey')Zhey)r9   exer   r   r   r   r;   r   r  sysversion_inforeplacer(   )r3   r@   r  r  veroutr7   r7   r8   test_exe  s     zTestProcess.test_exec                 C   s   t ddg}| |}tr.| g kr.tdts:ts:trP| d t ksJ nhtrt	r| d }|t krd
| dd  d
|dd  ksJ d S d
| d
|ksJ d S )NrX   2import time; [time.sleep(0.1) for x in range(100)]OPENBSD: returned EBUSYr    r[   )r   r9   r   cmdliner$   r   r	   r   r   r   join)r3   r  r@   pyexer7   r7   r8   test_cmdline  s    

,zTestProcess.test_cmdlinezbroken on PYPYc                 C   s   t g}|dgd  |ddg | |}trhz| |ksDJ W q tjyd   tdY q0 n*| }t	r|g krtd||ksJ d S )Nz-v2   rX   r  z#OPENBSD: process turned into zombier  )
r   extendr9   r	   r  r/   ZombieProcessr$   r   r   )r3   r  r@   r   r7   r7   r8   test_long_cmdline  s    

zTestProcess.test_long_cmdlinec                 C   sH   |   }|  }tjtjtj }|	|sDJ ||fd S r-   )
r9   ro   lowerr;   r   r   r   r  
executabler   )r3   r@   ro   r  r7   r7   r8   	test_name  s    zTestProcess.test_namezunreliable on PYPYc                 C   s   t | jtjd d}|ddg}| |}trz| tj	|ksHJ W q t
y   | tjkrtj	|| sJ n Y q0 n| tj	|ksJ d S )Nr   )suffixrX   r  )r"   r   stringdigitsr9   r	   ro   r;   r   r   r   statusr/   STATUS_ZOMBIEr   )r3   r  r  r@   r7   r7   r8   test_long_name  s    

zTestProcess.test_long_namec                 C   sX   t  }| \}}}|t ks&J |t ks6J ttdrTt | ksTJ d S N	getresuid)r/   r0   uidsr;   getuidgeteuidhasattrr-  r3   r@   realZ	effectiveZ_savedr7   r7   r8   	test_uidsD  s    
zTestProcess.test_uidsc                 C   sX   t  }| \}}}|t ks&J |t ks6J ttdrTt | ksTJ d S r,  )r/   r0   gidsr;   getgidgetegidr1  	getresgidr2  r7   r7   r8   	test_gidsQ  s    
zTestProcess.test_gidsc              
      s   fdd}t   tt  d W d    n1 s>0    Y    }| || tr(d }t jt j	t j
t jt jt jfD ]}| j|d z | W n t jy   Y nN0   }|t jt jt jhv r||ks|d u r|}||ksJ n||ksJ W d    q1 s0    Y  qnzttdrVttjt   ksVJ  d   dksrJ ttdrttjt   ksJ ts d   dksJ W n t jy   Y n0 d S )Nc                    s(   z  |  W n tjy"   Y n0 d S r-   )nicer/   rV   )r   r   r7   r8   r   _  s    z&TestProcess.test_nice.<locals>.cleanupstr)priogetpriorityr[   r   )r/   r0   r$   r=   r   r:  r   r   ZIDLE_PRIORITY_CLASSZBELOW_NORMAL_PRIORITY_CLASSZNORMAL_PRIORITY_CLASSZABOVE_NORMAL_PRIORITY_CLASSZHIGH_PRIORITY_CLASSZREALTIME_PRIORITY_CLASSsubTestrV   r1  r;   r=  PRIO_PROCESSr<   r   )r3   r   r   Zhighest_prior<  Znew_prior7   r   r8   	test_nice^  s`    (2

zTestProcess.test_nicec                 C   s   t  }| t jksJ d S r-   )r/   r0   r)  ZSTATUS_RUNNINGr?   r7   r7   r8   test_status  s    zTestProcess.test_statusc                 C   s|   |   }| }trh|d\}}t }|dr>td||ksJJ dt	j
v rx|t	j
d ksxJ n|t ksxJ d S )N\$zrunning as service accountZ
USERDOMAIN)r9   usernamer   splitgetpassgetuserendswithr$   r   r;   environ)r3   r@   rD  domainZgetpass_userr7   r7   r8   test_username  s    


zTestProcess.test_usernamec                 C   s    |   }| t ksJ d S r-   )r9   cwdr;   getcwdr?   r7   r7   r8   test_cwd  s    zTestProcess.test_cwdc                    s(   t ddg}| | t fdd d S )NrX   zFimport os, time; os.chdir('..'); [time.sleep(0.1) for x in range(100)]c                      s      tjt kS r-   )rL  r;   r   dirnamerM  r7   r   r7   r8   <lambda>  r   z(TestProcess.test_cwd_2.<locals>.<lambda>)r   r9   r   )r3   r_   r7   r   r8   
test_cwd_2  s    
zTestProcess.test_cwd_2c                 C   s  t  }| }|sJ || |j| ttdrL|tt|jksLJ t	|t	t
|ksdJ ttt	t jdd}|D ]j}||g | |gksJ ttdr| tt|jksJ t|dr| d | ksJ q|g  tr| |j ks(J n| |ks(J ttdrR| tt|jksRJ tt |d W d    n1 s~0    Y  || |t
| |t| d S )Nsched_getaffinityTZpercpunum_cpur   r[   )r/   r0   cpu_affinityr   r1  r;   listrR  r1   r   r   rz   ry   rT  r   _procZ_get_eligible_cpusr$   r=   r   r   )r3   r@   initialZall_cpusnr7   r7   r8   test_cpu_affinity  s4    



*
zTestProcess.test_cpu_affinityc                 C   s  |   }ttjddd g}tt || W d    n1 sH0    Y  tt  |tdd W d    n1 s0    Y  tt	tf |ddg W d    n1 s0    Y  tt |ddg W d    n1 s0    Y  d S )	NTrS  
   i'  i*  r   1rn   )
r9   r   r/   r   r$   r=   rp   rU  rz   r   )r3   r@   Zinvalid_cpur7   r7   r8   test_cpu_affinity_errs  s    (.,z"TestProcess.test_cpu_affinity_errsc                 C   s   t  }| }|sJ || |j| t|dkrB|d d }g }tt|d D ] }|dd t||D  qV|D ]&}|| t	| t	|ks|J q|d S )N   r[   c                 s   s   | ]}|rt |V  qd S r-   )rV  )r   Zsubsetr7   r7   r8   r     s   zATestProcess.test_cpu_affinity_all_combinations.<locals>.<genexpr>)
r/   r0   rU  r   r   rz   r   	itertoolscombinationssorted)r3   r@   rX  Zcombosr   Zcombor7   r7   r8   "test_cpu_affinity_all_combinations  s    



z.TestProcess.test_cpu_affinity_all_combinationszbroken on BSDc                    sv  t  |  }  | vs$J t|d}|d |  t fdd   dd  D }tj	
||v s~J tr D ]}|j	|kr|jdksJ qW d    n1 s0    Y   D ]}tj	|j	sJ |qd| d	}| td
|gtdD ]2}dd  D }||v r, qPtd qtj	
||v sPJ |D ]}tj	|sTJ |qTd S )Nr   s   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxc                      s   t  t  kS r-   )r   
open_filesr7   filesr@   r7   r8   rP    r   z-TestProcess.test_open_files.<locals>.<lambda>c                 S   s   g | ]}t j|jqS r7   r;   r   r  r   r7   r7   r8   r     r   z/TestProcess.test_open_files.<locals>.<listcomp>r   zimport time; f = open(r'z/', 'r'); [time.sleep(0.1) for x in range(100)];rX   rw   c                 S   s   g | ]}t j|jqS r7   rf  r   r7   r7   r8   r   -  r   ri   )r/   r0   r   rc  r   r   flushr   r;   r   r  r   positionisfiler9   r   rz   rr   sleep)r3   r   r   	filenamesfiler  r   r7   rd  r8   test_open_files  s8    

.

zTestProcess.test_open_filesc                 C   s  t  }tjj}|  }t|d}| D ]*}||j||jksR|j	|
 kr, qlq,| d| ||j||jksJ tr|j	dksJ n|j	|
 ksJ | d }|d |jksJ |d |j	ksJ |j| vsJ W d    n1 s0    Y  d S )Nwzno file found; files=rn   r   r[   )r/   r0   r;   r   r  r   r   rc  ro   fdfilenors   r   )r3   r@   r  r   fileobjrl  Zntupler7   r7   r8   test_open_files_27  s&    zTestProcess.test_open_files_2c                 C   s   t  }|  }| }t|d}| |j | |d ksBJ t }| |j | |d ksjJ |  |  | |ksJ d S )Nrn  r[   r   )r/   r0   r   Znum_fdsr   r   closesocket)r3   r@   r   startrl  sockr7   r7   r8   test_num_fdsR  s    
zTestProcess.test_num_fdsz not reliable on OPENBSD & NETBSDc                 C   sT   t  }t| }tdD ](}td t| }||kr d S q| dd S )Nr   g?z2num ctx switches still the same after 2 iterations)r/   r0   r   Znum_ctx_switchesrz   rr   rj  rs   )r3   r@   beforer}   afterr7   r7   r8   test_num_ctx_switchesa  s    
z!TestProcess.test_num_ctx_switchesc                 C   sF   t  }ttdr&| t ks&J |  }| t ksBJ d S )Ngetppid)r/   r0   r1  r;   ppidr{  r9   r<   r?   r7   r7   r8   	test_ppido  s
    
zTestProcess.test_ppidc                 C   sD   |   }| jt ksJ t d }t| d u s@J d S r   )r9   parentr1   r;   r<   r/   pidsr0   )r3   r@   Z
lowest_pidr7   r7   r8   test_parentv  s    zTestProcess.test_parentc                 C   s8   t  }|  \}}| |ks$J | |ks4J d S r-   )r/   r0   rj   r~  r3   r~  rk   rl   r7   r7   r8   test_parent_multi}  s    zTestProcess.test_parent_multic                 C   s`   t  }| sJ |  \}}| d |ks4J | d |ksHJ | d |ks\J d S r   )r/   r0   parentsrj   r  r7   r7   r8   test_parents  s    zTestProcess.test_parentsc                 C   s   t  }| rJ |jddr$J | jdd}| }|jdd}||fD ]>}t|dks`J |d j|jkstJ |d  |jksLJ qLd S )NT	recursiver   )creationflagsr[   )r/   r0   childrenr9   r   r1   r|  )r3   r~  rk   Z	children1Z	children2r  r7   r7   r8   test_children  s    zTestProcess.test_childrenc                 C   sb   t  }|  \}}| |gks&J |jdd||gks>J |  |  |jddr^J d S )NTr  )r/   r0   rj   r  rK   rC   r  r7   r7   r8   test_children_recursive  s    z#TestProcess.test_children_recursivec              	   C   s   t t}t D ]2}z||   d7  < W q tjyB   Y q0 qt| dd dd }t	rt|dkrtt
dt|}z|jdd}W n tjy   Y n0 t|tt|ksJ d S )	Nr[   c                 S   s   | d S )Nr[   r7   )r   r7   r7   r8   rP    r   z6TestProcess.test_children_duplicates.<locals>.<lambda>)keyr   zPID 0Tr  )collectionsdefaultdictr  r/   process_iterr|  Errormaxitemsr   r$   r   r0   r  rV   r   r   )r3   tabler@   r1   cr7   r7   r8   test_children_duplicates  s    


z$TestProcess.test_children_duplicatesc                 C   s|   t  }|  \}}|jdd}t|dks0J |d |ks@J |d |ksPJ | }|d |kshJ |d |ksxJ d S )NTr  r   r   r[   )r/   r0   rj   r  r   r  )r3   r~  rk   rl   r  r  r7   r7   r8   test_parents_and_children  s    z%TestProcess.test_parents_and_childrenc                 C   sX   |   }|  tdD ] }| tjkr. q:td q|  | tjksTJ d S )Nrw   ri   )	r9   rf   rz   r)  r/   ZSTATUS_STOPPEDrr   rj  rg   )r3   r@   r}   r7   r7   r8   test_suspend_resume  s    zTestProcess.test_suspend_resumec                 C   sl   t t td W d    n1 s*0    Y  t t td W d    n1 s^0    Y  d S )Nr\  rn   )r$   r=   r   r/   r0   rp   r3   r7   r7   r8   test_invalid_pid  s    (zTestProcess.test_invalid_pidc              	   C   s  t  }|jddgd}t| ddgks0J t tt  }|jdgdd}t|d tsp|d dkspJ t	j
ddt jd	, |jd
gddd
diksJ W d    n1 s0    Y  t	j
ddt |jdd	L tt j |jd
gd W d    n1 s0    Y  W d    n1 s,0    Y  t	j
ddt |jdd	. |jd
gddd
dikspJ W d    n1 s0    Y  t	j
ddtd	h | }d
t| vsJ tt |jd
gd W d    n1 s0    Y  W d    n1 s0    Y  tt |d W d    n1 sF0    Y  tt |dg W d    n1 s~0    Y  tt |ddg W d    n1 s0    Y  d S )Nr  ro   )attrsZnet_connectionsfoo)r  Zad_valuezpsutil.Process.niceT)createrR   r:  r[   bar)r/   r0   as_dictra  keysminr  r{   rV  r   rS   rV   r2   r1   r$   r=   r!  NotImplementedErrorr   rp   )r3   r@   dr7   r7   r8   test_as_dict  sJ    :N>N*,zTestProcess.test_as_dictc              	   C   s   t  }tdV}|   |  |  W d    n1 sB0    Y  |jdksZJ W d    n1 sn0    Y  td }|  |  W d    n1 s0    Y  |jdksJ d S )N$psutil._psplatform.Process.cpu_timesr[   r   r/   r0   r   rS   oneshotr   Z
call_countr3   r@   r   r7   r7   r8   test_oneshot  s    
&,&zTestProcess.test_oneshotc                 C   s8  t  }td}td}| X |  |  |   |  |  W d    n1 sh0    Y  W d    n1 s0    Y  |jdksJ |jdksJ W d    n1 s0    Y  W d    n1 s0    Y  td }|  |  W d    n1 s0    Y  |jdks4J d S )Nr  z(psutil._psplatform.Process.oneshot_enterr[   r   r  )r3   r@   m1m2r   r7   r7   r8   test_oneshot_twice  s    

DJ(zTestProcess.test_oneshot_twicec                 C   s   |   \}}| }| }||ks(J | 0 | |ksBJ | |ksRJ W d    n1 sf0    Y  | 0 | |ksJ | |ksJ W d    n1 s0    Y  d S r-   )rj   r|  r  )r3   p1p2Zp1_ppidZp2_ppidr7   r7   r8   test_oneshot_cache.  s    
.
zTestProcess.test_oneshot_cachec                    sn   fdd}         tr8t fdd   t }||jD ]\}}||| qVd S )Nc                    s   z
|  }W nN t jy     Y n` t jy2   Y nN t jyX   trR|dv rRY d S  Y n(0 trj|dv rjd S  | d|dd S )N>   r   r   >   r  ro   z didn't raise NSP and returned z instead)r/   r!  r2   rV   r	   r   rs   )funZfun_namer   r  r7   r8   assert_raises_nspE  s    
zFTestProcess.test_halfway_terminated_process.<locals>.assert_raises_nspc                      s    j t vS r-   )r1   r/   r  r7   r   r7   r8   rP  \  r   z=TestProcess.test_halfway_terminated_process.<locals>.<lambda>)	r9   rK   rC   r   r   rG   r#   iterall)r3   r  nsr  ro   r7   )r@   r3   r8   test_halfway_terminated_process=  s    
z+TestProcess.test_halfway_terminated_processc                 C   s   |   \}}| | d S r-   )Zspawn_zombieZassertProcessZombie)r3   _parentzombier7   r7   r8   test_zombie_processc  s    zTestProcess.test_zombie_processc                 C   sV   t  }tjdt dd&}| s*J |js4J W d    n1 sH0    Y  d S )Nzpsutil.Processr   rQ   )r/   r0   r   rS   r!  r  r   r  r7   r7   r8   $test_zombie_process_is_running_w_exch  s    
z0TestProcess.test_zombie_process_is_running_w_excc                 C   s\   t  }tjdt dd,}| t jks0J |js:J W d    n1 sN0    Y  d S )Nz!psutil._psplatform.Process.statusr   rQ   )r/   r0   r   rS   r!  r)  r*  r   r  r7   r7   r8    test_zombie_process_status_w_excs  s    z,TestProcess.test_zombie_process_status_w_excc              
   C   s  |   }t|j}|j| d f|_tt  |jtjv sDJ |	 rPJ t
jtjddJ tt }tt  W d    n1 s0    Y  W d    n1 s0    Y  d|j | v sJ |jtjvsJ |t|jksJ d}t|}|j|j|j ddD ]p\}}| j|dH tjtj|d	 |  W d    n1 s^0    Y  W d    n1 s~0    Y  qd
t|v sJ d
t|v sJ tjtj|d	 |  W d    n1 s0    Y  tjtj|d	 |  W d    n1 s0    Y  tjtj|d	 |  W d    n1 sT0    Y  tjtj|d	 |   W d    n1 s0    Y  d S )Nrw   ZPSUTIL_DEBUGTz+refreshing Process instance for reused PID z4process no longer exists and its PID has been reusedF)clear_cache)ro   r   zterminated + PID reused)!r.   r/   r0   r1   r   _identrV  r  Z_pmapr  r   rS   object_common
contextlibredirect_stderrioStringIOgetvaluer#   r  settersZkillersr>  r$   r=   r2   r;  reprr|  r~  r  r  )r3   Zsubpr@   r   msgr  r  ro   r7   r7   r8   test_reused_pid  s>    H
J(((zTestProcess.test_reused_pidc              	   C   s  dt  vrjtt j t d W d    n1 s80    Y  t drPJ t d dksfJ d S t d}tr~t j	nt
}t| |  W d    n1 s0    Y  t| |  W d    n1 s0    Y  t| |  W d    n1 s0    Y  t| |  W d    n1 sD0    Y  t| |  W d    n1 sx0    Y  t| |tj W d    n1 s0    Y  t|}||j|j D ]\}}z
| }W n t j	y   Y nb0 |dv r|jdks^J nD|dkrFtr.dnd}| |ks^J n|dkr|sJ |qtsdt  v szJ t dsJ d S )Nr   r[   >   r.  r5  rD  zNT AUTHORITY\SYSTEMrootro   )r/   r  r$   r=   r2   r0   Z
pid_existsr|  r   rV   rp   rC   rK   rf   rg   rB   rM   rD   rE   r#   r  Zgettersr  r3  rD  r	   )r3   r@   r   r  r  ro   r   r   r7   r7   r8   
test_pid_0  sH    (
&&(((,



zTestProcess.test_pid_0c                 C   sH   dd }d | _ t }|| }|tj }tsDtrD||ksDJ d S )Nc                 S   sB   g d}t r|g d |D ]}| |d  qdd |  D S )N)PLATHOMEZPYTEST_CURRENT_TESTZPYTEST_VERSION)Z__CF_USER_TEXT_ENCODINGZVERSIONER_PYTHON_PREFER_32_BITVERSIONER_PYTHON_VERSIONr  c                 S   s6   i | ].\}}| d d dd| d d ddqS )r  
)r  )r   kvr7   r7   r8   
<dictcomp>  s   z@TestProcess.test_environ.<locals>.clean_dict.<locals>.<dictcomp>)r   r   popr  )r  excludero   r7   r7   r8   
clean_dict  s    z,TestProcess.test_environ.<locals>.clean_dict)ZmaxDiffr/   r0   rI  r;   copyr
   r   )r3   r  r@   Zd1Zd2r7   r7   r8   test_environ  s    zTestProcess.test_environz<macOS 11+ can't get another process environment, issue #2084z(sometimes fails on `assert is_running()`c                 C   s   t d}t|  |d}| j|gtjtjd}t|j	}t
|j	 | sRJ |j dksdJ trtrz| }W q tjy   Y d S 0 n| }|dddksJ |  |jdksJ d S )	Na  
            #include <unistd.h>
            #include <fcntl.h>

            char * const argv[] = {"cat", 0};
            char * const envp[] = {"A=1", "X", "C=3", 0};

            int main(void) {
                // Close stderr on exec so parent can wait for the
                // execve to finish.
                if (fcntl(2, F_SETFD, FD_CLOEXEC) != 0)
                    return 0;
                return execve("/bin/cat", argv, envp);
            }
            )Zc_code)stdinrZ   r   r\  3)ACr   )textwrapdedentr!   r   r.   r]   r^   r/   r0   r1   r+   r  rZ   r   r   r   rI  rV   communicate
returncode)r3   rI   Zcexer6   r@   envr7   r7   r8   test_weird_environ  s$    


zTestProcess.test_weird_environN)h__name__
__module____qualname____doc__r9   rA   rJ   rL   rO   r$   markZskipifr   rW   r`   r   rh   rm   rq   ru   r   r   r   r   r   r   r   r   r   r*   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r'   r)   r   r   r   r   r   r  r  r  r  r  r  r   r"  r%  r+  r4  r9  r@  rA  rK  rN  rQ  r   rZ  r]  rb  r   rm  rr  rw  r	   rz  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r   r  r7   r7   r7   r8   r,   I   s  







&+








0
	

-

B
&


&





4&



).
r,   c                   @   s<   e Zd ZdZedd Zdd Zdd Zdd	 Zd
d Z	dS )	TestPopenzTests for psutil.Popen class.c                 C   s
   t   d S r-   r%   )clsr7   r7   r8   tearDownClass*  s    zTestPopen.tearDownClassc              	   C   s   t ddg}tj|tjtjtdj}|  |  |j t	|sDJ t
t |j W d    n1 sj0    Y  |  W d    n1 s0    Y  tr|dtj ksJ n|dtjksJ d S )NrX   3import time; [time.sleep(0.1) for x in range(100)];stdoutrZ   r  r\   )r   r/   Popenr]   r^   r   ro   r   r  r   r$   r=   r>   r  rK   r   rC   rD   rE   r3   r_   procr7   r7   r8   	test_misc.  s*    $&zTestPopen.test_miscc                 C   s|   t jtdgtjtjtjtd}|  W d    n1 s<0    Y  |jjsRJ |j	js^J |j
jsjJ |jdksxJ d S )Nz-V)r  rZ   r  r  r   )r/   r  r   r]   r^   r   r  r  closedrZ   r  r  )r3   r  r7   r7   r8   test_ctx_managerI  s    &zTestPopen.test_ctx_managerc              	   C   sr  t ddg}tj|tjtjtd:}|  |  t	tj
 |  W d    n1 s^0    Y  t	tj
 |  W d    n1 s0    Y  t	tj
 |tj W d    n1 s0    Y  trNt	tj
 |tj W d    n1 s
0    Y  t	tj
 |tj W d    n1 sD0    Y  W d    n1 sd0    Y  d S NrX   r  r  )r   r/   r  r]   r^   r   rK   rC   r$   r=   r2   rB   rM   rD   rE   r   ZCTRL_C_EVENTZCTRL_BREAK_EVENTr  r7   r7   r8   test_kill_terminateW  s0    &&*,zTestPopen.test_kill_terminatec              	   C   s   t ddg}tj|tjtjtdP}|  |  t	t
 |j W d    n1 sX0    Y  W d    n1 sv0    Y  d S r  )r   r/   r  r]   r^   r   rK   rC   r$   r=   r>   r  r  r7   r7   r8   test__getattribute__t  s    zTestPopen.test__getattribute__N)
r  r  r  r  classmethodr  r  r  r  r  r7   r7   r7   r8   r  '  s   
r  )>r  r  r  r   rF  r  r_  r;   rD   rt  r   r'  r]   r  r  rr   Zunittestr   r/   r   r   r   r   r   r	   r
   r   r   Zpsutil._commonr   Zpsutil.testsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r&   r'   r(   r)   r*   r+   r,   r  r7   r7   r7   r8   <module>   s              i