£Á°èZ¨Ä…–K§‚«“ô4“ÒÙ´dîfUÙÃÅ WKbyÊ¦•êŽ…È®FÒ¿ÊÎóCozá¬S@6{Í:›œêZÌ:Š•_%:¢¾¾~;‘Ã~èŠ©ÊÇí`ÔÑ©úë™µ'5I¿fš×WO%ø9¾«¾DK|€ùÍD”Ýs]nHÕ¶ê×Ó¼ãžªéUWŸÈË%DÒÕ¬ï‘]/Åcx  ‰ï2ß]ä6G[]S£ÔÏ¯rs{úëóµmÒï#UQxo·õÞCe]"±/aÙ&Eã4ú9Jé_ÞåëdãöKë)AÞ                  ¯¹ægƒÛowÐø^d™ý½ßB7áyMä9ÜÖUã
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
<html>
B
    =_7                 @   s  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ 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- ddl m.Z. dd l m/Z/ dd!l m0Z0 dd"l m1Z1 dd#l m2Z2 dd$l m3Z3 dd%l m4Z4 dd&l m5Z5 dd'l m6Z6 dd(l m7Z7 dd)l m8Z8 dd*l m9Z9 dd+l m:Z: dd,l m;Z; dd-l m<Z< G d.d/ d/e2Z=ere> dkrG d0d1 d1e=Z?G d2d3 d3e2Z@eAd4krdd5lBmCZC eCeD 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  e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	
e ddd Zdd Zdd 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	
e9dUdVdW Z:dXdY Z;e	
e9dZd[d\ Z<e	
e=d]e	
e>d^e	
e9dUd_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	
eKdue	
eLdvdwdx ZMe	
eKdue	
eLdvdydz ZNe	
e d&d{d| ZOeed)e	
ePpeQd}d~d ZRdd ZSdd ZTdd ZUdd ZVe, dd ZWdd ZXdd ZYdd ZZdd Z[dd Z\dd Z]dd Z^dd Z_dd Z`dd Zadd Zbe	
e d&dd Zce	
e d&dd Zde	
e d&dd Zedd Zfe	
eg d!dd Zhe	
eg d!e	
e d&dd ZidS )TestProcesszTests for psutil.Process class.c             O   s   | j ||}t|jS )N)spawn_testprocpsutilProcesspid)selfargskwargssproc r6   L/opt/alt/python37/lib64/python3.7/site-packages/psutil/tests/test_process.pyspawn_psprocN   s    zTestProcess.spawn_psprocc          	   C   s:   t  }| |jt  | t d|_W d Q R X d S )N!   )r/   r0   assertEqualr1   osgetpidassertRaisesAttributeError)r2   pr6   r6   r7   test_pidT   s    zTestProcess.test_pidc             C   sJ   |   }|  | }tr,| |tj n| |tj  | | d S )N)	r8   killwaitr   r:   signalSIGTERMSIGKILLassertProcessGone)r2   r?   coder6   r6   r7   	test_killZ   s    zTestProcess.test_killc             C   sJ   |   }|  | }tr,| |tj n| |tj  | | d S )N)r8   	terminaterB   r   r:   rC   rD   rF   )r2   r?   rG   r6   r6   r7   test_terminated   s    zTestProcess.test_terminatec             C   sX   t r
tjntj}|  }|| | }tr<| || n| ||  | 	| d S )N)
r	   rC   rE   rD   r8   send_signalrB   r   r:   rF   )r2   sigr?   rG   r6   r6   r7   test_send_signaln   s    
zTestProcess.test_send_signalz	not POSIXc          	   C   s   t j}|  }tjdttjdd | t	j
|j| W d Q R X |  }tjdttjdd | t	j|j| W d Q R X d S )Nzpsutil.os.kill )side_effect)rC   rD   r8   r    patchOSErrorerrnoZESRCHr=   r/   NoSuchProcessrK   EPERMAccessDenied)r2   rL   r?   r6   r6   r7   test_send_signal_mockedy   s    z#TestProcess.test_send_signal_mockedc             C   s   t ddg}| |}| }| |d | | t ddg}| j|tjd}| }| |d | | t ddg}| |}| }| |d | | t dd	g}| |}| }| |d | | d S )
Nz-cpassr   z1 / 0)stderr   zimport sys; sys.exit(5);   zimport os; os._exit(5);)r$   r8   rB   r:   rF   
subprocessPIPE)r2   cmdr?   rG   r6   r6   r7   test_wait_exited   s(    









zTestProcess.test_wait_exitedc             C   s   |   }tr|tj | jtj|jdd |tj	 | jtj|jdd |tj
 | | tj
  | | tj
  nd|  | jtj|jdd |  | jtj|jdd |  | | tj
 | | tj
 d S )NgMbP?)timeout)r8   r	   rK   rC   SIGSTOPr=   r/   TimeoutExpiredrB   SIGCONTrD   r:   suspendresumerI   )r2   r?   r6   r6   r7   test_wait_stopped   s     zTestProcess.test_wait_stoppedc             C   s   |   \}}| tj|jd | tj|jd |  |  | }| }trr| |tj	  | |d  n| |tj	 | |tj	 d S )Ng{Gz?)
spawn_children_pairr=   r/   ra   rB   rI   r	   r:   rC   rD   )r2   child
grandchildZ	child_retZgrandchild_retr6   r6   r7   test_wait_non_children   s    z"TestProcess.test_wait_non_childrenc             C   sH   |   }|  | tj|jd | tj|jd | t|jd d S )Ng{Gz?r   )r8   namer=   r/   ra   rB   
ValueError)r2   r?   r6   r6   r7   test_wait_timeout   s
    zTestProcess.test_wait_timeoutc             C   s   |   }| tj|jd |  t t }xBt |k rfy|d}P W q0 tjk
rb   Y q0X q0W | dt	r| 
|tj  n| 
|tj | | d S )Nr   r_   )r8   r=   r/   ra   rB   rA   timer   failr	   r:   rC   rE   rD   rF   )r2   r?   Zstop_atrG   r6   r6   r7   test_wait_timeout_nonblocking   s    


z)TestProcess.test_wait_timeout_nonblockingc          	   C   s|   t  }|jdd |jdd x4tdD ](}|jd d}| |t | |d q*W | t |jdd W d Q R X d S )NgMbP?)intervald   g        rj   )	r/   r0   cpu_percentrangeassertIsInstancefloatassertGreaterEqualr=   rl   )r2   r?   xpercentr6   r6   r7   test_cpu_percent   s    zTestProcess.test_cpu_percentc          	   C   s4   t jdd d}t   |js&tW d Q R X d S )Nzpsutil.cpu_count)Zreturn_value)r    rP   r/   r0   rs   calledAssertionError)r2   mr6   r6   r7   test_cpu_percent_numcpus_none   s    z)TestProcess.test_cpu_percent_numcpus_nonec          	   C   s   t   }|jdks(|jdks(t||jdks:t||jdksLt|trb|j	dksbt|x&|j
D ]}tdtt|| qjW d S )Ng        z%H:%M:%S)r/   r0   	cpu_timesusersystemr|   children_userchildren_systemr   Ziowait_fieldsrn   strftime	localtimegetattr)r2   timesrk   r6   r6   r7   test_cpu_times   s    zTestProcess.test_cpu_timesc             C   s   t   d d \}}t d d \}}t||gt||g dkrZ| d||f  t||gt||g dkr| d||f  d S )N   g?zexpected: %s, found: %s)r/   r0   r   r;   r   maxminro   )r2   	user_timeZkernel_timeutimeZktimer6   r6   r7   test_cpu_times_2  s    zTestProcess.test_cpu_times_2znot supportedc             C   sP   t  }| }| |d t  dkr4| |d | | tt   d S )Nr   rY   )r/   r0   Zcpu_numrw   	cpu_countr:   assertInrt   )r2   r?   numr6   r6   r7   test_cpu_num  s    zTestProcess.test_cpu_numc             C   sZ   |   }t }| }t|| }|dkr@| d|||f  tdt|  d S )Nr   z'expected: %s, found: %s, difference: %sz%Y %m %d %H:%M:%S)r8   rn   create_timeabsro   r   r   )r2   r?   nowr   
differencer6   r6   r7   test_create_time  s    zTestProcess.test_create_timez
POSIX onlyc             C   s4   t   }|d k	r0tjtd}| || d S )Ntty)r/   r0   terminalr;   pathrealpathr'   r:   )r2   r   r   r6   r6   r7   test_terminal'  s    zTestProcess.test_terminal)Zonly_ifc          	   C   s  t  }| }ttd}|  W d Q R X | }tsts| |j	|j	 | 
|j|j tr| |j|j | 
|j|j n | |j|j | |j|j | }t|  d.}tr|tdd d n|dd  W d Q R X | }| |j|j | |j|j | |j	|j	 | |j|j tr\| |j|j | |j|j xJtt|D ]:}tr|dkrqj| || d | || d qjW d S )Nrbwbrx   i@B asciir   r   )r/   r0   Zio_countersopenr$   readr   r   assertGreater
read_countr:   write_countr   Z
read_charsZwrite_charsrw   
read_byteswrite_bytes
get_testfnr   writebytesrt   len)r2   r?   Zio1fZio2ir6   r6   r7   test_io_counters.  s>    zTestProcess.test_io_countersz
linux onlyc          
   C   s  t  }ts"| | d t j | t jd | t jd | t jd | t jd | }z4|t j | t	| t jdf | 
t |jt jdd W d Q R X |t j | t	| t jdf |jt jdd | t	| t jdf | 
t |jt jdd W d Q R X y|jt jdd W n t jk
rT   Y nX | td|jt jd | td|jt jd | jtd	|jdd W d |\}}|t jkrd}||| X d S )
Nr   rY   r         )value   zioclass accepts no valuez$'ioclass' argument must be specified)r/   r0   r   r:   ioniceZIOPRIO_CLASS_NONEZIOPRIO_CLASS_RTZIOPRIO_CLASS_BEZIOPRIO_CLASS_IDLEtupler=   rl   rU   assertRaisesRegex)r2   r?   initioclassr   r6   r6   r7   test_ionice_linuxY  sH    zTestProcess.test_ionice_linuxz!not supported on this win versionc          	   C   s   t  }ts| | t j | }z|t j | | t j |t j | | t j y|t j W n t j	k
r   Y nX | | t j | j
td|jt jdd | 
td|jt jd  W d || X d S )Nz&value argument not accepted on WindowsrY   )r   zis not a valid priority)r/   r0   r   r:   r   ZIOPRIO_NORMALZIOPRIO_VERYLOWZ
IOPRIO_LOWZIOPRIO_HIGHrU   r   	TypeErrorrl   )r2   r?   r   r6   r6   r7   test_ionice_win  s*    zTestProcess.test_ionice_winc             C   s   dd l }tt }dd ttD }|s4t|x|D ]}tt|}| |d |t|kr| 	|t|| t
rxq:| 	|||| q:||}| 	t|d | |d d | |d d q:W d S )Nr   c             S   s   g | ]}| d r|qS )ZRLIMIT)
startswith).0rx   r6   r6   r7   
<listcomp>  s    z/TestProcess.test_rlimit_get.<locals>.<listcomp>r   rj   rY   )resourcer/   r0   r;   r<   dirr|   r   rw   r:   r#   rlimitZ	getrlimitr   )r2   r   r?   namesrk   r   retr6   r6   r7   test_rlimit_get  s     


zTestProcess.test_rlimit_getc          	   C   s   |   }|tjd | |tjd trX| td tj	dd W d Q R X | 
t |tjd W d Q R X d S )N)rZ   rZ   zcan't use prlimitr   )rZ   rZ   rZ   )r8   r   r/   ZRLIMIT_NOFILEr:   r   r   rl   Z_psplatformr0   r=   )r2   r?   r6   r6   r7   test_rlimit_set  s    zTestProcess.test_rlimit_setc             C   s   t  }|  }|t j\}}z|t jd|f t|d}|d W d Q R X | t&}t|d}|d W d Q R X W d Q R X | 	t
r|jjn|jd tj W d |t j||f | 	|t j||f X d S )Ni   r   s   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)	r/   r0   r   r   RLIM_INFINITYr   r   r   r:   )r2   r?   r   r   r   r6   r6   r7   test_rlimit_infinity  s    z TestProcess.test_rlimit_infinityc             C   s<   t  }|t j\}}| t j| |t j||f d S )N)r/   r0   r   r   r:   r   )r2   r?   r   r   r6   r6   r7   test_rlimit_infinity_value  s    z&TestProcess.test_rlimit_infinity_valuec          	   C   sr   t  }tr<y| }W qD t jk
r8   tdY qDX n| }t  | }| ||d  W d Q R X d S )Nz$on OpenBSD this requires root accessrY   )	r/   r0   r   num_threadsrU   r+   SkipTestr*   r:   )r2   r?   step1step2r6   r6   r7   test_num_threads  s    zTestProcess.test_num_threadszWINDOWS onlyc             C   s   t  }| | d d S )Nr   )r/   r0   r   Znum_handles)r2   r?   r6   r6   r7   test_num_handles  s    zTestProcess.test_num_handlesc          	   C   s   t  }tr<y| }W qD t jk
r8   tdY qDX n| }t d | }| t	|t	|d  |d }| |j
|d  | |j|d  | |j|d  W d Q R X d S )Nz$on OpenBSD this requires root accessrY   r   r   )r/   r0   r   threadsrU   r+   r   r*   r:   r   idr   system_time)r2   r?   r   r   Zathreadr6   r6   r7   test_threads  s    zTestProcess.test_threadsc             C   s   |   }tr:y|  W n  tjk
r8   tdY nX | j| j	t
dd | D dd | j| jt
dd | D dd d S )Nz$on OpenBSD this requires root accessc             S   s   g | ]
}|j qS r6   )r   )r   rx   r6   r6   r7   r   -  s    z.TestProcess.test_threads_2.<locals>.<listcomp>g?)deltac             S   s   g | ]
}|j qS r6   )r   )r   rx   r6   r6   r7   r   0  s    )r8   r   r   r/   rU   r+   r   ZassertAlmostEqualr   r   sumr   )r2   r?   r6   r6   r7   test_threads_2   s    zTestProcess.test_threads_2c             C   s   t  }| d d \}}| }| |d | |d d gd }| d d \}}| }| || | || | || ~tr| }	| |	j|	j	 | |	j
|	j | }	x |	jD ]}
| t|	|
d qW d S )Nr   r   i` )r/   r0   Zmemory_infomemory_percentr   rw   r   r:   ZrssZwsetvmsZpagefiler   r   )r2   r?   Zrss1Zvms1Zpercent1ZmemarrZrss2Zvms2Zpercent2memrk   r6   r6   r7   test_memory_info2  s&    
zTestProcess.test_memory_infoc             C   s   t  }t  j}| }xT|jD ]J}t||}| j|d||fd |dkrPts"t	rVq"| j
|||||fd q"W t	s|ts|tr| |jd t	r| |jd | |jd d S )Nr   )msgr   )r/   r0   Zvirtual_memorytotalZmemory_full_infor   r   rw   r   r   ZassertLessEqualr   r   ussZpssZswap)r2   r?   r   r   rk   r   r6   r6   r7   test_memory_full_infoQ  s    

z!TestProcess.test_memory_full_infoc             C   s  t  }| }dd |D }| t|tt| |jdd} x|D ]}|jdsJtj	|jsrt
|jtry*tj|jstj|jst
|jW nH t
k
r   ts n,td}| }W d Q R X d|j |kr Y nX qJdtj|jkrJyt|j}W n tk
r$   Y qJX t|jsJt
|jqJW xv|D ]n}xf|jD ]\}	t||	}
|	d	krpqRn<|	d
kr|
st
|
n"| |
ttf |
dksRt
|
qRW qFW d S )Nc             S   s   g | ]}|qS r6   r6   )r   rx   r6   r6   r7   r   e  s    z0TestProcess.test_memory_maps.<locals>.<listcomp>F)Zgrouped[z/proc/self/smapsz%s (deleted)Z64r   )addrZpermsr   )r/   r0   memory_mapsr:   r   setr   r   r;   isabsr|   r	   existsislinkr   r   r   basenamestatr   S_ISREGst_moder   r   ru   intr   )r2   r?   mapspathsZext_mapsntr   datastfnamer   r6   r6   r7   test_memory_mapsa  sD    





zTestProcess.test_memory_mapsc          	      sL   t  }t 4}dd   fdd| D }|  || W d Q R X d S )Nc             S   s   t jt j| S )N)r;   r   r   normcase)r?   r6   r6   r7   normpath  s    z8TestProcess.test_memory_maps_lists_lib.<locals>.normpathc                s   g | ]} |j qS r6   )r   )r   rx   )r   r6   r7   r     s   z:TestProcess.test_memory_maps_lists_lib.<locals>.<listcomp>)r/   r0   r   r   r   )r2   r?   r   Zlibpathsr6   )r   r7   test_memory_maps_lists_lib  s    
z&TestProcess.test_memory_maps_lists_libc             C   s>   t  }|  | jt|jdd ts.ts.tr:|jdd d S )Nz?!?)Zmemtyper   )r/   r0   r   r=   rl   r   r   r   )r2   r?   r6   r6   r7   test_memory_percent  s
    zTestProcess.test_memory_percentc             C   sL   |   }| st| s t|  |  | r<t| rHtd S )N)r8   
is_runningr|   rA   rB   )r2   r?   r6   r6   r7   test_is_running  s    zTestProcess.test_is_runningc             C   s   |   }| }y| |t W n tk
r   trbt|ttkrbtjj	}| |||t nNdt
jd t
jd f }y | ||dt|d W n tk
r   Y nX Y nX t|ddg}| |d d S )Nz%s.%sr   rY   rN   z-czimport os; print('hey')Zhey)r8   exer:   r$   r|   r   r   r;   r   r   sysversion_inforeplacer'   )r2   r?   r   r   veroutr6   r6   r7   test_exe  s     zTestProcess.test_exec             C   sV   t ddg}| |}ts ts tr6| | d t  n| d| d| d S )Nz-czimport time; time.sleep(60)r    )r$   r8   r   r   r   r:   cmdlinejoin)r2   r  r?   r6   r6   r7   test_cmdline  s
    

zTestProcess.test_cmdlinezbroken on PYPYc             C   s>   |   }t| |gdgd  }| |}| | | d S )N
0123456789   )r   r   r8   r:   r  )r2   r   r  r?   r6   r6   r7   test_long_cmdline  s
    
zTestProcess.test_long_cmdlinec             C   sJ   |  t}|  }tjtjtj	 }|
|sFt||fd S )N)r8   r$   rk   lowerr;   r   r   r   r   
executabler   r|   )r2   r?   rk   Zpyexer6   r6   r7   	test_name  s    
zTestProcess.test_namezunreliable on PYPYc             C   s:   | j dd}t| | |}| | tj| d S )NZ01234567890123456789)suffix)r   r   r8   r:   rk   r;   r   r   )r2   r   r?   r6   r6   r7   test_long_name  s    
zTestProcess.test_long_namezbroken on SUNOSzbroken on AIXc             C   s|   | j dd}t| |ddddddg}| |}| | | | | tj| | tj	|
 tj	| d S )Nz	foo bar ))r  z-cz9import time; [time.sleep(0.01) for x in range(3000)];arg1Zarg2rN   Zarg3)r   r   r8   r:   r  rk   r;   r   r   r   r   )r2   Z
funky_pathr  r?   r6   r6   r7   test_prog_w_funky_name  s    
z"TestProcess.test_prog_w_funky_namec             C   sX   t  }| \}}}| |t  | |t  ttdrT| t |  d S )N	getresuid)	r/   r0   uidsr:   r;   getuidgeteuidhasattrr  )r2   r?   real	effectivesavedr6   r6   r7   	test_uids  s    
zTestProcess.test_uidsc             C   sX   t  }| \}}}| |t  | |t  ttdrT| t |  d S )Nr  )	r/   r0   gidsr:   r;   getgidgetegidr  	getresgid)r2   r?   r  r  r  r6   r6   r7   	test_gids	  s    
zTestProcess.test_gidsc             C   sz  t  }| t|jd | }z(trxrt jt jt jt j	t j
t jgD ]R}| j|d< y|| W n t jk
r|   Y nX | | | W d Q R X qFW nyttdr| ttjt |  |d | | d ttdr| ttjt |  ts,|d | | d W n t jk
rF   Y nX W d y|| W n t jk
rr   Y nX X d S )Nstr)priogetpriorityrY   r   )r/   r0   r=   r   nicer   ZNORMAL_PRIORITY_CLASSZIDLE_PRIORITY_CLASSZBELOW_NORMAL_PRIORITY_CLASSZREALTIME_PRIORITY_CLASSZHIGH_PRIORITY_CLASSZABOVE_NORMAL_PRIORITY_CLASSZsubTestrU   r:   r  r;   r"  PRIO_PROCESSr<   r   )r2   r?   r   r!  r6   r6   r7   	test_nice  sJ     





zTestProcess.test_nicec             C   s   t  }| | t j d S )N)r/   r0   r:   statusSTATUS_RUNNING)r2   r?   r6   r6   r7   test_statusC  s    zTestProcess.test_statusc             C   sd   |   }| }trP|d\}}| |t  dtjkr`| |tjd  n| |t  d S )N\Z
USERDOMAIN)	r8   usernamer   splitr:   getpassZgetuserr;   environ)r2   r?   r*  domainr6   r6   r7   test_usernameG  s    
zTestProcess.test_usernamec             C   s    |   }| | t  d S )N)r8   r:   cwdr;   getcwd)r2   r?   r6   r6   r7   test_cwdR  s    zTestProcess.test_cwdc             C   s$   t ddg}| |}t|jd d S )Nz-cz/import os, time; os.chdir('..'); time.sleep(60)z#ret == os.path.dirname(os.getcwd()))r$   r8   r   r0  )r2   r]   r?   r6   r6   r7   
test_cwd_2V  s    
zTestProcess.test_cwd_2c             C   s  t  }| }|st|| |j| ttdrL| |tt	|j
 | t|tt| tttt jdd}xr|D ]j}||g | | |g ttdr| | tt	|j
 t|dr| | d |  qW |g  tr| | |j  n| | | ttdrP| | tt	|j
 | t|jd || |t| |t| d S )Nsched_getaffinityT)percpunum_cpur   rY   )r/   r0   cpu_affinityr|   
addCleanupr  r;   r:   listr4  r1   r   r   rt   rs   r6  r   _procZ_get_eligible_cpusr=   r   r   )r2   r?   initialZall_cpusnr6   r6   r7   test_cpu_affinity\  s6    







zTestProcess.test_cpu_affinityc             C   sp   |   }ttjddd g}| t|j| | t|jtdd | t|jddg | t|jddg d S )	NT)r5  
   i'  i*  r   1rj   )	r8   r   r/   r   r=   rl   r7  rt   r   )r2   r?   Zinvalid_cpur6   r6   r7   test_cpu_affinity_errs  s    z"TestProcess.test_cpu_affinity_errsc             C   s   t  }| }|st|| |j| t|dkrB|d d }g }xBtdt|d D ],}x&t||D ]}|rl|	t
| qlW qZW x.|D ]&}|| | t| t| qW d S )N   r   rY   )r/   r0   r7  r|   r8  r   rt   	itertoolscombinationsappendr9  r:   sorted)r2   r?   r;  Zcombosr   Zsubsetcombor6   r6   r7   "test_cpu_affinity_all_combinations  s    

z.TestProcess.test_cpu_affinity_all_combinationszbroken on BSDzunreliable on APPVEYORc       	   	   C   s`  t  }|  }| }| || t|dx}|d |  t|jdt	| }dd |D }| 
tj|| trx$|D ]}|j|kr| |jd qW W d Q R X x"|D ]}tj|jst|qW d| }| td|g}xLtd	D ],}d
d | D }||krP td qW | 
tj|| x$|D ]}tj|s<t|q<W d S )Nr   s   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxzlen(ret) != %ic             S   s   g | ]}t j|jqS r6   )r;   r   r   )r   rx   r6   r6   r7   r     s    z/TestProcess.test_open_files.<locals>.<listcomp>i   z2import time; f = open(r'%s', 'r'); time.sleep(60);z-crr   c             S   s   g | ]}t j|jqS r6   )r;   r   r   )r   rx   r6   r6   r7   r     s    g{Gz?)r/   r0   r   
open_filesassertNotInr   r   flushr   r   r   r;   r   r   r   r:   positionisfiler|   r8   r$   rt   rn   sleep)	r2   r?   r   filesr   	filenamesfiler  rx   r6   r6   r7   test_open_files  s4    





zTestProcess.test_open_filesc          	   C   s   t  }tjj}|  }t|d}xJ| D ](}||j||jksT|j	|
 kr.P q.W | dt|   | ||j||j tr| |j	d n| |j	|
  | d }| |d |j | |d |j	 | |j|  W d Q R X d S )Nwzno file found; files=%srj   r   rY   )r/   r0   r;   r   r   r   r   rH  rk   fdfilenoro   reprr:   r   rI  )r2   r?   r   r   fileobjrP  Zntupler6   r6   r7   test_open_files_2  s"    zTestProcess.test_open_files_2c             C   s   t  }|  }| }t|d}| |j | | |d  t }| |j | | |d  |  |  | | | d S )NrR  rY   r   )	r/   r0   r   Znum_fdsr   r8  closer:   socket)r2   r?   r   startrP  sockr6   r6   r7   test_num_fds  s    
zTestProcess.test_num_fdsz not reliable on OPENBSD & NETBSDc             C   sL   t  }t| }x(tdD ]}t| }||krd S qW | d d S )Ni  z7num ctx switches still the same after 50.000 iterations)r/   r0   r   Znum_ctx_switchesrt   ro   )r2   r?   beforerx   afterr6   r6   r7   test_num_ctx_switches  s    z!TestProcess.test_num_ctx_switchesc             C   sN   t  }ttdr&| | t  |  }| | t  t	rJd S d S )Ngetppid)
r/   r0   r  r;   r:   ppidr`  r8   r<   r   )r2   r?   r6   r6   r7   	test_ppid  s    
zTestProcess.test_ppidc             C   sB   |   }| | jt  t d }| t	|  d S )Nr   )
r8   r:   parentr1   r;   r<   r/   pidsassertIsNoner0   )r2   r?   Z
lowest_pidr6   r6   r7   test_parent  s    zTestProcess.test_parentc             C   s8   t  }|  \}}| | | | | | d S )N)r/   r0   rf   r:   rc  )r2   rc  rg   rh   r6   r6   r7   test_parent_multi  s    zTestProcess.test_parent_multic          	   C   s<   |   }tjdtddd | |  W d Q R X d S )Nzpsutil.Processr   foo)rO   )r8   r    rP   r/   rS   re  rc  )r2   r?   r6   r6   r7   test_parent_disappeared  s    z#TestProcess.test_parent_disappearedc             C   s`   t  }| st|  \}}| | d | | | d | | | d | d S )Nr   rY   )r/   r0   parentsr|   rf   r:   )r2   rc  rg   rh   r6   r6   r7   test_parents  s    zTestProcess.test_parentsc             C   s   t  }| | g  | |jddg  | jdd}| }|jdd}xJ||fD ]>}| t|d | |d j|j | |d  |j qVW d S )NT)	recursiver   )creationflagsrY   )r/   r0   r:   childrenr8   r   r1   ra  )r2   rc  rg   Z	children1Z	children2rn  r6   r6   r7   test_children%  s    zTestProcess.test_childrenc             C   sf   t  }|  \}}| | |g | |jdd||g |  |  | |jddg  d S )NT)rl  )r/   r0   rf   r:   rn  rI   rB   )r2   rc  rg   rh   r6   r6   r7   test_children_recursive4  s    z#TestProcess.test_children_recursivec          	   C   s   t t}x@t D ]4}y||   d7  < W q tjk
rF   Y qX qW t| dd dd d }t	r~|dkr~| 
dt|}y|jdd	}W n tjk
r   Y nX | t|tt| d S )
NrY   c             S   s   | d S )NrY   r6   )rx   r6   r6   r7   <lambda>J      z6TestProcess.test_children_duplicates.<locals>.<lambda>)keyrj   r   zPID 0T)rl  )collectionsdefaultdictr   r/   process_iterra  ErrorrE  itemsr   ZskipTestr0   rn  rU   r:   r   r   )r2   tabler?   r1   cr6   r6   r7   test_children_duplicatesA  s    



z$TestProcess.test_children_duplicatesc             C   s|   t  }|  \}}|jdd}| t|d | |d | | |d | | }| |d | | |d | d S )NT)rl  r   r   rY   )r/   r0   rf   rn  r:   r   rj  )r2   rc  rg   rh   rn  rj  r6   r6   r7   test_parents_and_childrenU  s    z%TestProcess.test_parents_and_childrenc             C   sZ   |   }|  x*tdD ]}| tjkr.P td qW |  | 	| tj d S )Nrr   g{Gz?)
r8   rc   rt   r&  r/   STATUS_STOPPEDrn   rM  rd   assertNotEqual)r2   r?   rx   r6   r6   r7   test_suspend_resumeb  s    zTestProcess.test_suspend_resumec             C   s$   |  ttjd |  ttjd d S )Nr?  rj   )r=   r   r/   r0   rl   )r2   r6   r6   r7   test_invalid_pidl  s    zTestProcess.test_invalid_pidc          
   C   s  t  }|jddgd}| t| ddg t tt  }|jdgdd}t|d t	sp| |d d t
jddt jd	" | |jd
gddd
di W d Q R X t
jddt |jdd	 | jt j|jd
gd W d Q R X t
jddt |jdd	" | |jd
gddd
di W d Q R X t
jddtd	F | }| d
t	|  | t |jd
gd W d Q R X W d Q R X | t |d W d Q R X | t |dg W d Q R X | t |ddg W d Q R X d S )Nr   rk   )attrsZconnectionsrh  )r  Zad_valuezpsutil.Process.niceT)createrO   r#  rY   Zbar)r/   r0   as_dictr:   rE  keysr   rd  
isinstancer9  r    rP   rU   rS   r1   r=   ZombieProcessNotImplementedErrorrI  r   rl   )r2   r?   dr6   r6   r7   test_as_dictp  s>    ""
"zTestProcess.test_as_dictc          
   C   s   t  }td8}|  |  |  W d Q R X | |jd W d Q R X td}|  |  W d Q R X | |jd d S )Nz$psutil._psplatform.Process.cpu_timesrY   r   )r/   r0   r    rP   oneshotr   r:   
call_count)r2   r?   r}   r6   r6   r7   test_oneshot  s    
zTestProcess.test_oneshotc             C   s   t  }td}tdj}| : |  |  |  |  |  W d Q R X W d Q R X | |jd | |jd W d Q R X W d Q R X td}|  |  W d Q R X | |jd d S )Nz$psutil._psplatform.Process.cpu_timesz(psutil._psplatform.Process.oneshot_enterrY   r   )r/   r0   r    rP   r  r   r:   r  )r2   r?   m1m2r}   r6   r6   r7   test_oneshot_twice  s    

"zTestProcess.test_oneshot_twicec          	   C   s   |   \}}| }| }| || | & | | | | | | W d Q R X | & | | | | | | W d Q R X d S )N)rf   ra  r~  r  r:   )r2   p1p2Zp1_ppidZp2_ppidr6   r6   r7   test_oneshot_cache  s    

zTestProcess.test_oneshot_cachec                s    fdd}   }|  |  tr:ttjd|j   | t	|}x"|
|jD ]\}}||| qZW trtstjj} || |t d S )Nc                s   y
|  }W nR t jk
r"    Y n^ t jk
r6   Y nJ t jk
r\   trV|dkrVd S  Y n$X trn|dkrnd S  d| |f d S )N)r   r   )r   rk   z+%r didn't raise NSP and returned %r instead)r/   r  rS   rU   r   r   ro   )funZfun_namer   )r2   r6   r7   assert_raises_nsp  s    
zFTestProcess.test_halfway_terminated_process.<locals>.assert_raises_nspz%s not in ret)r8   rI   rB   r   r   r/   rd  r1   rF   r!   iterallr   r;   r   r   r:   r   r$   )r2   r  r?   nsr  rk   r   r6   )r2   r7   test_halfway_terminated_process  s    
z+TestProcess.test_halfway_terminated_processc             C   s   dd }|   \}}t|j}| | tj | s>t|	  t
|}x ||jD ]\}}|| q\W t|jst| |jt  | |jdd t D  i t_| |jdd t D  d S )Nc          	   S   s(   y|  S  t jt jfk
r"   Y nX d S )N)r/   r  rU   )r  r6   r6   r7   succeed_or_zombie_p_exc  s    z@TestProcess.test_zombie_process.<locals>.succeed_or_zombie_p_excc             S   s   g | ]
}|j qS r6   )r1   )r   rx   r6   r6   r7   r     s    z3TestProcess.test_zombie_process.<locals>.<listcomp>c             S   s   g | ]
}|j qS r6   )r1   )r   rx   r6   r6   r7   r     s    )Zspawn_zombier/   r0   r1   r:   r&  STATUS_ZOMBIEr   r|   r  r!   r  r  
pid_existsr   rd  rv  Z_pmap)r2   r  rc  zombieZzprocr  r  rk   r6   r6   r7   test_zombie_process  s    zTestProcess.test_zombie_processc          	   C   sB   t  }tjdt dd}| s*t|js4tW d Q R X d S )Nzpsutil.Processr   )rO   )r/   r0   r    rP   r  r   r|   r{   )r2   r?   r}   r6   r6   r7   $test_zombie_process_is_running_w_exc  s
    z0TestProcess.test_zombie_process_is_running_w_excc          	   C   sH   t  }tjdt dd"}| | t j |js:t	W d Q R X d S )Nz!psutil._psplatform.Process.statusr   )rO   )
r/   r0   r    rP   r  r:   r&  r  r{   r|   )r2   r?   r}   r6   r6   r7    test_zombie_process_status_w_exc'  s
    z,TestProcess.test_zombie_process_status_w_excc          	   C   s  dt  krF| t jt jd t dr,t| t d d d S t d}t	rZt j
nt}| ||j | ||j | ||j | ||j | ||j | ||jtj t|}x||j|j D ]\}}y
| }W n t j
k
r   Y qX |dkr| |jd q|dkr@t	r*dnd}| | | q|dkr|st|qW ts~| dt   t ds~td S )Nr   rY   )r  r  r*  zNT AUTHORITY\SYSTEMrootrk   )r/   rd  r=   rS   r0   r  r|   r:   ra  r   rU   rl   rB   rI   rc   rd   rA   rK   rC   rD   r!   r  gettersZsettersr  r*  r   r   )r2   r?   r   r  r  rk   r   r   r6   r6   r7   
test_pid_01  s:    



zTestProcess.test_pid_0c             C   sH   dd }d | _ t }|| }|tj }tsDtrD| || d S )Nc             S   sb   |  dd  |  dd  |  dd  trL|  dd  |  dd  |  dd  tdd |  D S )	NZPSUTIL_TESTINGPLATHOMEZ__CF_USER_TEXT_ENCODINGZVERSIONER_PYTHON_PREFER_32_BITZVERSIONER_PYTHON_VERSIONc             S   s8   g | ]0\}}| d d dd| d d ddfqS )rN   
)r  )r   kvr6   r6   r7   r   d  s   z@TestProcess.test_environ.<locals>.clean_dict.<locals>.<listcomp>)popr   dictrx  )r  r6   r6   r7   
clean_dictZ  s    z,TestProcess.test_environ.<locals>.clean_dict)	ZmaxDiffr/   r0   r-  r;   copyr   r   r:   )r2   r  r?   Zd1Zd2r6   r6   r7   test_environX  s    zTestProcess.test_environc             C   s   t d}|  }t||d | j|gtjtjd}t|j	}t
|j	 | sVt| |j d trtry| }W q tjk
r   d S X n| }| |ddd |  | |jd d S )	Na  
            #include <unistd.h>
            #include <fcntl.h>
            char * const argv[] = {"cat", 0};
            char * const envp[] = {"A=1", "X", "C=3", 0};
            int main(void) {
                /* Close stderr on exec so parent can wait for the execve to
                 * finish. */
                if (fcntl(2, F_SETFD, FD_CLOEXEC) != 0)
                    return 0;
                return execve("/bin/cat", argv, envp);
            }
            )Zc_code)stdinrX   rr  r?  3)ACr   )textwrapdedentr   r   r.   r[   r\   r/   r0   r1   r,   r   r|   r:   rX   r   r   r   r-  rU   communicate
returncode)r2   rG   r   r5   r?   envr6   r6   r7   test_weird_environo  s&    
zTestProcess.test_weird_environN)j__name__
__module____qualname____doc__r8   r@   rH   rJ   rM   r+   skipIfr	   rV   r^   re   ri   rm   rp   rz   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   r   r   r   r   r   r   r  r  r#   r  r  r  r
   r   r  r  r  r%  r(  r/  r2  r3  r   r=  r@  rG  r   r   rQ  rW  r\  r   r   r_  rb  rf  rg  ri  rk  ro  rp  r{  r|  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r6   r6   r6   r7   r-   K   s   

	*+.	

,(	
#

	
.*%

'r-   c                   sn   e Zd ZdZeedr&e Ze Z	 fddZ
 fddZ fddZd	d
 Zedddd Z  ZS )LimitedUserTestCasezRepeat the previous tests by using a limited user.
        Executed only on UNIX and only if the user who run the test script
        is root.
        r  c                sX   t  j|| xDdd t| D D ].}t| |  fdd}t| |t||  q"W d S )Nc             S   s   g | ]}| d r|qS )test)r   )r   rx   r6   r6   r7   r     s    z0LimitedUserTestCase.__init__.<locals>.<listcomp>c                s&   y
   W n t jk
r    Y nX d S )N)r/   rU   )r2   )methr6   r7   test_  s    
z+LimitedUserTestCase.__init__.<locals>.test_)r   __init__r   r   setattrtypes
MethodType)r2   r3   r4   attrr  )	__class__)r  r7   r    s
    
zLimitedUserTestCase.__init__c                s"   t    td td d S )Ni  )r   setUpr;   setegidseteuid)r2   )r  r6   r7   r    s    

zLimitedUserTestCase.setUpc                s&   t | j t | j t   d S )N)r;   r  PROCESS_UIDr  PROCESS_GIDr   tearDown)r2   )r  r6   r7   r    s    zLimitedUserTestCase.tearDownc             C   s8   yt  d W n t jk
r(   Y nX | d d S )Nrj   zexception not raised)r/   r0   r#  rU   ro   )r2   r6   r6   r7   r%    s
    zLimitedUserTestCase.test_nicerY   zcauses problem as rootc             C   s   d S )Nr6   )r2   r6   r6   r7   r    s    z'LimitedUserTestCase.test_zombie_process)r  r  r  r  r  r;   r  r  r  r  r  r  r  r%  r+   r  r  __classcell__r6   r6   )r  r7   r    s   
r  c               @   s4   e Zd ZdZedd Zdd Zdd Zdd	 Zd
S )	TestPopenzTests for psutil.Popen class.c             C   s
   t   d S )N)r%   )clsr6   r6   r7   tearDownClass  s    zTestPopen.tearDownClassc          	   C   s   t ddg}tj|tjtjdB}|  |  |j | t	| | 
tt|d |  W d Q R X tr| |dtj  n| |dtj d S )Nz-czimport time; time.sleep(60);)stdoutrX   rh  rZ   )r$   r/   Popenr[   r\   rk   r   r  Z
assertTruer   r=   r>   r   rI   r	   r:   rB   rC   rD   )r2   r]   procr6   r6   r7   	test_misc  s    

zTestPopen.test_miscc          	   C   sf   t jtdgtjtjtjd}|  W d Q R X |jjs<t|j	jsHt|j
jsTt| |jd d S )Nz-V)r  rX   r  r   )r/   r  r$   r[   r\   r  r  closedr|   rX   r  r:   r  )r2   r  r6   r6   r7   test_ctx_manager  s    
zTestPopen.test_ctx_managerc          	   C   s   t ddg}tj|tjtjd}|  |  | tj|j | tj|j	 | tj|j
tj trtjdkr| tj|j
tj | tj|j
tj W d Q R X d S )Nz-czimport time; time.sleep(60);)r  rX   )r   r   )r$   r/   r  r[   r\   rI   rB   r=   rS   rA   rK   rC   rD   r   r   r   ZCTRL_C_EVENTZCTRL_BREAK_EVENT)r2   r]   r  r6   r6   r7   test_kill_terminate  s    

zTestPopen.test_kill_terminateN)	r  r  r  r  classmethodr  r  r  r  r6   r6   r6   r7   r    s
   r  __main__)run_from_name)Er  rt  rR   r,  rB  r;   rC   rY  r   r[   r   r  rn   r  r/   r   r   r   r   r   r   r   r	   r
   r   Zpsutil._commonr   Zpsutil._compatr   r   r   r   Zpsutil.testsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r  r  r  r  Zpsutil.tests.runnerr  __file__r6   r6   r6   r7   <module>   s             [48
