U
    ;g                     @   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   sD   | j ||}zt|jW S  tjk
r>   | |j  Y nX d S N)spawn_testprocpsutilProcesspidNoSuchProcessZassertPidGone)selfargskwargssproc r7   Y/opt/bot/vods_filmes/script/venv/lib/python3.8/site-packages/psutil/tests/test_process.pyspawn_psprocL   s    zTestProcess.spawn_psprocc              	   C   s:   t  }|jt ksttt d|_W 5 Q R X d S )N!   )	r/   r0   r1   osgetpidAssertionErrorr$   raisesAttributeErrorr3   pr7   r7   r8   test_pidV   s    zTestProcess.test_pidc                 C   sJ   |   }|  | }tr,|tjks<tn|tj ks<t| | d S r-   )	r9   killwaitr   signalSIGTERMr=   SIGKILLassertProcessGoner3   rA   coder7   r7   r8   	test_kill\   s    zTestProcess.test_killc                 C   sJ   |   }|  | }tr,|tjks<tn|tj ks<t| | d S r-   )r9   	terminaterD   r   rE   rF   r=   rH   rI   r7   r7   r8   test_terminatef   s    zTestProcess.test_terminatec                 C   sX   t r
tjntj}|  }|| | }tr<||ksJtn|| ksJt| 	| d S r-   )
r   rE   rG   rF   r9   send_signalrD   r   r=   rH   )r3   sigrA   rJ   r7   r7   r8   test_send_signalp   s    
zTestProcess.test_send_signalz	not POSIX)reasonc              
   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)rE   rF   r9   r   patchProcessLookupErrorr$   r>   r/   r2   rN   PermissionErrorAccessDenied)r3   rO   rA   r7   r7   r8   test_send_signal_mocked{   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   r9   rD   r=   rH   
subprocessPIPE)r3   cmdrA   rJ   r7   r7   r8   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)r9   r   rN   rE   SIGSTOPr$   r>   r/   TimeoutExpiredrD   SIGCONTrF   r=   suspendresumerL   r@   r7   r7   r8   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$   r>   r/   re   rD   rL   r   rE   rF   r=   )r3   child
grandchildZ	child_retZgrandchild_retr7   r7   r8   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 )Nrj   r   )r9   namer$   r>   r/   re   rD   
ValueErrorr@   r7   r7   r8   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>| 	dt
r|tj kstn|tjkst| | d S )Nr   rc   )r9   r$   r>   r/   re   rD   rC   timer   failr   rE   rG   r=   rF   rH   )r3   rA   stop_atrJ   r7   r7   r8   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 )Nrb   )intervald           ro   )
r/   r0   cpu_percentrange
isinstancefloatr=   r$   r>   rq   )r3   rA   _percentr7   r7   r8   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   rT   r/   r0   rz   calledr=   )r3   mr7   r7   r8   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 )Nry   z%H:%M:%S)r/   r0   	cpu_timesuserr=   systemchildren_userchildren_systemr   Ziowait_fieldsrs   strftime	localtimegetattr)r3   timesrp   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   r{   )ru   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  }| }|dkstt  dkr4|dks4t| tt  ksLtd S Nr   r\   )r/   r0   Zcpu_numr=   	cpu_countr{   )r3   rA   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   rs   create_timeabsrt   r   r   )r3   rA   nowr   
differencer7   r7   r8   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/   r0   terminalr;   pathrealpathr(   RuntimeErrorr$   skipr=   )r3   r   r   r7   r7   r8   test_terminal7  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/   r0   Zio_countersopenr   readr   r   Z
read_countr=   Zwrite_countr   Z
read_charsZwrite_chars
read_byteswrite_bytes
get_testfnwritebytesr{   len)r3   rA   Zio1fZio2ir7   r7   r8   test_io_countersC  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)initZioclassvaluerA   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   r=   ZIOPRIO_CLASS_RTZIOPRIO_CLASS_BEZIOPRIO_CLASS_IDLE
addCleanuptupler$   r>   rq   rW   )r3   r   r   r7   r   r8   test_ionice_linuxk  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   r\   r   zis not a valid priority)r/   r0   r   r   ZIOPRIO_NORMALr=   r   ZIOPRIO_VERYLOWZ
IOPRIO_LOWZIOPRIO_HIGHrW   r$   r>   	TypeErrorrq   )r3   rA   r   r7   r7   r8   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   r7   r7   r8   
<listcomp>  s     
 z/TestProcess.test_rlimit_get.<locals>.<listcomp>r   ro   r\   )resourcer/   r0   r;   r<   dirr=   r   r   rlimitZ	getrlimitr   )r3   r   rA   namesrp   r   retr7   r7   r8   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)r]   r]   zcan't use prlimitr   r   )r]   r]   r]   )r9   r   r/   ZRLIMIT_NOFILEr=   r   r$   r>   rq   Z_psplatformr0   r@   r7   r7   r8   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   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)	r/   r0   r   r   r=   RLIM_INFINITYr   r   r   )r3   rA   r   r   r   r7   r7   r8   test_rlimit_infinity  s    z TestProcess.test_rlimit_infinityc                 C   s<   t  }|t j\}}|t jks&t|t j||f d S r-   )r/   r0   r   r   r   r=   )r3   rA   r   r   r7   r7   r8   test_rlimit_infinity_value  s    z&TestProcess.test_rlimit_infinity_valuec              	   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 accessr\   )	r/   r0   r	   num_threadsrW   r$   r   r   r=   )r3   rA   step1step2r7   r7   r8   test_num_threads	  s    zTestProcess.test_num_threadszWINDOWS onlyc                 C   s   t  }| dkstd S r   )r/   r0   Znum_handlesr=   r@   r7   r7   r8   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   r\   r   r   )r/   r0   r	   threadsrW   r$   r   r   r   r=   id	user_timesystem_time)r3   rA   r   r   Zathreadr7   r7   r8   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   r7   r7   r8   	<genexpr>?  s     z-TestProcess.test_threads_2.<locals>.<genexpr>r   c                 s   s   | ]}|j V  qd S r-   )r   r   r7   r7   r8   r   C  s     )r9   r	   r   r/   rW   r$   r   r   r   r   sumr=   r   r@   r7   r7   r8   test_threads_24  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/   r0   Zmemory_infomemory_percentr=   r   ZrssZwsetvmsZpagefiler   r   )r3   rA   Zrss1Zvms1Zpercent1ZmemarrZrss2Zvms2Zpercent2memrp   r7   r7   r8   test_memory_infoG  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/   r0   Zvirtual_memorytotalZmemory_full_infor   r   r=   r
   r   r   r   ussZpssZswap)r3   rA   r   r   rp   r   r7   r7   r8   test_memory_full_infof  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)grouped[Zpvclockz/proc/self/smapsz
 (deleted)Z64r   >   addrpermsr   )r/   r0   memory_mapsr   setr=   r   r   r   r;   isabsr   existsislinkr   r   r   basenamestatFileNotFoundErrorS_ISREGst_moder   r   r|   int)
r3   rA   mapsZext_mapsntr   datastfnamer   r7   r7   r8   test_memory_mapsv  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-   )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     s     z:TestProcess.test_memory_maps_lists_lib.<locals>.<listcomp>)r/   r0   r    r   r=   )r3   rA   r   Zlibpathsr7   r
  r8   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/   r0   r   r$   r>   rq   r   r   r   r@   r7   r7   r8   test_memory_percent  s    zTestProcess.test_memory_percentc                 C   sL   |   }| st| s t|  |  | r<t| rHtd S r-   )r9   
is_runningr=   rC   rD   r@   r7   r7   r8   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   .r\    rY   zimport os; print('hey')Zhey)r9   exer   r=   r   r   r;   r   r  sysversion_inforeplacer(   )r3   rA   r  r  veroutr7   r7   r8   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 )NrY   2import time; [time.sleep(0.1) for x in range(100)]OPENBSD: returned EBUSYr    r\   )r   r9   r   cmdliner$   r   r	   r   r=   r   r   join)r3   r  rA   pyexer7   r7   r8   test_cmdline  s    

,zTestProcess.test_cmdlinezbroken on PYPYc                 C   s   t g}|dgd  |ddg | |}trjz| |ksDtW q tjk
rf   t	dY qX n*| }t
r|g krt	d||kstd S )Nz-v2   rY   r  z#OPENBSD: process turned into zombier  )r   extendr9   r	   r  r=   r/   ZombieProcessr$   r   r   )r3   r  rA   r   r7   r7   r8   test_long_cmdline  s    

zTestProcess.test_long_cmdlinec                 C   sH   |   }|  }tjtjtj }|	|sDt
||fd S r-   )r9   rp   lowerr;   r   r   r   r  
executabler   r=   )r3   rA   rp   r  r7   r7   r8   	test_name  s    zTestProcess.test_namezunreliable on PYPYc                 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   )suffixrY   r  )r"   r   stringdigitsr9   r	   rp   r;   r   r   r=   statusr/   STATUS_ZOMBIEr   )r3   r  r  rA   r7   r7   r8   test_long_name  s    

zTestProcess.test_long_namec                 C   sX   t  }| \}}}|t ks&t|t ks6tttdrTt | ksTtd S N	getresuid)	r/   r0   uidsr;   getuidr=   geteuidhasattrr-  r3   rA   realZ	effectiveZ_savedr7   r7   r8   	test_uidsD  s    
zTestProcess.test_uidsc                 C   sX   t  }| \}}}|t ks&t|t ks6tttdrTt | ksTtd S r,  )	r/   r0   gidsr;   getgidr=   getegidr1  	getresgidr2  r7   r7   r8   	test_gidsQ  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/   rW   )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_CLASSsubTestrW   r=   r1  r;   r=  PRIO_PROCESSr<   r   )r3   r   r   Zhighest_prior<  Znew_prior7   r   r8   	test_nice^  s`    

zTestProcess.test_nicec                 C   s   t  }| t jkstd S r-   )r/   r0   r)  ZSTATUS_RUNNINGr=   r@   r7   r7   r8   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)r9   usernamer   splitgetpassgetuserendswithr$   r   r=   r;   environ)r3   rA   rD  domainZgetpass_userr7   r7   r8   test_username  s    


zTestProcess.test_usernamec                 C   s    |   }| t kstd S r-   )r9   cwdr;   getcwdr=   r@   r7   r7   r8   test_cwd  s    zTestProcess.test_cwdc                    s(   t ddg}| | t fdd d S )NrY   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>      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  }| }|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   r\   )r/   r0   cpu_affinityr=   r   r1  r;   listrS  r1   r   r   r{   rz   rU  r   _procZ_get_eligible_cpusr$   r>   r   r   )r3   rA   initialZall_cpusnr7   r7   r8   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 )	NTrT  
   i'  i*  r   1ro   )
r9   r   r/   r   r$   r>   rq   rV  r{   r   )r3   rA   Zinvalid_cpur7   r7   r8   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   r\   c                 s   s   | ]}|rt |V  qd S r-   )rW  )r   Zsubsetr7   r7   r8   r     s   zATestProcess.test_cpu_affinity_all_combinations.<locals>.<genexpr>)r/   r0   rV  r=   r   r   r{   r   	itertoolscombinationssorted)r3   rA   rY  Zcombosr   Zcombor7   r7   r8   "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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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxc                      s   t  t  kS r-   )r   
open_filesr7   filesrA   r7   r8   rP    rQ  z-TestProcess.test_open_files.<locals>.<lambda>c                 S   s   g | ]}t j|jqS r7   r;   r   r  r   r7   r7   r8   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)];rY   rx   c                 S   s   g | ]}t j|jqS r7   rg  r   r7   r7   r8   r   -  s     rj   )r/   r0   r   rd  r=   r   r   flushr   r;   r   r  r   positionisfiler9   r   r{   rs   sleep)r3   r   r   	filenamesfiler  r   r7   re  r8   test_open_files  s8    



zTestProcess.test_open_filesc              	   C   s   t  }tjj}|  }t|d}| D ]*}||j||jksR|j	|
 kr, qlq,| d| ||j||jksttr|j	dkstn|j	|
 kst| d }|d |jkst|d |j	kst|j| kstW 5 Q R X d S )Nwzno file found; files=ro   r   r\   )r/   r0   r;   r   r  r   r   rd  rp   fdfilenort   r=   r   )r3   rA   r  r   fileobjrm  Zntupler7   r7   r8   test_open_files_27  s&    zTestProcess.test_open_files_2c                 C   s   t  }|  }| }t|d}| |j | |d ksBtt }| |j | |d ksjt|  |  | |kstd S )Nro  r\   r   )	r/   r0   r   Znum_fdsr   r   closer=   socket)r3   rA   r   startrm  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_switchesr{   rs   rk  rt   )r3   rA   beforer~   afterr7   r7   r8   test_num_ctx_switchesa  s    
z!TestProcess.test_num_ctx_switchesc                 C   sF   t  }ttdr&| t ks&t|  }| t ksBtd S )Ngetppid)	r/   r0   r1  r;   ppidr|  r=   r9   r<   r@   r7   r7   r8   	test_ppido  s
    
zTestProcess.test_ppidc                 C   sD   |   }| jt kstt d }t| d ks@td S r   )	r9   parentr1   r;   r<   r=   r/   pidsr0   )r3   rA   Z
lowest_pidr7   r7   r8   test_parentv  s    zTestProcess.test_parentc                 C   s8   t  }|  \}}| |ks$t| |ks4td S r-   )r/   r0   rk   r  r=   r3   r  rl   rm   r7   r7   r8   test_parent_multi}  s    zTestProcess.test_parent_multic                 C   s`   t  }| st|  \}}| d |ks4t| d |ksHt| d |ks\td S r   )r/   r0   parentsr=   rk   r  r7   r7   r8   test_parents  s    zTestProcess.test_parentsc                 C   s   t  }| rt|jddr$t| jdd}| }|jdd}||fD ]>}t|dks`t|d j|jkstt|d  |jksLtqLd S )NT	recursiver   )creationflagsr\   )r/   r0   childrenr=   r9   r   r1   r}  )r3   r  rl   Z	children1Z	children2r  r7   r7   r8   test_children  s    zTestProcess.test_childrenc                 C   sb   t  }|  \}}| |gks&t|jdd||gks>t|  |  |jddr^td S )NTr  )r/   r0   rk   r  r=   rL   rD   r  r7   r7   r8   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 )	Nr\   c                 S   s   | d S )Nr\   r7   )r   r7   r7   r8   rP    rQ  z6TestProcess.test_children_duplicates.<locals>.<lambda>)keyr   zPID 0Tr  )collectionsdefaultdictr  r/   process_iterr}  Errormaxitemsr   r$   r   r0   r  rW   r   r   r=   )r3   tablerA   r1   cr7   r7   r8   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   r\   )r/   r0   rk   r  r   r=   r  )r3   r  rl   rm   r  r  r7   r7   r8   test_parents_and_children  s    z%TestProcess.test_parents_and_childrenc                 C   sX   |   }|  tdD ] }| tjkr. q:td q|  | tjksTt	d S )Nrx   rj   )
r9   rg   r{   r)  r/   ZSTATUS_STOPPEDrs   rk  rh   r=   )r3   rA   r~   r7   r7   r8   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 )Nr]  ro   )r$   r>   r   r/   r0   rq   r3   r7   r7   r8   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  rp   )attrsZnet_connectionsfoo)r  Zad_valuezpsutil.Process.niceT)createrS   r:  r\   bar)r/   r0   as_dictrb  keysr=   minr  r|   rW  r   rT   rW   r2   r1   r$   r>   r!  NotImplementedErrorr   rq   )r3   rA   dr7   r7   r8   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_timesr\   r   r/   r0   r   rT   oneshotr   Z
call_countr=   r3   rA   r   r7   r7   r8   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_enterr\   r   r  )r3   rA   m1m2r   r7   r7   r8   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-   )rk   r}  r=   r  )r3   p1Zp2Zp1_ppidZp2_ppidr7   r7   r8   test_oneshot_cache.  s    

zTestProcess.test_oneshot_cachec                    sn   fdd}         tr8t fdd   t }||jD ]\}}||| qVd S )Nc                    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  | d|dd S )N>   r   r   >   rp   r  z didn't raise NSP and returned z instead)r/   r!  r2   rW   r	   r   rt   )funZfun_namer   r  r7   r8   assert_raises_nspE  s    
zFTestProcess.test_halfway_terminated_process.<locals>.assert_raises_nspc                      s    j t kS r-   )r1   r/   r  r7   r   r7   r8   rP  \  rQ  z=TestProcess.test_halfway_terminated_process.<locals>.<lambda>)	r9   rL   rD   r   r   rH   r#   iterall)r3   r  nsr  rp   r7   )rA   r3   r8   test_halfway_terminated_process=  s    
z+TestProcess.test_halfway_terminated_processc                 C   s   |   \}}| | d S r-   )Zspawn_zombieZassertProcessZombie)r3   Z_parentzombier7   r7   r8   test_zombie_processc  s    zTestProcess.test_zombie_processc              	   C   sB   t  }tjdt dd}| s*t|js4tW 5 Q R X d S )Nzpsutil.Processr   rR   )r/   r0   r   rT   r!  r  r=   r   r  r7   r7   r8   $test_zombie_process_is_running_w_exch  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   rR   )	r/   r0   r   rT   r!  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  |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 )Nrx   ZPSUTIL_DEBUGTz+refreshing Process instance for reused PID z4process no longer exists and its PID has been reusedF)clear_cache)rp   r   zterminated + PID reused)"r.   r/   r0   r1   r   _identrW  r  Z_pmapr=   r  r   rT   objectZ_common
contextlibredirect_stderrioStringIOgetvaluer#   r  settersZkillersr>  r$   r>   r2   r;  reprr}  r  r  r  )r3   ZsubprA   r   msgr  r  rp   r7   r7   r8   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   r\   >   r.  r5  rD  zNT AUTHORITY\SYSTEMrootrp   )r/   r  r$   r>   r2   r0   Z
pid_existsr=   r}  r   rW   rq   rD   rL   rg   rh   rC   rN   rE   rF   r#   r  gettersr  r3  rD  r	   )r3   rA   r   r  r  rp   r   r   r7   r7   r8   
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 )
NPLATHOMEZPYTEST_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   kvr7   r7   r8   
<dictcomp>  s      z@TestProcess.test_environ.<locals>.clean_dict.<locals>.<dictcomp>)r   r   popr  )r  excluderp   r7   r7   r8   
clean_dict  s    z,TestProcess.test_environ.<locals>.clean_dict)	ZmaxDiffr/   r0   rI  r;   copyr
   r   r=   )r3   r  rA   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	 | 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)stdinr[   rQ  r]  3)ACr   )textwrapdedentr!   r   r.   r^   r_   r/   r0   r1   r+   r  r=   r[   r   r   r   rI  rW   communicate
returncode)r3   rJ   Zcexer6   rA   envr7   r7   r8   test_weird_environ  s(    
  

zTestProcess.test_weird_environN)h__name__
__module____qualname____doc__r9   rB   rK   rM   rP   r$   markZskipifr   rX   ra   r   ri   rn   rr   rv   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  rR  r   r[  r^  rc  r   rn  rs  rx  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   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|tjtjtdL}|  |  |j t	|sDt
tt |j W 5 Q R X |  W 5 Q R X tr|dtj kst
n|dtjkst
d S )NrY   3import time; [time.sleep(0.1) for x in range(100)];stdoutr[   r  r]   )r   r/   Popenr^   r_   r   rp   r   r  r   r=   r$   r>   r?   r  rL   r   rD   rE   rF   r3   r`   procr7   r7   r8   	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  r[   r  r  r   )r/   r  r   r^   r_   r   r  r  closedr=   r[   r  r  )r3   r  r7   r7   r8   test_ctx_managerI  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 NrY   r  r  )r   r/   r  r^   r_   r   rL   rD   r$   r>   r2   rC   rN   rE   rF   r   ZCTRL_C_EVENTZCTRL_BREAK_EVENTr  r7   r7   r8   test_kill_terminateW  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   rL   rD   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;   rE   ru  r   r'  r^   r  r  rs   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