gf0dZddlZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ddlm Z ddl m Z dd l mZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%dd lm&Z&ddlZerd!Zd"Z'Gd#d$eZ(e e&j)ed%e&j)eoe d&Gd'd(e(Z*e&j)ed)Gd*d+e*Z+Gd,d-e(Z,e-d.krdd/l.m/Z/e/e0dSdS)0a: Notes about unicode handling in psutil ====================================== Starting from version 5.3.0 psutil adds unicode support, see: https://github.com/giampaolo/psutil/issues/1040 The notes below apply to *any* API returning a string such as process exe(), cwd() or username(): * all strings are encoded by using the OS filesystem encoding (sys.getfilesystemencoding()) which varies depending on the platform (e.g. "UTF-8" on macOS, "mbcs" on Win) * no API call is supposed to crash with UnicodeDecodeError * instead, in case of badly encoded data returned by the OS, the following error handlers are used to replace the corrupted characters in the string: * Python 3: sys.getfilesystemencodeerrors() (PY 3.6+) or "surrogatescape" on POSIX and "replace" on Windows * Python 2: "replace" * on Python 2 all APIs return bytes (str type), never unicode * on Python 2, you can go back to unicode by doing: >>> unicode(p.exe(), sys.getdefaultencoding(), errors="replace") For a detailed explanation of how psutil handles unicode see #1040. Tests ===== List of APIs returning or dealing with a string: ('not tested' means they are not tested to deal with non-ASCII strings): * Process.cmdline() * Process.connections('unix') * Process.cwd() * Process.environ() * Process.exe() * Process.memory_maps() * Process.name() * Process.open_files() * Process.username() (not tested) * disk_io_counters() (not tested) * disk_partitions() (not tested) * disk_usage(str) * net_connections('unix') * net_if_addrs() (not tested) * net_if_stats() (not tested) * net_io_counters() (not tested) * sensors_fans() (not tested) * sensors_temperatures() (not tested) * users() (not tested) * WindowsService.binpath() (not tested) * WindowsService.description() (not tested) * WindowsService.display_name() (not tested) * WindowsService.name() (not tested) * WindowsService.status() (not tested) * WindowsService.username() (not tested) In here we create a unicode path with a funky non-ASCII name and (where possible) make psutil return it back (e.g. on name(), exe(), open_files(), etc.) and make sure that: * psutil never crashes with UnicodeDecodeError * the returned path matches N)closing)BSD)OPENBSD)POSIX)WINDOWS)PY3)u)APPVEYOR)ASCII_FS)bind_unix_socket)chdir) CI_TESTING)copyload_shared_lib) create_exe) get_testfn)HAS_CONNECTIONS_UNIX) HAS_ENVIRON)HAS_MEMORY_MAPS)INVALID_UNICODE_SUFFIX)PsutilTestCase)PYPY) safe_mkdir safe_rmpath) serialrun)skip_on_access_denied)spawn_testproc) terminate TESTFN_PREFIX)UNICODE_SUFFIX)unittestcnddlm} ||S#t$rtjYdSwxYw)Nrr) psutil.testsr WindowsError traceback print_exc)pathrms {/builddir/build/BUILD/imunify360-venv-2.3.5/opt/imunify360/venv/lib64/python3.11/site-packages/psutil/tests/test_unicode.pyrrss[ 322222 "2d88O " " "   ! ! ! ! ! ! "s 44cd}t|} t|t|t|g}t j||dzt|dz |t |t|dS#ttf$r$Y|t |t|dSwxYw#|t |t|wxYw)z`Return True if both the fs and the subprocess module can deal with a unicode file name. Nsuffixcmdz-2TF) rrrrshutilcopyfilerUnicodeEncodeErrorIOError)r-sproctestfns r* try_unicoder6s E v & & &F F6F8,,, ...FTM"""   e   F  (   e   F   e   Fs$ABC!CCC"C*ceZdZdZdZdS)BaseUnicodeTestNcj|j)t|js|ddSdS)Nzcan't handle unicode str) funky_suffixr6skipTest)selfs r*setUpzBaseUnicodeTest.setUpsF   (t011 @mm$>??? ) ( @ @)__name__ __module__ __qualname__r:r=r>r*r8r8s-L@@@@@r>r8zASCII fsztoo much trouble on PYPY2ceZdZdZeZedZedZdZ dZ dZ dZ dZ ejeoed d Zeje d d Zeje d eje d edZdZeje deje dejeddZdS) TestFSAPIsz1Test FS APIs with a funky, valid, UTF8 path name.cbt|j|_t|jdS)Nr,)rr: funky_namerclss r* setUpClasszTestFSAPIs.setUpClasss-#3+;<<<3>"""""r>c.t|jdSN)rrFrGs r* tearDownClasszTestFSAPIs.tearDownClasssCN#####r>ct|jtrdntd}t j5t jd|jtj|vcdddS#1swxYwYdS)N.ignore) isinstancerFstrr warningscatch_warnings simplefilteroslistdir)r<heres r*expect_exact_path_matchz"TestFSAPIs.expect_exact_path_matchs!#66BssAcFF  $ & & 7 7  !( + + +?bj&6&66 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7s/A;;A?A?c||jg}tj|j}|}||t|rW| tj |tj |jdSdSNr.) rrFpsutilProcesspidexeassertIsInstancerQrX assertEqualrUr(normcase)r<subppr^s r* test_proc_exezTestFSAPIs.test_proc_exes""'8"99 N48 $ $eegg c3'''  ' ' ) ) @   RW--c22W--do>> @ @ @ @ @ @ @r>cf||jg}tj|j}||t|r:| |tj |jdSdSrZ) rrFr[r\r]namer_rQrXr`rUr(basename)r<rbrfs r*test_proc_namezTestFSAPIs.test_proc_names""'8"99~dh'',,.. dC(((  ' ' ) ) F   T27#3#3DO#D#D E E E E E F Fr>c<||jg}tj|j}|}|D]}||t|r| ||jgdSdSrZ) rrFr[r\r]cmdliner_rQrXr`)r<rbrcrjparts r*test_proc_cmdlinezTestFSAPIs.test_proc_cmdlines""'8"99 N48 $ $))++ - -D  ! !$ , , , ,  ' ' ) ) 9   Wt&7 8 8 8 8 8 9 9r>c|jdz}|t|t|t |5t j}|}dddn #1swxYwY||t| r| ||dSdSN2) rF addCleanuprrr r[r\cwdr_rQrXr`)r<dnamercrqs r* test_proc_cwdzTestFSAPIs.test_proc_cwds#%  U+++5 5\\    A%%''C                aeeggs+++  ' ' ) ) )   S% ( ( ( ( ( ) )s(A88A<?A<zfails on PYPY + WINDOWSctj}t|}t |jd5t|}dddn #1swxYwY||z j}||ttr|s| dS| rW| tj|tj|jdSdS)Nrbzopen_files on BSD is broken)r[r\set open_filesopenrFpopr(r_rQrr;rXr`rUra)r<rcstartnewr(s r*test_proc_open_fileszTestFSAPIs.test_proc_open_filessa N  ALLNN## $/4 ( ( & &allnn%%C & & & & & & & & & & & & & & &e   ""' dC(((  @t @==!>?? ?  ' ' ) ) @   RW--d33W--do>> @ @ @ @ @ @ @s "A88A<?A<z POSIX onlyc||j} t|}n*#t$rtrt jdwxYwt|5tj dd}| |j tts||j |ddddS#1swxYwYdS)Nr, not supportedunixr)rr:r r2rr"SkipTestrr[r\ connectionsr_laddrrQrr`)r<rfsockconns r*test_proc_connectionsz TestFSAPIs.test_proc_connectionss-d&788 9#D))DD! 9 9 9 9'888  9 T]] 3 3>##//77:D  ! !$*c 2 2 2 3  T222  3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3s-'A&A/C""C&)C&zcan't list UNIX socketscd}||j} t|}n*#t$rtrt jdwxYwt|5tj d}tsF||}| |j t||j |ddddS#1swxYwYdS)Nc|D]B}tj|jt r|cSCt d)Nzconnection not found)rUr(rgr startswithr ValueError)consrs r* find_sockz2TestFSAPIs.test_net_connections..find_sock sV  7##DJ//::=II KKK 344 4r>r,r~r)kind)rr:r r2rr"rrr[net_connectionsrr_rrQr`)r<rrfrrrs r*test_net_connectionszTestFSAPIs.test_net_connectionss> 5 5 5 d&788 9#D))DD! 9 9 9 9'888  9 T]] 3 3)v666D 3 y%%dj#666  T222  3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3s0'A)A#CC Cc|jdz}|t|t|t j|dSrn)rFrprrr[ disk_usage)r<rrs r*test_disk_usagezTestFSAPIs.test_disk_usage"sG#%  U+++5%     r>r~z&ctypes does not support unicode on PY2zunstable on PYPYcdt|j5}dfdtjD}d|D}||||D]}||t ddddS#1swxYwYdS)Nr,cztjtj|SrK)rUr(realpathra)rcs r*normpathz-TestFSAPIs.test_memory_maps..normpath/s(w''(8(8(;(;<<c0g|]}|jSrB)r().0xrs r* z/TestFSAPIs.test_memory_maps..1s;AAA!((AAAr>c$g|] }t|v |SrBr)rrs r*rz/TestFSAPIs.test_memory_maps..4s"BBBa}/A/A/A/A/Ar>)rr:r[r\ memory_mapsassertInr_rQ)r< funky_pathlibpathsr(rs @r*test_memory_mapszTestFSAPIs.test_memory_maps(s- !(9 : : : 1j = = =AAAA!'!1!1!=!=!?!?AAAHCB8BBBH MM((:.. 9 9 9  1 1%%dC0000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1sBB%%B),B)N)r?r@rA__doc__r!r: classmethodrIrLrXrdrhrlrsr"skipIfrrr|rrrrrrrrrrBr>r*rDrDs<;!L##[#$$[$777@@@FFF999 ) ) )X_T%g'@AA @ @BA @X_Y --33.-3 X_Y --X_--/HII33JI.-3.!!! X_(/::X_WFGGX_T-.. 1 1/.HG;: 1 1 1r>rDzunreliable on CIc,eZdZdZeZedZdS)TestFSAPIsWithInvalidPathz-Test FS APIs with a funky, invalid path name.cdS)NTrBrGs r*rXz1TestFSAPIsWithInvalidPath.expect_exact_path_match?s tr>N)r?r@rArrr:rrXrBr>r*rr:s877)L[r>rceZdZdZerendZeje deje oe ddZ dS) TestNonFSAPISz&Unicode tests for non fs-related APIs.èr~zsegfaults on PYPY + WINDOWSctj}|j|d<||}t j|j}|}|D];\}}| |t| |t<| |d|jdS)N FUNNY_ARG)env) rUenvironcopyr:rr[r\r]itemsr_rQr`)r<rr4rckvs r*test_proc_environzTestNonFSAPIS.test_proc_environNsjoo,K###,, N59 % %iikkIIKK * *DAq  ! !!S ) ) )  ! !!S ) ) ) ) [)4+<=====r>N) r?r@rArrr!r:r"rrrrrrBr>r*rrJsr00%(2>>dLX__o66X_T%g'DEE>>FE76>>>r>r__main__) run_from_name)1rrUr0r&rR contextlibrr[rrrrpsutil._compatrr r$r r r r rrrrrrrrrrrrrrrrr r!r"r6r8rrDrrr?psutil.tests.runnerr__file__rBr>r*rsBBH !!!!!!!!!!!!))))))######,,,,,,############------$$$$$$((((((//////''''''######$$$$$$""""""......''''''""""""&&&&&&''''''!!!!!!  """"$6@@@@@n@@@ :&&!c'#>??K1K1K1K1K1K1K1@?'& K1\/00 10>>>>>O>>>. z111111M(r>