bg/dZddlZddlZddlZddlZddlZddlmZddlZddlm Z ddlm Z ddlm Z ddl m Z ddl mZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%erdZ!d Z&Gd!d"eZ'e"ej(ed#ej(eoe d$Gd%d&e'Z)ej(ed'Gd(d)e)Z*Gd*d+e'Z+e,d,krdd-l-m.Z.e.e/dSdS).a: 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)POSIX)WINDOWS)PY3)u)APPVEYOR)ASCII_FS) CI_TESTING)HAS_CONNECTIONS_UNIX) HAS_ENVIRON)HAS_MEMORY_MAPS)INVALID_UNICODE_SUFFIX)PYPY TESTFN_PREFIX)UNICODE_SUFFIX)PsutilTestCase)bind_unix_socket)chdir)copyload_shared_lib) create_exe) get_testfn) safe_mkdir safe_rmpath) serialrun)skip_on_access_denied)spawn_testproc) terminatecnddlm} ||S#t$rtjYdSwxYw)Nrr) psutil.testsr WindowsError traceback print_exc)pathrms l/builddir/build/BUILD/cloudlinux-venv-1.0.7/venv/lib64/python3.11/site-packages/psutil/tests/test_unicode.pyrrrs[ 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) rrrrshutilcopyfiler UnicodeEncodeErrorIOError)r+sproctestfns r( try_unicoder4s 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_suffixr4skipTestselfs r(setUpzBaseUnicodeTest.setUpsF   (t011 @mm$>??? ) ( @ @)__name__ __module__ __qualname__r8r<r=r(r6r6s-L@@@@@r=r6zASCII 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*)rr8 funky_namerclss r( setUpClasszTestFSAPIs.setUpClasss-#3+;<<<3>"""""r=c.t|jdSN)rrErFs r( tearDownClasszTestFSAPIs.tearDownClasssCN#####r=ct|jtrdntd}t j5t jd|jtj|vcdddS#1swxYwYdS)N.ignore) isinstancerEstrrwarningscatch_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,) rrEpsutilProcesspidexeassertIsInstancerPrW assertEqualrTr&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 |jdSdSrY) rrErZr[r\namer^rPrWr_rTr&basename)r;rares 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| ||jgdSdSrY) rrErZr[r\cmdliner^rPrWr_)r;rarbriparts 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) rE addCleanuprrrrZr[cwdr^rPrWr_)r;dnamerbrps 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)rZr[set open_filesopenrEpopr&r^rPrr9rWr_rTr`)r;rbstartnewr&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 t||j |ddddS#1swxYwYdS)Nr* not supportedunixr)rr8rr0runittestSkipTestrrZr[ connectionsr^laddrrPr_)r;resockconns r(test_proc_connectionsz TestFSAPIs.test_proc_connectionss%d&788 9#D))DD! 9 9 9 9'888  9 T]] / />##//77:D  ! !$*c 2 2 2   TZ . . . / / / / / / / / / / / / / / / / / /s-'A&A(CC"Czcan't list UNIX socketscd}||j} t|}n*#t$rtrt jdwxYwt|5tj d}||}| |j t| |j |ddddS#1swxYwYdS)Nc|D]B}tj|jt r|cSCt d)Nzconnection not found)rTr&rfr startswithr ValueError)consrs r( find_sockz2TestFSAPIs.test_net_connections..find_sock sV  7##DJ//::=II KKK 344 4r=r*r}r~)kind)rr8rr0rrrrrZnet_connectionsr^rrPr_)r;rrerrrs r(test_net_connectionszTestFSAPIs.test_net_connectionss6 5 5 5 d&788 9#D))DD! 9 9 9 9'888  9 T]] / /)v666D9T??D  ! !$*c 2 2 2   TZ . . .  / / / / / / / / / / / / / / / / / /s0'A)ACCCc|jdz}|t|t|t j|dSrm)rErorrrZ disk_usage)r;rqs r(test_disk_usagezTestFSAPIs.test_disk_usagesG#%  U+++5%     r=r}z&ctypes does not support unicode on PY2zunstable on PYPYcdt|j5}dfdtjD}d|D}||||D]}||t ddddS#1swxYwYdS)Nr*cztjtj|SrJ)rTr&realpathr`)rbs r(normpathz-TestFSAPIs.test_memory_maps..normpath*s(w''(8(8(;(;<<z/TestFSAPIs.test_memory_maps..,s;AAA!((AAAr=c$g|] }t|v |SrAr)rrs r(rz/TestFSAPIs.test_memory_maps../s"BBBa}/A/A/A/A/Ar=)rr8rZr[ memory_mapsassertInr^rP)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?r@__doc__rr8 classmethodrHrKrWrcrgrkrrrskipIfrrr{rrr rrrrrrrAr=r(rCrCs<;!L##[#$$[$777@@@FFF999 ) ) )X_T%g'@AA @ @BA @X_Y -- / /.- /X_Y --X_--/HII//JI.-/*!!! X_(/::X_WFGGX_T-.. 1 1/.HG;: 1 1 1r=rCzunreliable on CIceZdZdZeZdZdS)TestFSAPIsWithInvalidPathz-Test FS APIs with a funky, invalid path name.cdS)NTrAr:s r(rWz1TestFSAPIsWithInvalidPath.expect_exact_path_match:str=N)r>r?r@rrr8rWrAr=r(rr5s.77)Lr=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) rTenvironcopyr8rrZr[r\itemsr^rPr_)r;rr2rbkvs r(test_proc_environzTestNonFSAPIS.test_proc_environHsjoo,K###,, N59 % %iikkIIKK * *DAq  ! !!S ) ) )  ! !!S ) ) ) ) [)4+<=====r=N) r>r?r@rrrr8rrr rrrrAr=r(rrDsr00%(2>>dLX__o66X_T%g'DEE>>FE76>>>r=r__main__) run_from_name)0rrTr.r$rrQ contextlibrrZrrrpsutil._compatrrr"r r r r r rrrrrrrrrrrrrrrrr r4r6rrCrrr>psutil.tests.runnerr__file__rAr=r(rsBBH  !!!!!!!!!!!!######------$$$$$$((((((//////&&&&&&'''''''''''')))))),,,,,,##################$$$$$$""""""......''''''"""""" """"$6@@@@@n@@@ :&&!c'#>??G1G1G1G1G1G1G1@?'& G1T/00 10>>>>>O>>>. z111111M(r=