U 1gn@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlmZddlmZddlmZddlmZddlmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlm Z ddlm!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"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"mZ>dd/l"m?Z?dd0l"m@Z@dd1l"mAZAdd2l"mBZBGd3d4d4e4ZCereDdkrGd5d6d6eCZEGd7d8d8e4ZFdS)9Tests for psutil.Process class.N)AIX)BSD)LINUX)MACOS)NETBSD)OPENBSD)OSX)POSIX)SUNOS)WINDOWS) open_text)PY3)FileNotFoundError)long)redirect_stderr)super)APPVEYOR) 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) QEMU_USER)PsutilTestCase) ThreadTask) call_until)copyload_shared_lib) create_c_exe) create_py_exe)mock)process_namespace)pytest reap_children)retry_on_failure)sh)skip_on_access_denied)skip_on_not_implemented) wait_for_pidc@seZdZdZddZddZddZdd Zd d Ze j j e d d ddZ ddZe j j edd ddZddZddZddZddZddZe j j edd d d!Ze j j edd d"d#Ze j j e d$d d%d&Zd'd(Ze j j e d)d d*d+Ze j j e d$d eed,d-d.Z e j j e! d$d e j j e d/d d0d1Z"e j j e! d$d e j j e# d2d d3d4Z$e j j e% d$d d5d6Z&e j j e% d$d d7d8Z'e j j e% d$d d9d:Z(e j j e% d$d d;d<Z)e j j e% d$d d=d>Z*d?d@Z+e j j e# dAd dBdCZ,e j j e- d$d dDdEZ.e/e0e1d,e j j e- d$d dFdGZ2e/dHdIZ3dJdKZ4e j j e5 d$d dLdMZ6e j j e5 d$d dNdOZ7dPdQZ8dRdSZ9e j j edd dTdUZ:dVdWZ;e j j ee j j e<ped]d e j j ed^d d_d`Z?e j j e@dad e j j eAdbd e j j e||jYnXdSN)spawn_testprocpsutilProcesspid NoSuchProcessZ assertPidGone)selfargskwargssprocr?I/opt/hc_python/lib64/python3.8/site-packages/psutil/tests/test_process.py spawn_psprocSs   zTestProcess.spawn_psprocc Cs:t}|jtksttt d|_W5QRXdS)N!) r7r8r9osgetpidAssertionErrorr,raisesAttributeErrorr;pr?r?r@test_pid]s zTestProcess.test_pidcCsJ|}||}tr,|tjkstjk rrYq>Xq>| dt r|t j kst n|t jkst ||dS)Nrrn)rAr,rFr7rprLrKtimerfailr rMrOrErNrP)r;rIZstop_atrRr?r?r@test_wait_timeout_nonblockings     z)TestProcess.test_wait_timeout_nonblockingc Cszt}|jdd|jddtdD]*}|jdd}t|tsFt|dks(tq(tt |jddW5QRXdS)Nrm)intervaldrz) r7r8 cpu_percentrange isinstancefloatrEr,rFr|)r;rI_percentr?r?r@test_cpu_percents     zTestProcess.test_cpu_percentc Cs4tjddd}t|js&tW5QRXdS)Nzpsutil.cpu_count)Z return_value)r*r^r7r8rcalledrE)r;mr?r?r@test_cpu_percent_numcpus_nones z)TestProcess.test_cpu_percent_numcpus_nonezQEMU user not supportedc Cst}|jdkst||jdks0t||jdksBt||jdksTt|trj|j dksjt||j D]}t dt t||qpdS)Nrz%H:%M:%S)r7r8 cpu_timesuserrEsystem children_userchildren_systemrZiowait_fieldsr~strftime localtimegetattr)r;timesr{r?r?r@test_cpu_timess  zTestProcess.test_cpu_timescCstdd\}}tdd\}}t||gt||gdkrZ|d||ft||gt||gdkr|d||fdS)N皙?zexpected: %s, found: %s)r7r8rrCrmaxminr)r; user_timeZ kernel_timeutimeZktimer?r?r@test_cpu_times_2s zTestProcess.test_cpu_times_2z not supportedcCsPt}|}|dksttdkr4|dks4t|ttksLtdSNrrg)r7r8Zcpu_numrE cpu_countr)r;rInumr?r?r@ test_cpu_num)s    zTestProcess.test_cpu_numcCsZ|}t}|}t||}|dkr@|d|||ftdt|dS)Nrz'expected: %s, found: %s, difference: %sz%Y %m %d %H:%M:%S)rAr~ create_timeabsrrr)r;rInowr differencer?r?r@test_create_time2s zTestProcess.test_create_timez POSIX onlycCsXt}|dk rTztjtd}Wntk rFt dYnX||ksTt dS)Nttyzcan't rely on `tty` CLI) r7r8terminalrCpathrealpathr0 RuntimeErrorr,skiprE)r;rrr?r?r@ test_terminalDs zTestProcess.test_terminal)Zonly_ifc Cst}|}ttd}|W5QRX|}tsts|j|jksNt |j |j ks^t t r|j |j ksrt |j |j kst n |j|jkst |j|jkst |}t|d.}tr|tdddn|ddW5QRX|}|j |j kst |j|jkst |j|jks,t |j|jks>t t rh|j |j ksVt |j |j ksht tt|D]>}tr|dkrqt||dkst ||dkstt qtdS)Nrbwbxi@Basciirr)r7r8Z io_countersopenr!readrrZ read_countrEZ write_countrZ read_charsZ write_chars read_bytes write_bytes get_testfnrwritebytesrlen)r;rIZio1fZio2ir?r?r@test_io_countersPs> zTestProcess.test_io_countersz linux onlyc sfdd}tts.dtjks.ttjdks.cleanuprrgrrzioclass accepts no valuematchz$'ioclass' argument must be specified)r7r8rrrrEZIOPRIO_CLASS_RTZIOPRIO_CLASS_BEZIOPRIO_CLASS_IDLE addCleanuptupler,rFr|rb)r;rrr?rr@test_ionice_linux{sB      zTestProcess.test_ionice_linuxz!not supported on this win versionc Cst}ts|tjkst|}||j||tj|tjksRt|tj|tjksptz|tj Wntj k rYnX|tj kstt j t dd|jtjddW5QRXt j tdd|tj dW5QRXdS)Nz&value argument not accepted on Windowsrrgrzis not a valid priority)r7r8rrZ IOPRIO_NORMALrErZIOPRIO_VERYLOWZ IOPRIO_LOWZ IOPRIO_HIGHrbr,rF TypeErrorr|)r;rIrr?r?r@test_ionice_wins*  zTestProcess.test_ionice_wincCsddl}tt}ddttD}|s4t||D]}tt|}|dksRt|t|kr|t||kspttrvq8| || |kstq8| |}t |dkst|ddkst|ddks8tq8dS)NrcSsg|]}|dr|qS)ZRLIMIT startswith.0rr?r?r@ s z/TestProcess.test_rlimit_get..rrzrg) resourcer7r8rCrDdirrErr rlimitZ getrlimitr)r;rrInamesr{rretr?r?r@test_rlimit_gets      zTestProcess.test_rlimit_getc Cs|}|tjd|tjdks*ttrZtjtddtj ddW5QRXtt|tjdW5QRXdS)N)rhrhzcan't use prlimitrr)rhrhrh) rArr7Z RLIMIT_NOFILErErr,rFr|Z _psplatformr8rHr?r?r@test_rlimit_sets zTestProcess.test_rlimit_setc Cst}|}|tj\}}z|tjd|ft|d}|dW5QRXt t &}t|d}|dW5QRXW5QRXt r|j j n|j dt jkstW5|tj||f|tj||fkstXdS)NrsXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX) r7r8rrrE RLIM_INFINITYrrr)r;rIrrrr?r?r@test_rlimit_infinitysz TestProcess.test_rlimit_infinitycCs<t}|tj\}}|tjks&t|tj||fdSr5)r7r8rrrrE)r;rIrrr?r?r@test_rlimit_infinity_value sz&TestProcess.test_rlimit_infinity_valuec Csrt}tr.rcSsg|] }|jqSr?)rrr?r?r@rUs) rArrr7rbr,rrrrsumrErrHr?r?r@test_threads_2Ds$ "zTestProcess.test_threads_2c Cst}|dd\}}|}|dks0t|dkstW5QRXdS)NcSstjtj|Sr5)rCrrnormcaserr?r?r@normpathsz8TestProcess.test_memory_maps_lists_lib..normpathcsg|]}|jqSr?)rrrr?r@rsz:TestProcess.test_memory_maps_lists_lib..)r7r8r'rrE)r;rIrZlibpathsr?rr@test_memory_maps_lists_libs z&TestProcess.test_memory_maps_lists_libc CsNt}|tt|jddW5QRXts>ts>trJ|jdddS)Nz?!?)Zmemtyper) r7r8rr,rFr|rrr rHr?r?r@test_memory_percents   zTestProcess.test_memory_percentcCsL|}|st|s t|||r.cleanupstr)prio getpriorityrgr)r7r8r,rFrrArr ZIDLE_PRIORITY_CLASSZBELOW_NORMAL_PRIORITY_CLASSZNORMAL_PRIORITY_CLASSZABOVE_NORMAL_PRIORITY_CLASSZHIGH_PRIORITY_CLASSZREALTIME_PRIORITY_CLASSsubTestrbrEr8rCrD PRIO_PROCESSrDr)r;rrZ highest_priorCZnew_prior?rr@ test_nicezs`       zTestProcess.test_nicecCst}|tjkstdSr5)r7r8r.ZSTATUS_RUNNINGrErHr?r?r@ test_statusszTestProcess.test_statuscCs||}|}trh|d\}}t}|dr>td||ksJt dt j krx|t j dksxt n|tksxt dS)N\$zrunning as service accountZ USERDOMAIN) rAusernamer splitgetpassgetuserendswithr,rrErCenviron)r;rIrKdomainZ getpass_userr?r?r@ test_usernames    zTestProcess.test_usernamecCs |}|tkstdSr5)rAcwdrCgetcwdrErHr?r?r@test_cwdszTestProcess.test_cwdcs(tddg}||tfdddS)NrdzFimport os, time; os.chdir('..'); [time.sleep(0.1) for x in range(100)]cstjtkSr5)rSrCrdirnamerTr?rr?r@z(TestProcess.test_cwd_2..)r!rAr&)r;rkr?rr@ test_cwd_2s  zTestProcess.test_cwd_2c Cst}|}|st|||j|ttdrL|tt|j ksLtt |t t |ksdttt t tj dd}|D]j}||g||gkstttdr|tt|j kstt|dr|d|kstq|gtr||jks(tn||ks(tttdrR|tt|j ksRttt|dW5QRX|||t ||t|dS)Nsched_getaffinityTZpercpunum_cpurrg)r7r8 cpu_affinityrErr8rClistrZr9rrrrr\r_procZ_get_eligible_cpusr,rFrr)r;rIinitialZall_cpusnr?r?r@test_cpu_affinitys4         zTestProcess.test_cpu_affinityc Cs|}ttjdddg}tt||W5QRXtt|tddW5QRXtt |ddgW5QRXtt|ddgW5QRXdS) NTr[ i'i*r1rz) rArr7rr,rFr|r]rr)r;rIZ invalid_cpur?r?r@test_cpu_affinity_errs s    z"TestProcess.test_cpu_affinity_errscCst}|}|st|||j|t|dkrB|dd}g}tt|dD](}t||D]}|rf| t |qfqV|D]&}||t |t |kstqdS)N rg) r7r8r]rErrr itertools combinationsappendr^sorted)r;rIr`ZcombosrZsubsetZcombor?r?r@"test_cpu_affinity_all_combinationss    z.TestProcess.test_cpu_affinity_all_combinationsz broken on BSDzunreliable on APPVEYORc s\t|}|ks$tt|dz}|d|tfddddD}t j ||ks~tt rD]}|j |kr|j dkstqW5QRXD]}t j |j st|qd|}|td |gtd D]0}d dD}||krq6td qt j ||ks6t|D]}t j |s:t|q:dS) NrsxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcsttkSr5)r open_filesr?filesrIr?r@rW7rXz-TestProcess.test_open_files..cSsg|]}tj|jqSr?rCrrrr?r?r@r9sz/TestProcess.test_open_files..rzIimport time; f = open(r'%s', 'r'); [time.sleep(0.1) for x in range(100)];rdrcSsg|]}tj|jqSr?rorr?r?r@rJsru)r7r8rrlrErrflushr&rCrrrpositionisfilerAr!rr~sleep)r;rr filenamesfilerrr?rmr@test_open_files+s<       zTestProcess.test_open_filesc Cst}tjj}|}t|d}|D]*}||j||jksR|j | kr,qnq,| dt |||j||jkst tr|j dkst n|j | kst |d}|d|jkst |d|j kst |j|kst W5QRXdS)Nwzno file found; files=%srzrrg)r7r8rCrrrrrlr{fdfilenorreprrEr )r;rIrrfileobjruZntupler?r?r@test_open_files_2Ts*    zTestProcess.test_open_files_2cCst}|}|}t|d}||j||dksBtt}||j||dksjt||||kstdS)Nrwrgr) r7r8rZnum_fdsrrcloserEsocket)r;rIrstartrusockr?r?r@ test_num_fdsss   zTestProcess.test_num_fdsz not reliable on OPENBSD & NETBSDcCsTt}t|}tdD](}tdt|}||krdSq|ddS)Nrg?z2num ctx switches still the same after 2 iterations)r7r8rZnum_ctx_switchesrr~rsr)r;rIbeforerafterr?r?r@test_num_ctx_switchess    z!TestProcess.test_num_ctx_switchescCsFt}ttdr&|tks&t|}|tksBtdS)Ngetppid) r7r8r8rCppidrrErArDrHr?r?r@ test_ppids  zTestProcess.test_ppidcCsD|}|jtksttd}t|dks@tdSr) rAparentr9rCrDrEr7pidsr8)r;rIZ lowest_pidr?r?r@ test_parents zTestProcess.test_parentcCs8t}|\}}||ks$t||ks4tdSr5)r7r8rvrrEr;rrwrxr?r?r@test_parent_multis zTestProcess.test_parent_multicCs`t}|st|\}}|d|ks4t|d|ksHt|d|ks\tdSr)r7r8parentsrErvrr?r?r@ test_parentss   zTestProcess.test_parentscCst}|gkst|jddgks,t|jdd}|}|jdd}||fD]>}t|dksht|dj|jks|t|d|jksTtqTdS)NT recursiver) creationflagsrg)r7r8childrenrErArr9r)r;rrwZ children1Z children2rr?r?r@ test_childrens   zTestProcess.test_childrencCsft}|\}}||gks&t|jdd||gks>t|||jddgksbtdS)NTr)r7r8rvrrErTrLrr?r?r@test_children_recursives z#TestProcess.test_children_recursivec Cstt}tD]4}z||d7<Wqtjk rDYqXqt|ddddd}t rz|dkrzt dt |}z|j dd }Wntjk rYnXt|tt|kstdS) NrgcSs|dS)Nrgr?)rr?r?r@rWrXz6TestProcess.test_children_duplicates..)keyrzrzPID 0Tr) collections defaultdictrr7 process_iterrErrorrjitemsrr,rr8rrbrrrE)r;tablerIr9cr?r?r@test_children_duplicatess     z$TestProcess.test_children_duplicatescCs|t}|\}}|jdd}t|dks0t|d|ks@t|d|ksPt|}|d|ksht|d|ksxtdS)NTrrrrg)r7r8rvrrrEr)r;rrwrxrrr?r?r@test_parents_and_childrens  z%TestProcess.test_parents_and_childrencCsX|}|tdD] }|tjkr.q:tdq||tjksTt dS)Nrru) rArrrr.r7ZSTATUS_STOPPEDr~rsrsrE)r;rIrr?r?r@test_suspend_resumes  zTestProcess.test_suspend_resumec CsDtttdW5QRXtttdW5QRXdS)Nrdrz)r,rFrr7r8r|r;r?r?r@test_invalid_pids  zTestProcess.test_invalid_pidc Cst}|jddgd}t|ddgks0tttt}|jdgdd}t|dt sp|ddksptt j ddtj d "|jd gd dd d ikstW5QRXt j ddt |jdd ,ttj |jd gdW5QRXW5QRXt j ddt|jdd $|jd gddd diks0tW5QRXt j ddtd H|}d t |ksjttt|jd gdW5QRXW5QRXtt|dW5QRXtt|dgW5QRXtt|dd gW5QRXdS) Nrr{)attrsZnet_connectionsfoo)rZad_valuezpsutil.Process.niceT)creater]rArgbar)r7r8as_dictrjkeysrErrrr^r*r^rbr:r9r,rFr%NotImplementedErrorrr|)r;rIdr?r?r@ test_as_dictsR& " ( "   zTestProcess.test_as_dictc Cst}td8}|||W5QRX|jdksFtW5QRXtd}||W5QRX|jdkstdS)N$psutil._psplatform.Process.cpu_timesrgrr7r8r*r^oneshotrZ call_countrEr;rIrr?r?r@ test_oneshot/s   zTestProcess.test_oneshotc Cst}td}tdj}|:|||||W5QRXW5QRX|jdksvt|jdkstW5QRXW5QRXtd}||W5QRX|jdkstdS)Nrz(psutil._psplatform.Process.oneshot_enterrgrr)r;rIm1m2rr?r?r@test_oneshot_twice<s    " zTestProcess.test_oneshot_twicec Cs|\}}|}|}||ks(t|&||ksBt||ksRtW5QRX|&||ksvt||kstW5QRXdSr5)rvrrEr)r;p1Zp2Zp1_ppidZp2_ppidr?r?r@test_oneshot_cachePs    zTestProcess.test_oneshot_cachecsnfdd}tr8tfddt}||jD]\}}|||qVdS)Ncsz |}WnTtjk r"Yn`tjk r6YnLtjk r^trX|dkrXYdSYn$Xtrp|dkrpdSd||fdS)N)rr)rr{z+%r didn't raise NSP and returned %r instead)r7r%r:rbrr r)funZfun_namerrr?r@assert_raises_nspgs    zFTestProcess.test_halfway_terminated_process..assert_raises_nspcsjtkSr5)r9r7rr?rr?r@rW~rXz=TestProcess.test_halfway_terminated_process..) rArTrLr r&rPr+iterall)r;rnsrr{r?)rIr;r@test_halfway_terminated_process_s  z+TestProcess.test_halfway_terminated_processcCs|\}}||dSr5)Z spawn_zombieZassertProcessZombie)r;Z_parentzombier?r?r@test_zombie_processs zTestProcess.test_zombie_processc CsBt}tjdtdd}|s*t|js4tW5QRXdS)Nzpsutil.Processrr\)r7r8r*r^r%rrErrr?r?r@$test_zombie_process_is_running_w_excs z0TestProcess.test_zombie_process_is_running_w_excc CsHt}tjdtdd"}|tjks0t|js:tW5QRXdS)Nz!psutil._psplatform.Process.statusrr\) r7r8r*r^r%r.r/rErrr?r?r@ test_zombie_process_status_w_excsz,TestProcess.test_zombie_process_status_w_excc Cstrddlm}n ddlm}|}t|j}|j|df|_t t |jtj ksbt | rnt tjtjdd(t|}t t W5QRXW5QRXd|j|kst |jtj kst |t|jkst d}t|}|j|j|jdd D]D\}}|j|d (tjtj|d  |W5QRXW5QRXq d t|ksbt d t|kstt tjtj|d |W5QRXtjtj|d |W5QRXtjtj|d | W5QRXtjtj|d |!W5QRXdS) Nr)StringIOrZ PSUTIL_DEBUGTz-refreshing Process instance for reused PID %sz4process no longer exists and its PID has been reusedF) clear_cache)r{rzterminated + PID reused)"riorr6r7r8r9r_identr^rZ_pmaprErr*r^objectZ_commonrgetvaluer+rsettersZkillersrEr,rFr:rBrzrrrr) r;rZsubprIrmsgrrr{r?r?r@test_reused_pidsD      zTestProcess.test_reused_pidc CsdtkrVttjtdW5QRXtdr.clean_dict..)rr$popdictr)rexcluder{r?r?r@ clean_dicts z,TestProcess.test_environ..clean_dict) ZmaxDiffr7r8rPrCcopyr rrE)r;rrIZd1Zd2r?r?r@ test_environs zTestProcess.test_environz #include 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)stdinrfrXrd3)ACr)textwrapdedentr(rr6rirjr7r8r9r3rrErfrrrrPrb communicate returncode)r;rRZcexer>rIenvr?r?r@test_weird_environs(      zTestProcess.test_weird_environN)m__name__ __module__ __qualname____doc__rArJrSrUrXr,markskipifr rcrlrrtryr}rrrr#rrrrrrrr2rrrrr rrrrrrrrrrrr/r1rrrrrr rrrrr"r r&r)r1r rr2r;r@rGrHrRrUrYrrbrerkrrrvr|rrrrrrrrrrrrrrrrrrrrrrrrrrrr?r?r?r@r4Ps&             ) +          /       B   &   &       4 &   .. r4csreZdZdZeedr&eZeZ fddZ fddZ fddZ d d Z ejjd d d ddZZS)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. r6csTtj||ddt|DD].}t||fdd}t||t||q dS)NcSsg|]}|dr|qS)testrrr?r?r@r`s z0LimitedUserTestCase.__init__..cs&z Wntjk r YnXdSr5)r7rbrmethr?r@test_cs z+LimitedUserTestCase.__init__..test_)r__init__rrsetattrtypes MethodType)r;r<r=attrr __class__rr@r\s   zLimitedUserTestCase.__init__cs"ttdtddS)Ni)rsetUprCsetegidseteuidrrr?r@rks  zLimitedUserTestCase.setUpcs&t|jt|jtdSr5)rCr PROCESS_UIDr PROCESS_GIDrtearDownrrr?r@rps  zLimitedUserTestCase.tearDowncCs8ztdWntjk r(Yn X|ddS)Nrzzexception not raised)r7r8rArbrrr?r?r@rGus zLimitedUserTestCase.test_niceTzcauses problem as rootrYcCsdSr5r?rr?r?r@r}sz'LimitedUserTestCase.test_zombie_process)rrrrr8rCr6rr=rrrrrGr,rrr __classcell__r?r?rr@rQs    rc@s4eZdZdZeddZddZddZdd Zd S) TestPopenzTests for psutil.Popen class.cCs tdSr5r-)clsr?r?r@ tearDownClassszTestPopen.tearDownClassc Cstddg}tj|tjtjtdL}|||jt |sDt t t  |jW5QRX|W5QRXtr|dtj kst n|dtjkst dS)Nrd3import time; [time.sleep(0.1) for x in range(100)];stdoutrfrrh)r!r7Popenrirjr"r{rrrrEr,rFrGrrTr rLrMrNr;rkprocr?r?r@ test_miscs*  zTestPopen.test_miscc Cshtjtdgtjtjtjtd}|W5QRX|jjs>t |j jsJt |j jsVt |j dksdt dS)Nz-V)rrfrrr) r7rr!rirjr"rrclosedrErfrr)r;rr?r?r@test_ctx_managers   zTestPopen.test_ctx_managerc Cstddg}tj|tjtjtd}||t tj |W5QRXt tj | W5QRXt tj | t jW5QRXtrt tj | t jW5QRXt tj | t jW5QRXW5QRXdS)Nrdrr)r!r7rrirjr"rTrLr,rFr:rKrVrMrNr Z CTRL_C_EVENTZCTRL_BREAK_EVENTrr?r?r@test_kill_terminates0zTestPopen.test_kill_terminateN) rrrr classmethodrrrrr?r?r?r@rs  r)Grrr`rMrgrCrMr~rr,rirrr~rr7rrrrrrr r r r Zpsutil._commonr Zpsutil._compatrrrrrZ psutil.testsrrrrrrrrrrrrrr r!r"r#r$r%r&r'r(r)r*r+r,r.r/r0r1r2r3r4r6rrr?r?r?r@s                                                 6