3 ̪e1@s8dZddlZddlZddlZddlZddlZddlmZddlZddlm Z ddlm Z ddlm Z ddl m Z ddl mZdd l mZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%ddlm&Z&erd d!Z"d"d#Z'Gd$d%d%eZ(e#ej)ed&ej)eoe d'Gd(d)d)e(Z*ej)ed*Gd+d,d,e*Z+Gd-d.d.e(Z,e-d/kr4dd0l.m/Z/e/e0dS)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.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)super)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_py_exe) get_testfn) safe_mkdir) safe_rmpath) serialrun)skip_on_access_denied)spawn_testproc) terminatec Cs6ddlm}y||Stk r0tjYnXdS)Nr)r) psutil.testsrZ WindowsError traceback print_exc)pathZrmr#E/usr/local/lib64/python3.6/site-packages/psutil/tests/test_unicode.pyrss rcCsd}t|d}z^y.find_sock)r%z not supportedrd)kind)rr1rr)rrerfrrHZnet_connectionsrLrgr@rM)r6rnrRrhrmrir#r#r$test_net_connectionss   zTestFSAPIs.test_net_connectionscCs,|jd}|jt|t|tj|dS)NrX)r0rYrrrH disk_usage)r6r[r#r#r$test_disk_usage's  zTestFSAPIs.test_disk_usagez not supportedz&ctypes does not support unicode on PY2zunstable on PYPYc svt|jd`}ddfddtjjD}dd|D}|j||x|D]}|j|tqTWWdQRXdS)N)r%cSstjjtjj|S)N)rDr"realpathrN)rPr#r#r$normpath5sz-TestFSAPIs.test_memory_maps..normpathcsg|]}|jqSr#)r").0x)rtr#r$ 9sz/TestFSAPIs.test_memory_maps..cSsg|]}t|kr|qSr#)r)rurvr#r#r$rw<s)rr1rHrIZ memory_mapsZassertInrLr@)r6Z funky_pathZlibpathsr"r#)rtr$test_memory_maps-s zTestFSAPIs.test_memory_mapsN)r7r8r9__doc__rr1rGrQrTrWr\reskipIfrrrcrrjr rrprrrrrxr#r#r#r$r<s         r<zunreliable on CIc@seZdZdZeZddZdS)TestFSAPIsWithInvalidPathz-Test FS APIs with a funky, invalid path name.cCsdS)NTr#)r6r#r#r$rGHsz1TestFSAPIsWithInvalidPath.expect_exact_path_matchN)r7r8r9ryrr1rGr#r#r#r$r{Bsr{c@sBeZdZdZerendZeje deje o.e dddZ dS) TestNonFSAPISz&Unicode tests for non fs-related APIs.èz not supportedzsegfaults on PYPY + WINDOWScCsxtjj}|j|d<|j|d}tj|j}|j}x,|jD] \}}|j |t |j |t q>W|j |d|jdS)NZ FUNNY_ARG)env) rDenvironcopyr1rrHrIrJitemsrLr@rM)r6r~r+rPkvr#r#r$test_proc_environWs     zTestNonFSAPIS.test_proc_environN) r7r8r9ryrrr1rerzr rrrr#r#r#r$r|Rs  r|__main__) run_from_name)1ryrDr'r rerA contextlibrrHrrrZpsutil._compatrrrrr r r r r rrrrrrrrrrrrrrrrrr,r-rzr<r{r|r7Zpsutil.tests.runnerr__file__r#r#r#r$Isd