o
    `qi                     @   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                   @   sL  e Zd ZdZdd Zdd Zdd Zdd	 Zej	j
e d
ddd Zdd Zej	j
edddd Zdd Zdd Zdd Zdd Zdd Zdd Zej	j
e ddd d! Zd"d# Zej	j
e d$dd%d& Zej	j
e ddeed'd(d) Zej	j
e ddej	j
e d*dd+d, Zej	j
e ddej	j
e  d-dd.d/ Z!ej	j
e" ddd0d1 Z#ej	j
e" ddd2d3 Z$ej	j
e" ddd4d5 Z%ej	j
e" ddd6d7 Z&ej	j
e" ddd8d9 Z'ej	j(d:d;d<d= Z)ej	j
e  d>dd?d@ Z*ej	j
e+ dddAdB Z,e- e.e/d'ej	j
e+ dddCdD Z0e- dEdF Z1dGdH Z2ej	j
e3 dddIdJ Z4ej	j
e3 dddKdL Z5dMdN Z6dOdP Z7dQdR Z8dSdT Z9dUdV Z:dWdX Z;e- dYdZ Z<ej	j
e d$dd[d\ Z=ej	j
e d$dd]d^ Z>d_d` Z?dadb Z@dcdd ZAdedf ZBdgdh ZCej	j
eD dddidj ZEej	j
eD dddkdl ZFej	j
eD dddmdn ZGej	j
eHdoddpdq ZIej	j
eHdoddrds ZJej	j
e d$dej	j(d:d;dtdu ZKeed'ej	j
eLpedvddwdx ZMdydz ZNd{d| ZOd}d~ ZPdd ZQe- dd ZRdd ZSdd ZTdd ZUdd ZVdd ZWdd ZXdd ZYdd ZZdd Z[dd Z\dd Z]dd Z^ej	j
e d$ddd Z_ej	j
e d$ddd Z`ej	j
e d$ddd Zadd Zbdd Zcej	j
ed dddd Zeej	j
ed ddej	j
e d$dej	j
efddej	j
edddd ZgdS )TestProcessr   c                 C   sP   t  }|jt ksJ tt d|_W d    d S 1 s!w   Y  d S )N!   )psutilProcesspidosgetpidr$   raisesAttributeErrorselfp r8   `/home/app/PaddleOCR-VL/.venv_paddleocr/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
grandchildZ	child_retZgrandchild_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	dS t
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   Zstop_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           rl   )	r.   r/   cpu_percentrange
isinstancefloatr$   r3   rn   )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/   rv   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 )Nru   z%H:%M:%S)r.   r/   	cpu_timesusersystemchildren_userchildren_systemr   Ziowait_fieldsrp   strftime	localtimegetattr)r6   timesrm   r8   r8   r9   test_cpu_times   s   
zTestProcess.test_cpu_timesznot supportedc                 C   sP   t  }| }|dksJ t  dkr|dksJ | tt  v s&J d S Nr   rY   )r.   r/   Zcpu_num	cpu_countrw   )r6   r7   numr8   r8   r9   test_cpu_num  s   zTestProcess.test_cpu_numc                 C   sB   |   }t }t| | dk sJ tdt|  d S )N   z%Y %m %d %H:%M:%S)r<   rp   abscreate_timer   r   )r6   r7   nowr8   r8   r9   test_create_time  s   zTestProcess.test_create_timez
POSIX onlyc                 C   sZ   t   }|d ur)z
tjtd}W n ty"   t	d Y S w ||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)Zonly_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 )Nrbwbxi@B asciir   r   )r.   r/   Zio_countersopenr   readr   r   Z
read_countZwrite_countr   Z
read_charsZwrite_chars
read_byteswrite_bytes
get_testfnwritebytesrw   len)r6   r7   Zio1fZio2ir8   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)initZioclassvaluer7   r8   r9   cleanupL  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   ZIOPRIO_CLASS_RTZIOPRIO_CLASS_BEZIOPRIO_CLASS_IDLE
addCleanuptupler$   r3   rn   rT   )r6   r   r   r8   r   r9   test_ionice_linuxI  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   ZIOPRIO_NORMALr   ZIOPRIO_VERYLOWZ
IOPRIO_LOWZIOPRIO_HIGHrT   r$   r3   	TypeErrorrn   )r6   r7   r   r8   r8   r9   test_ionice_winv  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 )ZRLIMIT)
startswith.0r   r8   r8   r9   
<listcomp>      z/TestProcess.test_rlimit_get.<locals>.<listcomp>r   rl   rY   )resourcer.   r/   r1   r2   dirr   r   rlimitZ	getrlimitr   )r6   r   r7   namesrm   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.   ZRLIMIT_NOFILEr   r$   r3   rn   _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   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)r.   r/   r   r   RLIMIT_FSIZEr   r   r$   r3   OSErrorr   errnoZEFBIG)r6   r7   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   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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<   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_valueserialrm   c                 C   s   t  }trz| }W n t jy   td Y S w | }t  | }||d ks0J W d    d S 1 s;w   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/   Znum_handlesr5   r8   r8   r9   test_num_handles  s   zTestProcess.test_num_handlesc                 C   s   t  }trz| }W n t jy   td Y S w | }t 8 | }t|t|d ks4J |d }|j	|d ksAJ |j
|d ksJJ |j|d ksSJ 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   Zathreadr8   r8   r9   test_threads  s    "zTestProcess.test_threadsc                 C   s   |   }trz|  W n tjy   td Y S w t| j	t
dd | D  dk s2J t| jt
dd | D  dk sHJ d S )Nr   c                 s       | ]}|j V  qd S r;   )r   r   r8   r8   r9   	<genexpr>      z-TestProcess.test_threads_2.<locals>.<genexpr>g?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/   Zmemory_infomemory_percentr   ZrssZwsetvmsZpagefiler   r   )r6   r7   Zrss1Zvms1Zpercent1ZmemarrZrss2Zvms2Zpercent2memrm   r8   r8   r9   test_memory_info&  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/   Zvirtual_memorytotalZmemory_full_infor   r   r
   r   r   r   ussZpssZswap)r6   r7   r   r   rm   r   r8   r8   r9   test_memory_full_infoE  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[Zpvclockz/proc/self/smapsz
 (deleted)Z64r   >   Zpermsaddrr   )r.   r/   memory_mapsr   setr   r   r   r1   isabsr   existsislinkAssertionErrorr   r   r   basenamestatFileNotFoundErrorS_ISREGst_moder   r   rx   int)
r6   r7   mapsZext_mapsntr   datastfnamer   r8   r8   r9   test_memory_mapsU  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   r  r8   r9   r     s    z:TestProcess.test_memory_maps_lists_lib.<locals>.<listcomp>)r.   r/   r    r   )r6   r7   r   Zlibpathsr8   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?!?)Zmemtyper   )	r.   r/   r   r$   r3   rn   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')Zhey)r<   exer   r   r   r   r1   r   r  sysversion_inforeplacer(   )r6   r7   r  r  veroutr8   r8   r9   test_exe  s$    zTestProcess.test_exec                 C   s   t ddg}| |}tr| g krtdS tststr)| 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   r  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=   td Y S w | dd  }t	rQ|g krQtdS ||ksWJ d S )Nz-v2   rV   r  rY   z#OPENBSD: process turned into zombier  )
r   extendr<   r	   r  r.   ZombieProcessr$   r   r   )r6   r  r7   r   r8   r8   r9   test_long_cmdline  s"   

zTestProcess.test_long_cmdlinec                 C   s^   |   }|  }|dr|d d }tjtjtj	 }|
|s-J ||fd S )Ntrl   )r<   rm   lowerendswithr1   r   r   r   r  
executabler   )r6   r7   rm   r  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   r  )r"   r   stringdigitsr<   r	   rm   r1   r   r   r   statusr.   STATUS_ZOMBIEr   )r6   r  r  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   getuidgeteuidhasattrr,  r6   r7   realZ	effectiveZ_savedr8   r8   r9   	test_uids     
zTestProcess.test_uidsc                 C   r*  r+  )r.   r/   gidsr1   getgidgetegidr0  	getresgidr1  r8   r8   r9   	test_gids#  r4  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   1  s
   z&TestProcess.test_nice.<locals>.cleanupstr)priogetpriorityrY   r   )r.   r/   r$   r3   r   r:  r   r   ZIDLE_PRIORITY_CLASSZBELOW_NORMAL_PRIORITY_CLASSZNORMAL_PRIORITY_CLASSZABOVE_NORMAL_PRIORITY_CLASSZHIGH_PRIORITY_CLASSZREALTIME_PRIORITY_CLASSsubTestrT   r0  r1   r=  PRIO_PROCESSr2   r   )r6   r   r   Zhighest_prior<  Znew_prior8   r   r9   	test_nice0  sp   



zTestProcess.test_nicec                 C   s   t  }| t jksJ d S r;   )r.   r/   r'  ZSTATUS_RUNNINGr5   r8   r8   r9   test_statusr  s   zTestProcess.test_statusc                 C   s   |   }| }tr7|d\}}t }|drtdS ||ks%J dt	j
v r3|t	j
d ks5J d S d S |t ks?J d S )N\$zrunning as service accountZ
USERDOMAIN)r<   usernamer   splitgetpassgetuserr!  r$   r   r1   environ)r6   r7   rD  domainZgetpass_userr8   r8   r9   test_usernamev  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;   )rK  r1   r   dirnamerL  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_getaffinityTZpercpunum_cpur   rY   )r.   r/   cpu_affinityr   r0  r1   listrQ  r0   r   r   rw   rv   rS  r   _procZ_get_eligible_cpusr$   r3   r   r   )r6   r7   initialZall_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 )	NTrR  
   i'  i*  r   1rl   )
r<   r   r.   r   r$   r3   rn   rT  rw   r   )r6   r7   Zinvalid_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;   )rU  )r   Zsubsetr8   r8   r9   r     s    
zATestProcess.test_cpu_affinity_all_combinations.<locals>.<genexpr>)
r.   r/   rT  r   r   rw   r  	itertoolscombinationssorted)r6   r7   rW  Zcombosr   Zcombor8   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	|sJ |qd S )Nr   s   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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.<locals>.<lambda>c                 S      g | ]	}t j|jqS r8   r1   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)];rV   rt   c                 S   re  r8   rf  r   r8   r8   r9   r     r   rg   )r.   r/   r   rb  r   r   flushr   r1   r   r  r   positionisfiler<   r   rw   rp   sleep)r6   r   r   	filenamesfiler  r   r8   rc  r9   test_open_files  s>   


zTestProcess.test_open_filesc                 C   s  t  }tjj}|  }t|dt}| D ]}||j||jks)|j	|
 kr+ nqtd| W  d    S ||j||jksIJ trS|j	dksRJ n	|j	|
 ks\J | d }|d |jkskJ |d |j	kstJ |j| vs}J W d    d S 1 sw   Y  d S )Nwzno file found; files=rl   r   rY   )r.   r/   r1   r   r  r   r   rb  rm   fdfilenor$   rq   r   )r6   r7   r  r   fileobjrl  Zntupler8   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 )Nrn  rY   r   )r.   r/   r   Znum_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 qtdS )Nr   g?z2num ctx switches still the same after 2 iterations)	r.   r/   r   Znum_ctx_switchesrw   rp   rj  r$   rq   )r6   r7   beforerz   afterr8   r8   r9   test_num_ctx_switches0  s   
z!TestProcess.test_num_ctx_switchesc                 C   sF   t  }ttdr| t ksJ |  }| t ks!J d S )Ngetppid)r.   r/   r0  r1   ppidry  r<   r2   r5   r8   r8   r9   	test_ppid@  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   Z
lowest_pidr8   r8   r9   test_parentG  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_ctimeN  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_multiZ  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_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   creationflagsrY   )r.   r/   childrenr<   r   r0   rz  r6   r|  ri   Z	children1Z	children2r  r8   r8   r9   test_childreni  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 )Ni Tr  r   r  rY   )	r.   r/   r   r  r  r<   r   r0   rz  r  r8   r8   r9   test_children_mocked_ctimex  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
dS t|}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   rO    s    z6TestProcess.test_children_duplicates.<locals>.<lambda>)keyr   zPID 0Tr  )collectionsdefaultdictr   r.   process_iterrz  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 )Nrt   rg   )	r<   rd   rw   r'  r.   ZSTATUS_STOPPEDrp   rj  re   )r6   r7   rz   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[  rl   )r$   r3   r   r.   r/   rn   )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 )Nr  rm   )attrsZnet_connectionsfoo)r  Zad_valuezpsutil.Process.niceT)createrO   r:  rY   bar)r.   r/   as_dictr`  keysminr}  rx   rU  r   rP   rT   rR   r0   r$   r3   r  NotImplementedErrorr   rn   )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   Z
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   rz  r  )r6   p1p2Zp1_ppidZp2_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|dS )N>   r   r   >   r  rm   z didn't raise NSP and returned z instead)r.   r  rR   rT   r	   r   r$   rq   )funZfun_namer   r8   r8   r9   assert_raises_nsp:  s    
zFTestProcess.test_halfway_terminated_process.<locals>.assert_raises_nspc                      s    j t vS r;   )r0   r.   r}  r8   r   r8   r9   rO  Q  s    z=TestProcess.test_halfway_terminated_process.<locals>.<lambda>)	r<   rG   r>   r   r   rB   r#   iterall)r6   r  nsr  rm   r8   r   r9   test_halfway_terminated_process2  s   
z+TestProcess.test_halfway_terminated_processc                 C   sV   |   \}}| | ttjjdr'tjjt rJ tjj|j	s)J d S d S )Nproc_is_zombie)
Zspawn_zombieZassert_proc_zombier0  r.   r   Zcextr  r1   r2   r0   )r6   _parentzombier8   r8   r9   test_zombie_processX  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   r  r	  r~   r  r8   r8   r9   $test_zombie_process_is_running_w_exc`  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   r  r'  r(  r~   r  r8   r8   r9    test_zombie_process_status_w_exck  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 )Nrt   ZPSUTIL_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   _identrU  r  Z_pmapr	  r   rP   object_common
contextlibredirect_stderrioStringIOgetvaluer#   r  settersZkillersr>  r$   r3   rR   r;  reprrz  r|  r  r  )r6   Zsubpr7   r   msgr  r  rm   r8   r8   r9   test_reused_pidw  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   >   r5  r-  rD  zNT AUTHORITY\SYSTEMrootrm   )r.   r}  r$   r3   rR   r/   Z
pid_existsrz  r   rT   rn   r>   rG   rd   re   r=   rI   r?   r@   r#   r  Zgettersr  r2  rD  r	   )r6   r7   r   r  r  rm   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   sZ   ddh}t r|g d t|  D ]}||v s|dr#| | qdd |  D S )NZPLATHOME)Z__CF_USER_TEXT_ENCODINGZVERSIONER_PYTHON_PREFER_32_BITZVERSIONER_PYTHON_VERSIONZPYTEST_c                 S   s6   i | ]\}}| d d dd| d d ddqS )r  
)r  )r   kvr8   r8   r9   
<dictcomp>  s
    z@TestProcess.test_environ.<locals>.clean_dict.<locals>.<dictcomp>)r   updaterU  r  r   popr  )r  excluderm   r8   r8   r9   
clean_dict  s   
z,TestProcess.test_environ.<locals>.clean_dict)ZmaxDiffr.   r/   rH  r1   copyr
   r   )r6   r  r7   Zd1Zd2r8   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);
            }
            )Zc_code)stdinrX       r[  3)ACr   )textwrapdedentr!   r   r  r[   r\   r.   r/   r0   r+   r	  rX   r   r   r   rH  rT   communicate
returncode)r6   rD   ZcexeZsprocr7   envr8   r8   r9   test_weird_environ  s&   

zTestProcess.test_weird_environN)h__name__
__module____qualname____doc__r:   rE   rH   rK   r$   markskipifr   rU   r^   r   rf   rk   ro   rr   r|   r   r   r   r   r   r   r   r*   r   r   r   r   r   r   r   r   r   r   r   r   Zxdist_groupr   r   r   r   r'   r)   r   r   r   r   r   r  r  r  r
  r  r  r  r#  r)  r3  r9  r@  rA  rJ  rM  rP  r   rY  r\  ra  r   rm  rr  ru  r	   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  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   r  rZ   )r   r.   Popenr[   r\   r   rm   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)r  rX   r  r  r   )r.   r  r   r[   r\   r   r  r  closedrX   r  r  )r6   r  r8   r8   r9   test_ctx_managerB  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   r  r  )r   r.   r  r[   r\   r   rG   r>   r$   r3   rR   r=   rI   r?   r@   r   ZCTRL_C_EVENTZCTRL_BREAK_EVENTr  r8   r8   r9   test_kill_terminateP  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.   r  r[   r\   r   rG   r>   r$   r3   r4   r  r  r8   r8   r9   test__getattribute__m  s&   "zTestPopen.test__getattribute__N)r  r  r  r  classmethodr  r$   r  r  r   r   r  r  r  r  r8   r8   r8   r9   r    s    

r  )>r  r  r  r   rF  r  r^  r1   r?   rs  r   r%  r[   r  r  rp   Zunittestr   r.   r   r   r   r   r   r	   r
   r   r   Zpsutil._commonr   Zpsutil.testsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r&   r'   r(   r)   r*   r+   r,   r  r8   r8   r8   r9   <module>   s              b