Your IP : 3.147.67.237


Current Path : /lib64/python3.6/site-packages/psutil/tests/__pycache__/
Upload File :
Current File : //lib64/python3.6/site-packages/psutil/tests/__pycache__/test_process.cpython-36.pyc

3

��JZ9��@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlmZddlmZddlmZddlmZddlmZddlmZdd	lmZdd
lmZddlmZddlmZdd
lmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%ddlm&Z&ddlm'Z'ddlm(Z(ddlm)Z)ddlm*Z*ddlm+Z+ddlm,Z,ddlm-Z-dd lm.Z.dd!lm/Z/dd"lm0Z0dd#lm1Z1dd$lm2Z2dd%lm3Z3dd&lm4Z4dd'lm5Z5dd(lm6Z6dd)lm7Z7dd*lm8Z8dd+lm9Z9dd,lm:Z:dd-lm;Z;dd.lm<Z<dd/lm=Z=Gd0d1�d1e;j>�Z?e�r�ej@�dk�r�Gd2d3�d3e?�ZAGd4d5�d5e;j>�ZBeCd6k�r�e2eD�dS)7zTests for psutil.Process class.�N)�AIX)�BSD)�LINUX)�NETBSD)�OPENBSD)�OSX)�POSIX)�SUNOS)�WINDOWS)�long)�PY3)�APPVEYOR)�
call_until)�copyload_shared_lib)�
create_exe)�create_proc_children_pair)�create_zombie_proc)�enum)�get_test_subprocess)�
get_winver)�HAS_CPU_AFFINITY)�HAS_ENVIRON)�
HAS_IONICE)�HAS_MEMORY_MAPS)�HAS_PROC_CPU_NUM)�HAS_PROC_IO_COUNTERS)�
HAS_RLIMIT)�HAS_THREADS)�mock)�PYPY)�
PYTHON_EXE)�
reap_children)�retry_before_failing)�run_test_module_by_name)�safe_rmpath)�sh)�skip_on_access_denied)�skip_on_not_implemented)�TESTFILE_PREFIX)�TESTFN)�
ThreadTask)�TRAVIS)�unittest)�wait_for_pid)�	WIN_VISTAc@sreZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zejed�dd��Zdd �Zejed!�ejed"�d#d$���Zejed�eed%�d&d'���Zejed�ejeo�e�e kd�d(d)���Z!ejed�eje�oe�e kd�d*d+���Z"eje#d�d,d-��Z$eje#d�d.d/��Z%eje#d�d0d1��Z&eje#d�d2d3��Z'eje#d�d4d5��Z(d6d7�Z)ejed8�d9d:��Z*eje+d�d;d<��Z,e-�e.e/d%�eje+d�d=d>����Z0d?d@�Z1dAdB�Z2eje3d�dCdD��Z4eje3d�dEdF��Z5dGdH�Z6dIdJ�Z7dKdL�Z8dMdN�Z9dOdP�Z:eje;dQ�eje<dR�dSdT���Z=ejed!�dUdV��Z>ejed!�dWdX��Z?dYdZ�Z@d[d\�ZAd]d^�ZBd_d`�ZCdadb�ZDejeEd�dcdd��ZFejeEd�dedf��ZGejeEd�dgdh��ZHejeIdi�ejeJdj�dkdl���ZKejeIdi�ejeJdj�dmdn���ZLejed!�dodp��ZMeed%�ejeN�pzeOdq�drds���ZPdtdu�ZQdvdw�ZRdxdy�ZSdzd{�ZTd|d}�ZUd~d�ZVd�d��ZWd�d��ZXd�d��ZYd�d��ZZd�d��Z[d�d��Z\ejed!�d�d���Z]ejed!�d�d���Z^ejed!�d�d���Z_d�d��Z`ejead�d�d���Zbejead�ejed!�d�d����Zcd�S)��TestProcesszTests for psutil.Process class.cCstt�dS)N)r$r))�self�r1�$/usr/lib64/python3.6/test_process.py�setUpNszTestProcess.setUpcCs
t�dS)N)r!)r0r1r1r2�tearDownQszTestProcess.tearDownc	CsXtj�}|j|jtj��t�}|jtj|j�j|j�|jt��d|_WdQRXdS)N�!)	�psutil�Process�assertEqual�pid�os�getpidr�assertRaises�AttributeError)r0�p�sprocr1r1r2�test_pidTszTestProcess.test_pidcCsNt�}|j}tj|�}|j�|j�}|jtj|��trJ|j	|t
j�dS)N)rr9r6r7�kill�wait�assertFalse�
pid_existsrr8�signal�SIGKILL)r0r?r@r>�sigr1r1r2�	test_kill\s
zTestProcess.test_killcCsNt�}|j}tj|�}|j�|j�}|jtj|��trJ|j	|t
j�dS)N)rr9r6r7�	terminaterBrCrDrr8rE�SIGTERM)r0r?r@r>rGr1r1r2�test_terminatefs
zTestProcess.test_terminatec CsLtr
tjntj}t�}tj|j�}|j|�|j	�}|j
tj|j��t�rH|j||�t�}tj|j�}|j|�t
jdttjd�d��(|jtj��|j|�WdQRXWdQRXt�}tj|j�}|j|�t
jdttjd�d��,|jtj��tj�j|�WdQRXWdQRXdtj�k�rHtjd�}|jt|jtj�dS)Nzpsutil.os.kill�)�side_effectr)rrErFrJrr6r7r9�send_signalrBrCrDr8r�patch�OSError�errnoZESRCHr<�
NoSuchProcessZEPERM�AccessDenied�pids�
ValueError)r0rGr?r>Zexit_sigr1r1r2�test_send_signalps2


"
zTestProcess.test_send_signalcCs^t�}tj|j�}|j�|j�}tr8|j|tj	�n|j|tj
�|j|j��t�}tj|j�}|j
�|j�}tr�|j|tj
�n|j|tj
�|j|j��d}ttd|g�}tj|j�}|j|j�d�|j|j��ttd|g�}tj|j�}|j|j�d�|j|j�d�t�}tj|j�}|j�|jtj|jd�|jt|jd�dS)Nz0import time, sys; time.sleep(0.01); sys.exit(5);z-c�g{�G�z�?�)rWN���)rr6r7r9rArBrr8rErFrJrC�
is_runningrIr �assertIn�namer<�TimeoutExpiredrU)r0r?r>�coder1r1r2�	test_wait�s<zTestProcess.test_waitcCs�t�\}}|jtj|jd�|jtj|jd�|j�|j�|j�}|j�}trp|j|tj	�|j|d�n|j|tj	�|j|tj	�dS)Ng{�G�z�?)
rr<r6r]rBrIrr8rErJ)r0�p1�p2Zret1Zret2r1r1r2�test_wait_non_children�s
z"TestProcess.test_wait_non_childrencCs�t�}tj|j�}|jtj|jd�|j�tj�d}x:y|jd�}Wn$tjk
rltj�|krh�Yq:XPq:Wt	r�|j
|tj�n|j
|tj
�|j|j��dS)Nr�)rr6r7r9r<r]rBrA�timerr8rErFrJrCrZ)r0r?r>Zstop_atr^r1r1r2�test_wait_timeout_0�s zTestProcess.test_wait_timeout_0c
Cs�tj�}|jdd�|jdd�xRtd�D]F}|jdd�}|j|t�|j|d�tsd|j|d�q*|j|d�q*W|j	t
��|jdd�WdQRXdS)Ng����MbP?)Zinterval�dggY@rXrY)r6r7�cpu_percent�range�assertIsInstance�float�assertGreaterEqualr�assertLessEqualr<rU)r0r>�xZpercentr1r1r2�test_cpu_percent�szTestProcess.test_cpu_percentc	Cs4tjddd��}tj�j�|js&t�WdQRXdS)Nzpsutil.cpu_count)Zreturn_value)rrOr6r7rg�called�AssertionError)r0�mr1r1r2�test_cpu_percent_numcpus_none�sz)TestProcess.test_cpu_percent_numcpus_nonecCsxtj�j�}|jdks(|jdks(t|��|jdks:t|��|jdksLt|��x&|jD]}t	j
dt	jt||���qTWdS)Ngz%H:%M:%S)
r6r7�	cpu_times�user�systemrp�
children_user�children_system�_fieldsrd�strftime�	localtime�getattr)r0�timesr\r1r1r2�test_cpu_times�szTestProcess.test_cpu_timescCs�tj�j�dd�\}}tj�dd�\}}t||g�t||g�dkrZ|jd||f�t||g�t||g�dkr�|jd||f�dS)Nrcg�������?zexpected: %s, found: %s)r6r7rsr:r|�max�min�fail)r0�	user_timeZkernel_time�utimeZktimer1r1r2�test_cpu_times_2szTestProcess.test_cpu_times_2z
not supportedcCsPtj�}|j�}|j|d�tj�dkr4|j|d�|j|j�ttj���dS)NrrX)r6r7Zcpu_numrk�	cpu_countr8r[rh)r0r>Znumr1r1r2�test_cpu_numszTestProcess.test_cpu_numcCsdt�}tj�}tj|j�}|j�}t||�}|dkrJ|jd|||f�tjdtj	|j���dS)Nrcz'expected: %s, found: %s, difference: %sz%Y %m %d %H:%M:%S)
rrdr6r7r9�create_time�absr�ryrz)r0r?Znowr>r��
differencer1r1r2�test_create_timeszTestProcess.test_create_timez
POSIX onlyznot reliable on TRAVIScCsLtj�j�}tjj�s tjj�r>tjj	t
d��}|j||�n
|j|�dS)N�tty)
r6r7�terminal�sys�stdin�isatty�stdoutr:�path�realpathr%r8�assertIsNone)r0r�r�r1r1r2�
test_terminal-s
zTestProcess.test_terminal)Zonly_ifcCs�tj�}|j�}ttd��}|j�WdQRX|j�}tr�tr�|j|j	|j	�|j
|j|j�tr�|j|j
|j
�|j
|j|j�n |j|j|j�|j|j|j�|j�}tjtd��.}tr�|jtddd��n|jdd�WdQRX|j�}|j|j|j�|j|j|j�|j|j	|j	�|j|j|j�t�r^|j|j|j�|j|j
|j
�xJtt|��D]:}t�r�|dk�r��ql|j||d�|j||d��qlWdS)N�rb)�prefixrmi@B�asciircr)r6r7Zio_counters�openr �readrr�
assertGreaterZ
read_countr8Zwrite_countrZ
read_charsZwrite_charsrkZ
read_bytesZwrite_bytes�tempfileZ
TemporaryFiler(r�write�bytesrh�len)r0r>Zio1�fZio2�ir1r1r2�test_io_counters7s>zTestProcess.test_io_countersc	Cs�t�r<ddlm}m}m}m}|j|d�|j|d�|j|d�|j|d�tj�}z�|jd�|j�\}}t	dk	r�|j
|t	j�|j|d�|j|d�|jd�|j�\}}|j|d�|j|d�|jdd�|j�\}}|j|d�|j|d�|jdd�|j�\}}|j|d�|j|d�Wd|j|�XnZtj�}|j�}|j
|t�z0d}||k�rld}|j|�|j|j�|�Wd|j|�XdS)Nr)�IOPRIO_CLASS_NONE�IOPRIO_CLASS_RT�IOPRIO_CLASS_BE�IOPRIO_CLASS_IDLErXrc���)
rr6r�r�r�r�r8r7�ionicerri�IntEnum�int)	r0r�r�r�r�r>Zioclass�value�originalr1r1r2�test_ionicecsJ



zTestProcess.test_ionicecCs�t�}tj|j�}tr�|jt|jdd�|jt|jdd�|jt|jd�|jt|jdd�|j	td|jtj
d�|j	td|jtjd�|j	td|jdd	�n"|jt|jd
�|jt|jdd�dS)Nrc�
rXr��fooz*can't specify value with IOPRIO_CLASS_NONEz*can't specify value with IOPRIO_CLASS_IDLEz$'ioclass' argument must be specified)r�r�rY)rr6r7r9rr<rUr��	TypeErrorZassertRaisesRegexr�r�)r0r?r>r1r1r2�test_ionice_errs�s$zTestProcess.test_ionice_errscCs�ddl}tjtj��}dd�tt�D�}|s4t|��x�|D]�}tt|�}|j|d�|t|�kr�|j	|t||��t
rxq:|j	|j|�|j|��q:|j|�}|j	t
|�d�|j|dd�|j|dd�q:WdS)NrcSsg|]}|jd�r|�qS)ZRLIMIT)�
startswith)�.0rmr1r1r2�
<listcomp>�sz/TestProcess.test_rlimit_get.<locals>.<listcomp>rcrXrYrY)�resourcer6r7r:r;�dirrpr{rkr8r�rlimitZ	getrlimitr�)r0r�r>�namesr\r��retr1r1r2�test_rlimit_get�s 


zTestProcess.test_rlimit_getcCs�t�}tj|j�}|jtjd�|j|jtj�d�|jt��tj	jd�jd�WdQRX|jt��|jtjd�WdQRXdS)NrWr)rWrW)rWrW)rWrWrW)
rr6r7r9r��
RLIMIT_NOFILEr8r<rUZ_psplatform)r0r?r>r1r1r2�test_rlimit_set�szTestProcess.test_rlimit_setc Cs�tj�}|jtj�\}}z�|jtjd|f�ttd��}|jdd�WdQRX|jt��*}ttd��}|jdd�WdQRXWdQRX|j	t
r�|jjn|jdtj
�Wd|jtj||f�|j	|jtj�||f�XdS)Ni�wb�Xir)r6r7r��RLIMIT_FSIZEr�r)r�r<�IOErrorr8rZ	exceptionrQZEFBIG)r0r>�soft�hardr��excr1r1r2�test_rlimit�s"zTestProcess.test_rlimitcCs�tj�}|jtj�\}}zN|jtjd|f�|jtjtj|f�ttd��}|jdd�WdQRXWd|jtj||f�|j|jtj�||f�XdS)Nir�r�i)	r6r7r�r��
RLIM_INFINITYr�r)r�r8)r0r>r�r�r�r1r1r2�test_rlimit_infinity�sz TestProcess.test_rlimit_infinitycCs<tj�}|jtj�\}}|jtj|�|jtj||f�dS)N)r6r7r�r�r8r�)r0r>r�r�r1r1r2�test_rlimit_infinity_value�sz&TestProcess.test_rlimit_infinity_valuecCsrtj�}tr<y|j�}WqDtjk
r8tjd��YqDXn|j�}t��|j�}|j||d�WdQRXdS)Nz$on OpenBSD this requires root accessrX)	r6r7r�num_threadsrSr,�SkipTestr*r8)r0r>�step1�step2r1r1r2�test_num_threads�szTestProcess.test_num_threadszWINDOWS onlycCstj�}|j|j�d�dS)Nr)r6r7r�Znum_handles)r0r>r1r1r2�test_num_handlesszTestProcess.test_num_handlescCs�tj�}tr<y|j�}WqDtjk
r8tjd��YqDXn|j�}t��~|j�}|jt	|�t	|�d�t
r�|j|djtj
��|d}|j|j|d�|j|j|d�|j|j|d�WdQRXdS)Nz$on OpenBSD this requires root accessrXrrc)r6r7r�threadsrSr,r�r*r8r�r�idr:r;r��system_time)r0r>r�r�Zathreadr1r1r2�test_threadss zTestProcess.test_threadsc
Cs�t�}tj|j�}trDy|j�Wn tjk
rBtjd��YnX|j	|j
�jtdd�|j�D��dd�|j	|j
�j
tdd�|j�D��dd�dS)Nz$on OpenBSD this requires root accesscSsg|]
}|j�qSr1)r�)r�rmr1r1r2r�7sz.TestProcess.test_threads_2.<locals>.<listcomp>g�������?)ZdeltacSsg|]
}|j�qSr1)r�)r�rmr1r1r2r�:s)rr6r7r9rr�rSr,r�ZassertAlmostEqualrsrt�sumru)r0r?r>r1r1r2�test_threads_2)szTestProcess.test_threads_2cCs�tj�}|j�dd�\}}|j�}|j|d�|j|d�dgd}|j�dd�\}}|j�}|j||�|j||�|j||�~tr�|j�}	|j|	j|	j	�|j|	j
|	j�|j�}	x |	jD]}
|jt
|	|
�d�q�WdS)Nrcri`�)r6r7Zmemory_info�memory_percentr�rkr
r8ZrssZwset�vmsZpagefilerxr{)r0r>Zrss1Zvms1Zpercent1ZmemarrZrss2Zvms2Zpercent2�memr\r1r1r2�test_memory_info<s&
zTestProcess.test_memory_infocCs�tj�j}tj�j�}xB|jD]8}t||�}|j|d||fd�|j|||||fd�qWt	sft
sftrt|j|jd�t	r�|j|j
d�|j|jd�dS)Nr)�msg)r6Zvirtual_memory�totalr7Zmemory_full_inforxr{rkrlrr
r�ussZpssZswap)r0r�r�r\r�r1r1r2�test_memory_full_infoZs

z!TestProcess.test_memory_full_infoc
Cs�tj�}|j�}dd�|D�}|jt|�tt|���|jdd�}x�|D]�}|jjd�sHtjj	|j�spt
|j��tr�y*tjj|j�s�tjj
|j�s�t
|j��WnHt
k
r�ts��n,td��}|j�}WdQRXd|j|kr�YnXqHdtjj|j�krHtjj|j�sHt
|j��qHWxv|D]n}xf|jD]\}t||�}	|d	k�rH�q*n<|d
k�rb|	�s�t
|	��n"|j|	ttf�|	dk�s*t
|	���q*W�qWdS)NcSsg|]}|�qSr1r1)r�rmr1r1r2r�ksz0TestProcess.test_memory_maps.<locals>.<listcomp>F)Zgrouped�[z/proc/self/smapsz%s (deleted)Z64r��addr�permsr)r�r�)r6r7�memory_mapsr8r��setr�r�r:�isabsrpr�exists�islinkrr�r��basenamerxr{rir�r)
r0r>�maps�pathsZext_maps�ntr��dataZfnamer�r1r1r2�test_memory_mapsgs<






zTestProcess.test_memory_mapsc
sHt��8}dd���fdd�tj�j�D�}|j�|�|�WdQRXdS)NcSstjjtjj|��S)N)r:r�r��normcase)r>r1r1r2�normpath�sz8TestProcess.test_memory_maps_lists_lib.<locals>.normpathcsg|]}�|j��qSr1)r�)r�rm)r�r1r2r��sz:TestProcess.test_memory_maps_lists_lib.<locals>.<listcomp>)rr6r7r�r[)r0r�Zlibpathsr1)r�r2�test_memory_maps_lists_lib�s

z&TestProcess.test_memory_maps_lists_libcCs�tj�}|j�}d|ko"dkns0t|��|jdd�}d|koNdkns\t|��d|kondkns|t|��|jt|jdd�ts�ts�tr�|jdd�}d|ko�dkns�t|��d|ko�dkns�t|��dS)Nrrfr�)Zmemtypez?!?r�)	r6r7r�rpr<rUrrr
)r0r>r�r1r1r2�test_memory_percent�s    zTestProcess.test_memory_percentcCsZt�}tj|j�}|j�st�|j�s*t�|j�|j�|j�sHt�|j�sVt�dS)N)rr6r7r9rZrprArB)r0r?r>r1r1r2�test_is_running�szTestProcess.test_is_runningcCs�t�}tj|j�j�}y|j|t�Wn�tk
r�trht	|�t	t�krht
jj}|j||�|t��nNdt
jdt
jdf}y |j|j|d�tj|d��Wntk
r�YnXYnXt|ddg�}|j|d�dS)Nz%s.%srrXrLz-czimport os; print('hey')Zhey)rr6r7r9�exer8r rpr
r�r:r�r�r��version_info�replacer%)r0r?r�r�Zver�outr1r1r2�test_exe�s zTestProcess.test_execCs�tddg}t|�}y(|jdjtj|j�j��dj|��Wn@tk
rzt	sVt
sVtrt|jtj|j�j�dt�n�YnXdS)Nz-czimport time; time.sleep(60)� r)r rr8�joinr6r7r9�cmdlinerprrr)r0r�r?r1r1r2�test_cmdline�s
zTestProcess.test_cmdlinecCsPtt�}tj|j�j�j�}tjj	tjj
tj��j�}|j
|�sLt||f��dS)N)rr r6r7r9r\�lowerr:r�r�r�r��
executabler�rp)r0r?r\Zpyexer1r1r2�	test_name�szTestProcess.test_namezbroken on SUNOSz
broken on AIXcs��fdd�}td�t��|j|��ddddddg}t|�}tj|j�}trZt|j�|j	|j
�|�|j	|j�tj
j���|j	tj
j|j��tj
j���dS)	Ncs&yt��Wntk
r YnXdS)N)r$rPr1)�
funky_pathr1r2�rm�sz.TestProcess.test_prog_w_funky_name.<locals>.rmz	foo bar )z-cz9import time; [time.sleep(0.01) for x in range(3000)];arg1Zarg2rLZarg3)r)r�
addCleanuprr6r7r9r+r-r8r�r\r:r�r�r�r�)r0r�r�r?r>r1)r�r2�test_prog_w_funky_name�s	

z"TestProcess.test_prog_w_funky_namecCsXtj�}|j�\}}}|j|tj��|j|tj��ttd�rT|jtj�|j��dS)N�	getresuid)	r6r7�uidsr8r:�getuid�geteuid�hasattrr�)r0r>�real�	effective�savedr1r1r2�	test_uidss
zTestProcess.test_uidscCsXtj�}|j�\}}}|j|tj��|j|tj��ttd�rT|jtj�|j��dS)Nr�)	r6r7�gidsr8r:�getgid�getegidr�	getresgid)r0r>rrrr1r1r2�	test_gidss
zTestProcess.test_gidscCs�tj�}|jt|jd�tr�z||j�}tjdkr@|j|t	j
�n|j|t�|j|tj
�|jtj�|j|j�tj�|jtj
�|j|j�tj
�Wd|jtj
�Xn�|j�}z�y�ttd�r�|jtjtjtj��|j��|jd�|j|j�d�ttd��r"|jtjtjtj��|j��t�sB|jd�|j|j�d�Wntjk
�r\YnXWdy|j|�Wntjk
�r�YnXXdS)N�strr�r��getpriorityrXr)r�r�)r6r7r<r��nicer
r�r�rirr�r�r8�NORMAL_PRIORITY_CLASSZHIGH_PRIORITY_CLASSrr:r�PRIO_PROCESSr;rrS)r0r>ZinitZ
first_nicer1r1r2�	test_nice"sB




zTestProcess.test_nicecCstj�}|j|j�tj�dS)N)r6r7r8�statusZSTATUS_RUNNING)r0r>r1r1r2�test_statusKszTestProcess.test_statuscCsnt�}tj|j�}|j�}trZ|jd�\}}|j|tj	��dt
jkrj|j|t
jd�n|j|tj	��dS)N�\Z
USERDOMAIN)rr6r7r9�usernamer
�splitr8�getpassZgetuserr:�environ)r0r?r>rZdomainr1r1r2�
test_usernameOs
zTestProcess.test_usernamecCs*t�}tj|j�}|j|j�tj��dS)N)rr6r7r9r8�cwdr:�getcwd)r0r?r>r1r1r2�test_cwd[szTestProcess.test_cwdcCs.tddg}t|�}tj|j�}t|jd�dS)Nz-cz/import os, time; os.chdir('..'); time.sleep(60)z#ret == os.path.dirname(os.getcwd()))r rr6r7r9rr)r0�cmdr?r>r1r1r2�
test_cwd_2`s
zTestProcess.test_cwd_2cCs�tj�}|j�}|st|��|j|j|�ttd�rL|j|ttj	|j
���|jt|�tt|���tt
ttjdd����}xzts�|n|D]j}|j|g�|j|j�|g�ttd�r�|j|j�ttj	|j
���t|d�r�|j|j�d|j��q�W|jg�t�r |j|j�|jj��n|j|j�|�ttd��rX|j|j�ttj	|j
���|jt|jd�|j|�|jt|��|jt|��dS)N�sched_getaffinityT)�percpu�num_cpurrX)r6r7�cpu_affinityrpr�rr:r8�listr r9r�r�rhrgr+r"r�_procZ_get_eligible_cpusr<r��tuple)r0r>�initialZall_cpus�nr1r1r2�test_cpu_affinitygs6






zTestProcess.test_cpu_affinitycCszt�}tj|j�}ttjdd��dg}|jt|j|�|jt|jt	dd��|jt
|jddg�|jt|jdd	g�dS)
NT)r!r�i'i�*r�1rXrY)rr6r7r9r�rsr<rUr#rhr�)r0r?r>Zinvalid_cpur1r1r2�test_cpu_affinity_errs�sz"TestProcess.test_cpu_affinity_errscCs�tj�}|j�}|st|��|j|j|�g}xBtdt|�d�D],}x&tj||�D]}|rT|j	t
|��qTWqBWx&|D]}|j|�|j|j�|�qxWdS)NrrX)r6r7r#rpr�rhr��	itertools�combinations�appendr$r8)r0r>r'Zcombos�lZsubsetZcombor1r1r2�"test_cpu_affinity_all_combinations�s

z.TestProcess.test_cpu_affinity_all_combinationsz
broken on BSDzunreliable on APPVEYORc	CsRtj�}|j�}|jt|k�ttd��n}|jdd�|j�t|jdt	|��}x<|D]"}|j
tkrZtrz|j|j
d�PqZW|jdt|��WdQRXx"|D]}tj
j|j
�s�t|��q�Wdt}ttd|g�}tj|j�}xDtd�D],}d	d
�|j�D�}t|k�rPtjd�q�W|jt|�x$|D]}tj
j|��s.t|���q.WdS)Nr��xizlen(ret) != %izno file found; files=%sz2import time; f = open(r'%s', 'r'); time.sleep(60);z-crfcSsg|]
}|j�qSr1)r�)r�rmr1r1r2r��sz/TestProcess.test_open_files.<locals>.<listcomp>g{�G�z�?)r6r7�
open_filesrCr)r�r��flushrr�r�rr8Zpositionr��reprr:�isfilerprr r9rhrd�sleepr[)	r0r>�filesr��filer�r?rm�	filenamesr1r1r2�test_open_files�s4




zTestProcess.test_open_filescCs�ttd���}tj�}xB|j�D] }|j|jks<|j|j�krPqW|j	dt
|j���|j|j|j�tr||j|jd�n|j|j|j��|j�d}|j|d|j�|j|d|j�|j
|j|j��WdQRXdS)N�wzno file found; files=%srXrrY)r�r)r6r7r2r�r\�fd�filenor�r4r8r
�assertNotIn)r0Zfileobjr>r8Zntupler1r1r2�test_open_files_2�szTestProcess.test_open_files_2cCs�tj�}|j�}ttd�}|j|j�|j|j�|d�tj�}|j|j�|j|j�|d�|j�|j�|j|j�|�dS)Nr;rXrc)	r6r7Znum_fdsr�r)r��closer8�socket)r0r>�startr8Zsockr1r1r2�test_num_fds�s
zTestProcess.test_num_fdsz not reliable on OPENBSD & NETBSDcCsLtj�}t|j��}x(td�D]}t|j��}||krdSqW|jd�dS)Ni �z7num ctx switches still the same after 50.000 iterations)r6r7r�Znum_ctx_switchesrhr�)r0r>Zbeforerm�afterr1r1r2�test_num_ctx_switches�sz!TestProcess.test_num_ctx_switchescCs�ttd�r"|jtj�j�tj��tj�}t�}tj|j	�}|j|j�|�t
dd�tr^dSx2tj�D]&}|j	|j	krzqh|j
|j�||d�qhWdS)N�getppidT)�	recursive)r�)rr:r8r6r7�ppidrFr;rr9r!r
�process_iter�assertNotEqual)r0�this_parentr?r>r1r1r2�	test_ppids

zTestProcess.test_ppidcCs0tj�}t�}tj|j�}|j|j�j|�dS)N)r:r;rr6r7r9r8�parent)r0rKr?r>r1r1r2�test_parentszTestProcess.test_parentc	CsFt�}tj|j�}tjdtjdd�d��|j|j��WdQRXdS)Nzpsutil.Processrr�)rM)	rr6r7r9rrOrRr�rM)r0r?r>r1r1r2�test_parent_disappeared s
z#TestProcess.test_parent_disappearedcCs�tj�}|j|j�g�|j|jdd�g�tdd�}|j�}|jdd�}xL||fD]@}|jt|�d�|j|dj|j�|j|dj�tj	��qTWdS)NT)rGr)Z
creationflagsrX)
r6r7r8�childrenrr�r9rHr:r;)r0r>r?Z	children1Z	children2rPr1r1r2�
test_children(s
zTestProcess.test_childrencCsdt�\}}tj�}|j|j�|g�|j|jdd�||g�|j�|j�|j|jdd�g�dS)NT)rG)rr6r7r8rPrIrB)r0r`rar>r1r1r2�test_children_recursive7s
z#TestProcess.test_children_recursivec
Cs�tjt�}x@tj�D]4}y||j�d7<Wqtjk
rFYqXqWt|j�dd�d�dd}tj	|�}y|j
dd�}Wntjk
r�YnX|jt
|�t
t|���dS)	NrXcSs|dS)NrXr1)rmr1r1r2�<lambda>Msz6TestProcess.test_children_duplicates.<locals>.<lambda>)�keyrT)rGrY)�collections�defaultdictr�r6rIrH�Error�sorted�itemsr7rPrSr8r�r�)r0�tabler>r9�cr1r1r2�test_children_duplicatesDs


z$TestProcess.test_children_duplicatescCsdt�}tj|j�}|j�x*td�D]}|j�tjkr8Ptj	d�q$W|j
�|j|j�tj�dS)Nrfg{�G�z�?)rr6r7r9�suspendrhrZSTATUS_STOPPEDrdr6�resumerJ)r0r?r>rmr1r1r2�test_suspend_resumeVszTestProcess.test_suspend_resumecCs$|jttjd�|jttjd�dS)Nr*rXrY)r<r�r6r7rU)r0r1r1r2�test_invalid_pidaszTestProcess.test_invalid_pidc;Cs�tj�}|jddgd�}|jt|j��ddg�tjttj���}|jdgdd�}t|dt	�sp|j|dd�t
jddtjd	��"|j|jd
gdd�d
di�WdQRXt
jddtj
|jd�d	��|jtj
|jd
gd�WdQRXt
jddtj|jd�d	��"|j|jd
gdd�d
di�WdQRXt
jddtd	��F|j�}|jd
t	|j���|jt��|jd
gd�WdQRXWdQRX|jt��|jd�WdQRX|jt��|jdg�WdQRX|jt��|jddg�WdQRXdS)
Nr�r\)�attrsZconnectionsr�)raZad_valuezpsutil.Process.niceT)ZcreaterMrrXZbar)r6r7�as_dictr8rX�keysrrT�
isinstancer$rrOrSrRr9r<�
ZombieProcess�NotImplementedErrorr>r�rU)r0r>�dr1r1r2�test_as_dictes>""
"zTestProcess.test_as_dictcCs�tjd��@}tj�}|j��|j�|j�WdQRX|j|jd�WdQRXtjd��}|j�|j�WdQRX|j|jd�dS)Nz$psutil._psplatform.Process.cpu_timesrXrc)rrOr6r7�oneshotrsr8�
call_count)r0rqr>r1r1r2�test_oneshot�s
zTestProcess.test_oneshotc&Cs�tjd���}tjd��r}tj�}|j��:|j�|j�|j��|j�|j�WdQRXWdQRX|j|jd�|j|jd�WdQRXWdQRXtjd��}|j�|j�WdQRX|j|jd�dS)Nz$psutil._psplatform.Process.cpu_timesz(psutil._psplatform.Process.oneshot_enterrXrc)rrOr6r7rirsr8rj)r0�m1�m2r>rqr1r1r2�test_oneshot_twice�s

"zTestProcess.test_oneshot_twicec
Cs�t�}tj|j�}|j�|j�tr8ttjd|j�|j	|j
��ddddddg}trjtrj|j
d��xTt|�D�]F}|jd	�sv||kr�qvy�t||�}|d
kr�tr�|d�}n
|tj�}np|dkr�|�}|d
�}nX|dkr�|tj�}|tjd�}n8|dk�r|�}|dg�}n|dk�r(|tj�}n|�}Wnztjk
�rV|jd|�Yqvtjk
�rlYqvtjk
�r�t�r�|dk�r�n�Yqvtk
�r�YqvX|jd||f�qvWdS)Nz
%s not in retr9rZrBr�riZmemory_info_exr��_rrXr�rcrWr#rrNz/ZombieProcess for %r was not supposed to happenr�r�z4NoSuchProcess exception not raised for %r, retval=%s)rWrW)r�r�)rr6r7r9rIrBr
rrTrCrZrrr.r�r�r{rrr�rErJrer�rRrSrrf)r0r?r>Zexcluded_namesr\�methr�r1r1r2�test_halfway_terminated_process�s^









z+TestProcess.test_halfway_terminated_processcCs�dd�}t�}|jtdd�tj|�}|j|j�tj�|j|j	��|j
�||j�}|dk	rl|j|g�t|d�r�||j
tj�||j
tjd�||j�t|d�r�y||jdg�Wn:tk
r�}ztr�tr�d	t|�kr�n�WYdd}~XnX||jd�t|d
��r0t�r$||jdd�n||jd�t|d��rL||j
tjd�||j�||j�||j�||j�|jtj|��t�r�t�r�|j|tj��|j|dd
�tj�D��it_|j|dd
�tj�D��dS)Nc_s,y
|||�Stjtjfk
r&YnXdS)N)r6rerS)Zfun�args�kwargsr1r1r2�succeed_or_zombie_p_exc�s
z@TestProcess.test_zombie_process.<locals>.succeed_or_zombie_p_excT)rGr�rWr#rznot eligibler�rccSsg|]
}|j�qSr1)r9)r�rmr1r1r2r�9sz3TestProcess.test_zombie_process.<locals>.<listcomp>cSsg|]
}|j�qSr1)r9)r�rmr1r1r2r�;s)rWrW)rWrW) rr�r!r6r7r8r�
STATUS_ZOMBIE�
assertTruerZrbr]rr�r�rMr#rUr+rr
rr�r^rIrArDrr[rTrIZ_pmap)r0rtZzpidZzprocr��errr1r1r2�test_zombie_process�sP










zTestProcess.test_zombie_processc	CsBtj�}tjdtjd�d��}|j�s*t�|js4t�WdQRXdS)Nzpsutil.Processr)rM)r6r7rrOrerZrpro)r0r>rqr1r1r2�$test_zombie_process_is_running_w_exc=s
z0TestProcess.test_zombie_process_is_running_w_excc
CsHtj�}tjdtjd�d��"}|j|j�tj�|js:t	�WdQRXdS)Nz!psutil._psplatform.Process.statusr)rM)
r6r7rrOrer8rrurorp)r0r>rqr1r1r2� test_zombie_process_status_w_excGs
z,TestProcess.test_zombie_process_status_w_exccCs:dtj�kr"|jtjtjd�dStjd�}x�tjD]�}|dkrBq4t||�}y
|�}Wntjk
rlYq4X|d
kr�|j|j	d�q4|dkr�t
r�|j|j�d�q�tr�|j|j�d�q4|dkr4|s4t
|��q4Wt|d	��ry|jtj�Wntjk
�rYnX|j�t�s6|jdtj��|jtjd��dS)Nrr9rrr�rootzNT AUTHORITY\SYSTEMr\r�)rr)r6rTr<rRr7Z_as_dict_attrnamesr{rSr8rrrr
rprr�r�rbrr[rvrD)r0r>r\rpr�r1r1r2�
test_pid_0Qs<


zTestProcess.test_pid_0cCs@dd�}d|_tj�}||j��}|tjj��}|j||�dS)NcSsb|jdd�|jdd�|jdd�trL|jdd�|jdd�|jdd�tdd�|j�D��S)	NZPSUTIL_TESTINGZPLAT�HOMEZ__CF_USER_TEXT_ENCODINGZVERSIONER_PYTHON_PREFER_32_BITZVERSIONER_PYTHON_VERSIONcSs$g|]\}}|jd�|jd�f�qS)z
)�rstrip)r��k�vr1r1r2r��sz@TestProcess.test_environ.<locals>.clean_dict.<locals>.<listcomp>)�popr�dictrY)rgr1r1r2�
clean_dictzsz,TestProcess.test_environ.<locals>.clean_dict)ZmaxDiffr6r7rr:�copyr8)r0r�r>Zd1Zd2r1r1r2�test_environxszTestProcess.test_environcCs�tjd�}t}t||d�|jt|�t|gtjtjd�}t	j
|j�}t|j�|j
|j��|j|jj�d�|j|j�ddd��|j�|j|jd�dS)	Na�
            #include <unistd.h>
            #include <fcntl.h>
            char * const argv[] = {"cat", 0};
            char * const envp[] = {"A=1", "X", "C=3", 0};
            int main(void) {
                /* Close stderr on exec so parent can wait for the execve to
                 * finish. */
                if (fcntl(2, F_SETFD, FD_CLOEXEC) != 0)
                    return 0;
                return execve("/bin/cat", argv, envp);
            }
            )Zc_code)r��stderr�r*�3)�A�Cr)�textwrap�dedentr)rr�r$r�
subprocess�PIPEr6r7r9r-rvrZr8r�r�r�communicate�
returncode)r0r^r�r?r>r1r1r2�test_weird_environ�s

zTestProcess.test_weird_environN)d�__name__�
__module__�__qualname__�__doc__r3r4r@rHrKrVr_rbrernrrr}r�r,ZskipIfrr�r�rr+r�rr'rr�rr
rr.r�r�rr�r�r�r�r�r�r�rr�r"r&rr�r�r�rr�r�r�r�r�r�r�r	rr�rrrrrrrrr)r+r0rr
r:r?rCrrrErLrNrOrQrRr\r_r`rhrkrnrqrxryrzr|rr�r�r1r1r1r2r/Ks�

 /	
		+ ."

)



)*

%


.
@I

'r/c@sReZdZdZeed�r&ej�Zej�Z	dd�Z
dd�Zdd�Zd	d
�Z
dd�Zd
S)�LimitedUserTestCasez�Repeat the previous tests by using a limited user.
        Executed only on UNIX and only if the user who run the test script
        is root.
        rcs\tj|f|�|�xDdd�t|�D�D].}t||���fdd�}t||tj||��q&WdS)NcSsg|]}|jd�r|�qS)Ztest)r�)r�rmr1r1r2r��sz0LimitedUserTestCase.__init__.<locals>.<listcomp>cs&y
��Wntjk
r YnXdS)N)r6rS)r0)rpr1r2�test_�s
z+LimitedUserTestCase.__init__.<locals>.test_)r/�__init__r�r{�setattr�types�
MethodType)r0rrrs�attrr�r1)rpr2r��s

zLimitedUserTestCase.__init__cCs*tt�tj|�tjd�tjd�dS)Ni�)r$r)r/r3r:�setegid�seteuid)r0r1r1r2r3�s

zLimitedUserTestCase.setUpcCs&tj|j�tj|j�tj|�dS)N)r:r��PROCESS_UIDr��PROCESS_GIDr/r4)r0r1r1r2r4�szLimitedUserTestCase.tearDowncCs8ytj�jd�Wntjk
r(YnX|jd�dS)NrXzexception not raisedrY)r6r7rrSr�)r0r1r1r2r�s
zLimitedUserTestCase.test_nicecCsdS)Nr1)r0r1r1r2rx�sz'LimitedUserTestCase.test_zombie_processN)r�r�r�r�rr:rr�r	r�r�r3r4rrxr1r1r1r2r��s
r�c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�	TestPopenzTests for psutil.Popen class.cCs
t�dS)N)r!)r0r1r1r2r4�szTestPopen.tearDowncCsjtddg}tj|tjtjd��B}|j�|j�|j|jt	|��|j
tt|d�|j
�WdQRXdS)Nz-czimport time; time.sleep(60);)r�r�r�)r r6�Popenr�r�r\rsr�rvr�r<r=r{rI)r0r�procr1r1r2�	test_misc�s

zTestPopen.test_miscc
Csftjtdgtjtjtjd��}|j�WdQRX|jjs<t�|j	jsHt�|j
jsTt�|j|jd�dS)Nz-V)r�r�r�r)
r6r�r r�r�r�r��closedrpr�r�r8r�)r0r�r1r1r2�test_ctx_manager�s
zTestPopen.test_ctx_managercCs�tddg}tj|tjtjd���}|j�|j�|jtj|j�|jtj|j	�|jtj|j
tj�t
r�tjdkr�|jtj|j
tj�|jtj|j
tj�WdQRXdS)Nz-czimport time; time.sleep(60);)r�r�rcr�)rcr�)r r6r�r�r�rIrBr<rRrArNrErJr
r�r�ZCTRL_C_EVENTZCTRL_BREAK_EVENT)r0rr�r1r1r2�test_kill_terminates

zTestPopen.test_kill_terminateN)r�r�r�r�r4r�r�r�r1r1r1r2r��s
r��__main__)Er�rUrQrr,r:rErAr�r�r�r�rdr�r6rrrrrrrr	r
Zpsutil._compatrrZpsutil.testsr
rrrrrrrrrrrrrrrrrrr r!r"r#r$r%r&r'r(r)r*r+r,r-r.ZTestCaser/rr�r�r��__file__r1r1r1r2�<module>s�q53


?>