o
    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   sP   t  }|jt ksJ tt d|_W d    d S 1 s!w   Y  d S )N!   )psutilProcesspidosgetpidr$   raisesAttributeErrorselfp r8   O/var/www/html/ai/venv/lib/python3.10/site-packages/psutil/tests/test_process.pytest_pidK   s
   "zTestProcess.test_pidc                 C   sJ   |   }|  | }tr|tjksJ n|tj ksJ | | d S N)spawn_psprockillwaitr   signalSIGTERMSIGKILLassert_proc_goner6   r7   coder8   r8   r9   	test_killQ      zTestProcess.test_killc                 C   sJ   |   }|  | }tr|tjksJ n|tj ksJ | | d S r;   )r<   	terminater>   r   r?   r@   rB   rC   r8   r8   r9   test_terminate[   rF   zTestProcess.test_terminatec                 C   sX   t rtjntj}|  }|| | }tr||ksJ n|| ks%J | | d S r;   )	r   r?   rA   r@   r<   send_signalr>   r   rB   )r6   sigr7   rD   r8   r8   r9   test_send_signale   s   
zTestProcess.test_send_signalz	not POSIXreasonc              	   C   s   t j}|  }tjdtd# ttj	 |
| W d    n1 s%w   Y  W d    n1 s4w   Y  |  }tjdtd, ttj |
| W d    n1 s[w   Y  W d    d S W d    d S 1 ssw   Y  d S )Nzpsutil.os.killside_effect)r?   r@   r<   r   patchProcessLookupErrorr$   r3   r.   NoSuchProcessrI   PermissionErrorAccessDenied)r6   rJ   r7   r8   r8   r9   test_send_signal_mockedp   s    "z#TestProcess.test_send_signal_mockedc                 C   s   t ddg}| |}| }|dksJ | | t ddg}| j|tjd}| }|dks0J | | t ddg}| |}| }|dksIJ | | t dd	g}| |}| }|dksbJ | | d S )
N-cpassr   z1 / 0)stderr   zimport sys; sys.exit(5);   zimport os; os._exit(5);)r   r<   r>   rB   
subprocessPIPE)r6   cmdr7   rD   r8   r8   r9   test_wait_exited}   s(   









zTestProcess.test_wait_exitedzfails on NETBSDc                 C   s|  |   }trf|tj ttj |j	dd W d    n1 s#w   Y  |tj
 ttj |j	dd W d    n1 sEw   Y  |tj |	 tj ksZJ |	 tj ksdJ d S |  ttj |j	dd W d    n1 sw   Y  |  ttj |j	dd W d    n1 sw   Y  |  |	 tjksJ |	 tjksJ d S )NMbP?)timeout)r<   r   rI   r?   SIGSTOPr$   r3   r.   TimeoutExpiredr>   SIGCONTr@   suspendresumerG   r5   r8   r8   r9   test_wait_stopped   s0   zTestProcess.test_wait_stoppedc                 C   s   |   \}}ttj |d W d    n1 sw   Y  ttj |d W d    n1 s7w   Y  |  |  | }| }tr^|tj	 ksVJ |d u s\J d S |tj	kseJ |tj	kslJ d S )N{Gz?)
spawn_children_pairr$   r3   r.   rb   r>   rG   r   r?   r@   )r6   child
grandchild	child_retgrandchild_retr8   r8   r9   test_wait_non_children   s    z"TestProcess.test_wait_non_childrenc                 C   s   |   }|  ttj |d W d    n1 sw   Y  ttj |d W d    n1 s9w   Y  tt |d W d    d S 1 sTw   Y  d S )Nrg   r   )r<   namer$   r3   r.   rb   r>   
ValueErrorr5   r8   r8   r9   test_wait_timeout   s   "zTestProcess.test_wait_timeoutc                 C   s   |   }ttj |d W d    n1 sw   Y  |  t t }t |k rGz|d}W n tjy@   Y nw t |k s/t	dt
rW|tj ksVJ n|tjks^J | | d S )Nr   r`   )r<   r$   r3   r.   rb   r>   r=   timer   failr   r?   rA   r@   rB   )r6   r7   stop_atrD   r8   r8   r9   test_wait_timeout_nonblocking   s&   

z)TestProcess.test_wait_timeout_nonblockingc                 C   s   t  }|jdd |jdd tdD ]}|jd d}t|ts#J |dks)J qtt |jdd W d    d S 1 sAw   Y  d S )Nr_   )intervald           rn   )	r.   r/   cpu_percentrange
isinstancefloatr$   r3   rp   )r6   r7   _percentr8   r8   r9   test_cpu_percent   s   "zTestProcess.test_cpu_percentc                 C   sJ   t jdd d}t   |jsJ W d    d S 1 sw   Y  d S )Nzpsutil.cpu_count)return_value)r   rP   r.   r/   ry   called)r6   mr8   r8   r9   test_cpu_percent_numcpus_none   s   "z)TestProcess.test_cpu_percent_numcpus_nonec              	   C   s   t   }|jdksJ ||jdksJ ||jdks!J ||jdks*J |tr5|jdks5J ||j	D ]}t
dt
t|| q8d S )Nrx   z%H:%M:%S)r.   r/   	cpu_timesusersystemchildren_userchildren_systemr   iowait_fieldsrr   strftime	localtimegetattr)r6   timesro   r8   r8   r9   test_cpu_times   s   
zTestProcess.test_cpu_timesc                 C   sV   dd }|  t   }t }t|j|j dk sJ t|j|j dk s)J d S )Nc                  S   sH   t  jd } t  j| k r"tdD ]}|dC }qt  j| k sd S d S )Ng?順    )r1   r   r   rz   )rt   xr8   r8   r9   	waste_cpu  s
   
z/TestProcess.test_cpu_times_2.<locals>.waste_cpu皙?)r.   r/   r   r1   r   absr   r   )r6   r   abr8   r8   r9   test_cpu_times_2  s   zTestProcess.test_cpu_times_2znot supportedc                 C   sP   t  }| }|dksJ t  dkr|dksJ | tt  v s&J d S Nr   rY   )r.   r/   cpu_num	cpu_countrz   )r6   r7   numr8   r8   r9   test_cpu_num  s   zTestProcess.test_cpu_numc                 C   sB   |   }t }t| | dk sJ tdt|  d S )Nr   z%Y %m %d %H:%M:%S)r<   rr   r   create_timer   r   )r6   r7   nowr8   r8   r9   test_create_time  s   zTestProcess.test_create_timez
POSIX onlyc                 C   sV   t   }|d ur'z
tjtd}W n ty    t	dw ||ks)J d S d S )Nttyzcan't rely on `tty` CLI)
r.   r/   terminalr1   pathrealpathr(   RuntimeErrorr$   skip)r6   r   r   r8   r8   r9   test_terminal"  s   
zTestProcess.test_terminal)only_ifc                 C   s  t  }| }ttd}|  W d    n1 sw   Y  | }tsLtsL|j|jks1J |j	|j	ks9J t
rK|j|jksCJ |j|jksKJ n|j|jksTJ |j|jks\J | }t|  d}|tdd d W d    n1 s|w   Y  | }|j	|j	ksJ |j|jksJ |j|jksJ |j|jksJ t
r|j|jksJ |j|jksJ tt|D ]}tr|dkrq|| dksJ || dksJ qd S )Nrbwbr   i@B asciir   r   )r.   r/   io_countersopenr   readr   r   
read_countwrite_countr   
read_charswrite_chars
read_byteswrite_bytes
get_testfnwritebytesrz   len)r6   r7   io1fio2ir8   r8   r9   test_io_counters.  sB   
zTestProcess.test_io_countersz
linux onlyc                    sH   fdd}t   ts  d t jksJ t jdksJ t jdks%J t jdks,J t jdks3J   }| ||  t j t	  t jdfksPJ t
t  jt jdd W d    n1 shw   Y   t j t	  t jdfksJ  jt jdd t	  t jdfksJ t
t  jt jd	d W d    n1 sw   Y  z
 jt jdd W n
 t jy   Y nw t
jtd
d  t jd W d    n1 sw   Y  t
jtd
d  t jd W d    n1 sw   Y  t
jtdd  jdd W d    d S 1 sw   Y  d S )Nc                    s&   | \}}|t jkrd} || d S Nr   )r.   IOPRIO_CLASS_NONEionice)initioclassvaluer7   r8   r9   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   IOPRIO_CLASS_RTIOPRIO_CLASS_BEIOPRIO_CLASS_IDLE
addCleanuptupler$   r3   rp   rT   )r6   r   r   r8   r   r9   test_ionice_linuxV  sL   $zTestProcess.test_ionice_linuxz!not supported on this win versionc                 C   s*  t  }ts| t jksJ | }| |j| |t j | t jks)J |t j | t jks8J z|t j W n
 t j	yJ   Y n
w | t jksTJ t
jtdd |jt jdd W d    n1 snw   Y  t
jtdd |t jd  W d    d S 1 sw   Y  d S )Nz&value argument not accepted on Windowsr   rY   r   zis not a valid priority)r.   r/   r   r   IOPRIO_NORMALr   IOPRIO_VERYLOW
IOPRIO_LOWIOPRIO_HIGHrT   r$   r3   	TypeErrorrp   )r6   r7   r   r8   r8   r9   test_ionice_win  s.   "zTestProcess.test_ionice_winc                 C   s   dd l }tt }dd ttD }|sJ ||D ]I}tt|}|dks)J |t|v rH|t||ks8J tr;q|||	|ksGJ q||}t
|dksUJ |d dks]J |d dkseJ qd S )Nr   c                 S   s   g | ]	}| d r|qS )RLIMIT)
startswith.0r   r8   r8   r9   
<listcomp>      z/TestProcess.test_rlimit_get.<locals>.<listcomp>r   rn   rY   )resourcer.   r/   r1   r2   dirr   r   rlimit	getrlimitr   )r6   r   r7   namesro   r   retr8   r8   r9   test_rlimit_get  s"   

zTestProcess.test_rlimit_getc                 C   s   |   }|tjd |tjdksJ tr7tjtdd tj	dd W d    n1 s2w   Y  tt |tjd W d    d S 1 sOw   Y  d S )N)rZ   rZ   zcan't use prlimitr   r   )rZ   rZ   rZ   )
r<   r   r.   RLIMIT_NOFILEr   r$   r3   rp   _psplatformr/   r5   r8   r8   r9   test_rlimit_set  s   "zTestProcess.test_rlimit_setc                 C   s4  t  }|  }|t j\}}zs|t jd|f t|d}|d W d    n1 s/w   Y  tt	"}t|d}|d W d    n1 sOw   Y  W d    n1 s^w   Y  |j
jtjkslJ W |t j||f |t j||fksJ d S |t j||f |t j||fksJ w )N   r   s   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)r.   r/   r   r   RLIM_INFINITYr   r   r   )r6   r7   r   r   r   r8   r8   r9   test_rlimit_infinity  s   z TestProcess.test_rlimit_infinityc                 C   s<   t  }|t j\}}|t jksJ |t j||f d S r;   )r.   r/   r   r   r   )r6   r7   r   r   r8   r8   r9   test_rlimit_infinity_value  s   z&TestProcess.test_rlimit_infinity_valueserialro   c                 C   s   t  }trz| }W n t jy   tdw | }t  | }||d ks.J W d    d S 1 s9w   Y  d S )N$on OpenBSD this requires root accessrY   )r.   r/   r	   num_threadsrT   r$   r   r   )r6   r7   step1step2r8   r8   r9   test_num_threads  s   
"zTestProcess.test_num_threadszWINDOWS onlyc                 C   s   t  }| dksJ d S r   )r.   r/   num_handlesr5   r8   r8   r9   test_num_handles  s   zTestProcess.test_num_handlesc                 C   s   t  }trz| }W n t jy   tdw | }t 8 | }t|t|d ks2J |d }|j	|d ks?J |j
|d ksHJ |j|d ksQJ W d    d S 1 s\w   Y  d S )Nr   rY   r   r   )r.   r/   r	   threadsrT   r$   r   r   r   id	user_timesystem_time)r6   r7   r   r   athreadr8   r8   r9   test_threads  s    
"zTestProcess.test_threadsc                 C   s   |   }trz|  W n tjy   tdw t| j	t
dd | D  dk s0J t| jt
dd | D  dk sFJ d S )Nr   c                 s       | ]}|j V  qd S r;   )r   r   r8   r8   r9   	<genexpr>+      z-TestProcess.test_threads_2.<locals>.<genexpr>r   c                 s   r  r;   )r  r   r8   r8   r9   r  /  r  )r<   r	   r   r.   rT   r$   r   r   r   r   sumr   r5   r8   r8   r9   test_threads_2   s   
""zTestProcess.test_threads_2c                 C   s   t  }| d d \}}| }|dksJ |dksJ d gd }| d d \}}| }||ks7J ||ks=J ||ksCJ ~trZ| }	|	j|	jksRJ |	j|	jksZJ | }	|	j	D ]}
t
|	|
dkslJ qad S )Nr   r   i` )r.   r/   memory_infomemory_percentr   rsswsetvmspagefiler   r   )r6   r7   rss1vms1percent1memarrrss2vms2percent2memro   r8   r8   r9   test_memory_info3  s(   

zTestProcess.test_memory_infoc                 C   s   t  }t  j}| }|jD ]}t||}|dksJ |dkr#ts%tr&q||ks,J qts3t	s3t
r:|jdks:J trJ|jdksCJ |jdksLJ d S d S )Nr   r  )r.   r/   virtual_memorytotalmemory_full_infor   r   r
   r   r   r   usspssswap)r6   r7   r  r  ro   r   r8   r8   r9   test_memory_full_infoR  s    


z!TestProcess.test_memory_full_infoc           
      C   s  t  }| }t|tt|ksJ |jdd}|D ]}|jdr%qtr-|jdkr-qtj	|js9J |jt
r~ztj|jsOtj|jsOJ |jW q ty}   tsZ td}| }W d    n1 smw   Y  |j d|vr{ Y qw dtj|jvrzt|j}W n	 ty   Y qw t|jsJ |jq|D ],}|jD ]&}t||}	|dkrq|d	v r|	sJ |	qt|	tsJ |	d
ksJ |	qqd S )NF)grouped[pvclockz/proc/self/smapsz
 (deleted)64r   >   addrpermsr   )r.   r/   memory_mapsr   setr   r   r   r1   isabsr   existsislinkAssertionErrorr   r   r   basenamestatFileNotFoundErrorS_ISREGst_moder   r   r{   int)
r6   r7   mapsext_mapsntr   datastfnamer   r8   r8   r9   test_memory_mapsb  s^   



zTestProcess.test_memory_mapsc                    sb   t  }t  }dd   fdd| D } ||v sJ W d    d S 1 s*w   Y  d S )Nc                 S   s   t jt j| S r;   )r1   r   r   normcaser   r8   r8   r9   normpath  s   z8TestProcess.test_memory_maps_lists_lib.<locals>.normpathc                    s   g | ]} |j qS r8   )r   r   r9  r8   r9   r     s    z:TestProcess.test_memory_maps_lists_lib.<locals>.<listcomp>)r.   r/   r    r%  )r6   r7   r   libpathsr8   r:  r9   test_memory_maps_lists_lib  s   "z&TestProcess.test_memory_maps_lists_libc                 C   sf   t  }|  tt |jdd W d    n1 sw   Y  ts)ts)tr1|jdd d S d S )Nz?!?)memtyper  )	r.   r/   r
  r$   r3   rp   r   r   r   r5   r8   r8   r9   test_memory_percent  s   zTestProcess.test_memory_percentc                 C   sL   |   }| s
J | sJ |  |  | rJ | r$J d S r;   )r<   
is_runningr=   r>   r5   r8   r8   r9   test_is_running  s   zTestProcess.test_is_runningc                 C   s   |   }| }z|tksJ W nI tyY   tr0t|ttkr0tjj}|||tks/J n't	j
d  dt	j
d  }z||dt|dksLJ W n	 tyV   Y nw Y nw t|ddg}|dksgJ d S )Nr   .rY    rV   zimport os; print('hey')hey)r<   exer   r*  r   r   r1   r   r8  sysversion_inforeplacer(   )r6   r7   rD  r8  veroutr8   r8   r9   test_exe  s$    zTestProcess.test_exec                 C   s   t ddg}| |}tr| g krtdtststr)| d t ks'J d S trOt	rO| d }|t krOd
| dd  d
|dd  ksMJ d S d
| d
|ks]J d S )NrV   2import time; [time.sleep(0.1) for x in range(100)]OPENBSD: returned EBUSYr    rY   )r   r<   r   cmdliner$   r   r	   r   r   r   join)r6   rN  r7   pyexer8   r8   r9   test_cmdline  s   

, zTestProcess.test_cmdlinec                 C   s   t g}|dgd  |ddg | |}|dd  }tr<z| dd  |ks,J W d S  tjy;   tdw | dd  }t	rO|g krOtd||ksUJ d S )Nz-v2   rV   rK  rY   z#OPENBSD: process turned into zombierL  )
r   extendr<   r	   rN  r.   ZombieProcessr$   r   r   )r6   rN  r7   r   r8   r8   r9   test_long_cmdline  s"   


zTestProcess.test_long_cmdlinec                 C   sH   |   }|  }tjtjtj }|	|s"J ||fd S r;   )
r<   ro   lowerr1   r   r+  r   rE  
executabler   )r6   r7   ro   rP  r8   r8   r9   	test_name   s   zTestProcess.test_namec                 C   s   t | jtjd d}|ddg}| |}trGz| tj	|ks$J W d S  t
yF   | tjkrBtj	|| sAJ n Y d S w | tj	|ksSJ d S )Nr   )suffixrV   rK  )r"   r   stringdigitsr<   r	   ro   r1   r   r+  r*  statusr.   STATUS_ZOMBIEr   )r6   rP  rN  r7   r8   r8   r9   test_long_name  s    
zTestProcess.test_long_namec                 C   \   t  }| \}}}|t ksJ |t ksJ ttdr*t | ks,J d S d S N	getresuid)r.   r/   uidsr1   getuidgeteuidhasattrra  r6   r7   real	effective_savedr8   r8   r9   	test_uids!     
zTestProcess.test_uidsc                 C   r_  r`  )r.   r/   gidsr1   getgidgetegidre  	getresgidrf  r8   r8   r9   	test_gids.  rk  zTestProcess.test_gidsc              
      s   fdd}t   tt  d W d    n1 sw   Y    }| || trd }t jt j	t j
t jt jt jfD ]O}| j|d? z | W n
 t jyZ   Y n&w   }|t jt jt jhv rz||ksq|d u ry|}||ksyJ n||ksJ W d    n1 sw   Y  q@d S zJttdrttjt   ksJ  d   dksJ ttdrttjt   ksJ ts׈ d   dksJ W d S W d S  t jy   Y d S w )Nc                    s(   z  |  W d S  tjy   Y d S w r;   )nicer.   rT   )r   r   r8   r9   r   <  s
   z&TestProcess.test_nice.<locals>.cleanupstr)priogetpriorityrY   r   )r.   r/   r$   r3   r   rq  r   r   IDLE_PRIORITY_CLASSBELOW_NORMAL_PRIORITY_CLASSNORMAL_PRIORITY_CLASSABOVE_NORMAL_PRIORITY_CLASSHIGH_PRIORITY_CLASSREALTIME_PRIORITY_CLASSsubTestrT   re  r1   rt  PRIO_PROCESSr2   r   )r6   r   r   highest_priors  new_prior8   r   r9   	test_nice;  sp   



zTestProcess.test_nicec                 C   s   t  }| t jksJ d S r;   )r.   r/   r\  STATUS_RUNNINGr5   r8   r8   r9   test_status}  s   zTestProcess.test_statusc                 C   s   |   }| }tr7|d\}}t }|drtd||ks%J dt	j
v r3|t	j
d ks5J d S d S |t ks?J d S )N\$zrunning as service account
USERDOMAIN)r<   usernamer   splitgetpassgetuserendswithr$   r   r1   environ)r6   r7   r  domaingetpass_userr8   r8   r9   test_username  s   


zTestProcess.test_usernamec                 C   s    |   }| t ksJ d S r;   )r<   cwdr1   getcwdr5   r8   r8   r9   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;   )r  r1   r   dirnamer  r8   r   r8   r9   <lambda>  s    z(TestProcess.test_cwd_2.<locals>.<lambda>)r   r<   r   )r6   r]   r8   r   r9   
test_cwd_2  s   
zTestProcess.test_cwd_2c                 C   s  t  }| }|sJ || |j| ttdr&|tt|jks&J t	|t	t
|ks2J ttt	t jdd}|D ]5}||g | |gksQJ ttdrd| tt|jksdJ t|dru| d | ksuJ q@|g  tr| |j ksJ n| |ksJ ttdr| tt|jksJ tt |d W d    n1 sw   Y  || |t
| |t| d S )Nsched_getaffinityTpercpunum_cpur   rY   )r.   r/   cpu_affinityr   re  r1   listr  r0   r   r&  rz   ry   r  r   _proc_get_eligible_cpusr$   r3   r   r   )r6   r7   initialall_cpusnr8   r8   r9   test_cpu_affinity  s8   





zTestProcess.test_cpu_affinityc                 C   s  |   }ttjddd g}tt || W d    n1 s$w   Y  tt |tdd W d    n1 sAw   Y  tt	tf |ddg W d    n1 s_w   Y  tt |ddg W d    d S 1 s|w   Y  d S )	NTr  
   i'  i*  r   1rn   )
r<   r   r.   r   r$   r3   rp   r  rz   r   )r6   r7   invalid_cpur8   r8   r9   test_cpu_affinity_errs  s   "z"TestProcess.test_cpu_affinity_errsc                 C   s   t  }| }|sJ || |j| t|dkr!|d d }g }tt|d D ]}|dd t||D  q+|D ]}|| t	| t	|ksQJ q>d S )N   rY   c                 s   s    | ]	}|rt |V  qd S r;   )r  )r   subsetr8   r8   r9   r    s    
zATestProcess.test_cpu_affinity_all_combinations.<locals>.<genexpr>)
r.   r/   r  r   r   rz   rS  	itertoolscombinationssorted)r6   r7   r  combosr   combor8   r8   r9   "test_cpu_affinity_all_combinations  s   



z.TestProcess.test_cpu_affinity_all_combinationszbroken on BSDc                    sj  t  |  }  | vsJ t|dB}|d |  t fdd   dd  D }tj	
||v s?J trR D ]}|j	|krQ|jdksQJ qCW d    n1 s\w   Y   D ]}tj	|j	spJ |qcd| d	}| td
|gtdD ]}dd  D }||v r ntd qtj	
||v sJ |D ]}tj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xxxxxxxxxxxxxxxxc                      s   t  t  kS r;   )r   
open_filesr8   filesr7   r8   r9   r    s    z-TestProcess.test_open_files.<locals>.<lambda>c                 S      g | ]	}t j|jqS r8   r1   r   r8  r   r8   r8   r9   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)];rV   rw   c                 S   r  r8   r  r   r8   r8   r9   r   
  r   rg   )r.   r/   r   r  r   r   flushr   r1   r   r8  r   positionisfiler<   r   rz   rr   sleep)r6   r   r   	filenamesfilerN  r   r8   r  r9   test_open_files  s>   


zTestProcess.test_open_filesc                 C   s  t  }tjj}|  }t|dm}| D ]}||j||jks)|j	|
 kr+ nqtd| ||j||jksBJ trL|j	dksKJ n	|j	|
 ksUJ | d }|d |jksdJ |d |j	ksmJ |j| vsvJ W d    d S 1 sw   Y  d S )Nwzno file found; files=rn   r   rY   )r.   r/   r1   r   r8  r   r   r  ro   fdfilenor$   rs   r   )r6   r7   r8  r   fileobjr  ntupler8   r8   r9   test_open_files_2  s&   "zTestProcess.test_open_files_2c              	   C   s   t  }|  }| }t|d0 | |d ksJ t  | |d ks+J W d    n1 s5w   Y  W d    n1 sDw   Y  | |ksQJ d S )Nr  rY   r   )r.   r/   r   num_fdsr   socket)r6   r7   r   startr8   r8   r9   test_num_fds/  s   
zTestProcess.test_num_fdsz not reliable on OPENBSD & NETBSDc                 C   sP   t  }t| }tdD ]}td t| }||kr" d S qtd)Nr   g?z2num ctx switches still the same after 2 iterations)	r.   r/   r  num_ctx_switchesrz   rr   r  r$   rs   )r6   r7   beforer}   afterr8   r8   r9   test_num_ctx_switches;  s   

z!TestProcess.test_num_ctx_switchesc                 C   sF   t  }ttdr| t ksJ |  }| t ks!J d S )Ngetppid)r.   r/   re  r1   ppidr  r<   r2   r5   r8   r8   r9   	test_ppidI  s
   
zTestProcess.test_ppidc                 C   sD   |   }| jt ksJ t d }t| d u s J d S r   )r<   parentr0   r1   r2   r.   pidsr/   )r6   r7   
lowest_pidr8   r8   r9   test_parentP  s   zTestProcess.test_parentc                 C   s:   |   }|  |jsJ d|_| jt ksJ d S NrY   )r<   r   _create_timer  r0   r1   r2   r5   r8   r8   r9   test_parent_mocked_ctimeW  s
   
z$TestProcess.test_parent_mocked_ctimec                 C   s8   t  }|  \}}| |ksJ | |ksJ d S r;   )r.   r/   rh   r  r6   r  ri   rj   r8   r8   r9   test_parent_multic  s   zTestProcess.test_parent_multic                 C   s`   t  }| s
J |  \}}| d |ksJ | d |ks$J | d |ks.J d S r   )r.   r/   parentsrh   r  r8   r8   r9   test_parentsi  s   zTestProcess.test_parentsc                 C   s   t  }| r
J |jddrJ | jdd}| }|jdd}||fD ]}t|dks0J |d j|jks:J |d  |jksEJ q&d S )NT	recursiver   creationflagsrY   )r.   r/   childrenr<   r   r0   r  r6   r  ri   	children1	children2r  r8   r8   r9   test_childrenr  s   zTestProcess.test_childrenc                 C   s   t  }|  |jsJ | jd7  _| rJ |jddr"J | jdd}| }|jdd}||fD ]}t|dks@J |d j|jksJJ |d  |jksUJ q6d S )Nr   Tr  r   r  rY   )	r.   r/   r   r  r  r<   r   r0   r  r  r8   r8   r9   test_children_mocked_ctime  s   
z&TestProcess.test_children_mocked_ctimec                 C   sb   t  }|  \}}| |gksJ |jdd||gksJ |  |  |jddr/J d S )NTr  )r.   r/   rh   r  rG   r>   r  r8   r8   r9   test_children_recursive  s   z#TestProcess.test_children_recursivec              	   C   s   t t}t D ]}z||   d7  < W q	 tjy!   Y q	w t| dd dd }t	r9|dkr9t
dt|}z|jdd}W n tjyQ   Y d S w t|tt|ks^J d S )	NrY   c                 S   s   | d S r  r8   )r   r8   r8   r9   r    s    z6TestProcess.test_children_duplicates.<locals>.<lambda>)keyr   zPID 0Tr  )collectionsdefaultdictr0  r.   process_iterr  Errormaxitemsr   r$   r   r/   r  rT   r   r&  )r6   tabler7   r0   cr8   r8   r9   test_children_duplicates  s"   


z$TestProcess.test_children_duplicatesc                 C   s|   t  }|  \}}|jdd}t|dksJ |d |ks J |d |ks(J | }|d |ks4J |d |ks<J d S )NTr  r   r   rY   )r.   r/   rh   r  r   r  )r6   r  ri   rj   r  r  r8   r8   r9   test_parents_and_children  s   z%TestProcess.test_parents_and_childrenc                 C   sX   |   }|  tdD ]}| tjkr ntd q|  | tjks*J d S )Nrw   rg   )	r<   rd   rz   r\  r.   STATUS_STOPPEDrr   r  re   )r6   r7   r}   r8   r8   r9   test_suspend_resume  s   zTestProcess.test_suspend_resumec                 C   sn   t t td W d    n1 sw   Y  t t td W d    d S 1 s0w   Y  d S )Nr  rn   )r$   r3   r   r.   r/   rp   )r6   r8   r8   r9   test_invalid_pid  s   "zTestProcess.test_invalid_pidc              	   C   s  t  }|jddgd}t| ddgksJ t tt  }|jdgdd}t|d ts8|d dks8J t	j
ddt jd	 |jd
gddd
diksPJ W d    n1 sZw   Y  t	j
ddt |jdd	% tt j |jd
gd W d    n1 sw   Y  W d    n1 sw   Y  t	j
ddt |jdd	 |jd
gddd
diksJ W d    n1 sw   Y  t	j
ddtd	2 | }d
t| vsJ tt |jd
gd W d    n1 sw   Y  W d    n	1 sw   Y  tt |d W d    n	1 sw   Y  tt |dg W d    n	1 s9w   Y  tt |ddg W d    d S 1 sWw   Y  d S )NrD  ro   )attrsnet_connectionsfoo)r  ad_valuezpsutil.Process.niceT)createrO   rq  rY   bar)r.   r/   as_dictr  keysminr  r{   r  r   rP   rT   rR   r0   r$   r3   rT  NotImplementedErrorr   rp   )r6   r7   dr8   r8   r9   test_as_dict  s`   		
$zTestProcess.test_as_dictc              	   C   s   t  }td+}|  |  |  W d    n1 s!w   Y  |jdks-J W d    n1 s7w   Y  td}|  |  W d    n1 sTw   Y  |jdks`J d S )N$psutil._psplatform.Process.cpu_timesrY   r   r.   r/   r   rP   oneshotr   
call_countr6   r7   r   r8   r8   r9   test_oneshot  s   


zTestProcess.test_oneshotc                 C   s4  t  }tdc}tdN}| , |  |  |  |  |  W d    n1 s4w   Y  W d    n1 sCw   Y  |jdksOJ |jdksVJ W d    n1 s`w   Y  W d    n1 sow   Y  td}|  |  W d    n1 sw   Y  |jdksJ d S )Nr  z(psutil._psplatform.Process.oneshot_enterrY   r   r  )r6   r7   m1m2r   r8   r8   r9   test_oneshot_twice  s,   



zTestProcess.test_oneshot_twicec                 C   s   |   \}}| }| }||ksJ |  | |ks!J | |ks)J W d    n1 s3w   Y  |  | |ksEJ | |ksMJ W d    d S 1 sXw   Y  d S r;   )rh   r  r  )r6   p1p2p1_ppidp2_ppidr8   r8   r9   test_oneshot_cache,  s   

"zTestProcess.test_oneshot_cachec                    sj   dd }|          trt fdd |   t }||jD ]	\}}||| q)d S )Nc                 S   sz   z|  }W n$ t jy     t jy   Y d S  t jy)   tr(|dv r(Y d S  w tr2|dv r2d S t| d|d)N>   r   r   >   rD  ro   z didn't raise NSP and returned z instead)r.   rT  rR   rT   r	   r   r$   rs   )funfun_namer   r8   r8   r9   assert_raises_nspC  s    
zFTestProcess.test_halfway_terminated_process.<locals>.assert_raises_nspc                      s    j t vS r;   )r0   r.   r  r8   r   r8   r9   r  Z  s    z=TestProcess.test_halfway_terminated_process.<locals>.<lambda>)	r<   rG   r>   r   r   rB   r#   iterall)r6   r	  nsr  ro   r8   r   r9   test_halfway_terminated_process;  s   
z+TestProcess.test_halfway_terminated_processc                 C   s   |   \}}| | d S r;   )spawn_zombieassert_proc_zombie)r6   _parentzombier8   r8   r9   test_zombie_processa  s   zTestProcess.test_zombie_processc                 C   sX   t  }tjdt dd}| sJ |jsJ W d    d S 1 s%w   Y  d S )Nzpsutil.Processr   rN   )r.   r/   r   rP   rT  r?  r   r  r8   r8   r9   $test_zombie_process_is_running_w_excf  s   
"z0TestProcess.test_zombie_process_is_running_w_excc                 C   s^   t  }tjdt dd}| t jksJ |jsJ W d    d S 1 s(w   Y  d S )Nz!psutil._psplatform.Process.statusr   rN   )r.   r/   r   rP   rT  r\  r]  r   r  r8   r8   r9    test_zombie_process_status_w_excq  s   "z,TestProcess.test_zombie_process_status_w_excc              
   C   s  |   }t|j}|j| d f|_tt  |jtjv s"J |	 r(J t
jtjdd% tt }tt  W d    n1 sJw   Y  W d    n1 sYw   Y  d|j | v sjJ |jtjvsrJ |t|jks|J d}t|}|j|j|j ddD ]5\}}| j|d# tjtj|d	 |  W d    n1 sw   Y  W d    n1 sw   Y  qd
t|v sJ d
t|v sJ tjtj|d	 |  W d    n1 sw   Y  tjtj|d	 |  W d    n	1 sw   Y  tjtj|d	 |  W d    n	1 s$w   Y  tjtj|d	 |   W d    d S 1 sBw   Y  d S )Nrw   PSUTIL_DEBUGTz+refreshing Process instance for reused PID z4process no longer exists and its PID has been reusedF)clear_cacher   r   zterminated + PID reused)!spawn_subprocr.   r/   r0   r   _identr  r  _pmapr?  r   rP   object_common
contextlibredirect_stderrioStringIOgetvaluer#   r
  setterskillersr{  r$   r3   rR   rr  reprr  r  r  r  )r6   subpr7   r   msgr  r  ro   r8   r8   r9   test_reused_pid}  sR   




$zTestProcess.test_reused_pidc              	   C   s  dt  vr5tt j t d W d    n1 sw   Y  t dr(J t d dks3J d S t d}tr?t j	nt
}t| |  W d    n1 sUw   Y  t| |  W d    n1 snw   Y  t| |  W d    n1 sw   Y  t| |  W d    n1 sw   Y  t| |  W d    n1 sw   Y  t| |tj W d    n1 sw   Y  t|}||j|j D ]D\}}z| }W n
 t j	y   Y qw |dv r|jdksJ q|dkrtrdnd}| |ksJ q|dkr*|s*J |qts?dt  v s7J t dsAJ d S d S )Nr   rY   >   rl  rb  r  zNT AUTHORITY\SYSTEMrootro   )r.   r  r$   r3   rR   r/   
pid_existsr  r   rT   rp   r>   rG   rd   re   r=   rI   r?   r@   r#   r
  gettersr!  rg  r  r	   )r6   r7   r   r  r  ro   r   r   r8   r8   r9   
test_pid_0  s^   









zTestProcess.test_pid_0c                 C   sP   dd }d | _ t }|| }|tj }ts"tr$||ks&J d S d S d S )Nc                 S   sB   g d}t r|g d |D ]}| |d  qdd |  D S )N)PLATHOMEPYTEST_CURRENT_TESTPYTEST_VERSION)__CF_USER_TEXT_ENCODINGVERSIONER_PYTHON_PREFER_32_BITVERSIONER_PYTHON_VERSIONr1  c                 S   s6   i | ]\}}| d d dd| d d ddqS )rB  
)rG  )r   kvr8   r8   r9   
<dictcomp>  s
    z@TestProcess.test_environ.<locals>.clean_dict.<locals>.<dictcomp>)r   rS  popr  )r  excludero   r8   r8   r9   
clean_dict  s   z,TestProcess.test_environ.<locals>.clean_dict)maxDiffr.   r/   r  r1   copyr
   r   )r6   r9  r7   d1d2r8   r8   r9   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	 | s)J |j dks2J trHtrHz| }W n tjyG   Y d S w | }|dddksUJ |  |jdks`J 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);
            }
            )c_code)stdinrX       r  3)ACr   )textwrapdedentr!   r   r  r[   r\   r.   r/   r0   r+   r?  rX   r   r   r   r  rT   communicate
returncode)r6   rD   cexesprocr7   envr8   r8   r9   test_weird_environ  s&   

zTestProcess.test_weird_environN)i__name__
__module____qualname____doc__r:   rE   rH   rK   r$   markskipifr   rU   r^   r   rf   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   xdist_groupr   r   r   r  r'   r)   r   r  r  r  r   r7  r<  r>  r@  rJ  rQ  rU  rX  r^  rj  rp  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   rL  r8   r8   r8   r9   r,   H   s   






&+









0
	


B
&


&





4&



).
r,   c                   @   sP   e Zd ZdZedd Zejje	oe
dddd Zdd	 Zd
d Zdd ZdS )	TestPopenzTests for psutil.Popen class.c                 C   s
   t   d S r;   r%   )clsr8   r8   r9   tearDownClass(  s   
zTestPopen.tearDownClasszhangs on OSX + CIrL   c              	   C   s   t ddg}tj|tjtjtd5}|  |  |j t	|s"J t
t |j W d    n1 s5w   Y  |  W d    n1 sHw   Y  tr\|dtj ksZJ d S |dtjksfJ d S )NrV   3import time; [time.sleep(0.1) for x in range(100)];stdoutrX   rK  rZ   )r   r.   Popenr[   r\   r   ro   r   r@  r   r$   r3   r4   r  rG   r   r>   r?   r@   r6   r]   procr8   r8   r9   	test_misc,  s.   
zTestPopen.test_miscc                 C   s|   t jtdgtjtjtjtd}|  W d    n1 sw   Y  |jjs)J |j	js/J |j
js5J |jdks<J d S )Nz-V)rY  rX   r@  rK  r   )r.   rZ  r   r[   r\   r   rG  rY  closedrX   r@  rH  )r6   r\  r8   r8   r9   test_ctx_managerH  s   
zTestPopen.test_ctx_managerc              	   C   s  t ddg}tj|tjtjtd}|  |  t	tj
 |  W d    n1 s.w   Y  t	tj
 |  W d    n1 sHw   Y  t	tj
 |tj W d    n1 sdw   Y  trt	tj
 |tj W d    n1 sw   Y  t	tj
 |tj W d    n1 sw   Y  W d    d S W d    d S W d    d S 1 sw   Y  d S NrV   rW  rX  )r   r.   rZ  r[   r\   r   rG   r>   r$   r3   rR   r=   rI   r?   r@   r   CTRL_C_EVENTCTRL_BREAK_EVENTr[  r8   r8   r9   test_kill_terminateV  sD   

"zTestPopen.test_kill_terminatec              	   C   s   t ddg}tj|tjtjtd1}|  |  t	t
 |j W d    n1 s,w   Y  W d    d S W d    d S 1 sDw   Y  d S r`  )r   r.   rZ  r[   r\   r   rG   r>   r$   r3   r4   r  r[  r8   r8   r9   test__getattribute__s  s&   "zTestPopen.test__getattribute__N)rM  rN  rO  rP  classmethodrV  r$   rQ  rR  r   r   r]  r_  rc  rd  r8   r8   r8   r9   rT  %  s    

rT  )>rP  r  r  r   r  r  r  r1   r?   r  r,  rZ  r[   rE  rE  rr   unittestr   r.   r   r   r   r   r   r	   r
   r   r   psutil._commonr   psutil.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,   rT  r8   r8   r8   r9   <module>   s              h