B Œ=Ý_«0ã@s<dZddlZddlZddlZddlZddlmZddlmZddlm 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&ddlZer¢d!d"„Zd#d$„Z'Gd%d&„d&eƒZ(e e& )ed'¡e& )eoÔe d(¡Gd)d*„d*e(ƒƒƒƒZ*e& )ed+¡Gd,d-„d-e*ƒƒZ+Gd.d/„d/e(ƒZ,e-d0kr8dd1l.m/Z/e/e0ƒdS)2a: 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)ÚunittestcCs6ddlm}y||ƒStk r0t ¡YnXdS)Nr)r)Ú psutil.testsrZ WindowsErrorÚ tracebackÚ print_exc)ÚpathZrm©r$úL/opt/alt/python37/lib64/python3.7/site-packages/psutil/tests/test_unicode.pyrss rc CsŒd}t|d}z^y.find_sock)r&z not supportedra)Zkind)rr.r r)rrrbrrEZnet_connectionsrrIrcr=rJ)r0rjrOrdrirer$r$r%Útest_net_connectionss   zTestFSAPIs.test_net_connectionscCs,|jd}| t|¡t|ƒt |¡dS)NrU)r6rVrrrEZ disk_usage)r0rXr$r$r%Útest_disk_usage"s  zTestFSAPIs.test_disk_usagez not supportedz&ctypes does not support unicode on PY2zunstable on PYPYc svt|jd`}dd„‰‡fdd„t ¡ ¡Dƒ}dd„|Dƒ}| ˆ|ƒ|¡x|D]}| |t¡qTWWdQRXdS)N)r&cSstj tj |¡¡S)N)rAr#ÚrealpathrK)rMr$r$r%Únormpath/sz-TestFSAPIs.test_memory_maps..normpathcsg|]}ˆ|jƒ‘qSr$)r#)Ú.0Úx)rnr$r%ú 1sz/TestFSAPIs.test_memory_maps..cSsg|]}t|kr|‘qSr$)r)rorpr$r$r%rq4s)rr.rErFZ memory_mapsZassertInrIr=)r0Z funky_pathZlibpathsr#r$)rnr%Útest_memory_maps(s  zTestFSAPIs.test_memory_mapsN)r2r3r4Ú__doc__rr.Ú classmethodr8r9rDrNrQrTrYrÚskipIfrrr`rrfrrrkrlrrrrr$r$r$r%r5©s$          r5zunreliable on CIc@s eZdZdZeZedd„ƒZdS)ÚTestFSAPIsWithInvalidPathz-Test FS APIs with a funky, invalid path name.cCsdS)NTr$)r7r$r$r%rD?sz1TestFSAPIsWithInvalidPath.expect_exact_path_matchN)r2r3r4rsrr.rtrDr$r$r$r%rv:srvc@sBeZdZdZerendZe e d¡e e o.e d¡dd„ƒƒZ dS)Ú TestNonFSAPISz&Unicode tests for non fs-related APIs.õèz not supportedzsegfaults on PYPY + WINDOWScCsxtj ¡}|j|d<|j|d}t |j¡}| ¡}x,| ¡D] \}}|  |t ¡|  |t ¡q>W|  |d|j¡dS)NZ FUNNY_ARG)Úenv) rAÚenvironÚcopyr.rrErFrGÚitemsrIr=rJ)r0ryr+rMÚkÚvr$r$r%Útest_proc_environNs     zTestNonFSAPIS.test_proc_environN) r2r3r4rsrrr.rrurrrrr$r$r$r%rwJs  rwÚ__main__)Ú run_from_name)1rsrAr(r!r>Ú contextlibrrErrrrZpsutil._compatrrr r r r r r rrrrrrrrrrrrrrrrrrr,r-rur5rvrwr2Zpsutil.tests.runnerrÚ__file__r$r$r$r%ÚJsd