O d Z ddlZddlZddlZddlZddlmZ ddlmZ ddlm Z ddlm Z ddlmZ ddlm Z dd lmZ dd lmZ ddlmZ ddlmZ dd lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z ddlm!Z! ddlm"Z" ddlm#Z# ddlm$Z$ ddlm%Z% dd lm&Z& ddlZerd! Zd" Z' G d# d$e Z(e e&j) ed% e&j) eoe d& G d' d(e( Z* e&j) ed) G d* d+e* Z+ G d, d-e( Z,e-d.k rdd/l.m/Z/ e/e0 dS dS )0a: 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_mkdirsafe_rmpath) serialrun)skip_on_access_denied)spawn_testproc) terminate TESTFN_PREFIX)UNICODE_SUFFIX)unittestc n ddl m} || S # t $ r t j Y d S w xY w)Nr r )psutil.testsr WindowsError traceback print_exc)pathrms {/builddir/build/BUILD/imunify360-venv-2.6.2/opt/imunify360/venv/lib64/python3.11/site-packages/psutil/tests/test_unicode.pyr r s s[ 322222 "2d88O " " "!!!!!! "s 44c d}t | } t | t | t |g }t j ||dz t |dz |t | t | dS # t t f$ r$ Y |t | t | dS w xY w# |t | t | w xY w)z`Return True if both the fs and the subprocess module can deal with a unicode file name. Nsuffixcmdz-2TF) r r r r shutilcopyfiler UnicodeEncodeErrorIOError)r- sproctestfns r* try_unicoder6 s E v & & &F F6F8,,, ...FTM""" eF ( eF eFs$ AB C!C CC "C*c e Zd ZdZd ZdS )BaseUnicodeTestNc j | j )t | j s| d d S d S )Nzcan't handle unicode str)funky_suffixr6 skipTest)selfs r* setUpzBaseUnicodeTest.setUp sF (t011 @mm$>??? )( @ @ )__name__ __module____qualname__r: r= r> r* r8 r8 s- L@ @ @ @ @r> r8 zASCII fsztoo much trouble on PYPY2c e Zd ZdZeZed Zed Zd Z d Z d Zd Zd Z ej eoed d Z ej e d d Z ej e d ej e d e d Zd Z ej e d ej e d ej ed d ZdS ) TestFSAPIsz1Test FS APIs with a funky, valid, UTF8 path name.c b t | j | _ t | j d S )Nr, )r r: funky_namer clss r* setUpClasszTestFSAPIs.setUpClass s- #3+;<<<3>"""""r> c . t | j d S N)r rF rG s r* tearDownClasszTestFSAPIs.tearDownClass s CN#####r> c t | j t rdnt d }t j 5 t j d | j t j | v cd d d S # 1 swxY w Y d S )N.ignore) isinstancerF strr warningscatch_warningssimplefilteroslistdir)r<