
    Xi                        d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlZddlmZ ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! ddlm"Z" ddlm#Z# ddlm$Z$ ddlm%Z% ddlm&Z& ddlm'Z' ddlm(Z( ddlm)Z) ddlm*Z* ddlm+Z+ ddlm,Z, ddlm-Z- ddlm.Z. ddlm/Z/ ddlm0Z0 dd lm1Z1 dd!lm2Z2 dd"lm3Z3 dd#lm4Z4 dd$lm5Z5 dd%lm6Z6 dd&lm7Z7 dd'lm8Z8 dd(lm9Z9 dd)lm:Z: dd*lm;Z;  G d+ d,e.          Z< G d- d.e.          Z=dS )/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)pytestreap_children)retry_on_failure)sh)skip_on_access_denied)skip_on_not_implemented)wait_for_pidc                       e Zd ZdZd Zd Zd Zd Zej	        
                    e d          d             Zd	 Zej	        
                    ed
          d             Zd Zd Zd Zd Zd Zd Zej	        
                    e d          d             Zd Zej	        
                    e d          d             Zej	        
                    e d           ee          d                         Zej	        
                    e d          ej	        
                    e d          d                         Zej	        
                    e d          ej	        
                    e  d          d                         Z!ej	        
                    e" d          d             Z#ej	        
                    e" d          d             Z$ej	        
                    e" d          d             Z%ej	        
                    e" d          d              Z&ej	        
                    e" d          d!             Z'ej	        (                    d"#          d$             Z)ej	        
                    e  d%          d&             Z*ej	        
                    e+ d          d'             Z, e-             e.e/          ej	        
                    e+ d          d(                                     Z0 e-            d)             Z1d* Z2ej	        
                    e3 d          d+             Z4ej	        
                    e3 d          d,             Z5d- Z6d. Z7d/ Z8d0 Z9d1 Z:d2 Z; e-            d3             Z<ej	        
                    e d          d4             Z=ej	        
                    e d          d5             Z>d6 Z?d7 Z@d8 ZAd9 ZBd: ZCej	        
                    eD d          d;             ZEej	        
                    eD d          d<             ZFej	        
                    eD d          d=             ZGej	        
                    eHd>          d?             ZIej	        
                    eHd>          d@             ZJej	        
                    e d          ej	        (                    d"#          dA                         ZK ee          ej	        
                    eLpedB          dC                         ZMdD ZNdE ZOdF ZPdG ZQ e-            dH             ZRdI ZSdJ ZTdK ZUdL ZVdM ZWdN ZXdO ZYdP ZZdQ Z[dR Z\dS Z]dT Z^ej	        
                    e d          dU             Z_ej	        
                    e d          dV             Z`ej	        
                    e d          dW             ZadX ZbdY Zcej	        
                    ed d          dZ             Zeej	        
                    ed d          ej	        
                    e d          ej	        
                    efd[          ej	        
                    ed\          d]                                                 Zgd^S )_TestProcessr   c                     t          j                    }|j        t          j                    k    sJ t          j        t                    5  d|_        d d d            d S # 1 swxY w Y   d S )N!   )psutilProcesspidosgetpidr%   raisesAttributeErrorselfps     q/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/psutil/tests/test_process.pytest_pidzTestProcess.test_pidK   s    Nu	####]>** 	 	AE	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A  A$'A$c                    |                                  }|                                 |                                }t          r|t          j        k    sJ n|t          j         k    sJ |                     |           d S N)spawn_psprockillwaitr   signalSIGTERMSIGKILLassert_proc_goner9   r:   codes      r;   	test_killzTestProcess.test_killQ   sz    	vvxx 	+6>)))))FN?****a         c                    |                                  }|                                 |                                }t          r|t          j        k    sJ n|t          j         k    sJ |                     |           d S r>   )r?   	terminaterA   r   rB   rC   rE   rF   s      r;   test_terminatezTestProcess.test_terminate[   sz    	vvxx 	+6>)))))FN?****a     rI   c                    t           rt          j        nt          j        }|                                 }|                    |           |                                }t          r	||k    sJ n	|| k    sJ |                     |           d S r>   )	r   rB   rD   rC   r?   send_signalrA   r   rE   )r9   sigr:   rG   s       r;   test_send_signalzTestProcess.test_send_signale   s     %9fnn6>	cvvxx 	 3;;;;;C4<<<<a     rI   z	not POSIXreasonc                 h   t           j        }|                                 }t          j        dt
                    5  t          j        t          j	                  5  |
                    |           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |                                 }t          j        dt                    5  t          j        t          j                  5  |
                    |           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nzpsutil.os.killside_effect)rB   rC   r?   r   patchProcessLookupErrorr%   r6   r1   NoSuchProcessrN   PermissionErrorAccessDenied)r9   rO   r:   s      r;   test_send_signal_mockedz#TestProcess.test_send_signal_mockedp   s   nZ(6HIII 	# 	#v344 # #c"""# # # # # # # # # # # # # # #	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# Z(oFFF 	# 	#v233 # #c"""# # # # # # # # # # # # # # #	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#sk   BA=1B=B	BB	BBBD'-DD'D	D'D	D''D+.D+c                    t           ddg}|                     |          }|                                }|dk    sJ |                     |           t           ddg}|                     |t          j                  }|                                }|dk    sJ |                     |           t           ddg}|                     |          }|                                }|dk    sJ |                     |           t           dd	g}|                     |          }|                                }|dk    sJ |                     |           d S )
N-cpassr   z1 / 0)stderr   zimport sys; sys.exit(5);   zimport os; os._exit(5);)r   r?   rA   rE   
subprocessPIPE)r9   cmdr:   rG   s       r;   test_wait_exitedzTestProcess.test_wait_exited}   sG    4(c""vvxxqyyyya   4)c*/::vvxxqyyyya   4!;<c""vvxxqyyyya   4!:;c""vvxxqyyyya     rI   zfails on NETBSDc                    |                                  }t          rA|                    t          j                   t          j        t          j                  5  |	                    d           d d d            n# 1 swxY w Y   |                    t          j
                   t          j        t          j                  5  |	                    d           d d d            n# 1 swxY w Y   |                    t          j                   |	                                t          j         k    sJ |	                                t          j         k    sJ d S |                                 t          j        t          j                  5  |	                    d           d d d            n# 1 swxY w Y   |                                 t          j        t          j                  5  |	                    d           d d d            n# 1 swxY w Y   |                                 |	                                t          j        k    sJ |	                                t          j        k    sJ d S )NMbP?)timeout)r?   r   rN   rB   SIGSTOPr%   r6   r1   TimeoutExpiredrA   SIGCONTrC   suspendresumerK   r8   s     r;   test_wait_stoppedzTestProcess.test_wait_stopped   s    	. MM&.)))v455 & &u%%%& & & & & & & & & & & & & & &MM&.)))v455 & &u%%%& & & & & & & & & & & & & & &MM&.)))6688....6688......IIKKKv455 & &u%%%& & & & & & & & & & & & & & &HHJJJv455 & &u%%%& & & & & & & & & & & & & & &KKMMM6688v~----6688v~------sH   A==BBC((C,/C,F33F7:F70HHHc                    |                                  \  }}t          j        t          j                  5  |                    d           d d d            n# 1 swxY w Y   t          j        t          j                  5  |                    d           d d d            n# 1 swxY w Y   |                                 |                                 |                                }|                                }t          r|t          j	         k    sJ |J d S |t          j	        k    sJ |t          j	        k    sJ d S )N{Gz?)
spawn_children_pairr%   r6   r1   rj   rA   rK   r   rB   rC   )r9   child
grandchild	child_retgrandchild_rets        r;   test_wait_non_childrenz"TestProcess.test_wait_non_children   s    !4466z]6011 	 	JJt	 	 	 	 	 	 	 	 	 	 	 	 	 	 	]6011 	" 	"OOD!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	JJLL	#** 	///// ")))))..........s#   AAAB##B'*B'c                    |                                  }|                                 t          j        t          j                  5  |                    d           d d d            n# 1 swxY w Y   t          j        t          j                  5  |                    d           d d d            n# 1 swxY w Y   t          j        t                    5  |                    d           d d d            d S # 1 swxY w Y   d S )Nrp   r   )r?   namer%   r6   r1   rj   rA   
ValueErrorr8   s     r;   test_wait_timeoutzTestProcess.test_wait_timeout   s   	]6011 	 	FF4LLL	 	 	 	 	 	 	 	 	 	 	 	 	 	 	]6011 	 	FF1III	 	 	 	 	 	 	 	 	 	 	 	 	 	 	]:&& 	 	FF2JJJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s6   A))A-0A-B44B8;B8C;;C?C?c                    |                                  }t          j        t          j                  5  |                    d           d d d            n# 1 swxY w Y   |                                 t          j                    t          z   }t          j                    |k     rC	 |                    d          }n@# t          j        $ r Y nw xY wt          j                    |k     Ct          j	        d          S t          r|t          j         k    sJ n|t          j        k    sJ |                     |           d S )Nr   rh   )r?   r%   r6   r1   rj   rA   r@   timer   failr   rB   rD   rC   rE   )r9   r:   stop_atrG   s       r;   test_wait_timeout_nonblockingz)TestProcess.test_wait_timeout_nonblocking   sd   ]6011 	 	FF1III	 	 	 	 	 	 	 	 	 	 	 	 	 	 		)++.ikkG##vvayy(   	 ikkG## ;y))) 	*FN?*****6>))))a     s#   AAA'B= =CCc                    t          j                    }|                    d           |                    d           t          d          D ]7}|                    d           }t	          |t
                    sJ |dk    sJ 8t          j        t                    5  |                    d           d d d            d S # 1 swxY w Y   d S )Nrg   )intervald           rx   )	r1   r2   cpu_percentrange
isinstancefloatr%   r6   rz   )r9   r:   _percents       r;   test_cpu_percentzTestProcess.test_cpu_percent   s   N	u%%%	u%%%s 	" 	"AmmTm22Ggu-----c>>>>>]:&& 	' 	'MM2M&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	's    CCCc                     t          j        dd           5 }t          j                                                     |j        sJ 	 d d d            d S # 1 swxY w Y   d S )Nzpsutil.cpu_count)return_value)r   rV   r1   r2   r   called)r9   ms     r;   test_cpu_percent_numcpus_nonez)TestProcess.test_cpu_percent_numcpus_none   s    Z*>>> 	!N((***8OOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   /AAAc           
         t          j                                                    }|j        dk    s
J |            |j        dk    s
J |            |j        dk    s
J |            |j        dk    s
J |            t          r|j        dk    s
J |            |j	        D ]7}t          j        dt          j        t          ||                               8d S )Nr   z%H:%M:%S)r1   r2   	cpu_timesusersystemchildren_userchildren_systemr   iowait_fieldsr}   strftime	localtimegetattr)r9   timesry   s      r;   test_cpu_timeszTestProcess.test_cpu_times   s      **,,zS   %   |s"""E""""c)))5)))$+++U+++ 	.<3&&&&&&M 	L 	LDM*dnWUD5I5I&J&JKKKK	L 	LrI   znot supportedc                    t          j                    }|                                }|dk    sJ t          j                    dk    r|dk    sJ |                                t	          t          j                              v sJ d S Nr   r`   )r1   r2   cpu_num	cpu_countr   )r9   r:   nums      r;   test_cpu_numzTestProcess.test_cpu_num  sz    Niikkaxxxx""!8888yy{{eF$4$6$677777777rI   c                    |                                  }t          j                    }t          |                                |z
            dk     sJ t          j        dt          j        |                                                     d S )N   z%Y %m %d %H:%M:%S)r?   r}   abscreate_timer   r   )r9   r:   nows      r;   test_create_timezTestProcess.test_create_time  so    ikk1==??S())A----)4>!--//+J+JKKKKKrI   z
POSIX onlyc                 
   t          j                                                    }|[	 t          j                            t          d                    }||k    sJ d S # t          $ r t          j	        d          cY S w xY wd S )Nttyzcan't rely on `tty` CLI)
r1   r2   terminalr4   pathrealpathr)   RuntimeErrorr%   skip)r9   r   r   s      r;   test_terminalzTestProcess.test_terminal  s    >##,,..'g&&r%yy11
  3	   > > >{#<=====>  s   ,A B ?B )only_ifc                    t          j                    }|                                }t          t          d          5 }|                                 d d d            n# 1 swxY w Y   |                                }t          sWt          sP|j        |j        k    sJ |j	        |j	        k    sJ t          r$|j        |j        k    sJ |j        |j        k    sJ n$|j        |j        k    sJ |j        |j        k    sJ |                                }t          |                                 d          5 }|                    t#          ddz  d                     d d d            n# 1 swxY w Y   |                                }|j	        |j	        k    sJ |j        |j        k    sJ |j        |j        k    sJ |j        |j        k    sJ t          r$|j        |j        k    sJ |j        |j        k    sJ t%          t'          |                    D ],}t          r|dk    r||         dk    sJ ||         dk    sJ -d S )Nrbwbxi@B asciir   r   )r1   r2   io_countersopenr   readr   r   
read_countwrite_countr   
read_charswrite_chars
read_byteswrite_bytes
get_testfnwritebytesr   len)r9   r:   io1fio2is         r;   test_io_counterszTestProcess.test_io_counters!  s    Nmmoo*d## 	qFFHHH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	mmoo 	63 	6>CN2222?co5555 :~6666#/9999>S^3333?co5555 mmoo$//##T** 	3aGGE#-11222	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3mmoo#/1111#/1111~////~//// 	4?S_4444>S^3333 s3xx 	 	A qAvvq6Q;;;;q6Q;;;;;	 	s#   AA"%A"5'E((E,/E,z
linux onlyc                    fd}t          j                    t          s*                                d         t           j        k    sJ t           j        dk    sJ t           j        dk    sJ t           j        dk    sJ t           j        dk    sJ                                 }|                     ||                               t           j                   t                                                    t           j        dfk    sJ t          j        t                    5                      t           j        d           d d d            n# 1 swxY w Y                       t           j                   t                                                    t           j        dfk    sJ                     t           j        d           t                                                    t           j        dfk    sJ t          j        t                    5                      t           j        d           d d d            n# 1 swxY w Y   	                     t           j        d           n# t           j        $ r Y nw xY wt          j        t          d	
          5                      t           j        d           d d d            n# 1 swxY w Y   t          j        t          d	
          5                      t           j        d           d d d            n# 1 swxY w Y   t          j        t          d
          5                      d           d d d            d S # 1 swxY w Y   d S )Nc                 b    | \  }}|t           j        k    rd}                    ||           d S Nr   )r1   IOPRIO_CLASS_NONEionice)initioclassvaluer:   s      r;   cleanupz.TestProcess.test_ionice_linux.<locals>.cleanupL  s9    !NGU&222HHWe$$$$$rI   r   r`   r         r      zioclass accepts no valuematchz$'ioclass' argument must be specified)r1   r2   r   r   r   IOPRIO_CLASS_RTIOPRIO_CLASS_BEIOPRIO_CLASS_IDLE
addCleanuptupler%   r6   rz   rZ   )r9   r   r   r:   s      @r;   test_ionice_linuxzTestProcess.test_ionice_linuxI  sc   	% 	% 	% 	% 	% N 	=88::a=F$<<<<<'1,,,,%****%****'1,,,,xxzz&&& 	
)***QXXZZ  V%=q$AAAAA]:&& 	8 	8HHV-QH777	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	
'(((QXXZZ  V%;Q$?????	'q111QXXZZ  V%;Q$?????]:&& 	6 	6HHV+1H555	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6	HHV+1H5555" 	 	 	D	 ]:-GHHH 	2 	2HHV-q111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2]:-GHHH 	2 	2HHV-q111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2]D
 
 
 	 	 HH1H	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	sl   ("EEE "III!I< <JJ,!KK K?!L,,L03L0M66M:=M:z!not supported on this win versionc                    t          j                    }t          s$|                                t           j        k    sJ |                                }|                     |j        |           |                    t           j                   |                                t           j        k    sJ |                    t           j                   |                                t           j        k    sJ 	 |                    t           j                   |                                t           j        k    sJ n# t           j	        $ r Y nw xY wt          j        t          d          5  |                    t           j        d           d d d            n# 1 swxY w Y   t          j        t          d          5  |                    t           j        dz              d d d            d S # 1 swxY w Y   d S )Nz&value argument not accepted on Windowsr   r`   r   zis not a valid priority)r1   r2   r   r   IOPRIO_NORMALr   IOPRIO_VERYLOW
IOPRIO_LOWIOPRIO_HIGHrZ   r%   r6   	TypeErrorrz   )r9   r:   r   s      r;   test_ionice_winzTestProcess.test_ionice_winv  sA   
 N 	688::!55555xxzz$''' 	
&'''xxzzV22222	"###xxzzV.....	4HHV'((( 88::!333333 " 	 	 	D	
 ]E
 
 
 	4 	4 HHV)H333	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 ]:-FGGG 	- 	-HHV'!+,,,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-s6   5D9 9E
E)"FFF=#G--G14G1c                 8   dd l }t          j        t          j                              }d t          t                    D             }|s
J |            |D ]}t          t          |          }|dk    sJ |t          |          v rM|t          ||          k    sJ t          rN|                    |          |	                    |          k    sJ }|                    |          }t          |          dk    sJ |d         dk    sJ |d         dk    sJ d S )Nr   c                 <    g | ]}|                     d           |S )RLIMIT)
startswith.0r   s     r;   
<listcomp>z/TestProcess.test_rlimit_get.<locals>.<listcomp>  s)    BBBq1<<+A+ABBBBrI   r   rx   r`   )resourcer1   r2   r4   r5   dirr   r   rlimit	getrlimitr   )r9   r   r:   namesry   r   rets          r;   test_rlimit_getzTestProcess.test_rlimit_get  s-   N29;;''BBCKKBBBe 	$ 	$DFD))EA::::s8}}$$$ 7 77777  xx(*<*<U*C*CCCCCChhuoo3xx1}}}}1v||||1v|||||	$ 	$rI   c                 4   |                                  }|                    t          j        d           |                    t          j                  dk    sJ t          ret          j        t          d          5  t          j        	                    d                              d           d d d            n# 1 swxY w Y   t          j        t                    5  |                    t          j        d           d d d            d S # 1 swxY w Y   d S )N)ra   ra   zcan't use prlimitr   r   )ra   ra   ra   )
r?   r   r1   RLIMIT_NOFILEr   r%   r6   rz   _psplatformr2   r8   s     r;   test_rlimit_setzTestProcess.test_rlimit_set  so   	%v...xx,--7777  	8z1DEEE 8 8"**1--44Q7778 8 8 8 8 8 8 8 8 8 8 8 8 8 8]:&& 	6 	6HHV)9555	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6s$   <3B;;B?B?!DDDc                    t          j                    }|                                 }|                    t           j                  \  }}	 |                    t           j        d|f           t          |d          5 }|                    d           d d d            n# 1 swxY w Y   t          j        t                    5 }t          |d          5 }|                    d           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |j
        j        t          j        k    sJ 	 |                    t           j        ||f           |                    t           j                  ||fk    sJ d S # |                    t           j        ||f           |                    t           j                  ||fk    sJ w xY w)N   r   s   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXs  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)r1   r2   r   r   RLIMIT_FSIZEr   r   r%   r6   OSErrorr   errnoEFBIG)r9   r:   testfnsofthardr   excs          r;   test_rlimitzTestProcess.test_rlimit  sR   N""XXf122
d	AHHV(4,777fd## %q$$$% % % % % % % % % % % % % % % w'' )3&$'' )1GGK((() ) ) ) ) ) ) ) ) ) ) ) ) ) )) ) ) ) ) ) ) ) ) ) ) ) ) ) ) 9?ek11111HHV(4,77788F/00T4L@@@@@@ HHV(4,77788F/00T4L@@@@@@@@sy   2F  =BF  B##F  &B#'F  DC6*D6C:	:D=C:	>DF  DF  DF   AGc                    t          j                    }|                    t           j                  \  }}	 |                    t           j        d|f           |                    t           j        t           j        |f           t          |                                 d          5 }|                    d           d d d            n# 1 swxY w Y   |                    t           j        ||f           |                    t           j                  ||fk    sJ d S # |                    t           j        ||f           |                    t           j                  ||fk    sJ w xY w)Nr   r   s   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)r1   r2   r   r   RLIM_INFINITYr   r   r   )r9   r:   r   r   r   s        r;   test_rlimit_infinityz TestProcess.test_rlimit_infinity  s|    NXXf122
d	AHHV(4,777HHV(6+?*FGGGdoo''.. %!$$$% % % % % % % % % % % % % % % HHV(4,77788F/00T4L@@@@@@ HHV(4,77788F/00T4L@@@@@@@@s1   A0D 'C	=D 	CD CD AE*c                     t          j                    }|                    t           j                  \  }}|t           j        k    sJ |                    t           j        ||f           d S r>   )r1   r2   r   r   r  )r9   r:   r   r   s       r;   test_rlimit_infinity_valuez&TestProcess.test_rlimit_infinity_value  s^     NXXf122
dv+++++	$tTl33333rI   serialry   c                 n   t          j                    }t          r?	 |                                }n=# t           j        $ r t          j        d          cY S w xY w|                                }t                      5  |                                }||dz   k    sJ 	 d d d            d S # 1 swxY w Y   d S )N$on OpenBSD this requires root accessr`   )r1   r2   r
   num_threadsrZ   r%   r   r   )r9   r:   step1step2s       r;   test_num_threadszTestProcess.test_num_threads  s	   
 N 	$K& K K K{#IJJJJJK MMOOE\\ 	& 	&MMOOEEAI%%%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&s!   1 #AA< B**B.1B.zWINDOWS onlyc                 `    t          j                    }|                                dk    sJ d S r   )r1   r2   num_handlesr8   s     r;   test_num_handleszTestProcess.test_num_handles  s0     N}}""""""rI   c                 $   t          j                    }t          r?	 |                                }n=# t           j        $ r t          j        d          cY S w xY w|                                }t                      5  |                                }t          |          t          |          dz   k    sJ |d         }|j	        |d         k    sJ |j
        |d         k    sJ |j        |d         k    sJ 	 d d d            d S # 1 swxY w Y   d S )Nr  r`   r   r   )r1   r2   r
   threadsrZ   r%   r   r   r   id	user_timesystem_time)r9   r:   r  r  athreads        r;   test_threadszTestProcess.test_threads  sc   N 	 K		& K K K{#IJJJJJK IIKKE\\ 	5 	5IIKKEu::Ua////AhG:++++$
2222&'!*44444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5s"   1 #AA<A;DD	D	c                    |                                  }t          r?	 |                                 n)# t          j        $ r t          j        d          cY S w xY wt          |                                j	        t          d |                                D                       z
            dk     sJ t          |                                j        t          d |                                D                       z
            dk     sJ d S )Nr  c              3   $   K   | ]}|j         V  d S r>   )r  r   s     r;   	<genexpr>z-TestProcess.test_threads_2.<locals>.<genexpr>  s$      (J(J(J(J(J(J(J(JrI   g?c              3   $   K   | ]}|j         V  d S r>   )r  r   s     r;   r  z-TestProcess.test_threads_2.<locals>.<genexpr>"  s$      *N*NQ1=*N*N*N*N*N*NrI   )r?   r
   r  r1   rZ   r%   r   r   r   r   sumr   r8   s     r;   test_threads_2zTestProcess.test_threads_2  s     	KK		& K K K{#IJJJJJK "S(J(Jaiikk(J(J(J%J%JJKK    $s*N*N!))++*N*N*N'N'NNOO     s   2 #AAc                 <   t          j                    }|                                d d         \  }}|                                }|dk    sJ |dk    sJ d gdz  }|                                d d         \  }}|                                }||k    sJ ||k    sJ ||k    sJ ~t          r8|                                }	|	j        |	j        k    sJ |	j        |	j        k    sJ |                                }	|	j	        D ]}
t          |	|
          dk    sJ d S )Nr   r   i` )r1   r2   memory_infomemory_percentr   rsswsetvmspagefiler   r   )r9   r:   rss1vms1percent1memarrrss2vms2percent2memry   s              r;   test_memory_infozTestProcess.test_memory_info&  sD   N ]]__RaR(
d##%%axxxxaxxxx '!]]__RaR(
d##%% d{{{{t||||("""" 	+--//C7ch&&&&7cl****mmooK 	+ 	+D3%%*****	+ 	+rI   c                    t          j                    }t          j                    j        }|                                }|j        D ]7}t          ||          }|dk    sJ |dk    rt          st          r/||k    sJ 8t          st          st          r|j        dk    sJ t          r|j        dk    sJ |j        dk    sJ d S d S )Nr   r%  )r1   r2   virtual_memorytotalmemory_full_infor   r   r   r   r   r   usspssswap)r9   r:   r2  r.  ry   r   s         r;   test_memory_full_infoz!TestProcess.test_memory_full_infoE  s    N%''-  ""K 	" 	"DC&&EA::::#%E>>>>> 	 G 	 u 	 7a<<<< 	!7a<<<<8q====	! 	! =rI   c                    t          j                    }|                                }t          |          t          t	          |                    k    sJ |                    d          }|D ]}|j                            d          rt          r|j        dk    r1t          j        	                    |j                  sJ |j                    t          r	 t          j                            |j                  s3t          j                            |j                  sJ |j                    # t          $ rT t          s t          d          5 }|                                }d d d            n# 1 swxY w Y   |j         d|vr Y !w xY wdt          j                            |j                  vrU	 t          j        |j                  }t%          j        |j                  sJ |j                    # t*          $ r Y w xY w|D ][}|j        D ]Q}t/          ||          }	|dk    r|d	v r|	s
J |	            *t1          |	t2                    sJ |	d
k    s
J |	            R\d S )NF)grouped[pvclockz/proc/self/smapsz
 (deleted)64r   >   addrpermsr   )r1   r2   memory_mapsr   setr   r   r   r4   isabsr   existsislinkAssertionErrorr   r   r   basenamestatS_ISREGst_modeFileNotFoundErrorr   r   r   int)
r9   r:   mapsext_mapsntr   datastfnamer   s
             r;   test_memory_mapszTestProcess.test_memory_mapsU  s   N}}4yyCD		NN****===// 	= 	=Bw!!#&&  rw)++7==))2227222 =7>>"'22 bgnn7 7  w   &     "#566 (! vvxx( ( ( ( ( ( ( ( ( ( ( ( ( ( ('---T99 :9 RW--bg6666=))B  <
33<<RW<<<< )   D 7  		- 		-B - -E**F??--- ''%''''%eS11111 A:::u::::-		- 		-sI   AD33!FE5)F5E99F<E9=FF;G>>
HHc                     t          j                    }t                      5 }d fd|                                D             } |          |v sJ 	 d d d            d S # 1 swxY w Y   d S )Nc                 z    t           j                            t           j                            |                     S r>   )r4   r   r   normcaser:   s    r;   normpathz8TestProcess.test_memory_maps_lists_lib.<locals>.normpath  s(    w''(8(8(;(;<<<rI   c                 0    g | ]} |j                   S  )r   )r   r   rV  s     r;   r   z:TestProcess.test_memory_maps_lists_lib.<locals>.<listcomp>  s%    BBBQ((BBBrI   )r1   r2   r!   r?  )r9   r:   r   libpathsrV  s       @r;   test_memory_maps_lists_libz&TestProcess.test_memory_maps_lists_lib  s     N "" 	.d= = = CBBB!--//BBBH8D>>X-----	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.s   3A$$A(+A(c                 <   t          j                    }|                                 t          j        t
                    5  |                    d           d d d            n# 1 swxY w Y   t          st          st          r|                    d           d S d S )Nz?!?)memtyper4  )	r1   r2   r"  r%   r6   rz   r   r   r   r8   s     r;   test_memory_percentzTestProcess.test_memory_percent  s    N	]:&& 	, 	,U+++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,E 	,W 	,U+++++	, 	,s   A$$A(+A(c                 .   |                                  }|                                sJ |                                sJ |                                 |                                 |                                rJ |                                rJ d S r>   )r?   
is_runningr@   rA   r8   s     r;   test_is_runningzTestProcess.test_is_running  s    ||~~||~~		<<>>!!!<<>>!!!!!rI   c                 R   |                                  }|                                }	 |t          k    sJ n# t          $ r t          rVt          |          t          t                    k    r1t          j        j        } ||           |t                    k    sJ nht          j
        d          dt          j
        d          }	 |                    |d          t          j        |d          k    sJ n# t          $ r Y nw xY wY nw xY wt          |ddg          }|dk    sJ d S )Nr   .r`    r]   zimport os; print('hey')hey)r?   exer   rD  r   r   r4   r   rT  sysversion_inforeplacer)   )r9   r:   re  rT  verouts         r;   test_exezTestProcess.test_exe  sE   eegg	*$$$$$ 	 	 	 3s88s:667+x}}(<(<<<<<< )!,DDs/?/BDD;;sB//:3Ec23N3NNNNNN%   D	& #t6788e||||||s4   8 BD
/C76D
7
DD
DD
	D
c                    t           ddg}|                     |          }t          r,|                                g k    rt	          j        d          S t          st          st          r'|                                d         t           k    sJ d S t          r~t          rw|                                d         }|t           k    rRd
                    |                                dd                    d
                    |dd                    k    sJ d S d
                    |                                          d
                    |          k    sJ d S )Nr]   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)r9   rp  r:   pyexes       r;   test_cmdlinezTestProcess.test_cmdline  s5   @

 g&& 	:aiikkR'';8999  	>W 	> 	>99;;q>Z//////    		AJ&&88AIIKKO448M8MMMMM488AIIKK((CHHW,=,=======rI   c                    t           g}|                    dgdz             |                    ddg           |                     |          }|dd          }t          rN	 |                                dd          |k    sJ d S # t
          j        $ r t          j        d          cY S w xY w|                                dd          }t          r|g k    rt          j        d          S ||k    sJ d S )Nz-v2   r]   rm  r`   z#OPENBSD: process turned into zombiern  )
r   extendr?   r
   rp  r1   ZombieProcessr%   r   r	   )r9   rp  r:   r   s       r;   test_long_cmdlinezTestProcess.test_long_cmdline  s   ,v{###GH	
 	
 	
 g&& !""+ 	"Jyy{{122'111111' J J J{#HIIIIIJ ))++abb/C >#)){#<==='>>>>>>s    "B #B*)B*c                    |                                  }|                                                                }|                    d          r
|d d         }t          j                            t          j                            t          j	                                                            }|
                    |          sJ ||f            d S )Ntrx   )r?   ry   lowerendswithr4   r   rE  r   rf  
executabler   )r9   r:   ry   rr  s       r;   	test_namezTestProcess.test_name  s    vvxx~~== 	9D  !1!1#.!A!ABBHHJJ%%44t}44444rI   c                    t          |                     t          j        dz                      }|ddg}|                     |          }t
          r	 |                                t          j        	                    |          k    sJ d S # t          $ rn |                                t          j        k    rGt          j        	                    |                              |                                          sJ n Y d S w xY w|                                t          j        	                    |          k    sJ d S )Nr   )suffixr]   rm  )r#   r   stringdigitsr?   r
   ry   r4   r   rE  rD  statusr1   STATUS_ZOMBIEr   )r9   rr  rp  r:   s       r;   test_long_namezTestProcess.test_long_name  s+   dooV]Q5FoGGHH@

 g&& 	7vvxx27#3#3E#:#:::::::!   88::!5557++E22==affhhGGGGGG HGG 6688rw//66666666s   7B A4DDc                 D   t          j                    }|                                \  }}}|t          j                    k    sJ |t          j                    k    sJ t          t          d          r+t          j                    |                                k    sJ d S d S N	getresuid)r1   r2   uidsr4   getuidgeteuidhasattrr  r9   r:   real	effective_saveds        r;   	test_uidszTestProcess.test_uids      N"#&&((iry{{""""BJLL(((( 2{## 	.<>>QVVXX----	. 	.--rI   c                 D   t          j                    }|                                \  }}}|t          j                    k    sJ |t          j                    k    sJ t          t          d          r+t          j                    |                                k    sJ d S d S r  )r1   r2   gidsr4   getgidgetegidr  	getresgidr  s        r;   	test_gidszTestProcess.test_gids#  r  rI   c                    fd}t          j                    t          j        t                    5                      d           d d d            n# 1 swxY w Y                                   }|                     ||           t          rd }t           j        t           j	        t           j
        t           j        t           j        t           j        fD ]}|                     |          5  	                     |                                           }|t           j        t           j        t           j        hv r||k    s|
|}||k    sJ n||k    sJ n# t           j        $ r Y nw xY wd d d            n# 1 swxY w Y   d S 	 t!          t"          d          rHt#          j        t"          j        t#          j                                                              k    sJ                     d                                           dk    sJ t!          t"          d          rHt#          j        t"          j        t#          j                                                              k    sJ t*          s/                    d                                           dk    sJ d S d S # t           j        $ r Y d S w xY w)Nc                 `    	                      |            d S # t          j        $ r Y d S w xY wr>   )nicer1   rZ   )r   r:   s    r;   r   z&TestProcess.test_nice.<locals>.cleanup1  s@    t&   s    --str)priogetpriorityr`   r   )r1   r2   r%   r6   r   r  r   r   IDLE_PRIORITY_CLASSBELOW_NORMAL_PRIORITY_CLASSNORMAL_PRIORITY_CLASSABOVE_NORMAL_PRIORITY_CLASSHIGH_PRIORITY_CLASSREALTIME_PRIORITY_CLASSsubTestrZ   r  r4   r  PRIO_PROCESSr5   r   )r9   r   r   highest_prior  new_prior:   s         @r;   	test_nicezTestProcess.test_nice0  s@   	 	 	 	 	 N]9%% 	 	FF5MMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	vvxx&&& 3	L*2,2*. 4 4 \\t\,, 4 44t $%6688
  ">"6":$  
  (4//<3G/3'/<'?'?'?'?#+t#3#3#3#3#3# ".   4 4 4 4 4 4 4 4 4 4 4 4 4 4 44 4<2}-- r	DD6688$ $ $ $ q			vvxx1}}}}2}-- r	DD6688$ $ $ $
  )FF1III6688q====) )(=&   s[   AAA/E<1EAE<E-*E<,E--E<<F 	F 	DJ. .K Kc                 t    t          j                    }|                                t           j        k    sJ d S r>   )r1   r2   r  STATUS_RUNNINGr8   s     r;   test_statuszTestProcess.test_statusr  s1    NxxzzV2222222rI   c                    |                                  }|                                }t          r|                    d          \  }}t	          j                    }|                    d          rt          j        d          S ||k    sJ dt          j
        v r|t          j
        d         k    sJ d S d S |t	          j                    k    sJ d S )N\$zrunning as service account
USERDOMAIN)r?   usernamer   splitgetpassgetuserr|  r%   r   r4   environ)r9   r:   r  domaingetpass_users        r;   test_usernamezTestProcess.test_usernamev  s    ::<< 	1'~~d33FH"?,,L$$S)) A {#?@@@|++++rz))L!99999 *)99w00000000rI   c                     |                                  }|                                t          j                    k    sJ d S r>   )r?   cwdr4   getcwdr8   s     r;   test_cwdzTestProcess.test_cwd  s8    uuww")++%%%%%%rI   c                 j    t           ddg}|                     |          t          fd           d S )Nr]   zFimport os, time; os.chdir('..'); [time.sleep(0.1) for x in range(100)]c                                                        t          j                            t          j                              k    S r>   )r  r4   r   dirnamer  rU  s   r;   <lambda>z(TestProcess.test_cwd_2.<locals>.<lambda>  s(    15577bgoobikk&B&BB rI   )r   r?   r    )r9   rd   r:   s     @r;   
test_cwd_2zTestProcess.test_cwd_2  sH    	
 c""BBBBCCCCCrI   c           	      D   t          j                    }|                                }|s
J |            |                     |j        |           t	          t
          d          r,|t          t          j        |j                            k    sJ t          |          t          t          |                    k    sJ t          t          t          t          j        d                                        }|D ]}|                    |g           |                                |gk    sJ t	          t
          d          r>|                                t          t          j        |j                            k    sJ t	          |d          r2|                                d         |                                k    sJ |                    g            t          r2|                                |j                                        k    sJ n|                                |k    sJ t	          t
          d          r>|                                t          t          j        |j                            k    sJ t#          j        t&                    5  |                    d           d d d            n# 1 swxY w Y   |                    |           |                    t          |                     |                    t)          |                     d S )Nsched_getaffinityTpercpunum_cpur   r`   )r1   r2   cpu_affinityr   r  r4   listr  r3   r   r@  r   r   r  r   _proc_get_eligible_cpusr%   r6   r   r   )r9   r:   initialall_cpusns        r;   test_cpu_affinityzTestProcess.test_cpu_affinity  s   N..""0002*++ 	@d2#7#>#>??????7||s3w<<000000c&"4D"A"A"ABBCCDD 	: 	:ANNA3>>##s****r.// M~~''40DQU0K0K+L+LLLLLq)$$ :~~''*aiikk9999
 	
r 	0>>##qw'A'A'C'CCCCCC>>##x////2*++ 	I>>##tB,@,G,G'H'HHHHH]9%% 	 	NN1	 	 	 	 	 	 	 	 	 	 	 	 	 	 		w	s8}}%%%	uX'''''s   J<<K K c                    |                                  }t          t          j        d                    dz   g}t	          j        t                    5  |                    |           d d d            n# 1 swxY w Y   t	          j        t                    5  |                    t          dd                     d d d            n# 1 swxY w Y   t	          j        t          t          f          5  |                    ddg           d d d            n# 1 swxY w Y   t	          j        t                    5  |                    ddg           d d d            d S # 1 swxY w Y   d S )	NTr  
   i'  i*  r   1rx   )
r?   r   r1   r   r%   r6   rz   r  r   r   )r9   r:   invalid_cpus      r;   test_cpu_affinity_errsz"TestProcess.test_cpu_affinity_errs  s8   6+488899B>?]:&& 	( 	(NN;'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(]:&& 	0 	0NN5..///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0]Iz233 	% 	%NNAs8$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%]:&& 	$ 	$NNAr7###	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$sH   A66A:=A:$C

CC5DD D=E""E&)E&c                 $   t          j                    }|                                }|s
J |            |                     |j        |           t	          |          dk    r
|d d         }g }t          t	          |          dz             D ]4}|                    d t          j        ||          D                        5|D ]K}|                    |           t          |                                          t          |          k    sJ Ld S )N   r`   c              3   8   K   | ]}|t          |          V  d S r>   )r  )r   subsets     r;   r  zATestProcess.test_cpu_affinity_all_combinations.<locals>.<genexpr>  sB        V     rI   )
r1   r2   r  r   r   r   rv  	itertoolscombinationssorted)r9   r:   r  combosr   combos         r;   "test_cpu_affinity_all_combinationsz.TestProcess.test_cpu_affinity_all_combinations  s,   N..""000 w<<"crclGs7||a'(( 	 	AMM  '4Wa@@        	= 	=ENN5!!!!..**++ve}}<<<<<	= 	=rI   zbroken on BSDc                    t          j                    |                                 }                                |vsJ t	          |d          5 }|                    d           |                                 t          fd                                           d D             }t          j	        
                    |          |v sJ t          rD ]}|j	        |k    r|j        dk    sJ d d d            n# 1 swxY w Y   D ]0}t          j	                            |j	                  s
J |            1d| d}|                     t          d|g          t!          d	          D ]:}d
                                 D             }||v r n8t#          j        d           ;t          j	        
                    |          |v sJ |D ]+}t          j	                            |          s
J |            ,d S )Nr   s   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxc                  h    t                                                    t                     k    S r>   )r   
open_files)filesr:   s   r;   r  z-TestProcess.test_open_files.<locals>.<lambda>  s"    s1<<>>22c%jj@ rI   c                 V    g | ]&}t           j                            |j                  'S rX  r4   r   rT  r   s     r;   r   z/TestProcess.test_open_files.<locals>.<listcomp>  s*    AAAa))!&11AAArI   r   zimport time; f = open(r'z/', 'r'); [time.sleep(0.1) for x in range(100)];r]   r   c                 V    g | ]&}t           j                            |j                  'S rX  r  r   s     r;   r   z/TestProcess.test_open_files.<locals>.<listcomp>  s*    JJJa))!&11JJJrI   rp   )r1   r2   r   r  r   r   flushr    r4   r   rT  r   positionisfiler?   r   r   r}   sleep)	r9   r   r   	filenamesfilerp  r   r  r:   s	          @@r;   test_open_fileszTestProcess.test_open_files  st   N""U""""&$ 	51GGK   GGIII@@@@@AAALLNNEAA5AAAI7##F++y8888 5! 5 5DyF**#}4444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5  	3 	3D7>>$),,22d2222v    	 z49::s 	9 	9AJJ1<<>>JJJI""Jt7##F++y8888 	. 	.D7>>$''------	. 	.s   B$DDDc                 N   t          j                    }t          j        j        }|                                 }t          |d          5 }|                                D ]C} ||j                   ||j                  k    s|j	        |
                                k    r n6Dt          j        d|                                          cd d d            S  ||j                   ||j                  k    sJ t          r|j	        dk    sJ n|j	        |
                                k    sJ |                                d         }|d         |j        k    sJ |d         |j	        k    sJ |j        |                                vsJ 	 d d d            d S # 1 swxY w Y   d S )Nwzno file found; files=rx   r   r`   )r1   r2   r4   r   rT  r   r   r  ry   fdfilenor%   r~   r   )r9   r:   rT  r   fileobjr  ntuples          r;   test_open_files_2zTestProcess.test_open_files_2	  s    N7#""&# 	6' O OHTY''88GL+A+AAAw'.."2"222E 3 {#M1<<>>#M#MNN	6 	6 	6 	6 	6 	6 	6 	6 8DI&&((7<*@*@@@@@ 3w"}}}}}w'.."2"22222\\^^A&F!9++++!9	))))<q||~~55555'	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6s   	BFB5FF!Fc                    t          j                    }|                                 }|                                }t	          |d          5  |                                |dz   k    sJ t          j                    5  |                                |dz   k    sJ 	 d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |                                |k    sJ d S )Nr  r`   r   )r1   r2   r   num_fdsr   socket)r9   r:   r   starts       r;   test_num_fdszTestProcess.test_num_fds$  sQ    N""		&# 	0 	099;;%!)++++ 0 0yy{{eai/////0 0 0 0 0 0 0 0 0 0 0 0 0 0 0	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 yy{{e######s6   1B?=B(B?(B,	,B?/B,	0B??CCz not reliable on OPENBSD & NETBSDc                 2   t          j                    }t          |                                          }t	          d          D ]@}t          j        d           t          |                                          }||k    r d S At          j        d          S )Nr   g?z2num ctx switches still the same after 2 iterations)	r1   r2   r  num_ctx_switchesr   r}   r  r%   r~   )r9   r:   beforer   afters        r;   test_num_ctx_switchesz!TestProcess.test_num_ctx_switches0  s    
 NQ''))**q 	 	AJt**,,--Ev~~tt {@
 
 	
rI   c                 *   t          j                    }t          t          d          r+|                                t          j                    k    sJ |                                 }|                                t          j                    k    sJ d S )Ngetppid)r1   r2   r  r4   ppidr  r?   r5   r8   s     r;   	test_ppidzTestProcess.test_ppid@  sv    N2y!! 	,6688rz||++++vvxx29;;&&&&&&rI   c                    |                                  }|                                j        t          j                    k    sJ t          j                    d         }t          j        |                                          J d S r   )r?   parentr3   r4   r5   r1   pidsr2   )r9   r:   
lowest_pids      r;   test_parentzTestProcess.test_parentG  sl    xxzz~,,,,[]]1%
~j))0022:::::rI   c                     |                                  }|                                 |j        sJ d|_        |                                j        t          j                    k    sJ d S Nr`   )r?   r   _create_timer  r3   r4   r5   r8   s     r;   test_parent_mocked_ctimez$TestProcess.test_parent_mocked_ctimeN  s^     	~xxzz~,,,,,,rI   c                     t          j                    }|                                 \  }}|                                |k    sJ |                                |k    sJ d S r>   )r1   r2   rq   r  r9   r  rr   rs   s       r;   test_parent_multizTestProcess.test_parent_multiZ  sa    !! 4466z  ""e++++||~~''''''rI   c                 F   t          j                    }|                                sJ |                                 \  }}|                                d         |k    sJ |                                d         |k    sJ |                                d         |k    sJ d S r   )r1   r2   parentsrq   r  s       r;   test_parentszTestProcess.test_parents`  s    !!~~ 4466z}}q!V++++!!##A&%////!!##A&&000000rI   c                    t          j                    }|                                rJ |                    d          rJ |                     d          }|                                }|                    d          }||fD ]T}t	          |          dk    sJ |d         j        |j        k    sJ |d                                         |j        k    sJ Ud S )NT	recursiver   creationflagsr`   )r1   r2   childrenr?   r   r3   r  r9   r  rr   	children1	children2r  s         r;   test_childrenzTestProcess.test_childreni  s    !!??$$$$$??T?22222 !!!22OO%%	OOdO33	"I. 	4 	4Hx==A%%%%A;?ei////A;##%%33333	4 	4rI   c                    t          j                    }|                                 |j        sJ |xj        dz  c_        |                                rJ |                    d          rJ |                     d          }|                                }|                    d          }||fD ]T}t          |          dk    sJ |d         j        |j        k    sJ |d                                         |j        k    sJ Ud S )Ni Tr  r   r  r`   )	r1   r2   r   r  r  r?   r   r3   r  r  s         r;   test_children_mocked_ctimez&TestProcess.test_children_mocked_ctimex  s$    !!""""v%??$$$$$??T?22222 !!!22OO%%	OOdO33	"I. 	4 	4Hx==A%%%%A;?ei////A;##%%33333	4 	4rI   c                 L   t          j                    }|                                 \  }}|                                |gk    sJ |                    d          ||gk    sJ |                                 |                                 |                    d          rJ d S )NTr  )r1   r2   rq   r  rK   rA   r  s       r;   test_children_recursivez#TestProcess.test_children_recursive  s     !! 4466z  UG++++..5*2EEEEE 	

??T?2222222rI   c                 J   t          j        t                    }t          j                    D ]:}	 ||                                xx         dz  cc<   &# t          j        $ r Y 7w xY wt          |                                d           d         }t          r|dk    rt          j        d          S t          j        |          }	 |                    d          }t          |          t          t          |                    k    sJ d S # t          j        $ r Y d S w xY w)Nr`   c                     | d         S r  rX  )r   s    r;   r  z6TestProcess.test_children_duplicates.<locals>.<lambda>  s
    qt rI   )keyr   zPID 0Tr  )collectionsdefaultdictrJ  r1   process_iterr  Errormaxitemsr   r%   r   r2   r  r   r@  rZ   )r9   tabler:   r3   cs        r;   test_children_duplicatesz$TestProcess.test_children_duplicates  s-   ',,$&& 	 	Aaffhh1$<    %++--^^444Q7 	(SAXX;w'''N3	)

T
**A q66SQ[[(((((( " 	 	 	DD	s#   "AA%$A%D D"!D"c                 H   t          j                    }|                                 \  }}|                    d          }t	          |          dk    sJ |d         |k    sJ |d         |k    sJ |                                }|d         |k    sJ |d         |k    sJ d S )NTr  r   r   r`   )r1   r2   rq   r  r   r  )r9   r  rr   rs   r  r  s         r;   test_parents_and_childrenz%TestProcess.test_parents_and_children  s    !! 4466z??T?228}}!!!!{e####{j(((($$&&qzU""""qzV######rI   c                 Z   |                                  }|                                 t          d          D ]:}|                                t          j        k    r nt          j        d           ;|                                 |                                t          j        k    sJ d S )Nr   rp   )	r?   rl   r   r  r1   STATUS_STOPPEDr}   r  rm   )r9   r:   r   s      r;   test_suspend_resumezTestProcess.test_suspend_resume  s    			s 	 	AxxzzV222Jt	


xxzzV2222222rI   c                    t          j        t                    5  t          j        d           d d d            n# 1 swxY w Y   t          j        t
                    5  t          j        d           d d d            d S # 1 swxY w Y   d S )Nr  rx   )r%   r6   r   r1   r2   rz   )r9   s    r;   test_invalid_pidzTestProcess.test_invalid_pid  s    ]9%% 	  	 N3	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 ]:&& 	 	N2	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   ;??BBBc                 V   t          j                    }|                    ddg          }t          |                                          ddgk    sJ t          j        t          t          j                                        }|                    dgd          }t          |d         t                    s|d         dk    sJ t          j
        ddt           j        	          5  |                    d
gd          d
dik    sJ 	 d d d            n# 1 swxY w Y   t          j
        ddt          j        |j        d          	          5  t          j        t           j                  5  |                    d
g           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   t          j
        ddt          j        |j        d          	          5  |                    d
gd          d
dik    sJ 	 d d d            n# 1 swxY w Y   t          j
        ddt"          	          5  |                                }d
t          |                                          vsJ t          j        t"                    5  |                    d
g           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   t          j        t$                    5  |                    d           d d d            n# 1 swxY w Y   t          j        t&                    5  |                    dg           d d d            n# 1 swxY w Y   t          j        t&                    5  |                    ddg           d d d            d S # 1 swxY w Y   d S )Nre  ry   )attrsnet_connectionsfoo)r2  ad_valuezpsutil.Process.niceT)createrU   r  r`   bar)r1   r2   as_dictr  keysminr  r   r  r   rV   rZ   rX   r3   r%   r6   rw  NotImplementedErrorr   rz   )r9   r:   ds      r;   test_as_dictzTestProcess.test_as_dict  s   NIIUFOI,,affhhE6?2222N3v{}}--..II./%I@@!-.55 	1&'50000 Z!$F<O
 
 
 	H 	H 99F8a988VQKGGGGG	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H Z!,QUF;;
 
 
 	* 	*
 v344 * *			)))* * * * * * * * * * * * * * *	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* Z!,QUF;;
 
 
 	P 	P
 99F8e9<<OOOOO	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P Z!$<O
 
 
 	* 	* 		Aaffhh////233 * *			)))* * * * * * * * * * * * * * *	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* ]9%% 	 	IIf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	]:&& 	 	IIug	 	 	 	 	 	 	 	 	 	 	 	 	 	 	]:&& 	& 	&IIuen%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&s   )!DDDF+0FF+F	F+F	F++F/2F/%!HHH:AKJ1%K1J5	5K8J5	9KKK,LLL2MMM9NN"%N"c                 
   t          j                    }t          j        d          5 }|                                5  |                                 |                                 d d d            n# 1 swxY w Y   |j        dk    sJ 	 d d d            n# 1 swxY w Y   t          j        d          5 }|                                 |                                 d d d            n# 1 swxY w Y   |j        dk    sJ d S )N$psutil._psplatform.Process.cpu_timesr`   r   r1   r2   r   rV   oneshotr   
call_countr9   r:   r   s      r;   test_oneshotzTestProcess.test_oneshot  s   NZ>?? 	%1                 <1$$$$$		% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% Z>?? 	1KKMMMKKMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 |q      sF   B)A2&B2A6	6B9A6	:BBB6)C++C/2C/c           	      $   t          j                    }t          j        d          5 }t          j        d          5 }|                                5  |                                 |                                 |                                5  |                                 |                                 d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |j        dk    sJ |j        dk    sJ 	 d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   t          j        d          5 }|                                 |                                 d d d            n# 1 swxY w Y   |j        dk    sJ d S )Nr?  z(psutil._psplatform.Process.oneshot_enterr`   r   r@  )r9   r:   m1m2r   s        r;   test_oneshot_twicezTestProcess.test_oneshot_twice  s    NZ>?? 		*2FGG *2YY[[ & &KKMMMKKMMM & && & & & & & & & & & & & & & && & & & & & & & & & & & & & & }))))})))))* * * * * * * * * * * * * * *		* 		* 		* 		* 		* 		* 		* 		* 		* 		* 		* 		* 		* 		* 		* Z>?? 	1KKMMMKKMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 |q      s   D$D=C)C	8CCCCCDCD"C#DD$D	D$D	D$$D(+D()E88E<?E<c                    |                                  \  }}|                                }|                                }||k    sJ |                                5  |                                |k    sJ |                                |k    sJ 	 d d d            n# 1 swxY w Y   |                                5  |                                |k    sJ |                                |k    sJ 	 d d d            d S # 1 swxY w Y   d S r>   )rq   r  rA  )r9   p1p2p1_ppidp2_ppids        r;   test_oneshot_cachezTestProcess.test_oneshot_cache#  s    ))++B''))''))'!!!!ZZ\\ 	( 	(7799''''7799'''''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( ZZ\\ 	( 	(7799''''7799'''''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(s$   5BB"%B"=5D  DDc                 X   d }|                                                                                                    t          rt	          fd           |                                t                    }|                    |j                  D ]\  }} |||           d S )Nc                     	  |             }t           r|dv rd S t          j        | d|d          S # t          j        $ r  t          j        $ r Y d S t          j        $ r t          r|dv rY d S  w xY w)N>   re  ry   z didn't raise NSP and returned z instead>   r  r  )r   r%   r~   r1   rw  rX   rZ   r
   )funfun_namer   s      r;   assert_raises_nspzFTestProcess.test_halfway_terminated_process.<locals>.assert_raises_nsp:  s    cee   x?::4{LLSLLL   '   '   &     x+EEE44s   
3 A2A20A2c                  8     j         t          j                    vS r>   )r3   r1   r  rU  s   r;   r  z=TestProcess.test_halfway_terminated_process.<locals>.<lambda>Q  s    quFKMM9 rI   )	r?   rK   rA   r   r    rE   r$   iterall)r9   rS  nsrQ  ry   r:   s        @r;   test_halfway_terminated_processz+TestProcess.test_halfway_terminated_process2  s    	 	 	& 		 	;9999:::a   q!! 	) 	)ICc4((((	) 	)rI   c                 d   |                                  \  }}|                     |           t          t          j        j        d          rbt          j        j                            t          j                              rJ t          j        j                            |j	                  sJ d S d S )Nproc_is_zombie)
spawn_zombieassert_proc_zombier  r1   r   cextrZ  r4   r5   r3   )r9   _parentzombies      r;   test_zombie_processzTestProcess.test_zombie_processX  s    ++--'''6%*,<== 	F).==bikkJJJJJ%*99&*EEEEE	F 	FEErI   c                     t          j                    }t          j        dt          j        d                    5 }|                                sJ |j        sJ 	 d d d            d S # 1 swxY w Y   d S )Nzpsutil.Processr   rT   )r1   r2   r   rV   rw  r_  r   rC  s      r;   $test_zombie_process_is_running_w_excz0TestProcess.test_zombie_process_is_running_w_exc`  s     NZ&*>q*A*A
 
 
 	<<>>!!!8OOOO		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s    A**A.1A.c                 
   t          j                    }t          j        dt          j        d                    5 }|                                t           j        k    sJ |j        sJ 	 d d d            d S # 1 swxY w Y   d S )Nz!psutil._psplatform.Process.statusr   rT   )r1   r2   r   rV   rw  r  r  r   rC  s      r;    test_zombie_process_status_w_excz,TestProcess.test_zombie_process_status_w_exck  s     NZ/,Q//
 
 
 	 88::!555558OOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   .A88A<?A<c                 (   |                                  }t          j        |j                  }|j        |                                dz   f|_        t          t          j                               |j        t          j        v sJ |	                                rJ t          j                            t          j        dd          5  t          j        t!          j                              5 }t          t          j                               d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d|j         |                                v sJ |j        t          j        vsJ |t          j        |j                  k    sJ d}t'          |          }|                    |j        |j        z   d          D ]u\  }}|                     |          5  t1          j        t          j        |	          5   |             d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   vd
t7          |          v sJ d
t9          |          v sJ t1          j        t          j        |	          5  |                                 d d d            n# 1 swxY w Y   t1          j        t          j        |	          5  |                                 d d d            n# 1 swxY w Y   t1          j        t          j        |	          5  |                                 d d d            n# 1 swxY w Y   t1          j        t          j        |	          5  |                                  d d d            d S # 1 swxY w Y   d S )Nr   PSUTIL_DEBUGTz+refreshing Process instance for reused PID z4process no longer exists and its PID has been reusedF)clear_cacher	  r   zterminated + PID reused)!spawn_subprocr1   r2   r3   r   _identr  r#  _pmapr_  r   rV   object_common
contextlibredirect_stderrioStringIOgetvaluer$   rU  setterskillersr  r%   r6   rX   r  reprr  r  r  r  )r9   subpr:   r   msgrW  rQ  ry   s           r;   test_reused_pidzTestProcess.test_reused_pidw  s   !!##N48$$E1==??S01V ""###u$$$$<<>>!!! Zv~~tDD 	, 	,+BKMM:: ,aV(**+++, , , , , , , , , , , , , , ,	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, B!%AAzz||    uFL((((FN48,,,,,,Dq!!bj!8eLL 	 	IC4((  ]6#7sCCC  CEEE                             )CFF2222(DGG3333]6/s;;; 	 	FFHHH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	]6/s;;; 	 	HHJJJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	]6/s;;; 	 	IIKKK	 	 	 	 	 	 	 	 	 	 	 	 	 	 	]6/s;;; 	 	JJLLL	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   &D1-!DD1D	D1!D	"D11D58D5 !H/HH/HH/H H//H3	6H3	J""J&)J&K..K25K2L::L>L>%NNNc                 x   dt          j                    vrt          j        t           j                  5  t          j        d           d d d            n# 1 swxY w Y   t          j        d          rJ t          j        d                                          dk    sJ d S t          j        d          }t          rt           j	        nt          }t          j        |          5  |                                 d d d            n# 1 swxY w Y   t          j        |          5  |                                 d d d            n# 1 swxY w Y   t          j        |          5  |                                 d d d            n# 1 swxY w Y   t          j        |          5  |                                 d d d            n# 1 swxY w Y   t          j        |          5  |                                 d d d            n# 1 swxY w Y   t          j        |          5  |                    t"          j                   d d d            n# 1 swxY w Y   t'          |          }|                    |j        |j        z             D ]u\  }}	  |            }|dv r|j        dk    sJ "|dk    r&t          rdnd}|                                |k    sJ N|dk    r|s
J |            a# t           j	        $ r Y rw xY wt2          s-dt          j                    v sJ t          j        d          sJ d S d S )Nr   r`   >   r  r  r  zNT AUTHORITY\SYSTEMrootry   )r1   r  r%   r6   rX   r2   
pid_existsr  r   rZ   rz   rA   rK   rl   rm   r@   rN   rB   rC   r$   rU  gettersrr  r  r  r
   )r9   r:   r  rW  rQ  ry   r   r   s           r;   
test_pid_0zTestProcess.test_pid_0  s   FKMM!!v344 " "q!!!" " " " " " " " " " " " " " " (+++++>!$$))++q0000FN1%,<f!!*]3 	 	FFHHH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	]3 	 	KKMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	]3 	 	IIKKK	 	 	 	 	 	 	 	 	 	 	 	 	 	 	]3 	 	HHJJJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	]3 	 	FFHHH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	]3 	* 	*MM&.)))	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* q!!bj!899 	& 	&IC&cee +++8q=====Z''5<H11&D::<<4/////V^^%%%%% &     	(%%%%$Q'''''	( 	(''s   AAA&DDD&EEE&FFF&GGG&HHH& III
K//L Lc                     d }d | _         t          j                    } ||                                          } |t          j                                                  }t          st          r
||k    sJ d S d S d S )Nc                 &   ddh}t           r|                    g d           t          |                                           D ]0}||v s|                    d          r|                     |           1d |                                 D             S )NPLATHOME)__CF_USER_TEXT_ENCODINGVERSIONER_PYTHON_PREFER_32_BITVERSIONER_PYTHON_VERSIONPYTEST_c                     i | ]X\  }}|                     d d                               dd          |                     d d                               dd          YS )rc  
)rh  )r   kvs      r;   
<dictcomp>z@TestProcess.test_environ.<locals>.clean_dict.<locals>.<dictcomp>  sm        Aq 		$##++D"55IIdB''//b99  rI   )r   updater  r9  r   popr&  )r<  excludery   s      r;   
clean_dictz,TestProcess.test_environ.<locals>.clean_dict  s    v&G          
 QVVXX    7??dooi&@&@?EE$KKK  GGII	   rI   )maxDiffr1   r2   r  r4   copyr   r   )r9   r  r:   d1d2s        r;   test_environzTestProcess.test_environ  s    	 	 	$ NZ		$$Z
))** 	~ 	8888	 	 	 	8rI   z<macOS 11+ can't get another process environment, issue #2084z(sometimes fails on `assert is_running()`c                    t          j        d          }t          |                                 |          }|                     |gt
          j        t
          j                  }t          j        |j	                  }t          |j	                   |                                sJ |j                                        dk    sJ t          r3t          r,	 |                                }n*# t          j        $ r Y d S w xY w|                                }|dddk    sJ |                                 |j        dk    s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_   rI   r  3)ACr   )textwrapdedentr"   r   rh  rb   rc   r1   r2   r3   r,   r_  r_   r   r   r   r  rZ   communicate
returncode)r9   rG   cexesprocr:   envs         r;   test_weird_environzTestProcess.test_weird_environ  sS         DOO--d;;;""F*/*/ # 
 
 N59%%QU||~~|  ""c)))) 	Z 	iikk&    
 ))++CCc******1$$$$$$s   C+ +C>=C>N)h__name__
__module____qualname____doc__r<   rH   rL   rP   r%   markskipifr   r[   re   r	   rn   rv   r{   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/  r7  r   rQ  rZ  r]  r`  rk  rs  rx  r~  r  r  r  r  r  r  r  r  r   r  r  r  r   r  r  r  r
   r  r   r  r	  r  r  r  r  r  r)  r+  r.  r0  r=  rD  rH  rN  rX  r`  rb  rd  rw  r|  r   r  r   r  rX  rI   r;   r.   r.   H   s
       ))  ! ! !! ! !	! 	! 	! [E	+66
# 
# 76
#! ! !6 ['899. . :9.2/ / /.  ! ! !(	' 	' 	'  
L 
L 
L [,,_EE8 8 FE8L L L [E	,77	' 	' 87	' [00IIU+++$ $ ,+ JI$L [J??[E	,77) ) 87 @?)V [J??[?   - -  @?-4 [J??$ $ @?$. [J??
6 
6 @?
6 [J??A A @?A$ [J??A A @?A [J??	4 	4 @?	4 [(++& & ,+&" [GN;;# # <;#
 [K@@5 5 A@5& 5)))[K@@
 
 A@ *) 
  + + +<! ! !  [O+ODD.- .- ED.-` [O+ODD	. 	. ED	., , ," " "  6> > >6" " "45 5 5 7 7 74 [E	,77
. 
. 87
. [E	,77
. 
. 87
.@ @ @D3 3 31 1 1"& & &
D 
D 
D [,,_EE$( $( FE$(L [,,_EE
$ 
$ FE
$ [,,_EE= = FE=, [O44#. #. 54#.L [O446 6 5464 [E	,77[(++$ $ ,+ 87$ U+++[6"D   

 

  ,+

' ' '; ; ;
- 
- 
-( ( ( 1 1 14 4 44 4 403 3 3) ) )($ $ $3 3 3  2& 2& 2&h! ! !! ! !(( ( ($) $) $)L [E	,77F F 87F [E	,77  87 [E	,77	 	 87	' ' 'R,( ,( ,(\ [K@@  A@4 [K@@[E	,77[M    [A   %% %% 	  87 A@%% %% %%rI   r.   c                       e Zd ZdZed             Zej                            e	oe
d          d             Zd Zd Zd Zd	S )
	TestPopenzTests for psutil.Popen class.c                 "    t                       d S r>   r&   )clss    r;   tearDownClasszTestPopen.tearDownClass"  s    rI   zhangs on OSX + CIrQ   c                 l   t           ddg}t          j        |t          j        t          j        t
                    5 }|                                 |                                 |j         t          |          sJ t          j        t                    5  |j         d d d            n# 1 swxY w Y   |                                 d d d            n# 1 swxY w Y   t          r(|                    d          t"          j         k    sJ d S |                    d          t"          j        k    sJ d S )Nr]   3import time; [time.sleep(0.1) for x in range(100)];stdoutr_   r  ra   )r   r1   Popenrb   rc   r   ry   r   r  r   r%   r6   r7   r4  rK   r   rA   rB   rC   r9   rd   procs      r;   	test_misczTestPopen.test_misc&  s    A

 \??	
 
 
 	
 IIKKKNNJJt99~..                NN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  	299Q<<FN?22222299Q<<6>111111s6   ACB*C*B.	.C1B.	2CCCc                 R   t          j        t          dgt          j        t          j        t          j        t
                    5 }|                                 d d d            n# 1 swxY w Y   |j        j        sJ |j	        j        sJ |j
        j        sJ |j        dk    sJ d S )Nz-V)r  r_   r  r  r   )r1   r  r   rb   rc   r   r  r  closedr_   r  r  )r9   r  s     r;   test_ctx_managerzTestPopen.test_ctx_managerB  s    \??/
 
 
 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 {!!!!{!!!!z    !######s   A%%A),A)c                 2   t           ddg}t          j        |t          j        t          j        t
                    5 }|                                 |                                 t          j	        t          j
                  5  |                                 d d d            n# 1 swxY w Y   t          j	        t          j
                  5  |                                 d d d            n# 1 swxY w Y   t          j	        t          j
                  5  |                    t          j                   d d d            n# 1 swxY w Y   t          rt          j	        t          j
                  5  |                    t          j                   d d d            n# 1 swxY w Y   t          j	        t          j
                  5  |                    t          j                   d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S Nr]   r  r  )r   r1   r  rb   rc   r   rK   rA   r%   r6   rX   r@   rN   rB   rC   r   CTRL_C_EVENTCTRL_BREAK_EVENTr  s      r;   test_kill_terminatezTestPopen.test_kill_terminateP  s/   
 A

 \??	
 
 
 	>
 NNIIKKKv344 ! !   ! ! ! ! ! ! ! ! ! ! ! ! ! ! !v344  		              v344 1 1  0001 1 1 1 1 1 1 1 1 1 1 1 1 1 1 >]6#788 : :$$V%8999: : : : : : : : : : : : : : :]6#788 > >$$V%<===> > > > > > > > > > > > > > >#	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	>s   AHB$H$B(	(H+B(	,!HC."H.C2	2H5C2	6!H E7HE	H
E	(H3 FHF#	#H&F#	'!H G4(H4G8	8H;G8	<HHHc                 n   t           ddg}t          j        |t          j        t          j        t
                    5 }|                                 |                                 t          j	        t                    5  |j         d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S r  )r   r1   r  rb   rc   r   rK   rA   r%   r6   r7   r4  r  s      r;   test__getattribute__zTestPopen.test__getattribute__m  s(   A

 \??	
 
 
 		
 NNIIKKK~..                		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		s6   AB*>BB*B	B*B	B**B.1B.N)r  r  r  r  classmethodr  r%   r  r  r   r   r  r  r  r  rX  rI   r;   r  r    s        ''  [ [0.9LMM2 2 NM26$ $ $> > >:    rI   r  )>r  r!  rm  r   r  ro  r  r4   rB   r  rF  r  rb   rf  r  r}   unittestr   r1   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.   r  rX  rI   r;   <module>r     s   & %           				     				         



                                                                $ $ $ $ $ $ # # # # # # ' ' ' ' ' ' ' ' ' ' ' ' ) ) ) ) ) ) $ $ $ $ $ $ # # # # # # ( ( ( ( ( ( ) ) ) ) ) ) - - - - - - # # # # # # $ $ $ $ $ $ % % % % % %       # # # # # # ' ' ' ' ' ' ' ' ' ' ' ' # # # # # # # # # # # # , , , , , , % % % % % % & & & & & & * * * * * *       & & & & & & ) ) ) ) ) )       . . . . . . 0 0 0 0 0 0 % % % % % %O% O% O% O% O%. O% O% O%n.] ] ] ] ] ] ] ] ] ]rI   