3 JZ[2@sdZddlZddlZddlZddlmZddlmZddlmZddlm Z ddlm Z ddlm Z dd l m Z dd l mZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%dd lm&Z&ddlZddlZd!d"Zd#d$Z'e re"j(d%d&j)d%d'Z*ne"d(Z*Gd)d*d*e+Z,e%j-e oe$d+e%j-ed,e%j-e'e# d-Gd.d/d/e,e%j.Z/e%j-e o*e$d+e%j-e'e* d0Gd1d2d2e,e%j.Z0e%j-e d3Gd4d5d5e%j.Z1Gd6d7d7e%j.Z2e3d8kree4dS)9a Notes about unicode handling in psutil ====================================== In psutil these are the 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 For a detailed explanation of how psutil handles unicode see: - https://github.com/giampaolo/psutil/issues/1040 - http://psutil.readthedocs.io/#unicode N)closing)BSD)OPENBSD)OSX)POSIX)WINDOWS)PY3)u)APPVEYOR)ASCII_FS)bind_unix_socket)chdir)copyload_shared_lib) create_exe)get_test_subprocess)HAS_CONNECTIONS_UNIX) HAS_ENVIRON)HAS_MEMORY_MAPS)mock) reap_children)run_test_module_by_name) safe_mkdir) safe_rmpath)skip_on_access_denied)TESTFILE_PREFIX)TESTFN)TESTFN_UNICODE)TRAVIS)unittest)unix_socket_pathc Cs8tr,yt|Stk r(tjYq4Xnt|SdS)N)r _safe_rmpathZ WindowsError traceback print_exc)pathr$$/usr/lib64/python3.6/test_unicode.pyr\s  rcCsTtrdSz>y t|t|t|gdWntk r>dSXdSWdtXdS)z`Return True if both the fs and the subprocess module can deal with a unicode file name. T)cmdFN)rrrrUnicodeEncodeErrorr)namer$r$r%subprocess_supports_unicodepsr)utf8sfsurrogateescapeufÀ€c@seZdZdZeddZeddZddZdd Zd d Z d d Z ddZ ddZ ddZ eje dddZeje deje deddZddZeje deje dddZdS) _BaseFSAPIsTestsNcCst|jt|jdS)N)r funky_namer)clsr$r$r% setUpClasss z_BaseFSAPIsTests.setUpClasscCstt|jdS)N)rrr-)r.r$r$r% tearDownClasssz_BaseFSAPIsTests.tearDownClasscCs tdS)N)r)selfr$r$r%tearDownsz_BaseFSAPIsTests.tearDowncCs tddS)Nzmust be implemented in subclass)NotImplementedError)r1r$r$r%expect_exact_path_matchsz(_BaseFSAPIsTests.expect_exact_path_matchcCsHt|jgd}tj|j}|j}|j|t|jrD|j ||jdS)N)r&) rr-psutilProcesspidexeassertIsInstancestrr4 assertEqual)r1subppr8r$r$r% test_proc_exes   z_BaseFSAPIsTests.test_proc_exec Cst|jgd}trRtjdtjtjd }tj |j j }|j sFt WdQRXntj |j j }|j|t|jr|j|tjj|jdS)N)r&z psutil._psplatform.cext.proc_exe) side_effect)rr-rrpatchr5 AccessDeniedosgetpidr6r7r(calledAssertionErrorr9r:r4r;r#basename)r1r<mr(r$r$r%test_proc_names z_BaseFSAPIsTests.test_proc_namecCsXt|jgd}tj|j}|j}x|D]}|j|tq(W|jrT|j ||jgdS)N)r&) rr-r5r6r7cmdliner9r:r4r;)r1r<r=rIpartr$r$r%test_proc_cmdlines  z"_BaseFSAPIsTests.test_proc_cmdlinec Csj|jd}|jt|t|t|tj}|j}WdQRX|j|jt |j rf|j ||dS)N2) r- addCleanuprrr r5r6cwdr9r:r4r;)r1dnamer=rNr$r$r% test_proc_cwds   z_BaseFSAPIsTests.test_proc_cwdc Cstj}t|j}t|jdt|j}WdQRX||jj}|j|t t rf| rf|j dS|j r|j tjj|tjj|jdS)Nrbzopen_files on BSD is broken)r5r6setZ open_filesopenr-popr#r9r:rZskipTestr4r;rBnormcase)r1r=startnewr#r$r$r%test_proc_open_filess    z%_BaseFSAPIsTests.test_proc_open_filesz POSIX onlycCstjj|j}t|d}y t|}Wn&tk rLtr>n tj dYnXt |8t j j dd}|j|jtts|j|j|WdQRXWdQRXdS)N)suffixz not supportedunixr)rBr#rFr-rr r'rrSkipTestrr5r6Z connectionsr9laddrr:rr;)r1rYr(sockconnr$r$r%test_proc_connectionss   z&_BaseFSAPIsTests.test_proc_connectionszcan't list UNIX socketscCsdd}tjj|j}t|d}y t|}Wn&tk rTtrFn tj dYnXt |:t j dd}t s||}|j|jt|j|j|WdQRXWdQRXdS)NcSs2x$|D]}tjj|jjtr|SqWtddS)Nzconnection not found)rBr#rFr\ startswithr ValueError)consr^r$r$r% find_socks z8_BaseFSAPIsTests.test_net_connections..find_sock)rYz not supportedrZ)Zkind)rBr#rFr-rr r'rrr[rr5Znet_connectionsrr9r\r:r;)r1rcrYr(r]rbr^r$r$r%test_net_connectionss    z%_BaseFSAPIsTests.test_net_connectionscCs,|jd}|jt|t|tj|dS)NrL)r-rMrrr5Z disk_usage)r1rOr$r$r%test_disk_usage s  z _BaseFSAPIsTests.test_disk_usagez not supportedz&ctypes does not support unicode on PY2c svt|jd`}ddfddtjjD}dd|D}|j||x|D]}|j|tqTWWdQRXdS)N)Z dst_prefixcSstjjtjj|S)N)rBr#realpathrU)r=r$r$r%normpathsz3_BaseFSAPIsTests.test_memory_maps..normpathcsg|]}|jqSr$)r#).0x)rgr$r% sz5_BaseFSAPIsTests.test_memory_maps..cSsg|]}t|kr|qSr$)r)rhrir$r$r%rjs)rr-r5r6Z memory_mapsZassertInr9r:)r1Z funky_pathZlibpathsr#r$)rgr%test_memory_mapss  z!_BaseFSAPIsTests.test_memory_maps)__name__ __module__ __qualname__r- classmethodr/r0r2r4r>rHrKrPrXrskipIfrr_rrrdrerrrkr$r$r$r%r,s"       r,zunreliable on TRAVISzASCII fsz"subprocess can't deal with unicodec@s eZdZdZeZeddZdS) TestFSAPIsz1Test FS APIs with a funky, valid, UTF8 path name.c CsNtrdSt|jtrdntd}tjtjd|jtj |kSQRXdS)NT.ignore) r isinstancer-r:r warningscatch_warnings simplefilterrBlistdir)r.herer$r$r%r4(s   z"TestFSAPIs.expect_exact_path_matchN)rlrmrn__doc__rr-ror4r$r$r$r%rq srqz*subprocess can't deal with invalid unicodec@s eZdZdZeZeddZdS)TestFSAPIsWithInvalidPathz-Test FS APIs with a funky, invalid path name.cCsdS)NTr$)r.r$r$r%r4<sz1TestFSAPIsWithInvalidPath.expect_exact_path_matchN)rlrmrnrz INVALID_NAMEr-ror4r$r$r$r%r{5sr{z WINDOWS onlyc@seZdZddZdS)TestWinProcessNamec CsFtjdtjtjd$}|jtjjt |j s8t WdQRXdS)Nz psutil._psplatform.cext.proc_exe)r?) rr@r5rArBrCr9r6r(r:rDrE)r1rGr$r$r%test_name_typeEsz!TestWinProcessName.test_name_typeN)rlrmrnr~r$r$r$r%r}Bsr}c@s.eZdZdZddZeje dddZdS) TestNonFSAPISz&Unicode tests for non fs-related APIs.cCs tdS)N)r)r1r$r$r%r2WszTestNonFSAPIS.tearDownz not supportedcCs~tjj}trtnd}||d<t|d}tj|j}|j}x,|j D] \}}|j |t |j |t qFW|j |d|dS)NèZ FUNNY_ARG)env) rBenvironcopyrrrr5r6r7itemsr9r:r;)r1rZ funky_strZsprocr=kvr$r$r%test_proc_environZs     zTestNonFSAPIS.test_proc_environN) rlrmrnrzr2rrprrr$r$r$r%rTsr__main__)5rzrBr!ru contextlibrr5rrrrrZpsutil._compatrr Z psutil.testsr r r r rrrrrrrrrrrr rrrrrrrr)encodedecoder|objectr,rpZTestCaserqr{r}rrl__file__r$r$r$r%4sr