U
    !h                     @   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                   @   sT  e Zd ZdZdd Zdd Zdd Zdd	 Zej	j
e d
ddd Zdd Zej	j
edddd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zej	j
e d!dd"d# Zd$d% Zej	j
e d&dd'd( Zej	j
e d!deed)d*d+ Zej	j
e d!dej	j
e d,dd-d. Z ej	j
e d!dej	j
e! d/dd0d1 Z"ej	j
e# d!dd2d3 Z$ej	j
e# d!dd4d5 Z%ej	j
e# d!dd6d7 Z&ej	j
e# d!dd8d9 Z'ej	j
e# d!dd:d; Z(ej	j)d<d=d>d? Z*ej	j
e! d@ddAdB Z+ej	j
e, d!ddCdD Z-e. e/e0d)ej	j
e, d!ddEdF Z1e. dGdH Z2dIdJ Z3ej	j
e4 d!ddKdL Z5ej	j
e4 d!ddMdN Z6dOdP Z7dQdR Z8dSdT Z9dUdV Z:dWdX Z;dYdZ Z<e. d[d\ Z=ej	j
e d&dd]d^ Z>ej	j
e d&dd_d` Z?dadb Z@dcdd ZAdedf ZBdgdh ZCdidj ZDej	j
eE d!ddkdl ZFej	j
eE d!ddmdn ZGej	j
eE d!ddodp ZHej	j
eIdqddrds ZJej	j
eIdqddtdu ZKej	j
e d&dej	j)d<d=dvdw ZLeed)ej	j
eMpedxddydz ZNd{d| ZOd}d~ ZPdd ZQdd ZRe. dd ZSdd ZTdd ZUdd ZVdd ZWdd ZXdd ZYdd ZZdd Z[dd Z\dd Z]dd Z^dd Z_ej	j
e d&ddd Z`ej	j
e d&ddd Zaej	j
e d&ddd Zbdd Zcdd Zdej	j
ee d!ddd Zfej	j
ee d!dej	j
e d&dej	j
egddej	j
edddd ZhdS )TestProcessr   c              	   C   s:   t  }|jt ksttt d|_W 5 Q R X d S )N!   )	psutilProcesspidosgetpidAssertionErrorr$   raisesAttributeErrorselfp r9   ?./venv/lib/python3.8/site-packages/psutil/tests/test_process.pytest_pidK   s    zTestProcess.test_pidc                 C   sJ   |   }|  | }tr,|tjks<tn|tj ks<t| | d S N)	spawn_psprockillwaitr   signalSIGTERMr3   SIGKILLassert_proc_goner7   r8   coder9   r9   r:   	test_killQ   s    zTestProcess.test_killc                 C   sJ   |   }|  | }tr,|tjks<tn|tj ks<t| | d S r<   )r=   	terminater?   r   r@   rA   r3   rC   rD   r9   r9   r:   test_terminate[   s    zTestProcess.test_terminatec                 C   sX   t r
tjntj}|  }|| | }tr<||ksJtn|| ksJt| 	| d S r<   )
r   r@   rB   rA   r=   send_signalr?   r   r3   rC   )r7   sigr8   rE   r9   r9   r:   test_send_signale   s    
zTestProcess.test_send_signalz	not POSIXreasonc              
   C   s   t j}|  }tjdtd( ttj	 |
| W 5 Q R X W 5 Q R X |  }tjdtd( ttj |
| W 5 Q R X W 5 Q R X d S )Nzpsutil.os.killside_effect)r@   rA   r=   r   patchProcessLookupErrorr$   r4   r.   NoSuchProcessrI   PermissionErrorAccessDenied)r7   rJ   r8   r9   r9   r:   test_send_signal_mockedp   s    z#TestProcess.test_send_signal_mockedc                 C   s   t ddg}| |}| }|dks(t| | t ddg}| j|tjd}| }|dks`t| | t ddg}| |}| }|dkst| | t dd	g}| |}| }|dkst| | d S )
N-cpassr   z1 / 0)stderr   zimport sys; sys.exit(5);   zimport os; os._exit(5);)r   r=   r?   r3   rC   
subprocessPIPE)r7   cmdr8   rE   r9   r9   r:   test_wait_exited}   s(    









zTestProcess.test_wait_exitedzfails on NETBSDc              	   C   s.  |   }tr|tj ttj |j	dd W 5 Q R X |tj
 ttj |j	dd W 5 Q R X |tj |	 tj kst|	 tj kstn|  ttj |j	dd W 5 Q R X |  ttj |j	dd W 5 Q R X |  |	 tjkst|	 tjks*td S )NMbP?)timeout)r=   r   rI   r@   SIGSTOPr$   r4   r.   TimeoutExpiredr?   SIGCONTrA   r3   suspendresumerG   r6   r9   r9   r:   test_wait_stopped   s(    zTestProcess.test_wait_stoppedc              	   C   s   |   \}}ttj |d W 5 Q R X ttj |d W 5 Q R X |  |  | }| }tr|tj	 kst
|d kst
n|tj	kst
|tj	kst
d S )N{Gz?)spawn_children_pairr$   r4   r.   rb   r?   rG   r   r@   rA   r3   )r7   child
grandchildZ	child_retZgrandchild_retr9   r9   r:   test_wait_non_children   s    z"TestProcess.test_wait_non_childrenc              	   C   sx   |   }|  ttj |d W 5 Q R X ttj |d W 5 Q R X tt |d W 5 Q R X d S )Nrg   r   )r=   namer$   r4   r.   rb   r?   
ValueErrorr6   r9   r9   r:   test_wait_timeout   s    zTestProcess.test_wait_timeoutc              	   C   s   |   }ttj |d W 5 Q R X |  t t }t |k rvz|d}W qW q> tjk
rr   Y q>X q>t	dt
r|tj kstn|tjkst| | d S )Nr   r`   )r=   r$   r4   r.   rb   r?   r>   timer   failr   r@   rB   r3   rA   rC   )r7   r8   stop_atrE   r9   r9   r:   test_wait_timeout_nonblocking   s     

z)TestProcess.test_wait_timeout_nonblockingc              	   C   sz   t  }|jdd |jdd tdD ]*}|jd d}t|tsFt|dks(tq(tt	 |jdd W 5 Q R X d S )Nr_   )intervald           rl   )
r.   r/   cpu_percentrange
isinstancefloatr3   r$   r4   rn   )r7   r8   _percentr9   r9   r:   test_cpu_percent   s    zTestProcess.test_cpu_percentc              	   C   s4   t jdd d}t   |js&tW 5 Q R X d S )Nzpsutil.cpu_count)Zreturn_value)r   rP   r.   r/   rw   calledr3   )r7   mr9   r9   r:   test_cpu_percent_numcpus_none   s    z)TestProcess.test_cpu_percent_numcpus_nonec              	   C   s   t   }|jdkst||jdks0t||jdksBt||jdksTt|trj|j	dksjt||j
D ]}tdtt|| qpd S )Nrv   z%H:%M:%S)r.   r/   	cpu_timesuserr3   systemchildren_userchildren_systemr   Ziowait_fieldsrp   strftime	localtimegetattr)r7   timesrm   r9   r9   r:   test_cpu_times   s    
zTestProcess.test_cpu_timesc                 C   sV   dd }|  t   }t }t|j|j dk s:tt|j|j dk sRtd S )Nc                  S   s8   t  jd } t  j| k r4tdD ]}|dC }q$qd S )Ng?順    )r1   r   r   rx   )rr   xr9   r9   r:   	waste_cpu  s    z/TestProcess.test_cpu_times_2.<locals>.waste_cpu皙?)	r.   r/   r   r1   r   absr   r3   r   )r7   r   abr9   r9   r:   test_cpu_times_2  s    zTestProcess.test_cpu_times_2znot supportedc                 C   sP   t  }| }|dkstt  dkr4|dks4t| tt  ksLtd S Nr   rY   )r.   r/   Zcpu_numr3   	cpu_countrx   )r7   r8   Znumr9   r9   r:   test_cpu_num  s    zTestProcess.test_cpu_numc                 C   sB   |   }t }t| | dk s(ttdt|  d S )Nr   z%Y %m %d %H:%M:%S)r=   rp   r   create_timer3   r   r   )r7   r8   Znowr9   r9   r:   test_create_time  s    zTestProcess.test_create_timez
POSIX onlyc                 C   sX   t   }|d k	rTztjtd}W n tk
rF   t	dY nX ||ksTt
d S )Nttyzcan't rely on `tty` CLI)r.   r/   terminalr1   pathrealpathr(   RuntimeErrorr$   skipr3   )r7   r   r   r9   r9   r:   test_terminal"  s    zTestProcess.test_terminal)Zonly_ifc              	   C   s  t  }| }ttd}|  W 5 Q R X | }tsts|j|jksNt	|j
|j
ks^t	tr|j|jksrt	|j|jkst	n |j|jkst	|j|jkst	| }t|  d}|tdd d W 5 Q R X | }|j
|j
kst	|j|jkst	|j|jkst	|j|jks(t	trR|j|jks@t	|j|jksRt	tt|D ]>}trv|dkrvq^|| dkst	|| dks^t	q^d S )Nrbwbr   i@B asciir   r   )r.   r/   Zio_countersopenr   readr   r   Z
read_countr3   Zwrite_countr   Z
read_charsZwrite_charsZ
read_bytesZwrite_bytes
get_testfnwritebytesrx   len)r7   r8   Zio1fZio2ir9   r9   r:   test_io_counters.  s:    zTestProcess.test_io_countersz
linux onlyc              	      s   fdd}t   ts.  d t jks.tt jdks<tt jdksJtt jdksXtt jdksft  }| 	||  t j t
  t jdfksttt  jt jdd W 5 Q R X  t j t
  t jdfkst jt jdd t
  t jdfksttt  jt jd	d W 5 Q R X z jt jdd W n t jk
rj   Y nX tjtd
d  t jd W 5 Q R X tjtd
d  t jd W 5 Q R X tjtdd  jdd W 5 Q R X d S )Nc                    s&   | \}}|t jkrd} || d S Nr   )r.   IOPRIO_CLASS_NONEionice)initZioclassvaluer8   r9   r:   cleanupY  s    
z.TestProcess.test_ionice_linux.<locals>.cleanupr   rY   r         r      zioclass accepts no valuematchz$'ioclass' argument must be specified)r.   r/   r   r   r   r3   ZIOPRIO_CLASS_RTZIOPRIO_CLASS_BEZIOPRIO_CLASS_IDLE
addCleanuptupler$   r4   rn   rT   )r7   r   r   r9   r   r:   test_ionice_linuxV  sB     zTestProcess.test_ionice_linuxz!not supported on this win versionc              	   C   s  t  }ts| t jkst| }| |j| |t j | t jksRt|t j | t jksptz|t j	 W n t j
k
r   Y nX | t j	ksttjtdd |jt jdd W 5 Q R X tjtdd |t j	d  W 5 Q R X d S )Nz&value argument not accepted on Windowsr   rY   r   zis not a valid priority)r.   r/   r   r   ZIOPRIO_NORMALr3   r   ZIOPRIO_VERYLOWZ
IOPRIO_LOWZIOPRIO_HIGHrT   r$   r4   	TypeErrorrn   )r7   r8   r   r9   r9   r:   test_ionice_win  s*     zTestProcess.test_ionice_winc                 C   s   dd l }tt }dd ttD }|s4t||D ]}tt|}|dksRt|t|kr|t||kspttrvq8|	||
|kstq8|	|}t|dkst|d dkst|d dks8tq8d S )Nr   c                 S   s   g | ]}| d r|qS )ZRLIMIT)
startswith.0r   r9   r9   r:   
<listcomp>  s     
 z/TestProcess.test_rlimit_get.<locals>.<listcomp>r   rl   rY   )resourcer.   r/   r1   r2   dirr3   r   r   rlimitZ	getrlimitr   )r7   r   r8   namesrm   r   retr9   r9   r:   test_rlimit_get  s     

zTestProcess.test_rlimit_getc              	   C   s   |   }|tjd |tjdks*ttrZtjtdd tj	
dd W 5 Q R X tt |tjd W 5 Q R X d S )N)rZ   rZ   zcan't use prlimitr   r   )rZ   rZ   rZ   )r=   r   r.   ZRLIMIT_NOFILEr3   r   r$   r4   rn   Z_psplatformr/   r6   r9   r9   r:   test_rlimit_set  s    zTestProcess.test_rlimit_setc                 C   s   t  }|  }|t j\}}z~|t jd|f t|d}|d W 5 Q R X t	t
&}t|d}|d W 5 Q R X W 5 Q R X |jjtjkstW 5 |t j||f |t j||fkstX d S )N   r   s   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)r.   r/   r   r   RLIMIT_FSIZEr3   r   r   r$   r4   OSErrorr   errnoZEFBIG)r7   r8   testfnsofthardr   excr9   r9   r:   test_rlimit  s    zTestProcess.test_rlimitc                 C   s   t  }|t j\}}zN|t jd|f |t jt j|f t|  d}|d W 5 Q R X W 5 |t j||f |t j||fkstX d S )Nr   r   s   XXXXXXXXXXXXXXXXXXXXX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FINITYr   r   r   )r7   r8   r   r   r   r9   r9   r:   test_rlimit_infinity  s    z TestProcess.test_rlimit_infinityc                 C   s<   t  }|t j\}}|t jks&t|t j||f d S r<   )r.   r/   r   r   r   r3   )r7   r8   r   r   r9   r9   r:   test_rlimit_infinity_value  s    z&TestProcess.test_rlimit_infinity_valueserialrm   c              	   C   sr   t  }tr<z| }W qD t jk
r8   tdY qDX n| }t  | }||d ksdtW 5 Q R X d S )N$on OpenBSD this requires root accessrY   )	r.   r/   r	   num_threadsrT   r$   r   r   r3   )r7   r8   step1step2r9   r9   r:   test_num_threads  s    zTestProcess.test_num_threadszWINDOWS onlyc                 C   s   t  }| dkstd S r   )r.   r/   Znum_handlesr3   r6   r9   r9   r:   test_num_handles  s    zTestProcess.test_num_handlesc              	   C   s   t  }tr<z| }W qD t jk
r8   tdY qDX n| }t d | }t|t|d kslt	|d }|j
|d kst	|j|d kst	|j|d kst	W 5 Q R X d S )Nr   rY   r   r   )r.   r/   r	   threadsrT   r$   r   r   r   r3   id	user_timesystem_time)r7   r8   r   r   Zathreadr9   r9   r:   test_threads  s    zTestProcess.test_threadsc                 C   s   |   }tr:z|  W n  tjk
r8   tdY nX t| j	t
dd | D  dk sftt| jt
dd | D  dk std S )Nr   c                 s   s   | ]}|j V  qd S r<   )r   r   r9   r9   r:   	<genexpr>+  s     z-TestProcess.test_threads_2.<locals>.<genexpr>r   c                 s   s   | ]}|j V  qd S r<   )r   r   r9   r9   r:   r   /  s     )r=   r	   r   r.   rT   r$   r   r   r   r   sumr3   r   r6   r9   r9   r:   test_threads_2   s    ""zTestProcess.test_threads_2c                 C   s   t  }| d d \}}| }|dks0t|dks<td gd }| d d \}}| }||ksnt||kszt||kst~tr| }	|	j|	jkst|	j|	j	kst| }	|	j
D ]}
t|	|
dkstqd S )Nr   r   i` )r.   r/   Zmemory_infomemory_percentr3   r   ZrssZwsetvmsZpagefiler   r   )r7   r8   Zrss1Zvms1Zpercent1ZmemarrZrss2Zvms2Zpercent2memrm   r9   r9   r:   test_memory_info3  s&    

zTestProcess.test_memory_infoc                 C   s   t  }t  j}| }|jD ]8}t||}|dks:t|dkrFts t	rLq ||ks tq t	sft
sftrt|jdksttt	r|jdkst|jdkstd S )Nr   r   )r.   r/   Zvirtual_memorytotalZmemory_full_infor   r   r3   r
   r   r   r   ussZpssZswap)r7   r8   r   r   rm   r   r9   r9   r:   test_memory_full_infoR  s    


z!TestProcess.test_memory_full_infoc           
      C   s  t  }| }t|tt|ks(t|jdd}|D ]}|jdrLq8tr\|jdkr\q8t	j
|jstt|jtrz*t	j|jst	j|jst|jW nH tk
r   ts td}| }W 5 Q R X |j d|kr Y nX q8dt	j|jkr8zt	|j}W n tk
r&   Y q8X t|js8t|jq8|D ]h}|jD ]Z}t||}	|dkrjqN|d	kr|	st|	n"t|	tst|	d
ksNt|	qNqDd S )NF)Zgrouped[Zpvclockz/proc/self/smapsz
 (deleted)Z64r   >   permsaddrr   )r.   r/   memory_mapsr   setr3   r   r   r   r1   isabsr   existsislinkr   r   r   basenamestatFileNotFoundErrorS_ISREGst_moder   r   ry   int)
r7   r8   mapsZext_mapsntr   datastfnamer   r9   r9   r:   test_memory_mapsb  sN    






zTestProcess.test_memory_mapsc              	      sL   t  }t 4}dd   fdd| D } ||ks>tW 5 Q R X d S )Nc                 S   s   t jt j| S r<   )r1   r   r   normcaser   r9   r9   r:   normpath  s    z8TestProcess.test_memory_maps_lists_lib.<locals>.normpathc                    s   g | ]} |j qS r9   )r   r   r  r9   r:   r     s     z:TestProcess.test_memory_maps_lists_lib.<locals>.<listcomp>)r.   r/   r    r   r3   )r7   r8   r   Zlibpathsr9   r  r:   test_memory_maps_lists_lib  s
    z&TestProcess.test_memory_maps_lists_libc              	   C   sN   t  }|  tt |jdd W 5 Q R X ts>ts>trJ|jdd d S )Nz?!?)Zmemtyper   )	r.   r/   r   r$   r4   rn   r   r   r   r6   r9   r9   r:   test_memory_percent  s    zTestProcess.test_memory_percentc                 C   sL   |   }| st| s t|  |  | r<t| rHtd S r<   )r=   
is_runningr3   r>   r?   r6   r9   r9   r:   test_is_running  s    zTestProcess.test_is_runningc                 C   s   |   }| }z|tkstW n tk
r   trbt|ttkrbtjj}|||tkstnPt	j
d  dt	j
d  }z ||dt|dkstW n tk
r   Y nX Y nX t|ddg}|dkstd S )Nr   .rY    rV   zimport os; print('hey')Zhey)r=   exer   r3   r   r   r1   r   r  sysversion_inforeplacer(   )r7   r8   r
  r  Zveroutr9   r9   r:   test_exe  s     zTestProcess.test_exec                 C   s   t ddg}| |}tr.| g kr.tdts:ts:trP| d t kstnht	rt
r| d }|t krd| dd  d|dd  kstd S d| d|kstd S )NrV   2import time; [time.sleep(0.1) for x in range(100)]OPENBSD: returned EBUSYr    rY   )r   r=   r   cmdliner$   r   r	   r   r3   r   r   join)r7   r  r8   pyexer9   r9   r:   test_cmdline  s    

,zTestProcess.test_cmdlinec                 C   s   t g}|dgd  |ddg | |}|dd  }tr~z| dd  |ksXtW q tjk
rz   t	dY qX n2| dd  }t
r|g krt	d||kstd S )Nz-v2   rV   r  rY   z#OPENBSD: process turned into zombier  )r   extendr=   r	   r  r3   r.   ZombieProcessr$   r   r   )r7   r  r8   r   r9   r9   r:   test_long_cmdline  s     

zTestProcess.test_long_cmdlinec                 C   sH   |   }|  }tjtjtj }|	|sDt
||fd S r<   )r=   rm   lowerr1   r   r   r   r  
executabler   r3   )r7   r8   rm   r  r9   r9   r:   	test_name   s    zTestProcess.test_namec                 C   s   t | jtjd d}|ddg}| |}trz| tj	|ksHt
W q t
k
r   | tjkrtj	|| st
n Y qX n| tj	|kst
d S )Nr   )suffixrV   r  )r"   r   stringdigitsr=   r	   rm   r1   r   r   r3   statusr.   STATUS_ZOMBIEr   )r7   r  r  r8   r9   r9   r:   test_long_name  s    

zTestProcess.test_long_namec                 C   sX   t  }| \}}}|t ks&t|t ks6tttdrTt | ksTtd S N	getresuid)	r.   r/   uidsr1   getuidr3   geteuidhasattrr%  r7   r8   realZ	effectiveZ_savedr9   r9   r:   	test_uids!  s    
zTestProcess.test_uidsc                 C   sX   t  }| \}}}|t ks&t|t ks6tttdrTt | ksTtd S r$  )	r.   r/   gidsr1   getgidr3   getegidr)  	getresgidr*  r9   r9   r:   	test_gids.  s    
zTestProcess.test_gidsc              
      s   fdd}t   tt  d W 5 Q R X   }| || trd }t jt j	t j
t jt jt jfD ]}| j|dv z | W n t jk
r   Y nLX   }|t jt jt jhkr||ks|d kr|}||kstn||kstW 5 Q R X qlnzttdr*ttjt   ks*t d   dksFtttdrrttjt   ksrtts d   dkstW n t jk
r   Y nX d S )Nc                    s*   z  |  W n tjk
r$   Y nX d S r<   )nicer.   rT   )r   r   r9   r:   r   <  s    z&TestProcess.test_nice.<locals>.cleanupstr)priogetpriorityrY   r   )r.   r/   r$   r4   r   r2  r   r   ZIDLE_PRIORITY_CLASSZBELOW_NORMAL_PRIORITY_CLASSZNORMAL_PRIORITY_CLASSZABOVE_NORMAL_PRIORITY_CLASSZHIGH_PRIORITY_CLASSZREALTIME_PRIORITY_CLASSsubTestrT   r3   r)  r1   r5  PRIO_PROCESSr2   r   )r7   r   r   Zhighest_prior4  Znew_prior9   r   r:   	test_nice;  s`    

zTestProcess.test_nicec                 C   s   t  }| t jkstd S r<   )r.   r/   r!  ZSTATUS_RUNNINGr3   r6   r9   r9   r:   test_status}  s    zTestProcess.test_statusc                 C   s|   |   }| }trh|d\}}t }|dr>td||ksJt	dt
jkrx|t
jd ksxt	n|t ksxt	d S )N\$zrunning as service accountZ
USERDOMAIN)r=   usernamer   splitgetpassZgetuserendswithr$   r   r3   r1   environ)r7   r8   r<  domainZgetpass_userr9   r9   r:   test_username  s    


zTestProcess.test_usernamec                 C   s    |   }| t kstd S r<   )r=   cwdr1   getcwdr3   r6   r9   r9   r:   test_cwd  s    zTestProcess.test_cwdc                    s(   t ddg}| | t fdd d S )NrV   zFimport os, time; os.chdir('..'); [time.sleep(0.1) for x in range(100)]c                      s      tjt kS r<   )rC  r1   r   dirnamerD  r9   r   r9   r:   <lambda>      z(TestProcess.test_cwd_2.<locals>.<lambda>)r   r=   r   )r7   r]   r9   r   r:   
test_cwd_2  s    
zTestProcess.test_cwd_2c              	   C   s  t  }| }|st|| |j| ttdrL|tt|j	ksLtt
|t
t|ksdtttt
t jdd}|D ]j}||g | |gkstttdr| tt|j	kstt|dr| d | kstq|g  tr| |j ks(tn| |ks(tttdrR| tt|j	ksRttt |d W 5 Q R X || |t| |t| d S )Nsched_getaffinityTZpercpunum_cpur   rY   )r.   r/   cpu_affinityr3   r   r)  r1   listrJ  r0   r   r   rx   rw   rL  r   _procZ_get_eligible_cpusr$   r4   r   r   )r7   r8   initialZall_cpusnr9   r9   r:   test_cpu_affinity  s4    




zTestProcess.test_cpu_affinityc              	   C   s   |   }ttjddd g}tt || W 5 Q R X tt |tdd W 5 Q R X tt	tf |ddg W 5 Q R X tt |ddg W 5 Q R X d S )	NTrK  
   i'  i*  r   1rl   )
r=   r   r.   r   r$   r4   rn   rM  rx   r   )r7   r8   Zinvalid_cpur9   r9   r:   test_cpu_affinity_errs  s    z"TestProcess.test_cpu_affinity_errsc                 C   s   t  }| }|st|| |j| t|dkrB|d d }g }tt|d D ] }|dd t	||D  qV|D ]&}|| t
| t
|ks|tq|d S )N   rY   c                 s   s   | ]}|rt |V  qd S r<   )rN  )r   Zsubsetr9   r9   r:   r     s   zATestProcess.test_cpu_affinity_all_combinations.<locals>.<genexpr>)r.   r/   rM  r3   r   r   rx   r  	itertoolscombinationssorted)r7   r8   rP  Zcombosr   Zcombor9   r9   r:   "test_cpu_affinity_all_combinations  s    



z.TestProcess.test_cpu_affinity_all_combinationszbroken on BSDc              	      s`  t  |  }  | ks$tt|dz}|d |  t fdd   dd  D }t	j
||ks~ttr D ]}|j
|kr|jdkstqW 5 Q R X  D ]}t	j
|j
st|qd| d	}| td
|gtdD ]0}dd  D }||kr q:td qt	j
||ks:t|D ]}t	j
|s>t|q>d S )Nr   s   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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    rH  z-TestProcess.test_open_files.<locals>.<lambda>c                 S   s   g | ]}t j|jqS r9   r1   r   r  r   r9   r9   r:   r     s     z/TestProcess.test_open_files.<locals>.<listcomp>r   zimport time; f = open(r'z/', 'r'); [time.sleep(0.1) for x in range(100)];rV   ru   c                 S   s   g | ]}t j|jqS r9   r^  r   r9   r9   r:   r   
  s     rg   )r.   r/   r   r[  r3   r   r   flushr   r1   r   r  r   Zpositionisfiler=   r   rx   rp   sleep)r7   r   r   	filenamesfiler  r   r9   r\  r:   test_open_files  s8    



zTestProcess.test_open_filesc              	   C   s   t  }tjj}|  }t|d}| D ]*}||j||jksR|j	|
 kr, qlq,td| ||j||jksttr|j	dkstn|j	|
 kst| d }|d |jkst|d |j	kst|j| kstW 5 Q R X d S )Nwzno file found; files=rl   r   rY   )r.   r/   r1   r   r  r   r   r[  rm   fdfilenor$   rq   r3   r   )r7   r8   r  r   Zfileobjrc  Zntupler9   r9   r:   test_open_files_2  s&    zTestProcess.test_open_files_2c              
   C   s~   t  }|  }| }t|dB | |d ks8tt  | |d ksVtW 5 Q R X W 5 Q R X | |ksztd S )Nre  rY   r   )r.   r/   r   Znum_fdsr   r3   socket)r7   r8   r   startr9   r9   r:   test_num_fds/  s    
(zTestProcess.test_num_fdsz not reliable on OPENBSD & NETBSDc                 C   sT   t  }t| }tdD ](}td t| }||kr d S qtdd S )Nr   g?z2num ctx switches still the same after 2 iterations)	r.   r/   r   Znum_ctx_switchesrx   rp   ra  r$   rq   )r7   r8   beforer{   Zafterr9   r9   r:   test_num_ctx_switches;  s    
z!TestProcess.test_num_ctx_switchesc                 C   sF   t  }ttdr&| t ks&t|  }| t ksBtd S )Ngetppid)	r.   r/   r)  r1   ppidrn  r3   r=   r2   r6   r9   r9   r:   	test_ppidI  s
    
zTestProcess.test_ppidc                 C   sD   |   }| jt kstt d }t| d ks@td S r   )	r=   parentr0   r1   r2   r3   r.   pidsr/   )r7   r8   Z
lowest_pidr9   r9   r:   test_parentP  s    zTestProcess.test_parentc                 C   s:   |   }|  |jstd|_| jt ks6td S NrY   )r=   r   _create_timer3   rq  r0   r1   r2   r6   r9   r9   r:   test_parent_mocked_ctimeW  s
    
z$TestProcess.test_parent_mocked_ctimec                 C   s8   t  }|  \}}| |ks$t| |ks4td S r<   )r.   r/   rh   rq  r3   r7   rq  ri   rj   r9   r9   r:   test_parent_multic  s    zTestProcess.test_parent_multic                 C   s`   t  }| st|  \}}| d |ks4t| d |ksHt| d |ks\td S r   )r.   r/   parentsr3   rh   rw  r9   r9   r:   test_parentsi  s    zTestProcess.test_parentsc                 C   s   t  }| rt|jddr$t| jdd}| }|jdd}||fD ]>}t|dks`t|d j|jkstt|d  |jksLtqLd S )NT	recursiver   ZcreationflagsrY   )r.   r/   childrenr3   r=   r   r0   ro  r7   rq  ri   Z	children1Z	children2r~  r9   r9   r:   test_childrenr  s    zTestProcess.test_childrenc                 C   s   t  }|  |jst| jd7  _| r4t|jddrDt| jdd}| }|jdd}||fD ]>}t|dkst|d j|jkst|d 	 |jksltqld S )Nr   Tr{  r   r}  rY   )
r.   r/   r   ru  r3   r~  r=   r   r0   ro  r  r9   r9   r:   test_children_mocked_ctime  s    
z&TestProcess.test_children_mocked_ctimec                 C   sb   t  }|  \}}| |gks&t|jdd||gks>t|  |  |jddr^td S )NTr{  )r.   r/   rh   r~  r3   rG   r?   rw  r9   r9   r:   test_children_recursive  s    z#TestProcess.test_children_recursivec              	   C   s   t t}t D ]4}z||   d7  < W q tjk
rD   Y qX qt| dd dd }t	rv|dkrvt
dt|}z|jdd}W n tjk
r   Y nX t|tt|kstd S )	NrY   c                 S   s   | d S rt  r9   )r   r9   r9   r:   rG    rH  z6TestProcess.test_children_duplicates.<locals>.<lambda>)keyr   zPID 0Tr{  )collectionsdefaultdictr   r.   process_iterro  Errormaxitemsr   r$   r   r/   r~  rT   r   r   r3   )r7   tabler8   r0   cr9   r9   r:   test_children_duplicates  s    


z$TestProcess.test_children_duplicatesc                 C   s|   t  }|  \}}|jdd}t|dks0t|d |ks@t|d |ksPt| }|d |ksht|d |ksxtd S )NTr{  r   r   rY   )r.   r/   rh   r~  r   r3   ry  )r7   rq  ri   rj   r~  ry  r9   r9   r:   test_parents_and_children  s    z%TestProcess.test_parents_and_childrenc                 C   sX   |   }|  tdD ] }| tjkr. q:td q|  | tjksTt	d S )Nru   rg   )
r=   rd   rx   r!  r.   ZSTATUS_STOPPEDrp   ra  re   r3   )r7   r8   r{   r9   r9   r:   test_suspend_resume  s    zTestProcess.test_suspend_resumec              	   C   sD   t t td W 5 Q R X t t td W 5 Q R X d S )NrT  rl   )r$   r4   r   r.   r/   rn   )r7   r9   r9   r:   test_invalid_pid  s    zTestProcess.test_invalid_pidc              
   C   s  t  }|jddgd}t| ddgks0tt tt  }|jdgdd}t|d t	sp|d dksptt
jddt jd	" |jd
gddd
dikstW 5 Q R X t
jddt |jdd	, tt j |jd
gd W 5 Q R X W 5 Q R X t
jddt |jdd	$ |jd
gddd
diks0tW 5 Q R X t
jddtd	H | }d
t	| ksjttt |jd
gd W 5 Q R X W 5 Q R X tt |d W 5 Q R X tt |dg W 5 Q R X tt |ddg W 5 Q R X d S )Nr
  rm   )attrsZnet_connectionsfoo)r  Zad_valuezpsutil.Process.niceT)ZcreaterO   r2  rY   Zbar)r.   r/   Zas_dictrY  keysr3   minrr  ry   rN  r   rP   rT   rR   r0   r$   r4   r  NotImplementedErrorr   rn   )r7   r8   dr9   r9   r:   test_as_dict  sR      &"(  "zTestProcess.test_as_dictc              
   C   s   t  }td8}|  |  |  W 5 Q R X |jdksFtW 5 Q R X td}|  |  W 5 Q R X |jdkstd S )N$psutil._psplatform.Process.cpu_timesrY   r   r.   r/   r   rP   oneshotr   Z
call_countr3   r7   r8   r   r9   r9   r:   test_oneshot  s    
zTestProcess.test_oneshotc                 C   s   t  }td}tdj}| : |  |  |  |  |  W 5 Q R X W 5 Q R X |jdksvt|jdkstW 5 Q R X W 5 Q R X td}|  |  W 5 Q R X |jdkstd S )Nr  z(psutil._psplatform.Process.oneshot_enterrY   r   r  )r7   r8   Zm1Zm2r   r9   r9   r:   test_oneshot_twice  s    

"zTestProcess.test_oneshot_twicec              	   C   s   |   \}}| }| }||ks(t| & | |ksBt| |ksRtW 5 Q R X | & | |ksvt| |kstW 5 Q R X d S r<   )rh   ro  r3   r  )r7   Zp1Zp2Zp1_ppidZp2_ppidr9   r9   r:   test_oneshot_cache,  s    

zTestProcess.test_oneshot_cachec                    sj   dd }|          tr4t fdd |   t }||jD ]\}}||| qRd S )Nc                 S   s   z
|  }W nT t jk
r"    Y nd t jk
r6   Y nP t jk
r^   trX|dkrXY d S  Y n(X trp|dkrpd S t| d|dd S )N>   r   r   >   rm   r
  z didn't raise NSP and returned z instead)r.   r  rR   rT   r	   r   r$   rq   )funZfun_namer   r9   r9   r:   assert_raises_nspC  s    
zFTestProcess.test_halfway_terminated_process.<locals>.assert_raises_nspc                      s    j t kS r<   )r0   r.   rr  r9   r   r9   r:   rG  Z  rH  z=TestProcess.test_halfway_terminated_process.<locals>.<lambda>)	r=   rG   r?   r   r   rC   r#   iterall)r7   r  nsr  rm   r9   r   r:   test_halfway_terminated_process;  s    
z+TestProcess.test_halfway_terminated_processc                 C   s   |   \}}| | d S r<   )Zspawn_zombieZassert_proc_zombie)r7   Z_parentZzombier9   r9   r:   test_zombie_processa  s    zTestProcess.test_zombie_processc              	   C   sB   t  }tjdt dd}| s*t|js4tW 5 Q R X d S )Nzpsutil.Processr   rN   )r.   r/   r   rP   r  r  r3   r~   r  r9   r9   r:   $test_zombie_process_is_running_w_excf  s     z0TestProcess.test_zombie_process_is_running_w_excc              	   C   sH   t  }tjdt dd"}| t jks0t|js:tW 5 Q R X d S )Nz!psutil._psplatform.Process.statusr   rN   )	r.   r/   r   rP   r  r!  r"  r3   r~   r  r9   r9   r:    test_zombie_process_status_w_excq  s    z,TestProcess.test_zombie_process_status_w_excc                 C   s  |   }t|j}|j| d f|_tt  |jtjksDt	|
 rPt	tjtjdd, tt }tt  W 5 Q R X W 5 Q R X d|j | kst	|jtjkst	|t|jkst	d}t|}|j|j|j ddD ]B\}}| j|d( tjtj|d	 |  W 5 Q R X W 5 Q R X qd
t|ksHt	d
t|ksZt	tjtj|d	 |  W 5 Q R X tjtj|d	 |  W 5 Q R X tjtj|d	 |   W 5 Q R X tjtj|d	 |!  W 5 Q R X d S )Nru   ZPSUTIL_DEBUGTz+refreshing Process instance for reused PID z4process no longer exists and its PID has been reusedF)Zclear_cacher   r   zterminated + PID reused)"spawn_subprocr.   r/   r0   r   _identrN  r  Z_pmapr3   r  r   rP   objectZ_common
contextlibredirect_stderrioStringIOgetvaluer#   r  settersZkillersr6  r$   r4   rR   r3  reprro  rq  ry  r~  )r7   Zsubpr8   r   msgr  r  rm   r9   r9   r:   test_reused_pid}  s>     
zTestProcess.test_reused_pidc              	   C   s  dt  krVtt j t d W 5 Q R X t dr<tt d dksRtd S t d}t	rjt j
nt}t| |  W 5 Q R X t| |  W 5 Q R X t| |  W 5 Q R X t| |  W 5 Q R X t| |  W 5 Q R X t| |tj W 5 Q R X t|}||j|j D ]\}}z
| }W n t j
k
rj   Y nbX |dkr|jdkstnD|dkrt	rdnd}| |kstn|dkr@|s@t|q@tsdt  kstt dstd S )Nr   rY   >   r&  r-  r<  zNT AUTHORITY\SYSTEMrootrm   )r.   rr  r$   r4   rR   r/   Z
pid_existsr3   ro  r   rT   rn   r?   rG   rd   re   r>   rI   r@   rA   r#   r  Zgettersr  r+  r<  r	   )r7   r8   r   r  r  rm   r   r   r9   r9   r:   
test_pid_0  sH    




zTestProcess.test_pid_0c                 C   sH   dd }d | _ t }|| }|tj }tsDtrD||ksDtd S )Nc                 S   sJ   ddddg}t r"|ddddg |D ]}| |d  q&dd	 |  D S )
NZPLATHOMEZPYTEST_CURRENT_TESTZPYTEST_VERSIONZ__CF_USER_TEXT_ENCODINGZVERSIONER_PYTHON_PREFER_32_BITZVERSIONER_PYTHON_VERSIONc                 S   s6   i | ].\}}| d d dd| d d ddqS )r	  
)r  )r   kvr9   r9   r:   
<dictcomp>  s   z@TestProcess.test_environ.<locals>.clean_dict.<locals>.<dictcomp>)r   r  popr  )r  Zexcluderm   r9   r9   r:   
clean_dict  s    z,TestProcess.test_environ.<locals>.clean_dict)	ZmaxDiffr.   r/   r@  r1   copyr
   r   r3   )r7   r  r8   Zd1Zd2r9   r9   r:   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	 | sRt|j dksdttrtrz| }W q tjk
r   Y d S X n| }|dddkst|  |jdkstd 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)stdinrX   rH  rT  3)ACr   )textwrapdedentr!   r   r  r[   r\   r.   r/   r0   r+   r  r3   rX   r   r   r   r@  rT   communicate
returncode)r7   rE   ZcexeZsprocr8   envr9   r9   r:   test_weird_environ  s(    
  

zTestProcess.test_weird_environN)i__name__
__module____qualname____doc__r;   rF   rH   rK   r$   markskipifr   rU   r^   r   rf   rk   ro   rs   r}   r   r   r   r   r   r   r   r   r*   r   r   r   r   r   r   r   r   r   r   r   r   Zxdist_groupr   r   r   r   r'   r)   r   r   r   r   r   r   r  r  r  r  r  r  r  r#  r,  r1  r8  r9  rB  rE  rI  r   rR  rU  rZ  r   rd  rh  rk  r	   rm  rp  rs  rv  rx  rz  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r   r  r9   r9   r9   r:   r,   H   s  






&+ 









0
	


B
&


&

 


4&



).
 r,   c                   @   sP   e Zd ZdZedd Zejje	o$e
dddd Zdd	 Zd
d Zdd ZdS )	TestPopenzTests for psutil.Popen class.c                 C   s
   t   d S r<   r%   )clsr9   r9   r:   tearDownClass(  s    zTestPopen.tearDownClasszhangs on OSX + CIrL   c              
   C   s   t ddg}tj|tjtjtdL}|  |  |j t	|sDt
tt |j W 5 Q R X |  W 5 Q R X tr|dtj kst
n|dtjkst
d S )NrV   3import time; [time.sleep(0.1) for x in range(100)];stdoutrX   r  rZ   )r   r.   Popenr[   r\   r   rm   r   r  r   r3   r$   r4   r5   r  rG   r   r?   r@   rA   r7   r]   procr9   r9   r:   	test_misc,  s*    zTestPopen.test_miscc              	   C   sh   t jtdgtjtjtjtd}|  W 5 Q R X |jjs>t	|j
jsJt	|jjsVt	|jdksdt	d S )Nz-V)r  rX   r  r  r   )r.   r  r   r[   r\   r   r  r  closedr3   rX   r  r  )r7   r  r9   r9   r:   test_ctx_managerH  s    zTestPopen.test_ctx_managerc              
   C   s   t ddg}tj|tjtjtd}|  |  t	tj
 |  W 5 Q R X t	tj
 |  W 5 Q R X t	tj
 |tj W 5 Q R X trt	tj
 |tj W 5 Q R X t	tj
 |tj W 5 Q R X W 5 Q R X d S NrV   r  r  )r   r.   r  r[   r\   r   rG   r?   r$   r4   rR   r>   rI   r@   rA   r   ZCTRL_C_EVENTZCTRL_BREAK_EVENTr  r9   r9   r:   test_kill_terminateV  s0    zTestPopen.test_kill_terminatec              
   C   s\   t ddg}tj|tjtjtd2}|  |  t	t
 |j W 5 Q R X W 5 Q R X d S r  )r   r.   r  r[   r\   r   rG   r?   r$   r4   r5   r  r  r9   r9   r:   test__getattribute__s  s    zTestPopen.test__getattribute__N)r  r  r  r  classmethodr  r$   r  r  r   r   r  r  r  r  r9   r9   r9   r:   r  %  s   

r  )>r  r  r  r   r>  r  rW  r1   r@   ri  r   r  r[   r  r  rp   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  r9   r9   r9   r:   <module>   s              h