0i)dZddlZddlZddlZddlmZddlZddlmZddlmZddlm Z ddlm Z ddlm Z dd lm Z dd l mZdd l mZdd l mZdd l mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddl m Z ddl m!Z!ddl m"Z"dZ#Gd d!eZ$ejJjMd"#ejJjOed$%Gd&d'e$Z(ejJjOed(%Gd)d*e(Z)Gd+d,e$Z*y)-aYNotes 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: * sys.getfilesystemencodeerrors() or "surrogatescape" on POSIX and "replace" on Windows. 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.cwd() * Process.environ() * Process.exe() * Process.memory_maps() * Process.name() * Process.net_connections('unix') * 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)BSDMACOS)NETBSD)OPENBSD)POSIX)WINDOWS)ASCII_FS) CI_TESTING) HAS_ENVIRON)HAS_MEMORY_MAPS)HAS_NET_CONNECTIONS_UNIX)INVALID_UNICODE_SUFFIX)PYPY) TESTFN_PREFIX)UNICODE_SUFFIX)PsutilTestCase)bind_unix_socket)chdir)copyload_shared_lib) create_py_exe) get_testfn)pytest) safe_mkdir) safe_rmpath)skip_on_access_denied) spawn_subproc) terminatec|d}t|} t|t|t|g}t j ||dzt|dz | t |t|y#ttf$rY| t |t|ywxYw#| t |t|wxYw)z`Return True if both the fs and the subprocess module can deal with a unicode file name. Nsuffix)cmdz-2TF) rrrrshutilcopyfilerUnicodeEncodeErrorOSError)r"sproctestfns J/opt/hc_python/lib64/python3.12/site-packages/psutil/tests/test_unicode.py try_unicoder+hs E v &F Ff6(+ .FTM"   e F  (   e F   e Fs$A A44BB!BB!!B;c6eZdZdZefdZfdZxZS)BaseUnicodeTestNct|d|_d|_|jNt |jsd|_yt |j|_t|jyy)NFTr!)super setUpClass skip_tests funky_name funky_suffixr+rr)cls __class__s r*r0zBaseUnicodeTest.setUpClasss`     's//0!%!+33C3C!Dcnn- (cdt||jrtjdSy)Nzcan't handle unicode str)r/setUpr1rskip)selfr5s r*r8zBaseUnicodeTest.setUps(   ??;;9: : r6)__name__ __module__ __qualname__r3 classmethodr0r8 __classcell__)r5s@r*r-r-s$L . .;;r6r-serial)namezASCII fsreasonc>eZdZdZeZdZdZdZdZ dZ e jjexrede jjexsed d Ze jje d e jje d d Ze jje d e jje d edZdZe jje ddZy) TestFSAPIsz1Test FS APIs with a funky, valid, UTF8 path name.ctj5tjd|jt j dvcdddS#1swYyxYw)Nignore.)warningscatch_warnings simplefilterr2oslistdirr:s r*expect_exact_path_matchz"TestFSAPIs.expect_exact_path_matchs<  $ $ &  ! !( +??bjjo5' & &s 6AAc~|jddg}|j|}tj|j}|j }t |tsJ|jrLtjj|tjj|jk(sJyyNz-cz2import time; [time.sleep(0.1) for x in range(100)]) r2rpsutilProcesspidexe isinstancestrrOrLpathnormcase)r:r#subpprUs r* test_proc_exezTestFSAPIs.test_proc_exes OO  @  !!#& NN488 $eeg#s###  ' ' )77##C(BGG,<, !> *r6c\|jdz}|jt|t|t |5t j }|j}dddtjtsJ|jr|k(sJyy#1swYBxYwN2) r2 addCleanuprrrrRrScwdrVrWrO)r:dnamer[rhs r* test_proc_cwdzTestFSAPIs.test_proc_cwds#%  U+5 5\ A%%'C!%%'3'''  ' ' )%< < * \s %B""B+zfails on PYPY + WINDOWSrBzbroken on NETBSD or OPENBSDc6tj}t|j}t |j d5t|j}ddd|z j j}t|tsJtr|stjdS|jrLtjj|tjj|j k(sJyy#1swYxYw)Nrbzopen_files on BSD is broken)rRrSset open_filesopenr2poprXrVrWrrr9rOrLrY)r:r[startnewrXs r*test_proc_open_fileszTestFSAPIs.test_proc_open_filess NN ALLN# $//4 (alln%C)e   "''$$$$ t;;<= =  ' ' )77##D)RWW-=-=doo-NN NN *) (s DDz POSIX onlyzcan't list UNIX socketsc|j|j}t|}t|5t j j dd}t|jtsJ|js*tr$trtjdcdddS|j|k(sJ dddy#1swYyxYw)Nr!unixrzunreliable on OSX)rr3rrrRrSnet_connectionsrVladdrrWrr rr9)r:rAsockconns r*test_proc_net_connectionsz$TestFSAPIs.test_proc_net_connectionss d&7&78% T]>>#33F;A>Ddjj#. ..::%J{{#67 ] ::% %% ]]sA/C,CCc0d}|j|j}t|}t|5t j d}||}t |jtsJ|j|k(sJ dddy#1swYyxYw)Nc|D]B}tjj|jj t s@|cSt d)Nzconnection not found)rLrXr^rw startswithr ValueError)consrys r* find_sockz2TestFSAPIs.test_net_connections..find_socks@77##DJJ/::=IK34 4r6r!ru)kind) rr3rrrRrvrVrwrW)r:rrArxrrys r*test_net_connectionszTestFSAPIs.test_net_connectionssy  5 d&7&78% T]))v6DT?Ddjj#. ..::% %% ]]s A B  Bc|jdz}|jt|t|t j |yre)r2rgrrrR disk_usage)r:ris r*test_disk_usagezTestFSAPIs.test_disk_usage s4#%  U+5% r6 not supportedcnt|j5}d}tjj Dcgc]}||j }}|Dcgc] }t |vs |}}|||vsJ|D]}t|trJ dddycc}wcc}w#1swYyxYw)Nr!cztjjtjj|SN)rLrXrealpathrY)r[s r*normpathz-TestFSAPIs.test_memory_maps..normpaths&ww''(8(8(;<rs<|  !#$(1/&''),&##$.&"6;n;*h'HZ0E-E-1(E-PJ'9: ;5O5r6