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                   @   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   s>   | j |i |}zt|jW S  tjy   | |j  w N)spawn_testprocpsutilProcesspidNoSuchProcessassertPidGone)selfargskwargssproc r8   M/var/www/vscode/kcb/lib/python3.10/site-packages/psutil/tests/test_process.pyspawn_psprocL   s   zTestProcess.spawn_psprocc                 C   sP   t  }|jt ksJ tt d|_W d    d S 1 s!w   Y  d S )N!   )r/   r0   r1   osgetpidr$   raisesAttributeErrorr4   pr8   r8   r9   test_pidV   s
   "zTestProcess.test_pidc                 C   sJ   |   }|  | }tr|tjksJ n|tj ksJ | | d S r-   )r:   killwaitr   signalSIGTERMSIGKILLassertProcessGoner4   rA   coder8   r8   r9   	test_kill\      zTestProcess.test_killc                 C   sJ   |   }|  | }tr|tjksJ n|tj ksJ | | d S r-   )r:   	terminaterD   r   rE   rF   rH   rI   r8   r8   r9   test_terminatef   rL   zTestProcess.test_terminatec                 C   sX   t rtjntj}|  }|| | }tr||ksJ n|| ks%J | | d S r-   )	r   rE   rG   rF   r:   send_signalrD   r   rH   )r4   sigrA   rJ   r8   r8   r9   test_send_signalp   s   
zTestProcess.test_send_signalz	not POSIX)reasonc              	   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)rE   rF   r:   r   patchProcessLookupErrorr$   r>   r/   r2   rO   PermissionErrorAccessDenied)r4   rP   rA   r8   r8   r9   test_send_signal_mocked{   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:   rD   rH   
subprocessPIPE)r4   cmdrA   rJ   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   rO   rE   SIGSTOPr$   r>   r/   TimeoutExpiredrD   SIGCONTrF   suspendresumerM   r@   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$   r>   r/   rf   rD   rM   r   rE   rF   )r4   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 )Nrk   r   )r:   namer$   r>   r/   rf   rD   
ValueErrorr@   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/| 	dt
rW|tj ksVJ n|tjks^J | | d S )Nr   rd   )r:   r$   r>   r/   rf   rD   rC   timer   failr   rE   rG   rF   rH   )r4   rA   stop_atrJ   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 )Nrc   )intervald           rr   )	r/   r0   cpu_percentrange
isinstancefloatr$   r>   rt   )r4   rA   _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   rU   r/   r0   r}   called)r4   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 )Nr|   z%H:%M:%S)r/   r0   	cpu_timesusersystemchildren_userchildren_systemr   iowait_fieldsrv   strftime	localtimegetattr)r4   timesrs   r8   r8   r9   test_cpu_times  s   
zTestProcess.test_cpu_timesc                 C   sN   dd }|  t   }t }| j|j|jdd | j|j|jdd d S )Nc                  S   sH   t  jd } t  j| k r"tdD ]}|dC }qt  j| k sd S d S )Ng?i    )r<   r   r   r~   )rx   xr8   r8   r9   	waste_cpu  s
   
z/TestProcess.test_cpu_times_2.<locals>.waste_cpu皙?)delta)r/   r0   r   r<   r   assertAlmostEqualr   r   )r4   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   r]   )r/   r0   cpu_num	cpu_countr~   )r4   rA   numr8   r8   r9   test_cpu_num  s   zTestProcess.test_cpu_numc                 C   sb   |   }t }| }t|| }|dkr$| d| d| d| tdt|  d S )Nr   z
expected: z	, found: z, difference: z%Y %m %d %H:%M:%S)r:   rv   create_timeabsrw   r   r   )r4   rA   nowr   
differencer8   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/   r0   terminalr<   pathrealpathr(   RuntimeErrorr$   skip)r4   r   r   r8   r8   r9   test_terminal7  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/   r0   io_countersopenr   readr   r   
read_countwrite_countr   
read_charswrite_chars
read_byteswrite_bytes
get_testfnwritebytesr~   len)r4   rA   io1fio2ir8   r8   r9   test_io_countersC  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)initioclassvaluerA   r8   r9   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   IOPRIO_CLASS_RTIOPRIO_CLASS_BEIOPRIO_CLASS_IDLE
addCleanuptupler$   r>   rt   rX   )r4   r   r   r8   r   r9   test_ionice_linuxk  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   r]   r   zis not a valid priority)r/   r0   r   r   IOPRIO_NORMALr   IOPRIO_VERYLOW
IOPRIO_LOWIOPRIO_HIGHrX   r$   r>   	TypeErrorrt   )r4   rA   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   rr   r]   )resourcer/   r0   r<   r=   dirr   r   rlimit	getrlimitr   )r4   r   rA   namesrs   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)r^   r^   zcan't use prlimitr   r   )r^   r^   r^   )
r:   r   r/   RLIMIT_NOFILEr   r$   r>   rt   _psplatformr0   r@   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
jt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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)r/   r0   r   r   RLIMIT_FSIZEr   r   r$   r>   OSErrorr   errnoEFBIG)r4   rA   testfnsofthardr   excr8   r8   r9   test_rlimit  s&   zTestProcess.test_rlimitc                 C   s   t  }|t j\}}zG|t jd|f |t jt j|f t|  d}|d W d    n1 s7w   Y  W |t j||f |t j||fksRJ d S |t j||f |t j||fksiJ w )Nr   r   s   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)r/   r0   r   r   RLIM_INFINITYr   r   r   )r4   rA   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/   r0   r   r   r   )r4   rA   r   r   r8   r8   r9   test_rlimit_infinity_value  s   z&TestProcess.test_rlimit_infinity_valuec                 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 accessr]   )r/   r0   r	   num_threadsrX   r$   r   r   )r4   rA   step1step2r8   r8   r9   test_num_threads	  s   
"zTestProcess.test_num_threadszWINDOWS onlyc                 C   s   t  }| dksJ d S r   )r/   r0   num_handlesr@   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   r]   r   r   )r/   r0   r	   threadsrX   r$   r   r   r   id	user_timesystem_time)r4   rA   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	  C  r
  )r:   r	   r  r/   rX   r$   r   r   r   r   sumr   r@   r8   r8   r9   test_threads_24  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/   r0   memory_infomemory_percentr   rsswsetvmspagefiler   r   )r4   rA   rss1vms1percent1memarrrss2vms2percent2memrs   r8   r8   r9   test_memory_infoG  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/   r0   virtual_memorytotalmemory_full_infor   r   r
   r   r   r   usspssswap)r4   rA   r  r  rs   r   r8   r8   r9   test_memory_full_infof  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/   r0   memory_mapsr   setr   r   r   r<   isabsr   existsislinkAssertionErrorr   r   r   basenamestatFileNotFoundErrorS_ISREGst_moder   r   r   int)
r4   rA   mapsext_mapsntr   datastfnamer   r8   r8   r9   test_memory_mapsv  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-   )r<   r   r   normcaser   r8   r8   r9   normpath  s   z8TestProcess.test_memory_maps_lists_lib.<locals>.normpathc                    s   g | ]} |j qS r8   )r   r   r=  r8   r9   r     s    z:TestProcess.test_memory_maps_lists_lib.<locals>.<listcomp>)r/   r0   r    r)  )r4   rA   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/   r0   r  r$   r>   rt   r   r   r   r@   r8   r8   r9   test_memory_percent  s   zTestProcess.test_memory_percentc                 C   sL   |   }| s
J | sJ |  |  | rJ | r$J d S r-   )r:   
is_runningrC   rD   r@   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   .r]    rZ   zimport os; print('hey')hey)r:   exer   r.  r   r   r<   r   r<  sysversion_inforeplacer(   )r4   rA   rH  r<  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 )NrZ   2import time; [time.sleep(0.1) for x in range(100)]OPENBSD: returned EBUSYr    r]   )r   r:   r   cmdliner$   r   r	   r   r   r   join)r4   rR  rA   pyexer8   r8   r9   test_cmdline  s   

, zTestProcess.test_cmdlinezbroken on PYPYc                 C   s   t g}|dgd  |ddg | |}tr2z| |ks"J W d S  tjy1   tdw | }t	rA|g krAtd||ksGJ d S )Nz-v2   rZ   rO  z#OPENBSD: process turned into zombierP  )
r   extendr:   r	   rR  r/   ZombieProcessr$   r   r   )r4   rR  rA   r   r8   r8   r9   test_long_cmdline  s    


zTestProcess.test_long_cmdlinec                 C   sH   |   }|  }tjtjtj }|	|s"J ||fd S r-   )
r:   rs   lowerr<   r   r/  r   rI  
executabler   )r4   rA   rs   rT  r8   r8   r9   	test_name  s   zTestProcess.test_namezunreliable on PYPYc                 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   )suffixrZ   rO  )r"   r   stringdigitsr:   r	   rs   r<   r   r/  r.  statusr/   STATUS_ZOMBIEr   )r4   rT  rR  rA   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/   r0   uidsr<   getuidgeteuidhasattrre  r4   rA   real	effective_savedr8   r8   r9   	test_uidsD     
zTestProcess.test_uidsc                 C   rc  rd  )r/   r0   gidsr<   getgidgetegidri  	getresgidrj  r8   r8   r9   	test_gidsQ  ro  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/   rX   )r   r   r8   r9   r   _  s
   z&TestProcess.test_nice.<locals>.cleanupstr)priogetpriorityr]   r   )r/   r0   r$   r>   r   ru  r   r   IDLE_PRIORITY_CLASSBELOW_NORMAL_PRIORITY_CLASSNORMAL_PRIORITY_CLASSABOVE_NORMAL_PRIORITY_CLASSHIGH_PRIORITY_CLASSREALTIME_PRIORITY_CLASSsubTestrX   ri  r<   rx  PRIO_PROCESSr=   r   )r4   r   r   highest_priorw  new_prior8   r   r9   	test_nice^  sp   



zTestProcess.test_nicec                 C   s   t  }| t jksJ d S r-   )r/   r0   r`  STATUS_RUNNINGr@   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   r<   environ)r4   rA   r  domaingetpass_userr8   r8   r9   test_username  s   


zTestProcess.test_usernamec                 C   s    |   }| t ksJ d S r-   )r:   cwdr<   getcwdr@   r8   r8   r9   test_cwd  s   zTestProcess.test_cwdc                    s(   t ddg}| | t fdd d S )NrZ   zFimport os, time; os.chdir('..'); [time.sleep(0.1) for x in range(100)]c                      s      tjt kS r-   )r  r<   r   dirnamer  r8   r   r8   r9   <lambda>  s    z(TestProcess.test_cwd_2.<locals>.<lambda>)r   r:   r   )r4   ra   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   r]   )r/   r0   cpu_affinityr   ri  r<   listr  r1   r   r*  r~   r}   r  r   _proc_get_eligible_cpusr$   r>   r   r   )r4   rA   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   1rr   )
r:   r   r/   r   r$   r>   rt   r  r~   r   )r4   rA   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   r]   c                 s   s    | ]	}|rt |V  qd S r-   )r  )r   subsetr8   r8   r9   r	    s    
zATestProcess.test_cpu_affinity_all_combinations.<locals>.<genexpr>)
r/   r0   r  r   r   r~   rW  	itertoolscombinationssorted)r4   rA   r  combosr   combor8   r8   r9   "test_cpu_affinity_all_combinations  s   



z.TestProcess.test_cpu_affinity_all_combinationszbro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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxc                      s   t  t  kS r-   )r   
open_filesr8   filesrA   r8   r9   r    s    z-TestProcess.test_open_files.<locals>.<lambda>c                 S      g | ]	}t j|jqS r8   r<   r   r<  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)];rZ   r{   c                 S   r  r8   r  r   r8   r8   r9   r   -  r   rk   )r/   r0   r   r  r   r   flushr   r<   r   r<  r   positionisfiler:   r   r~   rv   sleep)r4   r   r   	filenamesfilerR  r   r8   r  r9   test_open_files  s>   


zTestProcess.test_open_filesc                 C   s  t  }tjj}|  }t|dm}| D ]}||j||jks)|j	|
 kr+ nq| d| ||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=rr   r   r]   )r/   r0   r<   r   r<  r   r   r  rs   fdfilenorw   r   )r4   rA   r<  r   fileobjr  ntupler8   r8   r9   test_open_files_27  s&   "zTestProcess.test_open_files_2c                 C   s   t  }|  }| }t|d}| |j | |d ks!J t }| |j | |d ks5J |  |  | |ksEJ d S )Nr  r]   r   )r/   r0   r   num_fdsr   r   closesocket)r4   rA   r   startr  sockr8   r8   r9   test_num_fdsR  s   
zTestProcess.test_num_fdsz not reliable on OPENBSD & NETBSDc                 C   sP   t  }t| }tdD ]}td t| }||kr" d S q| d)Nr   g?z2num ctx switches still the same after 2 iterations)r/   r0   r  num_ctx_switchesr~   rv   r  rw   )r4   rA   beforer   afterr8   r8   r9   test_num_ctx_switchesa  s   

z!TestProcess.test_num_ctx_switchesc                 C   sF   t  }ttdr| t ksJ |  }| t ks!J d S )Ngetppid)r/   r0   ri  r<   ppidr  r:   r=   r@   r8   r8   r9   	test_ppido  s
   
zTestProcess.test_ppidc                 C   sD   |   }| jt ksJ t d }t| d u s J d S r   )r:   parentr1   r<   r=   r/   pidsr0   )r4   rA   
lowest_pidr8   r8   r9   test_parentv  s   zTestProcess.test_parentc                 C   s8   t  }|  \}}| |ksJ | |ksJ d S r-   )r/   r0   rl   r  r4   r  rm   rn   r8   r8   r9   test_parent_multi}  s   zTestProcess.test_parent_multic                 C   s`   t  }| s
J |  \}}| d |ksJ | d |ks$J | d |ks.J d S r   )r/   r0   parentsrl   r  r8   r8   r9   test_parents  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   )creationflagsr]   )r/   r0   childrenr:   r   r1   r  )r4   r  rm   	children1	children2r  r8   r8   r9   test_children  s   zTestProcess.test_childrenc                 C   sb   t  }|  \}}| |gksJ |jdd||gksJ |  |  |jddr/J d S )NTr  )r/   r0   rl   r  rM   rD   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 )	Nr]   c                 S   s   | d S )Nr]   r8   )r   r8   r8   r9   r    s    z6TestProcess.test_children_duplicates.<locals>.<lambda>)keyr   zPID 0Tr  )collectionsdefaultdictr4  r/   process_iterr  Errormaxitemsr   r$   r   r0   r  rX   r   r*  )r4   tablerA   r1   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   r]   )r/   r0   rl   r  r   r  )r4   r  rm   rn   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 )Nr{   rk   )	r:   rh   r~   r`  r/   STATUS_STOPPEDrv   r  ri   )r4   rA   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  rr   )r$   r>   r   r/   r0   rt   r4   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 )NrH  rs   )attrsnet_connectionsfoo)r  ad_valuezpsutil.Process.niceT)createrT   ru  r]   bar)r/   r0   as_dictr  keysminr  r   r  r   rU   rX   r2   r1   r$   r>   rX  NotImplementedErrorr   rt   )r4   rA   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_timesr]   r   r/   r0   r   rU   oneshotr   
call_countr4   rA   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_enterr]   r   r  )r4   rA   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-   )rl   r  r  )r4   p1p2p1_ppidp2_ppidr8   r8   r9   test_oneshot_cache.  s   

"zTestProcess.test_oneshot_cachec                    sn   fdd}         trt fdd   t }||jD ]	\}}||| q+d S )Nc                    sz   z|  }W n$ t jy     t jy   Y d S  t jy)   tr(|dv r(Y d S  w tr2|dv r2d S  | d|d)N>   r  r   >   rH  rs   z didn't raise NSP and returned z instead)r/   rX  r2   rX   r	   r   rw   )funfun_namer   r  r8   r9   assert_raises_nspE  s    
zFTestProcess.test_halfway_terminated_process.<locals>.assert_raises_nspc                      s    j t vS r-   )r1   r/   r  r8   r   r8   r9   r  \  s    z=TestProcess.test_halfway_terminated_process.<locals>.<lambda>)	r:   rM   rD   r   r   rH   r#   iterall)r4   r
  nsr  rs   r8   )rA   r4   r9   test_halfway_terminated_process=  s   
z+TestProcess.test_halfway_terminated_processc                 C   s   |   \}}| | d S r-   )spawn_zombieassertProcessZombie)r4   _parentzombier8   r8   r9   test_zombie_processc  s   zTestProcess.test_zombie_processc                 C   sX   t  }tjdt dd}| sJ |jsJ W d    d S 1 s%w   Y  d S )Nzpsutil.Processr   rS   )r/   r0   r   rU   rX  rC  r   r  r8   r8   r9   $test_zombie_process_is_running_w_exch  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   rS   )r/   r0   r   rU   rX  r`  ra  r   r  r8   r8   r9    test_zombie_process_status_w_excs  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 )Nr{   PSUTIL_DEBUGTz+refreshing Process instance for reused PID z4process no longer exists and its PID has been reusedF)clear_cache)rs   r   zterminated + PID reused)!r.   r/   r0   r1   r   _identr  r  _pmaprC  r   rU   object_common
contextlibredirect_stderrioStringIOgetvaluer#   r  setterskillersr  r$   r>   r2   rv  reprr  r  r  r  )r4   subprA   r   msgr  r  rs   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   r]   >   rp  rf  r  zNT AUTHORITY\SYSTEMrootrs   )r/   r  r$   r>   r2   r0   
pid_existsr  r   rX   rt   rD   rM   rh   ri   rC   rO   rE   rF   r#   r  gettersr!  rk  r  r	   )r4   rA   r   r  r  rs   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 )rF  
)rK  )r   kvr8   r8   r9   
<dictcomp>  s    z@TestProcess.test_environ.<locals>.clean_dict.<locals>.<dictcomp>)r   rW  popr  )r  excluders   r8   r8   r9   
clean_dict  s   z,TestProcess.test_environ.<locals>.clean_dict)maxDiffr/   r0   r  r<   copyr
   r   )r4   r9  rA   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)stdinr\       r  3)ACr   )textwrapdedentr!   r   r.   r_   r`   r/   r0   r1   r+   rC  r\   r   r   r   r  rX   communicate
returncode)r4   rJ   cexer7   rA   envr8   r8   r9   test_weird_environ  s&   

zTestProcess.test_weird_environN)h__name__
__module____qualname____doc__r:   rB   rK   rN   rQ   r$   markskipifr   rY   rb   r   rj   rq   ru   ry   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@  rB  rD  rN  rU  r   rY  r\  rb  rn  rt  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   rK  r8   r8   r8   r9   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%   )clsr8   r8   r9   tearDownClass*  s   
zTestPopen.tearDownClassc              	   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 )NrZ   3import time; [time.sleep(0.1) for x in range(100)];stdoutr\   rJ  r^   )r   r/   Popenr_   r`   r   rs   r   r@  r   r$   r>   r?   r  rM   r   rD   rE   rF   r4   ra   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)rW  r\   r@  rJ  r   )r/   rX  r   r_   r`   r   rG  rW  closedr\   r@  rH  )r4   rZ  r8   r8   r9   test_ctx_managerI  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 NrZ   rU  rV  )r   r/   rX  r_   r`   r   rM   rD   r$   r>   r2   rC   rO   rE   rF   r   CTRL_C_EVENTCTRL_BREAK_EVENTrY  r8   r8   r9   test_kill_terminateW  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/   rX  r_   r`   r   rM   rD   r$   r>   r?   r  rY  r8   r8   r9   test__getattribute__t  s&   "zTestPopen.test__getattribute__N)
rL  rM  rN  rO  classmethodrT  r[  r]  ra  rb  r8   r8   r8   r9   rR  '  s    
rR  )>rO  r  r  r   r  r  r  r<   rE   r  r0  r^  r_   rI  rE  rv   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,   rR  r8   r8   r8   r9   <module>   s              i