U 1g;1@sdZddlZddlZddlZddlZddlmZddlZddlmZddlm Z ddlm Z ddl m Z ddl m Z dd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$erdd Z!d!d"Z%Gd#d$d$eZ&ej'j(d%d&ej'j)ed'd(ej'j)eoe d)d(Gd*d+d+e&Z*ej'j)ed,d(Gd-d.d.e*Z+Gd/d0d0e&Z,dS)1a> 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.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)BSD)POSIX)WINDOWS)PY3)super)APPVEYOR)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_testproc) terminatecCs8ddlm}z ||WStk r2tYnXdS)Nrr) psutil.testsrZ WindowsError traceback print_exc)pathZrmr$I/opt/hc_python/lib64/python3.8/site-packages/psutil/tests/test_unicode.pyrqs  rc Csd}t|d}zhz.find_sockr&rlrm)kind)rr3rr+rrr8rrQrnrGrorHrU)r:rvrArprurqr$r$r%test_net_connectionss   zTestFSAPIs.test_net_connectionscCs,|jd}|t|t|t|dSr_)r2rarrrQ disk_usage)r:rcr$r$r%test_disk_usage/s  zTestFSAPIs.test_disk_usagerlz&ctypes does not support unicode on PY2zunstable on PYPYc stt|jd^}ddfddtD}dd|D}||ksNt|D]}t|tsRtqRW5QRXdS)Nr&cSstjtj|S)N)rLr#realpathrV)rXr$r$r%normpath?sz-TestFSAPIs.test_memory_maps..normpathcsg|]}|jqSr$)r#.0xr|r$r% Bsz/TestFSAPIs.test_memory_maps..cSsg|]}t|kr|qSr$rr}r$r$r%rFs)rr3rQrRZ memory_mapsrUrGrH)r:Z funky_pathZlibpathsr#r$rr%test_memory_maps5s  zTestFSAPIs.test_memory_mapsN)r;r<r=__doc__rr3rOrYr[r^rdrmarkskipifrrrkrrrr rrxrzr rrr$r$r$r%rDs4      rDzunreliable on CIc@seZdZdZeZddZdS)TestFSAPIsWithInvalidPathz-Test FS APIs with a funky, invalid path name.cCsdS)NTr$r9r$r$r%rORsz1TestFSAPIsWithInvalidPath.expect_exact_path_matchN)r;r<r=rrr3rOr$r$r$r%rLsrc@sJeZdZdZerendZejj e ddejj e o4e ddddZ dS) TestNonFSAPISz&Unicode tests for non fs-related APIs.èrlrBzsegfaults on PYPY + WINDOWScCsxtj}|j|d<|j|d}t|j}|}|D]$\}}t |t sRt t |t ssZC