HEX
Server: Apache
System: Linux zacp120.webway.host 4.18.0-553.50.1.lve.el8.x86_64 #1 SMP Thu Apr 17 19:10:24 UTC 2025 x86_64
User: govancoz (1003)
PHP: 8.3.26
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: //opt/osm/venv/lib64/python3.10/site-packages/psutil/tests/__pycache__/test_process.cpython-310.pyc
o

=i��@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlmZddlZddlmZddlmZddlmZddlmZddlmZdd	lmZdd
lmZddlmZddlmZdd
lmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%ddlm&Z&ddlm'Z'ddlm(Z(ddlm)Z)ddlm*Z*ddlm+Z+ddlm,Z,ddlm-Z-ddlm.Z.ddlm/Z/ddlm0Z0dd lm1Z1dd!lm2Z2dd"lm3Z3dd#lm4Z4dd$lm5Z5dd%lm6Z6dd&lm7Z7dd'lm8Z8dd(lm9Z9dd)lm:Z:dd*lm;Z;Gd+d,�d,e.�Z<Gd-d.�d.e.�Z=dS)/�Tests for psutil.Process class.�N)�mock)�AIX)�BSD)�LINUX)�MACOS)�NETBSD)�OPENBSD)�OSX)�POSIX)�WINDOWS)�	open_text)�
CI_TESTING)�GITHUB_ACTIONS)�GLOBAL_TIMEOUT)�HAS_CPU_AFFINITY)�HAS_ENVIRON)�
HAS_IONICE)�HAS_MEMORY_MAPS)�HAS_PROC_CPU_NUM)�HAS_PROC_IO_COUNTERS)�
HAS_RLIMIT)�HAS_THREADS)�MACOS_11PLUS)�PYPY)�
PYTHON_EXE)�PYTHON_EXE_ENV)�PsutilTestCase)�
ThreadTask)�
call_until)�copyload_shared_lib)�create_c_exe)�
create_py_exe)�process_namespace)�pytest��
reap_children)�retry_on_failure)�sh)�skip_on_access_denied)�skip_on_not_implemented)�wait_for_pidc@sLeZdZdZdd�Zdd�Zdd�Zdd	�Zej	j
ed
d�dd
��Zdd�Z
ej	j
edd�dd��Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zej	j
edd�d d!��Zd"d#�Zej	j
ed$d�d%d&��Zej	j
edd�eed'�d(d)���Zej	j
edd�ej	j
ed*d�d+d,���Zej	j
edd�ej	j
e d-d�d.d/���Z!ej	j
e"dd�d0d1��Z#ej	j
e"dd�d2d3��Z$ej	j
e"dd�d4d5��Z%ej	j
e"dd�d6d7��Z&ej	j
e"dd�d8d9��Z'ej	j(d:d;�d<d=��Z)ej	j
e d>d�d?d@��Z*ej	j
e+dd�dAdB��Z,e-�e.e/d'�ej	j
e+dd�dCdD����Z0e-�dEdF��Z1dGdH�Z2ej	j
e3dd�dIdJ��Z4ej	j
e3dd�dKdL��Z5dMdN�Z6dOdP�Z7dQdR�Z8dSdT�Z9dUdV�Z:dWdX�Z;e-�dYdZ��Z<ej	j
ed$d�d[d\��Z=ej	j
ed$d�d]d^��Z>d_d`�Z?dadb�Z@dcdd�ZAdedf�ZBdgdh�ZCej	j
eDdd�didj��ZEej	j
eDdd�dkdl��ZFej	j
eDdd�dmdn��ZGej	j
eHdod�dpdq��ZIej	j
eHdod�drds��ZJej	j
ed$d�ej	j(d:d;�dtdu���ZKeed'�ej	j
eL�p�edvd�dwdx���ZMdydz�ZNd{d|�ZOd}d~�ZPdd��ZQe-�d�d���ZRd�d��ZSd�d��ZTd�d��ZUd�d��ZVd�d��ZWd�d��ZXd�d��ZYd�d��ZZd�d��Z[d�d��Z\d�d��Z]d�d��Z^ej	j
ed$d�d�d���Z_ej	j
ed$d�d�d���Z`ej	j
ed$d�d�d���Zad�d��Zbd�d��Zcej	j
eddd�d�d���Zeej	j
eddd�ej	j
ed$d�ej	j
efd�d�ej	j
ed�d�d�d������Zgd�S)��TestProcessrcCsPt��}|jt��ks
J�t�t��d|_Wd�dS1s!wYdS)N�!)�psutil�Process�pid�os�getpidr$�raises�AttributeError��self�p�r8�G/opt/osm/venv/lib/python3.10/site-packages/psutil/tests/test_process.py�test_pidKs
"�zTestProcess.test_pidcCsJ|��}|��|��}tr|tjksJ�n|tjksJ�|�|�dS�N)�spawn_psproc�kill�waitr�signal�SIGTERM�SIGKILL�assert_proc_gone�r6r7�coder8r8r9�	test_killQ�zTestProcess.test_killcCsJ|��}|��|��}tr|tjksJ�n|tjksJ�|�|�dSr;)r<�	terminater>rr?r@rBrCr8r8r9�test_terminate[rFzTestProcess.test_terminatecCsXtrtjntj}|��}|�|�|��}tr||ksJ�n||ks%J�|�|�dSr;)	rr?rAr@r<�send_signalr>rrB)r6�sigr7rDr8r8r9�test_send_signales
zTestProcess.test_send_signalz	not POSIX��reasonc	Cs�tj}|��}tjdtd��#t�tj	��
|�
|�Wd�n1s%wYWd�n1s4wY|��}tjdtd��,t�tj��
|�
|�Wd�n1s[wYWd�dSWd�dS1sswYdS)Nzpsutil.os.kill��side_effect)
r?r@r<r�patch�ProcessLookupErrorr$r3r.�
NoSuchProcessrI�PermissionError�AccessDenied)r6rJr7r8r8r9�test_send_signal_mockedps �����"�z#TestProcess.test_send_signal_mockedcCs�tddg}|�|�}|��}|dksJ�|�|�tddg}|j|tjd�}|��}|dks0J�|�|�tddg}|�|�}|��}|dksIJ�|�|�tdd	g}|�|�}|��}|dksbJ�|�|�dS)
N�-c�passrz1 / 0)�stderr�zimport sys; sys.exit(5);�zimport os; os._exit(5);)rr<r>rB�
subprocess�PIPE)r6�cmdr7rDr8r8r9�test_wait_exited}s(









zTestProcess.test_wait_exitedzfails on NETBSDcCs||��}trf|�tj�t�tj��|j	dd�Wd�n1s#wY|�tj
�t�tj��|j	dd�Wd�n1sEwY|�tj�|�	�tjksZJ�|�	�tjksdJ�dS|��t�tj��|j	dd�Wd�n1s�wY|�
�t�tj��|j	dd�Wd�n1s�wY|��|�	�tjks�J�|�	�tjks�J�dS)N���MbP?)�timeout)r<rrIr?�SIGSTOPr$r3r.�TimeoutExpiredr>�SIGCONTr@�suspend�resumerGr5r8r8r9�test_wait_stopped�s0����zTestProcess.test_wait_stoppedcCs�|��\}}t�tj��
|�d�Wd�n1swYt�tj��
|�d�Wd�n1s7wY|��|��|��}|��}tr^|tj	ksVJ�|dus\J�dS|tj	kseJ�|tj	kslJ�dS)N�{�G�z�?)
�spawn_children_pairr$r3r.rbr>rGrr?r@)r6�child�
grandchildZ	child_retZgrandchild_retr8r8r9�test_wait_non_children�s ��z"TestProcess.test_wait_non_childrencCs�|��}|��t�tj��
|�d�Wd�n1swYt�tj��
|�d�Wd�n1s9wYt�t��|�d�Wd�dS1sTwYdS)Nrgr���)r<�namer$r3r.rbr>�
ValueErrorr5r8r8r9�test_wait_timeout�s��"�zTestProcess.test_wait_timeoutcCs�|��}t�tj��
|�d�Wd�n1swY|��t��t}t��|krGz|�d�}Wntjy@Ynwt��|ks/t�	d�St
rW|tjksVJ�n|tj
ks^J�|�|�dS)Nrr`)r<r$r3r.rbr>r=�timer�failrr?rAr@rB)r6r7Zstop_atrDr8r8r9�test_wait_timeout_nonblocking�s&�
��
z)TestProcess.test_wait_timeout_nonblockingcCs�t��}|jdd�|jdd�td�D]}|jdd�}t|t�s#J�|dks)J�qt�t��|jdd�Wd�dS1sAwYdS)Nr_)�interval�d�rl)	r.r/�cpu_percent�range�
isinstance�floatr$r3rn)r6r7�_�percentr8r8r9�test_cpu_percent�s"�zTestProcess.test_cpu_percentcCsJtjddd��}t����|jsJ�Wd�dS1swYdS)Nzpsutil.cpu_count)�return_value)rrPr.r/rv�called)r6�mr8r8r9�test_cpu_percent_numcpus_none�s"�z)TestProcess.test_cpu_percent_numcpus_nonec	Cs�t����}|jdksJ|��|jdksJ|��|jdks!J|��|jdks*J|��tr5|jdks5J|��|j	D]}t
�dt
�t
||���q8dS)Nruz%H:%M:%S)r.r/�	cpu_times�user�system�
children_user�children_systemrZiowait�_fieldsrp�strftime�	localtime�getattr)r6�timesrmr8r8r9�test_cpu_times�s
�zTestProcess.test_cpu_timesz
not supportedcCsPt��}|��}|dksJ�t��dkr|dksJ�|��tt���vs&J�dS�NrrY)r.r/Zcpu_num�	cpu_countrw)r6r7�numr8r8r9�test_cpu_numszTestProcess.test_cpu_numcCsB|��}t��}t|��|�dksJ�t�dt�|����dS)N�z%Y %m %d %H:%M:%S)r<rp�abs�create_timer�r�)r6r7�nowr8r8r9�test_create_time
szTestProcess.test_create_timez
POSIX onlycCsZt����}|dur)z
tj�td��}Wnty"t�	d�YSw||ks+J�dSdS)N�ttyzcan't rely on `tty` CLI)
r.r/�terminalr1�path�realpathr(�RuntimeErrorr$�skip)r6r�r�r8r8r9�
test_terminals��zTestProcess.test_terminal)Zonly_ifcCs�t��}|��}ttd��}|��Wd�n1swY|��}tsLtsL|j|jks1J�|j	|j	ks9J�t
rK|j|jksCJ�|j|jksKJ�n|j
|j
ksTJ�|j|jks\J�|��}t|��d��}|�tddd��Wd�n1s|wY|��}|j	|j	ks�J�|j|jks�J�|j|jks�J�|j
|j
ks�J�t
r�|j|jks�J�|j|jks�J�tt|��D]}tr�|dkr�q�||dks�J�||dks�J�q�dS)N�rb�wb�xi@B�asciir�r)r.r/Zio_counters�openr�readrrZ
read_countZwrite_countrZ
read_charsZwrite_chars�
read_bytes�write_bytes�
get_testfn�write�bytesrw�len)r6r7Zio1�fZio2�ir8r8r9�test_io_counters!sB
����zTestProcess.test_io_countersz
linux onlycsH�fdd�}t���ts���dtjksJ�tjdksJ�tjdks%J�tjdks,J�tjdks3J����}|�||���tj�t	����tjdfksPJ�t
�t���jtjdd�Wd�n1shwY��tj�t	����tjdfks�J��jtjdd�t	����tjdfks�J�t
�t���jtjd	d�Wd�n1s�wYz
�jtjdd�Wn
tj
y�Ynwt
jtd
d����tjd�Wd�n1s�wYt
jtd
d����tjd�Wd�n1s�wYt
jtdd���jdd�Wd�dS1�swYdS)
Ncs&|\}}|tjkrd}��||�dS�Nr)r.�IOPRIO_CLASS_NONE�ionice)�initZioclass�value�r7r8r9�cleanupLs
z.TestProcess.test_ionice_linux.<locals>.cleanuprrYr����r��zioclass accepts no value��matchz$'ioclass' argument must be specified)r.r/rr�r�ZIOPRIO_CLASS_RTZIOPRIO_CLASS_BEZIOPRIO_CLASS_IDLE�
addCleanup�tupler$r3rnrT)r6r�r�r8r�r9�test_ionice_linuxIsL������$�zTestProcess.test_ionice_linuxz!not supported on this win versioncCs*t��}ts|��tjksJ�|��}|�|j|�|�tj�|��tjks)J�|�tj�|��tjks8J�z|�tj�Wn
tj	yJYn
w|��tjksTJ�t
jtdd��|jtjdd�Wd�n1snwYt
jt
dd��|�tjd�Wd�dS1s�wYdS)Nz&value argument not accepted on Windowsr�rYr�zis not a valid priority)r.r/rr�Z
IOPRIO_NORMALr�ZIOPRIO_VERYLOWZ
IOPRIO_LOWZIOPRIO_HIGHrTr$r3�	TypeErrorrn)r6r7r�r8r8r9�test_ionice_winvs.���"�zTestProcess.test_ionice_wincCs�ddl}t�t���}dd�tt�D�}|sJ|��|D]I}tt|�}|dks)J�|t|�vrH|t||�ks8J�tr;q|�|�|�	|�ksGJ�q|�|�}t
|�dksUJ�|ddks]J�|ddkseJ�qdS)NrcSsg|]	}|�d�r|�qS)ZRLIMIT)�
startswith��.0r�r8r8r9�
<listcomp>��z/TestProcess.test_rlimit_get.<locals>.<listcomp>r�rlrY)�resourcer.r/r1r2�dirr�r�rlimitZ	getrlimitr�)r6r�r7�namesrmr��retr8r8r9�test_rlimit_get�s"

�zTestProcess.test_rlimit_getcCs�|��}|�tjd�|�tj�dksJ�tr7tjtdd��tj�	d��d�Wd�n1s2wYt�t��|�tjd�Wd�dS1sOwYdS)N)rZrZzcan't use prlimitr�r)rZrZrZ)
r<r�r.Z
RLIMIT_NOFILErr$r3rnZ_psplatformr/r5r8r8r9�test_rlimit_set�s�"�zTestProcess.test_rlimit_setcCs4t��}|��}|�tj�\}}zs|�tjd|f�t|d��
}|�d�Wd�n1s/wYt�t	��"}t|d��
}|�d�Wd�n1s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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)
r.r/r�r��RLIMIT_FSIZEr�r�r$r3�OSErrorr��errnoZEFBIG)r6r7�testfn�soft�hardr��excr8r8r9�test_rlimit�s&�����zTestProcess.test_rlimitcCs�t��}|�tj�\}}zG|�tjd|f�|�tjtj|f�t|��d��
}|�d�Wd�n1s7wYW|�tj||f�|�tj�||fksRJ�dS|�tj||f�|�tj�||fksiJ�w)Nr�r�sXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)r.r/r�r��
RLIM_INFINITYr�r�r�)r6r7r�r�r�r8r8r9�test_rlimit_infinity�s���z TestProcess.test_rlimit_infinitycCs<t��}|�tj�\}}|tjksJ�|�tj||f�dSr;)r.r/r�r�r�)r6r7r�r�r8r8r9�test_rlimit_infinity_value�sz&TestProcess.test_rlimit_infinity_value�serial�rmcCs�t��}trz|��}Wntjyt�d�YSw|��}t��|��}||dks0J�Wd�dS1s;wYdS)N�$on OpenBSD this requires root accessrY)r.r/r	�num_threadsrTr$r�r)r6r7�step1�step2r8r8r9�test_num_threads�s�"�zTestProcess.test_num_threadszWINDOWS onlycCst��}|��dksJ�dSr�)r.r/Znum_handlesr5r8r8r9�test_num_handles�szTestProcess.test_num_handlescCs�t��}trz|��}Wntjyt�d�YSw|��}t��8|��}t|�t|�dks4J�|d}|j	|dksAJ�|j
|dksJJ�|j|dksSJ�Wd�dS1s^wYdS)Nr�rYrr�)r.r/r	�threadsrTr$r�rr��id�	user_time�system_time)r6r7r�r�Zathreadr8r8r9�test_threads�s �"�zTestProcess.test_threadscCs�|��}trz|��Wntjyt�d�YSwt|��j	t
dd�|��D���dks2J�t|��jt
dd�|��D���dksHJ�dS)Nr�cs��|]}|jVqdSr;)r�r�r8r8r9�	<genexpr>��z-TestProcess.test_threads_2.<locals>.<genexpr>g�������?csr�r;)r�r�r8r8r9r�"r�)r<r	r�r.rTr$r�r�r�r��sumr�r5r8r8r9�test_threads_2s�"�"�zTestProcess.test_threads_2cCs�t��}|��dd�\}}|��}|dksJ�|dksJ�dgd}|��dd�\}}|��}||ks7J�||ks=J�||ksCJ�~trZ|��}	|	j|	jksRJ�|	j|	jksZJ�|��}	|	j	D]}
t
|	|
�dkslJ�qadS)Nr�ri`�)r.r/Zmemory_info�memory_percentrZrssZwset�vmsZpagefiler�r�)r6r7Zrss1Zvms1Zpercent1ZmemarrZrss2Zvms2Zpercent2�memrmr8r8r9�test_memory_info&s(

�zTestProcess.test_memory_infocCs�t��}t��j}|��}|jD]}t||�}|dksJ�|dkr#ts%tr&q||ks,J�qts3t	s3t
r:|jdks:J�trJ|jdksCJ�|j
dksLJ�dSdS)Nrr�)r.r/Zvirtual_memory�totalZmemory_full_infor�r�r
rrr�ussZpssZswap)r6r7r�r�rmr�r8r8r9�test_memory_full_infoEs 


�z!TestProcess.test_memory_full_infoc
Cs�t��}|��}t|�tt|��ksJ�|jdd�}|D]�}|j�d�r%qtr-|jdkr-qtj�	|j�s9J|j��t
r~ztj�|j�sOtj�|j�sOJ|j��Wqt
y}tsZ�td��}|��}Wd�n1smwY|j�d�|vr{�Yqwdtj�|j�vr�zt�|j�}Wn	ty�Yqwt�|j�s�J|j��q|D],}|jD]&}t||�}	|dkr�q�|d	vr�|	s�J|	��q�t|	t�s�J�|	d
ks�J|	��q�q�dS)NF)�grouped�[Zpvclockz/proc/self/smapsz
 (deleted)Z64r�>Zperms�addrr)r.r/�memory_mapsr��setr�r�rr1�isabsr�exists�islink�AssertionErrorrr
r��basename�stat�FileNotFoundError�S_ISREG�st_moder�r�rx�int)
r6r7�mapsZext_maps�ntr��data�st�fnamer�r8r8r9�test_memory_mapsUs^���

�����

��zTestProcess.test_memory_mapscsbt��}t�� }dd���fdd�|��D�}�|�|vsJ�Wd�dS1s*wYdS)NcSstj�tj�|��Sr;)r1r�r��normcaser�r8r8r9�normpath�sz8TestProcess.test_memory_maps_lists_lib.<locals>.normpathcsg|]}�|j��qSr8)r�r��rr8r9r��sz:TestProcess.test_memory_maps_lists_lib.<locals>.<listcomp>)r.r/r r�)r6r7r�Zlibpathsr8rr9�test_memory_maps_lists_lib�s"�z&TestProcess.test_memory_maps_lists_libcCsft��}|��t�t��|jdd�Wd�n1swYts)ts)tr1|jdd�dSdS)Nz?!?)Zmemtyper�)	r.r/r�r$r3rnrrrr5r8r8r9�test_memory_percent�s��zTestProcess.test_memory_percentcCsL|��}|��s
J�|��sJ�|��|��|��rJ�|��r$J�dSr;)r<�
is_runningr=r>r5r8r8r9�test_is_running�szTestProcess.test_is_runningcCs�|��}|��}z|tksJ�WnItyYtr0t|�tt�kr0tjj}||�|t�ks/J�n't	j
d�dt	j
d��}z|�|d�t�|d�ksLJ�Wn	tyVYnwYnwt|ddg�}|dksgJ�dS)Nr�.rY�rVzimport os; print('hey')Zhey)
r<�exerr�rr�r1r�r�sys�version_info�replacer()r6r7rr�ver�outr8r8r9�test_exe�s$ ���zTestProcess.test_execCs�tddg}|�|�}tr|��gkrt�d�Stststr)|��dtks'J�dStrOt	rO|��d}|tkrOd�
|��dd��d�
|dd��ksMJ�dSd�
|���d�
|�ks]J�dS)NrV�2import time; [time.sleep(0.1) for x in range(100)]�OPENBSD: returned EBUSYr� rY)rr<r�cmdliner$r�r	rrr�join)r6rr7�pyexer8r8r9�test_cmdline�s�

, zTestProcess.test_cmdlinecCs�tg}|�dgd�|�ddg�|�|�}|dd�}tr>z|��dd�|ks,J�WdStjy=t�d�YSw|��dd�}t	rQ|gkrQt�d�S||ksWJ�dS)Nz-v�2rVrrYz#OPENBSD: process turned into zombier)
r�extendr<r	rr.�
ZombieProcessr$r�r)r6rr7r�r8r8r9�test_long_cmdline�s"�
�
zTestProcess.test_long_cmdlinecCs^|��}|����}|�d�r|dd�}tj�tj�tj	����}|�
|�s-J||f��dS)N�trl)r<rm�lower�endswithr1r�r�r�r
�
executabler�)r6r7rmrr8r8r9�	test_name�s
zTestProcess.test_namecCs�t|jtjdd��}|ddg}|�|�}trGz|��tj�	|�ks$J�WdSt
yF|��tj
krBtj�	|��|���sAJ�n�YdSw|��tj�	|�ksSJ�dS)Nr�)�suffixrVr)r"r��string�digitsr<r	rmr1r�r�r��statusr.�
STATUS_ZOMBIEr�)r6rrr7r8r8r9�test_long_name�s �
��zTestProcess.test_long_namecC�\t��}|��\}}}|t��ksJ�|t��ksJ�ttd�r*t��|��ks,J�dSdS�N�	getresuid)r.r/�uidsr1�getuid�geteuid�hasattrr+�r6r7�realZ	effectiveZ_savedr8r8r9�	test_uids�
�zTestProcess.test_uidscCr)r*)r.r/�gidsr1�getgid�getegidr/�	getresgidr0r8r8r9�	test_gids#r3zTestProcess.test_gidsc
s��fdd�}t���t�t��
��d�Wd�n1swY���}|�||�tr�d}tjtj	tj
tjtjtj
fD]O}|j|d��?z��|�Wn
tjyZYn&w���}|tjtjtj
hvrz||ksq|dury|}||ksyJ�n||ks�J�Wd�n1s�wYq@dSzJttd�r�t�tjt������ks�J���d����dks�J�ttd�r�t�tjt������ks�J�ts׈�d����dks�J�WdSWdStjy�YdSw)Ncs(z��|�WdStjyYdSwr;)�nicer.rT)r�r�r8r9r�1s
�z&TestProcess.test_nice.<locals>.cleanup�str)�prio�getpriorityrYr)r.r/r$r3r�r9r�rZIDLE_PRIORITY_CLASSZBELOW_NORMAL_PRIORITY_CLASSZNORMAL_PRIORITY_CLASSZABOVE_NORMAL_PRIORITY_CLASSZHIGH_PRIORITY_CLASSZREALTIME_PRIORITY_CLASS�subTestrTr/r1r<�PRIO_PROCESSr2r)r6r�r�Zhighest_prior;Znew_prior8r�r9�	test_nice0sp���������
�

�
��zTestProcess.test_nicecCst��}|��tjks
J�dSr;)r.r/r&ZSTATUS_RUNNINGr5r8r8r9�test_statusrszTestProcess.test_statuscCs�|��}|��}tr7|�d�\}}t��}|�d�rt�d�S||ks%J�dt	j
vr3|t	j
dks5J�dSdS|t��ks?J�dS)N�\�$zrunning as service accountZ
USERDOMAIN)r<�usernamer�split�getpass�getuserr r$r�r1�environ)r6r7rC�domainZgetpass_userr8r8r9�
test_usernamevs


�zTestProcess.test_usernamecCs |��}|��t��ksJ�dSr;)r<�cwdr1�getcwdr5r8r8r9�test_cwd�szTestProcess.test_cwdcs(tddg}|�|��t�fdd��dS)NrVzFimport os, time; os.chdir('..'); [time.sleep(0.1) for x in range(100)]cs���tj�t���kSr;)rJr1r��dirnamerKr8r�r8r9�<lambda>�sz(TestProcess.test_cwd_2.<locals>.<lambda>)rr<r)r6r]r8r�r9�
test_cwd_2�s�
zTestProcess.test_cwd_2cCs�t��}|��}|sJ|��|�|j|�ttd�r&|tt�|j��ks&J�t	|�t	t
|��ks2J�ttt	tjdd����}|D]5}|�|g�|��|gksQJ�ttd�rd|��tt�|j��ksdJ�t|d�ru|��d|�
�ksuJ�q@|�g�tr�|��|j��ks�J�n|��|ks�J�ttd�r�|��tt�|j��ks�J�t�t��
|�d�Wd�n1s�wY|�|�|�t
|��|�t|��dS)N�sched_getaffinityT�Zpercpu�num_cpurrY)r.r/�cpu_affinityr�r/r1�listrPr0r�r�rwrvrRr�_procZ_get_eligible_cpusr$r3r�r�)r6r7�initialZall_cpus�nr8r8r9�test_cpu_affinity�s8


�

�
zTestProcess.test_cpu_affinitycCs|��}ttjdd��dg}t�t��
|�|�Wd�n1s$wYt�t��|�tdd��Wd�n1sAwYt�t	tf��|�ddg�Wd�n1s_wYt�t��|�ddg�Wd�dS1s|wYdS)	NTrQ�
i'i�*r�1rl)
r<r�r.r�r$r3rnrSrwr�)r6r7Zinvalid_cpur8r8r9�test_cpu_affinity_errs�s���"�z"TestProcess.test_cpu_affinity_errscCs�t��}|��}|sJ|��|�|j|�t|�dkr!|dd�}g}tt|�d�D]}|�dd�t�||�D��q+|D]}|�|�t	|���t	|�ksQJ�q>dS)N�rYcss�|]	}|rt|�VqdSr;)rT)r�Zsubsetr8r8r9r��s��
�zATestProcess.test_cpu_affinity_all_combinations.<locals>.<genexpr>)
r.r/rSr�r�rwr�	itertools�combinations�sorted)r6r7rVZcombosr�Zcombor8r8r9�"test_cpu_affinity_all_combinations�s


�
�z.TestProcess.test_cpu_affinity_all_combinationsz
broken on BSDcsjt���|��}����|�vsJ�t|d��B}|�d�|��t��fdd������dd��D�}tj	�
|�|vs?J�trR�D]}|j	|krQ|jdksQJ�qCWd�n1s\wY�D]
}tj	�
|j	�spJ|��qcd|�d	�}|�td
|g��td�D]}dd����D�}||vr�nt�d
�q�tj	�
|�|vs�J�|D]}tj	�
|�s�J|��q�dS)Nr�sxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcst����t��kSr;)r��
open_filesr8��filesr7r8r9rN�sz-TestProcess.test_open_files.<locals>.<lambda>cS�g|]	}tj�|j��qSr8�r1r�rr�r8r8r9r��r�z/TestProcess.test_open_files.<locals>.<listcomp>r�zimport time; f = open(r'z/', 'r'); [time.sleep(0.1) for x in range(100)];rVrtcSrdr8rer�r8r8r9r��r�rg)r.r/r�rar�r��flushrr1r�rr�position�isfiler<rrwrp�sleep)r6r�r��	filenames�filerr�r8rbr9�test_open_files�s>

��
��zTestProcess.test_open_filescCst��}tjj}|��}t|d��t}|��D]}||j�||j�ks)|j	|�
�kr+nqt�d|�����Wd�S||j�||j�ksIJ�t
rS|j	dksRJ�n	|j	|�
�ks\J�|��d}|d|jkskJ�|d|j	kstJ�|j|��vs}J�Wd�dS1s�wYdS)N�wzno file found; files=rlrrY)r.r/r1r�rr�r�rarm�fd�filenor$rqr)r6r7rr��fileobjrkZntupler8r8r9�test_open_files_2	s(��	"�zTestProcess.test_open_files_2c	Cs�t��}|��}|��}t|d��0|��|dksJ�t���|��|dks+J�Wd�n1s5wYWd�n1sDwY|��|ksQJ�dS)NrmrYr�)r.r/r�Znum_fdsr��socket)r6r7r��startr8r8r9�test_num_fds$s
���zTestProcess.test_num_fdsz not reliable on OPENBSD & NETBSDcCsPt��}t|���}td�D]}t�d�t|���}||kr"dSqt�d�S)Nr�g�������?z2num ctx switches still the same after 2 iterations)	r.r/r�Znum_ctx_switchesrwrprir$rq)r6r7�beforerz�afterr8r8r9�test_num_ctx_switches0s
��z!TestProcess.test_num_ctx_switchescCsFt��}ttd�r|��t��ksJ�|��}|��t��ks!J�dS)N�getppid)r.r/r/r1�ppidrxr<r2r5r8r8r9�	test_ppid@s

zTestProcess.test_ppidcCsD|��}|��jt��ksJ�t��d}t�|���dus J�dSr�)r<�parentr0r1r2r.�pidsr/)r6r7Z
lowest_pidr8r8r9�test_parentGszTestProcess.test_parentcCs:|��}|��|js
J�d|_|��jt��ksJ�dS�NrY)r<r��_create_timer{r0r1r2r5r8r8r9�test_parent_mocked_ctimeNs

z$TestProcess.test_parent_mocked_ctimecCs8t��}|��\}}|��|ksJ�|��|ksJ�dSr;)r.r/rhr{�r6r{rirjr8r8r9�test_parent_multiZszTestProcess.test_parent_multicCs`t��}|��s
J�|��\}}|��d|ksJ�|��d|ks$J�|��d|ks.J�dSr�)r.r/�parentsrhr�r8r8r9�test_parents`szTestProcess.test_parentscCs�t��}|��r
J�|jdd�rJ�|jdd�}|��}|jdd�}||fD]}t|�dks0J�|dj|jks:J�|d��|jksEJ�q&dS)NT��	recursiver��
creationflagsrY)r.r/�childrenr<r�r0ry�r6r{riZ	children1Z	children2r�r8r8r9�
test_childrenis�zTestProcess.test_childrencCs�t��}|��|js
J�|jd7_|��rJ�|jdd�r"J�|jdd�}|��}|jdd�}||fD]}t|�dks@J�|dj|jksJJ�|d��|jksUJ�q6dS)Ni��Tr�rr�rY)	r.r/r�rr�r<r�r0ryr�r8r8r9�test_children_mocked_ctimexs
�z&TestProcess.test_children_mocked_ctimecCsbt��}|��\}}|��|gksJ�|jdd�||gksJ�|��|��|jdd�r/J�dS)NTr�)r.r/rhr�rGr>r�r8r8r9�test_children_recursive�sz#TestProcess.test_children_recursivec	Cs�t�t�}t��D]}z||��d7<Wq	tjy!Yq	wt|��dd�d�d}t	r9|dkr9t
�d�St�|�}z|j
dd�}WntjyQYdSwt|�tt|��ks^J�dS)	NrYcSs|dSr~r8)r�r8r8r9rN�sz6TestProcess.test_children_duplicates.<locals>.<lambda>)�keyrzPID 0Tr�)�collections�defaultdictr�r.�process_iterry�Error�max�itemsrr$r�r/r�rTr�r�)r6�tabler7r0�cr8r8r9�test_children_duplicates�s"
�

�z$TestProcess.test_children_duplicatescCs|t��}|��\}}|jdd�}t|�dksJ�|d|ks J�|d|ks(J�|��}|d|ks4J�|d|ks<J�dS)NTr�r�rrY)r.r/rhr�r�r�)r6r{rirjr�r�r8r8r9�test_parents_and_children�sz%TestProcess.test_parents_and_childrencCsX|��}|��td�D]}|��tjkrnt�d�q|��|��tjks*J�dS)Nrtrg)	r<rdrwr&r.ZSTATUS_STOPPEDrprire)r6r7rzr8r8r9�test_suspend_resume�szTestProcess.test_suspend_resumecCsnt�t��
t�d�Wd�n1swYt�t��t�d�Wd�dS1s0wYdS)NrZrl)r$r3r�r.r/rn)r6r8r8r9�test_invalid_pid�s�"�zTestProcess.test_invalid_pidc	Cs�t��}|jddgd�}t|���ddgksJ�t�tt����}|jdgdd�}t|dt�s8|ddks8J�t	j
ddtjd	��|jd
gdd�d
diksPJ�Wd�n1sZwYt	j
ddt�|j
d�d	��%t�tj��|jd
gd�Wd�n1s�wYWd�n1s�wYt	j
ddt�|j
d�d	��|jd
gdd�d
diks�J�Wd�n1s�wYt	j
ddtd	��2|��}d
t|���vs�J�t�t��|jd
gd�Wd�n1s�wYWd�n	1�swYt�t��
|�d�Wd�n	1�swYt�t��|�dg�Wd�n	1�s9wYt�t��|�ddg�Wd�dS1�sWwYdS)
Nrrm)�attrsZnet_connections�foo)r�Zad_valuezpsutil.Process.niceT)�createrOr9rY�bar)r.r/�as_dictr_�keys�minr|rxrTrrPrTrRr0r$r3r�NotImplementedErrorr�rn)r6r7�dr8r8r9�test_as_dict�s`������	��	����
��$�zTestProcess.test_as_dictc	Cs�t��}t�d��+}|���|��|��Wd�n1s!wY|jdks-J�Wd�n1s7wYt�d��}|��|��Wd�n1sTwY|jdks`J�dS)N�$psutil._psplatform.Process.cpu_timesrYr��r.r/rrP�oneshotr�Z
call_count�r6r7rr8r8r9�test_oneshots

��
�zTestProcess.test_oneshotcCs4t��}t�d��c}t�d��N}|���,|��|��|���|��|��Wd�n1s4wYWd�n1sCwY|jdksOJ�|jdksVJ�Wd�n1s`wYWd�n1sowYt�d��}|��|��Wd�n1s�wY|jdks�J�dS)Nr�z(psutil._psplatform.Process.oneshot_enterrYr�r�)r6r7�m1�m2rr8r8r9�test_oneshot_twices,


������
�zTestProcess.test_oneshot_twicecCs�|��\}}|��}|��}||ksJ�|���|��|ks!J�|��|ks)J�Wd�n1s3wY|���|��|ksEJ�|��|ksMJ�Wd�dS1sXwYdSr;)rhryr�)r6�p1Zp2Zp1_ppidZp2_ppidr8r8r9�test_oneshot_cache#s
�
"�zTestProcess.test_oneshot_cachecsjdd�}|���������trt�fdd��|���t��}|�|j�D]	\}}|||�q)dS)NcSszz|�}Wn$tjy�tjyYdStjy)tr(|dvr(YdS�wtr2|dvr2dSt�|�d|�d��S)N>r�r�>rmrz didn't raise NSP and returned z instead)r.rrRrTr	rr$rq)�funZfun_namer�r8r8r9�assert_raises_nsp:s 
��zFTestProcess.test_halfway_terminated_process.<locals>.assert_raises_nspcs�jt��vSr;)r0r.r|r8r�r8r9rNQsz=TestProcess.test_halfway_terminated_process.<locals>.<lambda>)	r<rGr>rrrBr#�iter�all)r6r��nsr�rmr8r�r9�test_halfway_terminated_process2s
�z+TestProcess.test_halfway_terminated_processcCs|��\}}|�|�dSr;)Zspawn_zombieZassert_proc_zombie)r6Z_parent�zombier8r8r9�test_zombie_processXszTestProcess.test_zombie_processcCsXt��}tjdt�d�d��}|��sJ�|jsJ�Wd�dS1s%wYdS)Nzpsutil.ProcessrrN)r.r/rrPrrr~r�r8r8r9�$test_zombie_process_is_running_w_exc]s
�"�z0TestProcess.test_zombie_process_is_running_w_exccCs^t��}tjdt�d�d��}|��tjksJ�|jsJ�Wd�dS1s(wYdS)Nz!psutil._psplatform.Process.statusrrN)r.r/rrPrr&r'r~r�r8r8r9� test_zombie_process_status_w_exchs�"�z,TestProcess.test_zombie_process_status_w_excc
Cs�|��}t�|j�}|j|��df|_tt���|jtjvs"J�|�	�r(J�t
j�tj
dd��%t�t����}tt���Wd�n1sJwYWd�n1sYwYd|j��|��vsjJ�|jtjvsrJ�|t�|j�ks|J�d}t|�}|j|j|jdd�D]5\}}|j|d��#tjtj|d	��|�Wd�n1s�wYWd�n1s�wYq�d
t|�vs�J�d
t|�vs�J�tjtj|d	��|��Wd�n1s�wYtjtj|d	��|��Wd�n	1�swYtjtj|d	��|��Wd�n	1�s$wYtjtj|d	��
|� �Wd�dS1�sBwYdS)NrtZPSUTIL_DEBUGTz+refreshing Process instance for reused PID z4process no longer exists and its PID has been reusedF)�clear_cacher�r�zterminated + PID reused)!�
spawn_subprocr.r/r0r��_identrTr�Z_pmaprrrP�object�_common�
contextlib�redirect_stderr�io�StringIO�getvaluer#r��settersZkillersr=r$r3rRr:�reprryr{r�r�)r6Zsubpr7r��msgr�r�rmr8r8r9�test_reused_pidtsR���
�����
�
�
�
$�zTestProcess.test_reused_pidc	Cs�dt��vr5t�tj��
t�d�Wd�n1swYt�d�r(J�t�d���dks3J�dSt�d�}tr?tj	nt
}t�|��|��Wd�n1sUwYt�|��|��Wd�n1snwYt�|��|�
�Wd�n1s�wYt�|��|��Wd�n1s�wYt�|��|��Wd�n1s�wYt�|��|�tj�Wd�n1s�wYt|�}|�|j|j�D]D\}}z|�}Wn
tj	y�Yq�w|dv�r|jdk�sJ�q�|dk�rt�rdnd}|��|k�sJ�q�|dk�r*|�s*J|��q�t�s?dt��v�s7J�t�d��sAJ�dSdS)NrrY>r,r4rCzNT AUTHORITY\SYSTEM�rootrm)r.r|r$r3rRr/Z
pid_existsryrrTrnr>rGrdrer=rIr?r@r#r�Zgettersr�r1rCr	)r6r7r�r�r�rmr�r�r8r8r9�
test_pid_0�s^�

�
�
�
�
��
�


��zTestProcess.test_pid_0cCsPdd�}d|_t��}||���}|tj���}ts"tr$||ks&J�dSdSdS)NcSsZddh}tr
|�gd��t|���D]}||vs|�d�r#|�|�qdd�|��D�S)NZPLAT�HOME)Z__CF_USER_TEXT_ENCODINGZVERSIONER_PYTHON_PREFER_32_BITZVERSIONER_PYTHON_VERSIONZPYTEST_cSs6i|]\}}|�dd��dd�|�dd��dd��qS)�
r�
)r)r��k�vr8r8r9�
<dictcomp>�s
��z@TestProcess.test_environ.<locals>.clean_dict.<locals>.<dictcomp>)r�updaterTr�r��popr�)r��excludermr8r8r9�
clean_dict�s
��z,TestProcess.test_environ.<locals>.clean_dict)ZmaxDiffr.r/rGr1�copyr
r)r6r�r7Zd1Zd2r8r8r9�test_environ�s�zTestProcess.test_environz<macOS 11+ can't get another process environment, issue #2084z(sometimes fails on `assert is_running()`cCs�t�d�}t|��|d�}|j|gtjtjd�}t�|j	�}t
|j	�|��s)J�|j�
�dks2J�trHtrHz|��}WntjyGYdSw|��}|ddd�ksUJ�|��|jdks`J�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)�stdinrX�rZ�3)�A�Cr)�textwrap�dedentr!r�r�r[r\r.r/r0r+rrXr�rrrGrT�communicate�
returncode)r6rDZcexeZsprocr7�envr8r8r9�test_weird_environ�s&
�
�zTestProcess.test_weird_environN)h�__name__�
__module__�__qualname__�__doc__r:rErHrKr$�mark�skipifrrUr^rrfrkrorrr|r�r�rr�r�r�rr*rr�rr�rr�rr�r�r�r�r�Zxdist_groupr�r�rr�r'r)rr�r�r�rrrrr	rrrr"r(r2r8r?r@rIrLrOrrXr[r`rrlrqrtr	rwrzr}r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr�rr�r8r8r8r9r,Hs







&+�









0
	


B
&


&


�



4
&



).
��r,c@sPeZdZdZedd��Zejje	oe
dd�dd��Zdd	�Zd
d�Z
dd
�ZdS)�	TestPopenzTests for psutil.Popen class.cCs
t�dSr;r%)�clsr8r8r9�
tearDownClasss
zTestPopen.tearDownClasszhangs on OSX + CIrLc	Cs�tddg}tj|tjtjtd��5}|��|��|jt	|�s"J�t
�t��|j
Wd�n1s5wY|��Wd�n1sHwYtr\|�d�tjksZJ�dS|�d�tjksfJ�dS)NrV�3import time; [time.sleep(0.1) for x in range(100)];��stdoutrXr�rZ)rr.�Popenr[r\rrmr�r�r�r$r3r4r�rGrr>r?r@�r6r]�procr8r8r9�	test_misc#s.���
�
zTestPopen.test_misccCs|tjtdgtjtjtjtd��}|��Wd�n1swY|jjs)J�|j	js/J�|j
js5J�|jdks<J�dS)Nz-V)r�rXr�r�r)r.r�rr[r\rr�r��closedrXr�r�)r6r�r8r8r9�test_ctx_manager?s�
�zTestPopen.test_ctx_managerc	Cs�tddg}tj|tjtjtd���}|��|��t�	tj
��|��Wd�n1s.wYt�	tj
��|��Wd�n1sHwYt�	tj
��|�t
j�Wd�n1sdwYtr�t�	tj
��|�t
j�Wd�n1s�wYt�	tj
��|�t
j�Wd�n1s�wYWd�dSWd�dSWd�dS1s�wYdS�NrVr�r�)rr.r�r[r\rrGr>r$r3rRr=rIr?r@rZCTRL_C_EVENTZCTRL_BREAK_EVENTr�r8r8r9�test_kill_terminateMsD��
�
������"�zTestPopen.test_kill_terminatec	Cs�tddg}tj|tjtjtd��1}|��|��t�	t
��|jWd�n1s,wYWd�dSWd�dS1sDwYdSr�)rr.r�r[r\rrGr>r$r3r4r�r�r8r8r9�test__getattribute__js&����"�zTestPopen.test__getattribute__N)r�r�r�r��classmethodr�r$r�r�rrr�r�r�r�r8r8r8r9r�s

r�)>r�r�r�r�rEr�r]r1r?rrr�r$r[r
r�rpZunittestrr.rrrrrr	r
rrZpsutil._commonr
Zpsutil.testsrrrrrrrrrrrrrrrrrrr r!r"r#r$r&r'r(r)r*r+r,r�r8r8r8r9�<module>s�_