B �=�_7��@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlmZddlmZddlmZddlmZddlmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddl m!Z!ddl m"Z"ddl m#Z#ddl m$Z$ddl m%Z%ddl m&Z&ddl m'Z'ddl m(Z(ddl m)Z)ddl m*Z*ddl m+Z+ddl m,Z,ddl m-Z-ddl m.Z.dd l m/Z/dd!l m0Z0dd"l m1Z1dd#l m2Z2dd$l m3Z3dd%l m4Z4dd&l m5Z5dd'l m6Z6dd(l m7Z7dd)l m8Z8dd*l m9Z9dd+l m:Z:dd,l m;Z;dd-l m<Z<Gd.d/�d/e2�Z=e�r�e�>�dk�r�Gd0d1�d1e=�Z?Gd2d3�d3e2�Z@eAd4k�r�dd5lBmCZCeCeD�dS)6zTests for psutil.Process class.�N)�AIX)�BSD)�LINUX)�MACOS)�NETBSD)�OPENBSD)�OSX)�POSIX)�SUNOS)�WINDOWS)� open_text)�FileNotFoundError)�long)�PY3)�super)�APPVEYOR)� call_until)� CI_TESTING)�copyload_shared_lib)� create_exe)�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)�mock)�process_namespace)�PsutilTestCase)�PYPY)� PYTHON_EXE)� reap_children)�retry_on_failure)�sh)�skip_on_access_denied)�skip_on_not_implemented)� ThreadTask)�unittest)� wait_for_pidc@s�eZdZdZdd�Zdd�Zdd�Zdd �Zd d �Ze � e d �d d��Z dd�Z dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Ze � e d!�d"d#��Zd$d%�Ze � e d&�d'd(��Ze � e d!�eed)�d*d+���Ze � e d!�e � e d,�d-d.���Ze � e d!�e � e d/�d0d1���Z!e � e" d!�d2d3��Z#e � e" d!�d4d5��Z$e � e" d!�d6d7��Z%e � e" d!�d8d9��Z&e � e" d!�d:d;��Z'd<d=�Z(e � e d>�d?d@��Z)e � e* d!�dAdB��Z+e,�e-e.d)�e � e* d!�dCdD����Z/e,�dEdF��Z0dGdH�Z1e � e2 d!�dIdJ��Z3e � e2 d!�dKdL��Z4dMdN�Z5dOdP�Z6dQdR�Z7dSdT�Z8e � e9dU�dVdW��Z:dXdY�Z;e � e9dZ�d[d\��Z<e � e=d]�e � e>d^�e � e9dU�d_d`����Z?e � e d&�dadb��Z@e � e d&�dcdd��ZAdedf�ZBdgdh�ZCdidj�ZDdkdl�ZEdmdn�ZFe � eG d!�dodp��ZHe � eG d!�dqdr��ZIe � eG d!�dsdt��ZJe � eKdu�e � eLdv�dwdx���ZMe � eKdu�e � eLdv�dydz���ZNe � e d&�d{d|��ZOeed)�e � eP�p�eQd}�d~d���ZRd�d��ZSd�d��ZTd�d��ZUd�d��ZVe,�d�d���ZWd�d��ZXd�d��ZYd�d��ZZd�d��Z[d�d��Z\d�d��Z]d�d��Z^d�d��Z_d�d��Z`d�d��Zad�d��Zbe � e d&�d�d���Zce � e d&�d�d���Zde � e d&�d�d���Zed�d��Zfe � eg d!�d�d���Zhe � eg d!�e � e d&�d�d����Zid�S)�� TestProcesszTests for psutil.Process class.cOs|j||�}t�|j�S)N)�spawn_testproc�psutil�Process�pid)�self�args�kwargs�sproc�r6�L/opt/alt/python37/lib64/python3.7/site-packages/psutil/tests/test_process.py� spawn_psprocNs zTestProcess.spawn_psprocc Cs:t��}|�|jt���|�t�� d|_WdQRXdS)N�!)r/r0� assertEqualr1�os�getpid� assertRaises�AttributeError)r2�pr6r6r7�test_pidTs zTestProcess.test_pidcCsJ|��}|��|��}tr,|�|tj�n|�|tj �|�|�dS)N) r8�kill�waitr r:�signal�SIGTERM�SIGKILL�assertProcessGone)r2r?�coder6r6r7� test_killZszTestProcess.test_killcCsJ|��}|��|��}tr,|�|tj�n|�|tj �|�|�dS)N)r8� terminaterBr r:rCrDrF)r2r?rGr6r6r7�test_terminatedszTestProcess.test_terminatecCsXtr tjntj}|��}|�|�|��}tr<|�||�n|�|| �|� |�dS)N) r rCrErDr8� send_signalrBr r:rF)r2�sigr?rGr6r6r7�test_send_signalns zTestProcess.test_send_signalz not POSIXc Cs�tj}|��}tjdttjd�d��|�t j |j |�WdQRX|��}tjdttj d�d��|�t j |j |�WdQRXdS)Nzpsutil.os.kill�)� side_effect)rCrDr8r �patch�OSError�errnoZESRCHr=r/� NoSuchProcessrKZEPERM� AccessDenied)r2rLr?r6r6r7�test_send_signal_mockedysz#TestProcess.test_send_signal_mockedcCs�tddg}|�|�}|��}|�|d�|�|�tddg}|j|tjd�}|��}|�|d�|�|�tddg}|�|�}|��}|�|d�|�|�tdd g}|�|�}|��}|�|d�|�|�dS) Nz-c�passrz1 / 0)�stderr�zimport sys; sys.exit(5);�zimport os; os._exit(5);)r$r8rBr:rF� subprocess�PIPE)r2�cmdr?rGr6r6r7�test_wait_exited�s(              zTestProcess.test_wait_exitedcCs�|��}tr�|�tj�|jtj|jdd�|�tj �|jtj|jdd�|�tj �|� |��tj �|� |��tj �nd|� �|jtj|jdd�|� �|jtj|jdd�|��|� |��tj �|� |��tj �dS)Ng����MbP?)�timeout)r8r rKrC�SIGSTOPr=r/�TimeoutExpiredrB�SIGCONTrDr:�suspend�resumerI)r2r?r6r6r7�test_wait_stopped�s    zTestProcess.test_wait_stoppedcCs�|��\}}|�tj|jd�|�tj|jd�|��|��|��}|��}trr|�|tj �|�|d�n|�|tj �|�|tj �dS)Ng{�G�z�?) �spawn_children_pairr=r/r`rBrIr r:rCrD)r2�child� grandchildZ child_retZgrandchild_retr6r6r7�test_wait_non_children�s z"TestProcess.test_wait_non_childrencCsH|��}|��|�tj|jd�|�tj|jd�|�t|jd�dS)Ng{�G�z�?r���)r8�namer=r/r`rB� ValueError)r2r?r6r6r7�test_wait_timeout�s zTestProcess.test_wait_timeoutcCs�|��}|�tj|jd�|��t��t}xBt��|krfy|�d�}PWq0tjk rbYq0Xq0W|�d��t r�|� |t j �n|� |t j �|�|�dS)Nrr^)r8r=r/r`rBrA�timer�failr r:rCrErDrF)r2r?Zstop_atrGr6r6r7�test_wait_timeout_nonblocking�s    z)TestProcess.test_wait_timeout_nonblockingc Cs|t��}|jdd�|jdd�x4td�D](}|jdd�}|�|t�|�|d�q*W|�t��|jdd�WdQRXdS)Ng����MbP?)Zinterval�dgri) r/r0� cpu_percent�range�assertIsInstance�float�assertGreaterEqualr=rk)r2r?�xZpercentr6r6r7�test_cpu_percent�s     zTestProcess.test_cpu_percentc Cs4tjddd��}t����|js&t�WdQRXdS)Nzpsutil.cpu_count)Z return_value)r rPr/r0rq�called�AssertionError)r2�mr6r6r7�test_cpu_percent_numcpus_none�s z)TestProcess.test_cpu_percent_numcpus_nonec Cs�t����}|jdks(|jdks(t|��|jdks:t|��|jdksLt|��trb|j dksbt|��x&|j D]}t � dt � t||���qjWdS)Ngz%H:%M:%S)r/r0� cpu_times�user�systemry� children_user�children_systemrZiowait�_fieldsrm�strftime� localtime�getattr)r2�timesrjr6r6r7�test_cpu_times�s  zTestProcess.test_cpu_timescCs�t����dd�\}}t��dd�\}}t||g�t||g�dkrZ|�d||f�t||g�t||g�dkr�|�d||f�dS)N�g�������?zexpected: %s, found: %s)r/r0r|r;r��max�minrn)r2� user_timeZ kernel_time�utimeZktimer6r6r7�test_cpu_times_2s zTestProcess.test_cpu_times_2z not supportedcCsPt��}|��}|�|d�t��dkr4|�|d�|�|��tt����dS)NrrX)r/r0Zcpu_numru� cpu_countr:�assertInrr)r2r?Znumr6r6r7� test_cpu_nums    zTestProcess.test_cpu_numcCsZ|��}t��}|��}t||�}|dkr@|�d|||f�t�dt�|����dS)Nr�z'expected: %s, found: %s, difference: %sz%Y %m %d %H:%M:%S)r8rm� create_time�absrnr�r�)r2r?Znowr�� differencer6r6r7�test_create_times zTestProcess.test_create_timez POSIX onlycCs4t����}|dk r0tj�td��}|�||�dS)N�tty)r/r0�terminalr;�path�realpathr'r:)r2r�r�r6r6r7� test_terminal's zTestProcess.test_terminal)Zonly_ifc Cs�t��}|��}ttd��}|��WdQRX|��}ts�ts�|�|j |j �|� |j |j �t r�|�|j |j �|� |j|j�n |�|j|j�|�|j|j�|��}t|��d��.}tr�|�tddd��n|�dd�WdQRX|��}|�|j |j �|�|j|j�|�|j |j �|�|j|j�t �r\|�|j|j�|�|j |j �xJtt|��D]:}t�r�|dk�r��qj|�||d�|�||d��qjWdS)N�rb�wbrvi@B�asciir�r)r/r0Z io_counters�openr$�readrr� assertGreaterZ read_countr:Z write_countrZ read_charsZ write_charsruZ read_bytesZ write_bytes� get_testfnr�write�bytesrr�len)r2r?Zio1�fZio2�ir6r6r7�test_io_counters.s> zTestProcess.test_io_countersz linux onlyc Cs�t��}ts"|�|��dtj�|�tjd�|�tjd�|�tjd�|�tjd�|��}�z4|�tj�|�t |���tjdf�|� t ��|jtjdd�WdQRX|�tj�|�t |���tjdf�|jtjdd�|�t |���tjdf�|� t ��|jtjdd�WdQRXy|jtjdd�Wntj k �rTYnX|� t d|jtjd�|� t d|jtjd�|j t d |jdd�Wd|\}}|tjk�r�d}|�||�XdS) NrrXr���)�value�zioclass accepts no valuez$'ioclass' argument must be specified)r/r0rr:�ioniceZIOPRIO_CLASS_NONEZIOPRIO_CLASS_RTZIOPRIO_CLASS_BEZIOPRIO_CLASS_IDLE�tupler=rkrT�assertRaisesRegex)r2r?�initZioclassr�r6r6r7�test_ionice_linuxYsH     zTestProcess.test_ionice_linuxz!not supported on this win versionc Cs�t��}ts|�|��tj�|��}z�|�tj�|�|��tj�|�tj�|�|��tj�y|�tj�Wntj k r�YnX|�|��tj�|j t d|jtjdd�|� t d|jtjd�Wd|�|�XdS)Nz&value argument not accepted on WindowsrX)r�zis not a valid priority) r/r0rr:r�Z IOPRIO_NORMALZIOPRIO_VERYLOWZ IOPRIO_LOWZ IOPRIO_HIGHrTr�� TypeErrorrk)r2r?r�r6r6r7�test_ionice_win�s*  zTestProcess.test_ionice_wincCs�ddl}t�t���}dd�tt�D�}|s4t|��x�|D]�}tt|�}|�|d�|t|�kr�|� |t||��t rxq:|� |� |�|� |��q:|� |�}|� t |�d�|�|dd�|�|dd�q:WdS)NrcSsg|]}|�d�r|�qS)ZRLIMIT)� startswith)�.0rvr6r6r7� <listcomp>�sz/TestProcess.test_rlimit_get.<locals>.<listcomp>r�rirX)�resourcer/r0r;r<�dirryr�rur:r#�rlimitZ getrlimitr�)r2r�r?�namesrjr��retr6r6r7�test_rlimit_get�s       zTestProcess.test_rlimit_getc Cs�|��}|�tjd�|�|�tj�d�trX|�td��tj� d��d�WdQRX|� t��|�tjd�WdQRXdS)N)rYrYzcan't use prlimitr)rYrYrY) r8r�r/Z RLIMIT_NOFILEr:rr�rkZ _psplatformr0r=)r2r?r6r6r7�test_rlimit_set�s zTestProcess.test_rlimit_setc Cs�t��}|��}|�tj�\}}z�|�tjd|f�t|d��}|�d�WdQRX|�t��&}t|d��}|�d�WdQRXWdQRX|� t r�|j j n|j dt j �Wd|�tj||f�|� |�tj�||f�XdS)Nir�sXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXsXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXr)r/r0r�r�� RLIMIT_FSIZEr�r�r=�IOErrorr:rZ exceptionrRZEFBIG)r2r?�testfn�soft�hardr��excr6r6r7� test_rlimit�s    zTestProcess.test_rlimitc Cs�t��}|�tj�\}}zN|�tjd|f�|�tjtj|f�t|��d��}|�d�WdQRXWd|�tj||f�|�|�tj�||f�XdS)Nir�sXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX) r/r0r�r�� RLIM_INFINITYr�r�r�r:)r2r?r�r�r�r6r6r7�test_rlimit_infinity�sz TestProcess.test_rlimit_infinitycCs<t��}|�tj�\}}|�tj|�|�tj||f�dS)N)r/r0r�r�r:r�)r2r?r�r�r6r6r7�test_rlimit_infinity_value�sz&TestProcess.test_rlimit_infinity_valuec Csrt��}tr<y |��}WqDtjk r8t�d��YqDXn|��}t��|��}|�||d�WdQRXdS)Nz$on OpenBSD this requires root accessrX) r/r0r� num_threadsrTr+�SkipTestr*r:)r2r?�step1�step2r6r6r7�test_num_threads�s zTestProcess.test_num_threadsz WINDOWS onlycCst��}|�|��d�dS)Nr)r/r0r�Z num_handles)r2r?r6r6r7�test_num_handlesszTestProcess.test_num_handlesc Cs�t��}tr<y |��}WqDtjk r8t�d��YqDXn|��}t��d|��}|�t |�t |�d�|d}|�|j |d�|�|j |d�|�|j |d�WdQRXdS)Nz$on OpenBSD this requires root accessrXrr�) r/r0r�threadsrTr+r�r*r:r��idr�� system_time)r2r?r�r�Zathreadr6r6r7� test_threads s zTestProcess.test_threadscCs�|��}tr:y |��Wn tjk r8t�d��YnX|j|��j t dd�|��D��dd�|j|��j t dd�|��D��dd�dS)Nz$on OpenBSD this requires root accesscSsg|] }|j�qSr6)r�)r�rvr6r6r7r�-sz.TestProcess.test_threads_2.<locals>.<listcomp>g�������?)ZdeltacSsg|] }|j�qSr6)r�)r�rvr6r6r7r�0s) r8rr�r/rTr+r�ZassertAlmostEqualr|r}�sumr~)r2r?r6r6r7�test_threads_2 s  zTestProcess.test_threads_2c Cs�t��}|��dd�\}}|��}|�|d�|�|d�dgd}|��dd�\}}|��}|�||�|�||�|�||�~tr�|��} |�| j| j �|�| j | j �|��} x | j D]} |�t | | �d�q�WdS)Nr�ri`�)r/r0Z memory_info�memory_percentr�rur r:ZrssZwset�vmsZpagefiler�r�) r2r?Zrss1Zvms1Zpercent1ZmemarrZrss2Zvms2Zpercent2�memrjr6r6r7�test_memory_info2s&       zTestProcess.test_memory_infocCs�t��}t��j}|��}xT|jD]J}t||�}|j|d||fd�|dkrPts"t rVq"|j |||||fd�q"Wt s|t s|t r�|�|j d�t r�|�|jd�|�|jd�dS)Nr)�msgr�)r/r0Zvirtual_memory�totalZmemory_full_infor�r�rurrZassertLessEqualr r�ussZpssZswap)r2r?r�r�rjr�r6r6r7�test_memory_full_infoQs    z!TestProcess.test_memory_full_infoc Cs�t��}|��}dd�|D�}|�t|�tt|���|jdd�}�x�|D]�}|j�d�sJtj� |j�srt |j��t r�y*tj� |j�s�tj� |j�s�t |j��WnHt k r�ts��n,td��}|��}WdQRXd|j|kr�YnXqJdtj�|j�krJyt�|j�}Wntk �r$YqJXt�|j�sJt |j��qJWxv|D]n}xf|jD]\} t|| �} | d k�rp�qRn<| d k�r�| �s�t | ��n"|�| ttf�| d k�sRt | ���qRW�qFWdS) NcSsg|]}|�qSr6r6)r�rvr6r6r7r�esz0TestProcess.test_memory_maps.<locals>.<listcomp>F)Zgrouped�[z/proc/self/smapsz %s (deleted)Z64r�)ZaddrZpermsr)r/r0� memory_mapsr:r��setr�r�r;�isabsryr �exists�islinkrr r��basename�statr �S_ISREG�st_moder�r�rs�intr) r2r?�maps�pathsZext_maps�ntr��data�stZfnamer�r6r6r7�test_memory_mapsasD          zTestProcess.test_memory_mapsc sLt��}t��4}dd���fdd�|��D�}|��|�|�WdQRXdS)NcSstj�tj�|��S)N)r;r�r��normcase)r?r6r6r7�normpath�sz8TestProcess.test_memory_maps_lists_lib.<locals>.normpathcsg|]}�|j��qSr6)r�)r�rv)r�r6r7r��sz:TestProcess.test_memory_maps_lists_lib.<locals>.<listcomp>)r/r0rr�r�)r2r?r�Zlibpathsr6)r�r7�test_memory_maps_lists_lib�s   z&TestProcess.test_memory_maps_lists_libcCs>t��}|��|jt|jdd�ts.ts.tr:|jdd�dS)Nz?!?)Zmemtyper�)r/r0r�r=rkrrr )r2r?r6r6r7�test_memory_percent�s  zTestProcess.test_memory_percentcCsL|��}|��st�|��s t�|��|��|��r<t�|��rHt�dS)N)r8� is_runningryrArB)r2r?r6r6r7�test_is_running�s   zTestProcess.test_is_runningc Cs�|��}|��}y|�|t�Wn�tk r�trbt|�tt�krbtjj }|�||�|t��nNdt j dt j df}y |�|� |d�t� |d��Wntk r�YnXYnXt |ddg�}|�|d�dS)Nz%s.%srrXrNz-czimport os; print('hey')Zhey)r8�exer:r$ryr r�r;r�r��sys� version_info�replacer')r2r?r�r�Zver�outr6r6r7�test_exe�s  zTestProcess.test_execCsVtddg}|�|�}ts ts tr6|�|��dt�n|�d�|���d�|��dS)Nz-czimport time; time.sleep(60)r� )r$r8rrrr:�cmdline�join)r2r�r?r6r6r7� test_cmdline�s    zTestProcess.test_cmdlinezbroken on PYPYcCs>|��}t|�|gdgd}|�|�}|�|��|�dS)N� 0123456789�)r�rr8r:r�)r2r�r�r?r6r6r7�test_long_cmdline�s  zTestProcess.test_long_cmdlinecCsJ|�t�}|����}tj�tj�tj ����}|� |�sFt ||f��dS)N) r8r$rj�lowerr;r�r�r�r�� executabler�ry)r2r?rjZpyexer6r6r7� test_name�s  zTestProcess.test_namezunreliable on PYPYcCs:|jdd�}t|�|�|�}|�|��tj�|��dS)NZ01234567890123456789)�suffix)r�rr8r:rjr;r�r�)r2r�r?r6r6r7�test_long_name�s  zTestProcess.test_long_namezbroken on SUNOSz broken on AIXcCs||jdd�}t|�|ddddddg}|�|�}|�|��|�|�|��tj�|��|�tj� |� ��tj� |��dS)Nz foo bar ))rz-cz9import time; [time.sleep(0.01) for x in range(3000)];arg1Zarg2rNZarg3) r�rr8r:r�rjr;r�r�r�r�)r2Z funky_pathr�r?r6r6r7�test_prog_w_funky_name�s   z"TestProcess.test_prog_w_funky_namecCsXt��}|��\}}}|�|t���|�|t���ttd�rT|�t��|���dS)N� getresuid) r/r0�uidsr:r;�getuid�geteuid�hasattrr)r2r?�real� effective�savedr6r6r7� test_uids�s  zTestProcess.test_uidscCsXt��}|��\}}}|�|t���|�|t���ttd�rT|�t��|���dS)Nr) r/r0�gidsr:r;�getgid�getegidr� getresgid)r2r?rr r r6r6r7� test_gids s  zTestProcess.test_gidscCszt��}|�t|jd�|��}�z(tr�xrtjtjtjtj tj tj gD]R}|j |d��<y|�|�Wntj k r|YnX|�|��|�WdQRXqFWn�y�ttd�r�|�t�tjt���|���|�d�|�|��d�ttd��r |�t�tjt���|���t�s,|�d�|�|��d�Wntj k �rFYnXWdy|�|�Wntj k �rrYnXXdS)N�str)�prio� getpriorityrXr)r/r0r=r��nicer ZNORMAL_PRIORITY_CLASSZIDLE_PRIORITY_CLASSZBELOW_NORMAL_PRIORITY_CLASSZREALTIME_PRIORITY_CLASSZHIGH_PRIORITY_CLASSZABOVE_NORMAL_PRIORITY_CLASSZsubTestrTr:rr;r� PRIO_PROCESSr<r)r2r?r�rr6r6r7� test_nicesJ         zTestProcess.test_nicecCst��}|�|��tj�dS)N)r/r0r:�statusZSTATUS_RUNNING)r2r?r6r6r7� test_statusCszTestProcess.test_statuscCsd|��}|��}trP|�d�\}}|�|t���dtjkr`|�|tjd�n|�|t���dS)N�\Z USERDOMAIN) r8�usernamer �splitr:�getpassZgetuserr;�environ)r2r?rZdomainr6r6r7� test_usernameGs zTestProcess.test_usernamecCs |��}|�|��t���dS)N)r8r:�cwdr;�getcwd)r2r?r6r6r7�test_cwdRszTestProcess.test_cwdcCs$tddg}|�|�}t|jd�dS)Nz-cz/import os, time; os.chdir('..'); time.sleep(60)z#ret == os.path.dirname(os.getcwd()))r$r8rr)r2r\r?r6r6r7� test_cwd_2Vs zTestProcess.test_cwd_2cCs�t��}|��}|st|��|�|j|�ttd�rL|�|tt� |j ���|�t |�t t |���tt t tjdd����}xr|D]j}|�|g�|�|��|g�ttd�r�|�|��tt� |j ���t|d�r�|�|��d|���q�W|�g�t�r|�|��|j���n|�|��|�ttd��rP|�|��tt� |j ���|�t|jd�|�|�|�t |��|�t|��dS)N�sched_getaffinityT)�percpu�num_cpurrX)r/r0� cpu_affinityry� addCleanuprr;r:�listr#r1r�r�rrrqr%r�_procZ_get_eligible_cpusr=r�r�)r2r?�initialZall_cpus�nr6r6r7�test_cpu_affinity\s6           zTestProcess.test_cpu_affinitycCsp|��}ttjdd��dg}|�t|j|�|�t|jtdd��|�t|jddg�|�t|jddg�dS) NT)r$� i'i�*r�1ri) r8r�r/r|r=rkr&rrr�)r2r?Z invalid_cpur6r6r7�test_cpu_affinity_errs�s z"TestProcess.test_cpu_affinity_errscCs�t��}|��}|st|��|�|j|�t|�dkrB|dd�}g}xBtdt|�d�D],}x&t�||�D]}|rl|� t |��qlWqZWx.|D]&}|�|�|� t |���t |��q�WdS)N� rrX) r/r0r&ryr'r�rr� itertools� combinations�appendr(r:�sorted)r2r?r*Zcombosr�ZsubsetZcombor6r6r7�"test_cpu_affinity_all_combinations�s     z.TestProcess.test_cpu_affinity_all_combinationsz broken on BSDzunreliable on APPVEYORc Cs`t��}|��}|��}|�||�t|d��x}|�d�|��t|jdt |��}dd�|D�}|� t j � |�|�tr�x$|D]}|j |kr�|�|jd�q�WWdQRXx"|D]}t j �|j �s�t|��q�Wd|}|�td|g�}xLtd �D],}d d�|��D�}||k�rPt�d �q�W|� t j � |�|�x$|D]}t j �|��s<t|���q<WdS) Nr�sxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxzlen(ret) != %icSsg|]}tj�|j��qSr6)r;r�r�)r�rvr6r6r7r��sz/TestProcess.test_open_files.<locals>.<listcomp>iz2import time; f = open(r'%s', 'r'); time.sleep(60);z-crpcSsg|]}tj�|j��qSr6)r;r�r�)r�rvr6r6r7r��sg{�G�z�?)r/r0r�� open_files� assertNotInr�r��flushrr�r�r;r�r�rr:Zposition�isfileryr8r$rrrm�sleep) r2r?r��filesr�� filenames�filer�rvr6r6r7�test_open_files�s4        zTestProcess.test_open_filesc Cs�t��}tjj}|��}t|d���}xJ|��D](}||j�||j�ksT|j |� �kr.Pq.W|� dt |����|� ||j�||j��tr�|� |j d�n|� |j |� ��|��d}|� |d|j�|� |d|j �|�|j|���WdQRXdS)N�wzno file found; files=%srirrX)r/r0r;r�r�r�r�r6rj�fd�filenorn�reprr:r r7)r2r?r�r�Zfileobjr=Zntupler6r6r7�test_open_files_2�s"  zTestProcess.test_open_files_2cCs�t��}|��}|��}t|d�}|�|j�|�|��|d�t��}|�|j�|�|��|d�|��|��|�|��|�dS)Nr?rXr�) r/r0r�Znum_fdsr�r'�closer:�socket)r2r?r��startr=Zsockr6r6r7� test_num_fds�s   zTestProcess.test_num_fdsz not reliable on OPENBSD & NETBSDcCsLt��}t|���}x(td�D]}t|���}||krdSqW|�d�dS)Ni �z7num ctx switches still the same after 50.000 iterations)r/r0r�Znum_ctx_switchesrrrn)r2r?ZbeforervZafterr6r6r7�test_num_ctx_switches�s  z!TestProcess.test_num_ctx_switchescCsNt��}ttd�r&|�|��t���|��}|�|��t���t rJdSdS)N�getppid) r/r0rr;r:�ppidrIr8r<r)r2r?r6r6r7� test_ppid�s zTestProcess.test_ppidcCsB|��}|�|��jt���t��d}|�t� |����dS)Nr) r8r:�parentr1r;r<r/�pids� assertIsNoner0)r2r?Z lowest_pidr6r6r7� test_parents zTestProcess.test_parentcCs8t��}|��\}}|�|��|�|�|��|�dS)N)r/r0rer:rL)r2rLrfrgr6r6r7�test_parent_multis zTestProcess.test_parent_multic Cs<|��}tjdt�dd�d��|�|���WdQRXdS)Nzpsutil.Processr�foo)rO)r8r rPr/rSrNrL)r2r?r6r6r7�test_parent_disappearedsz#TestProcess.test_parent_disappearedcCs`t��}|��st�|��\}}|�|��d|�|�|��d|�|�|��d|�dS)NrrX)r/r0�parentsryrer:)r2rLrfrgr6r6r7� test_parentss   zTestProcess.test_parentscCs�t��}|�|��g�|�|jdd�g�|jdd�}|��}|jdd�}xJ||fD]>}|�t|�d�|�|dj|j�|�|d��|j�qVWdS)NT)� recursiver)Z creationflagsrX)r/r0r:�childrenr8r�r1rJ)r2rLrfZ children1Z children2rVr6r6r7� test_children%s  zTestProcess.test_childrencCsft��}|��\}}|�|��|g�|�|jdd�||g�|��|��|�|jdd�g�dS)NT)rU)r/r0rer:rVrIrB)r2rLrfrgr6r6r7�test_children_recursive4s z#TestProcess.test_children_recursivec Cs�t�t�}x@t��D]4}y||��d7<Wqtjk rFYqXqWt|��dd�d�dd}t r~|dkr~|� d��t� |�}y|j dd �}Wntj k r�YnX|�t|�tt|���dS) NrXcSs|dS)NrXr6)rvr6r6r7�<lambda>J�z6TestProcess.test_children_duplicates.<locals>.<lambda>)�keyrirzPID 0T)rU)� collections� defaultdictr�r/� process_iterrJ�Errorr4�itemsrZskipTestr0rVrTr:r�r�)r2�tabler?r1�cr6r6r7�test_children_duplicatesAs     z$TestProcess.test_children_duplicatescCs|t��}|��\}}|jdd�}|�t|�d�|�|d|�|�|d|�|��}|�|d|�|�|d|�dS)NT)rUr�rrX)r/r0rerVr:r�rS)r2rLrfrgrVrSr6r6r7�test_parents_and_childrenUs  z%TestProcess.test_parents_and_childrencCsZ|��}|��x*td�D]}|��tjkr.Pt�d�qW|��|� |��tj�dS)Nrpg{�G�z�?) r8rbrrrr/ZSTATUS_STOPPEDrmr:rc�assertNotEqual)r2r?rvr6r6r7�test_suspend_resumebszTestProcess.test_suspend_resumecCs$|�ttjd�|�ttjd�dS)Nr.ri)r=r�r/r0rk)r2r6r6r7�test_invalid_pidlszTestProcess.test_invalid_pidc Cs�t��}|jddgd�}|�t|���ddg�t�tt����}|jdgdd�}t|dt �sp|�|dd�t j ddtj d ��"|�|jd gd d�d d i�WdQRXt j ddt� |jd�d ��|jtj |jd gd�WdQRXt j ddt�|jd�d ��"|�|jd gdd�d di�WdQRXt j ddtd ��F|��}|�d t |����|�t��|jd gd�WdQRXWdQRX|�t��|�d�WdQRX|�t��|�dg�WdQRX|�t��|�dd g�WdQRXdS) Nr�rj)�attrsZ connectionsrQ)rhZad_valuezpsutil.Process.niceT)ZcreaterOrrXZbar)r/r0�as_dictr:r4�keysr�rM� isinstancer(r rPrTrSr1r=� ZombieProcess�NotImplementedErrorr7r�rk)r2r?�dr6r6r7� test_as_dictps> ""  "   zTestProcess.test_as_dictc Cs�t��}t�d��8}|���|��|��WdQRX|�|jd�WdQRXt�d��}|��|��WdQRX|�|jd�dS)Nz$psutil._psplatform.Process.cpu_timesrXr�)r/r0r rP�oneshotr|r:� call_count)r2r?rzr6r6r7� test_oneshot�s   zTestProcess.test_oneshotc Cs�t��}t�d���}t�d��j}|���:|��|��|���|��|��WdQRXWdQRX|�|jd�|�|jd�WdQRXWdQRXt�d��}|��|��WdQRX|�|jd�dS)Nz$psutil._psplatform.Process.cpu_timesz(psutil._psplatform.Process.oneshot_enterrXr�)r/r0r rPrpr|r:rq)r2r?Zm1Zm2rzr6r6r7�test_oneshot_twice�s    " zTestProcess.test_oneshot_twicec Cs�|��\}}|��}|��}|�||�|���&|�|��|�|�|��|�WdQRX|���&|�|��|�|�|��|�WdQRXdS)N)rerJrerpr:)r2Zp1Zp2Zp1_ppidZp2_ppidr6r6r7�test_oneshot_cache�s    zTestProcess.test_oneshot_cachecs��fdd�}���}|��|��tr:ttjd|j���|�t |�}x"|� |j �D]\}}|||�qZWtr�t s�t jj}��||���|t��dS)Ncs�y |�}WnRtjk r"�Yn^tjk r6YnJtjk r\trV|dkrVdS�Yn$Xtrn|dkrndS��d||f��dS)N)r�r�)r�rjz+%r didn't raise NSP and returned %r instead)r/rlrSrTrr rn)�funZfun_namer�)r2r6r7�assert_raises_nsp�s   zFTestProcess.test_halfway_terminated_process.<locals>.assert_raises_nspz %s not in ret)r8rIrBr rr/rMr1rFr!�iter�allrr;r�r�r:r�r$)r2rvr?�nsrurjr�r6)r2r7�test_halfway_terminated_process�s  z+TestProcess.test_halfway_terminated_processcCs�dd�}|��\}}t�|j�}|�|��tj�|��s>t�|� �t |�}x |� |j �D]\}}||�q\Wt� |j�s�t�|�|jt���|�|jdd�t��D��it_|�|jdd�t��D��dS)Nc Ss(y|�Stjtjfk r"YnXdS)N)r/rlrT)rur6r6r7�succeed_or_zombie_p_exc�sz@TestProcess.test_zombie_process.<locals>.succeed_or_zombie_p_exccSsg|] }|j�qSr6)r1)r�rvr6r6r7r�sz3TestProcess.test_zombie_process.<locals>.<listcomp>cSsg|] }|j�qSr6)r1)r�rvr6r6r7r�s)Z spawn_zombier/r0r1r:r� STATUS_ZOMBIEr�ryrir!rwrx� pid_existsr�rMr^Z_pmap)r2r{rLZzombieZzprocryrurjr6r6r7�test_zombie_process�s     zTestProcess.test_zombie_processc CsBt��}tjdt�d�d��}|��s*t�|js4t�WdQRXdS)Nzpsutil.Processr)rO)r/r0r rPrlr�ryrx)r2r?rzr6r6r7�$test_zombie_process_is_running_w_excs  z0TestProcess.test_zombie_process_is_running_w_excc CsHt��}tjdt�d�d��"}|�|��tj�|js:t �WdQRXdS)Nz!psutil._psplatform.Process.statusr)rO) r/r0r rPrlr:rr|rxry)r2r?rzr6r6r7� test_zombie_process_status_w_exc's z,TestProcess.test_zombie_process_status_w_excc Cs�dt��krF|�tjtjd�t�d�r,t�|�t�d���d�dSt�d�}t rZtj nt }|�||j �|�||j �|�||j�|�||j�|�||j�|�||jtj�t|�}x�|�|j|j�D]�\}}y |�}Wntj k r�Yq�X|dk�r|�|jd�q�|dk�r@t �r*dnd}|�|��|�q�|dkr�|s�t|��q�Wt�s~|�dt���t�d��s~t�dS)NrrX)rr rzNT AUTHORITY\SYSTEM�rootrj)r/rMr=rSr0r}ryr:rJr rTrkrBrIrbrcrArKrCrDr!rwZgettersZsettersrrrr�)r2r?r�ryrurjr�r}r6r6r7� test_pid_01s:     zTestProcess.test_pid_0cCsHdd�}d|_t��}||���}|tj���}tsDtrD|�||�dS)NcSsb|�dd�|�dd�|�dd�trL|�dd�|�dd�|�dd�tdd�|��D��S) NZPSUTIL_TESTINGZPLAT�HOMEZ__CF_USER_TEXT_ENCODINGZVERSIONER_PYTHON_PREFER_32_BITZVERSIONER_PYTHON_VERSIONcSs8g|]0\}}|�dd��dd�|�dd��dd�f�qS)� rN� )r�)r��k�vr6r6r7r�dsz@TestProcess.test_environ.<locals>.clean_dict.<locals>.<listcomp>)�popr�dictr`)rnr6r6r7� clean_dictZs      z,TestProcess.test_environ.<locals>.clean_dict) ZmaxDiffr/r0rr;�copyrrr:)r2r�r?Zd1Zd2r6r6r7� test_environXs zTestProcess.test_environcCs�t�d�}|��}t||d�|j|gtjtjd�}t�|j �}t |j �|� �sVt �|� |j��d�tr�tr�y |��}Wq�tjk r�dSXn|��}|� |ddd��|��|� |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)�stdinrWrZr.�3)�A�Cr)�textwrap�dedentr�rr.rZr[r/r0r1r,r�ryr:rWr�rrrrT� communicate� returncode)r2rGr�r5r?�envr6r6r7�test_weird_environos&      zTestProcess.test_weird_environN)j�__name__� __module__� __qualname__�__doc__r8r@rHrJrMr+�skipIfr rUr]rdrhrlrorwr{r�r�rr�r�r�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�r�rr rrr rrrrr!r"rr,r/r5rrr>rCrGrrrHrKrOrPrRrTrWrXrcrdrfrgrorrrsrtrzr~rr�r�rr�r�r6r6r6r7r-Ks�         * +   .     , (  #       . *%  ' r-csneZdZdZeed�r&e��Ze��Z �fdd�Z �fdd�Z �fdd�Z d d �Z e�d d �d d��Z�ZS)�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. rcsXt�j||�xDdd�t|�D�D].}t||���fdd�}t||t�||��q"WdS)NcSsg|]}|�d�r|�qS)Ztest)r�)r�rvr6r6r7r��sz0LimitedUserTestCase.__init__.<locals>.<listcomp>cs&y ��Wntjk r YnXdS)N)r/rT)r2)�methr6r7�test_�s z+LimitedUserTestCase.__init__.<locals>.test_)r�__init__r�r��setattr�types� MethodType)r2r3r4�attrr�)� __class__)r�r7r��s   zLimitedUserTestCase.__init__cs"t���t�d�t�d�dS)Ni�)r�setUpr;�setegid�seteuid)r2)r�r6r7r��s  zLimitedUserTestCase.setUpcs&t�|j�t�|j�t���dS)N)r;r�� PROCESS_UIDr�� PROCESS_GIDr�tearDown)r2)r�r6r7r��s  zLimitedUserTestCase.tearDowncCs8yt���d�Wntjk r(Yn X|�d�dS)Nrizexception not raised)r/r0rrTrn)r2r6r6r7r�s zLimitedUserTestCase.test_nicerXzcauses problem as rootcCsdS)Nr6)r2r6r6r7r~�sz'LimitedUserTestCase.test_zombie_process)r�r�r�r�rr;rr�r r�r�r�r�rr+r�r~� __classcell__r6r6)r�r7r��s    r�c@s4eZdZdZedd��Zdd�Zdd�Zdd �Zd S) � TestPopenzTests for psutil.Popen class.cCs t�dS)N)r%)�clsr6r6r7� tearDownClass�szTestPopen.tearDownClassc Cs�tddg}tj|tjtjd��B}|��|��|j|�t |��|� t t |d�|� �WdQRXtr�|�|�d�tj �n|�|�d�tj�dS)Nz-czimport time; time.sleep(60);)�stdoutrWrQrY)r$r/�PopenrZr[rjr|r�Z assertTruer�r=r>r�rIr r:rBrCrD)r2r\�procr6r6r7� test_misc�s   zTestPopen.test_miscc Csftjtdgtjtjtjd��}|��WdQRX|jjs<t�|j jsHt�|j jsTt�|� |j d�dS)Nz-V)r�rWr�r) r/r�r$rZr[r�r��closedryrWr�r:r�)r2r�r6r6r7�test_ctx_manager�s     zTestPopen.test_ctx_managerc Cs�tddg}tj|tjtjd���}|��|��|�tj|j�|�tj|j �|�tj|j t j �t r�tjdkr�|�tj|j t j�|�tj|j t j�WdQRXdS)Nz-czimport time; time.sleep(60);)r�rW)r�r�)r$r/r�rZr[rIrBr=rSrArKrCrDr r�r�Z CTRL_C_EVENTZCTRL_BREAK_EVENT)r2r\r�r6r6r7�test_kill_terminate�s      zTestPopen.test_kill_terminateN) r�r�r�r�� classmethodr�r�r�r�r6r6r6r7r��s   r��__main__)� run_from_name)Er�r\rRrr1r;rCrEr�rZr�r�rmr�r/rrrrrrrr r r Zpsutil._commonr Zpsutil._compatr rrrZ psutil.testsrrrrrrrrrrrrrrrr r!r"r#r$r%r&r'r(r)r*r+r,r-rr�r�r�Zpsutil.tests.runnerr��__file__r6r6r6r7�<module>s�                                           [48