HOME


sh-3ll 1.0
DIR:/usr/local/lib64/python3.6/site-packages/psutil/tests/__pycache__/
Upload File :
Current File : //usr/local/lib64/python3.6/site-packages/psutil/tests/__pycache__/test_unicode.cpython-36.pyc
3

�[g;1�@sdZddlZddlZddlZddlZddlmZddlZddlmZddlm	Z	ddlm
Z
ddlmZddlm
Z
dd	lmZdd
lmZddlmZddlmZdd
lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$e�r�dd �Z!d!d"�Z%Gd#d$�d$e�Z&ej'j(d%d&�ej'j)ed'd(�ej'j)e�o�ed)d(�Gd*d+�d+e&����Z*ej'j)ed,d(�Gd-d.�d.e*��Z+Gd/d0�d0e&�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)�	terminatecCs6ddlm}y||�Stk
r0tj�YnXdS)Nr)r)�psutil.testsrZWindowsError�	traceback�	print_exc)�pathZrm�r"�E/usr/local/lib64/python3.6/site-packages/psutil/tests/test_unicode.pyrqs
rcCs�d}t|d�}z^y<t|�t|�t|gd�}tj||d�t|d�Wnttfk
rddSXdSWd|dk	r~t|�t|�XdS)z`Return True if both the fs and the subprocess module can
    deal with a unicode file name.
    N)�suffix)�cmdz-2FT)	rrrr�shutil�copyfile�UnicodeEncodeError�IOErrorr)r$�sprocZtestfnr"r"r#�try_unicode�s
r+cs0eZdZdZe�fdd��Z�fdd�Z�ZS)�BaseUnicodeTestNcsNt�j�d|_d|_|jdk	rJt|j�s2d|_nt|jd�|_t|j�dS)NFT)r$)r�
setUpClass�
skip_tests�
funky_name�funky_suffixr+rr)�cls)�	__class__r"r#r-�s


zBaseUnicodeTest.setUpClasscst�j�|jrtjd��dS)Nzcan't handle unicode str)r�setUpr.r�skip)�self)r2r"r#r3�s
zBaseUnicodeTest.setUp)�__name__�
__module__�__qualname__r0�classmethodr-r3�
__classcell__r"r")r2r#r,�sr,�serial)�namezASCII fs)�reasonztoo much trouble on PYPY2c@s�eZdZdZeZdd�Zdd�Zdd�Zdd	�Z	d
d�Z
ejj
eoDedd
�dd��Zejj
edd
�dd��Zejj
edd
�ejj
edd
�e�dd����Zdd�Zejj
edd
�ejj
edd
�ejj
edd
�dd����ZdS)�
TestFSAPIsz1Test FS APIs with a funky, valid, UTF8 path name.c
CsBt|jt�rdnd}tj��tjd�|jtj|�kSQRXdS)N�.�ignore)�
isinstancer/�str�warnings�catch_warnings�simplefilter�os�listdir)r5�herer"r"r#�expect_exact_path_match�s

z"TestFSAPIs.expect_exact_path_matchcCsb|jddg}|j|�}tj|j�}|j�}t|t�s8t�|j	�r^t
jj|�t
jj|j�ks^t�dS)Nz-cz2import time; [time.sleep(0.1) for x in range(100)])
r/r�psutil�Process�pid�exerArB�AssertionErrorrIrFr!�normcase)r5r%�subp�prMr"r"r#�
test_proc_exe�s
zTestFSAPIs.test_proc_execCsV|jddg}|j|�}tj|j�j�}t|t�s4t�|j	�rR|t
jj|j�ksRt�dS)Nz-cz2import time; [time.sleep(0.1) for x in range(100)])
r/rrJrKrLr<rArBrNrIrFr!�basename)r5r%rPr<r"r"r#�test_proc_name�s
zTestFSAPIs.test_proc_namecCs^|jddg}|j|�}tj|j�}|j�}x|D]}t|t�s0t�q0W|j	�rZ||ksZt�dS)Nz-cz2import time; [time.sleep(0.1) for x in range(100)])
r/rrJrKrL�cmdlinerArBrNrI)r5r%rPrQrU�partr"r"r#�test_proc_cmdline�s

zTestFSAPIs.test_proc_cmdlinec
Csl|jd}|jt|�t|�t|��tj�}|j�}WdQRXt|j�t	�sTt
�|j�rh||ksht
�dS)N�2)r/�
addCleanuprrrrJrK�cwdrArBrNrI)r5�dnamerQrZr"r"r#�
test_proc_cwd�s

zTestFSAPIs.test_proc_cwdzfails on PYPY + WINDOWS)r=c
Cs�tj�}t|j��}t|jd��t|j��}WdQRX||j�j}t|t	�sTt
�trh|rhtj
d��|j�r�tjj|�tjj|j�ks�t
�dS)N�rbzopen_files on BSD is broken)rJrK�setZ
open_files�openr/�popr!rArBrNrrr4rIrFrO)r5rQ�start�newr!r"r"r#�test_proc_open_files�s

zTestFSAPIs.test_proc_open_filesz
POSIX onlycCs�|j|jd�}yt|�}Wn&tk
r@tr2�n
tjd��YnXt|��6tj	�j
d�d}t|jt
�snt�|j|ks|t�WdQRXdS)N)r$z
not supported�unixr)rr0rr(rrr4rrJrK�net_connectionsrA�laddrrBrN)r5r<�sock�connr"r"r#�test_proc_net_connectionss
z$TestFSAPIs.test_proc_net_connectionszcan't list UNIX socketscCs�dd�}|j|jd�}yt|�}Wn&tk
rHtr:�n
tjd��YnXt|��8tj	dd�}||�}t
|jt�sxt
�|j|ks�t
�WdQRXdS)NcSs2x$|D]}tjj|j�jt�r|SqWtd��dS)Nzconnection not found)rFr!rSrf�
startswithr�
ValueError)�consrhr"r"r#�	find_socks
z2TestFSAPIs.test_net_connections.<locals>.find_sock)r$z
not supportedrd)�kind)rr0rr(rrr4rrJrerArfrBrN)r5rmr<rgrlrhr"r"r#�test_net_connectionss
zTestFSAPIs.test_net_connectionscCs,|jd}|jt|�t|�tj|�dS)NrX)r/rYrrrJ�
disk_usage)r5r[r"r"r#�test_disk_usage/s
zTestFSAPIs.test_disk_usagez
not supportedz&ctypes does not support unicode on PY2zunstable on PYPYcsxt|jd��b}dd���fdd�tj�j�D�}dd�|D�}�|�|ksNt�x|D]}t|t�sTt�qTWWdQRXdS)N)r$cSstjjtjj|��S)N)rFr!�realpathrO)rQr"r"r#�normpath?sz-TestFSAPIs.test_memory_maps.<locals>.normpathcsg|]}�|j��qSr")r!)�.0�x)rsr"r#�
<listcomp>Csz/TestFSAPIs.test_memory_maps.<locals>.<listcomp>cSsg|]}t|kr|�qSr")r)rtrur"r"r#rvFs)rr0rJrKZmemory_mapsrNrArB)r5Z
funky_pathZlibpathsr!r")rsr#�test_memory_maps5s
zTestFSAPIs.test_memory_mapsN)r6r7r8�__doc__rr0rIrRrTrWr\r�mark�skipifrrrcrrir
rrorqrrrwr"r"r"r#r>�s$



r>zunreliable on CIc@seZdZdZeZdd�ZdS)�TestFSAPIsWithInvalidPathz-Test FS APIs with a funky, invalid path name.cCsdS)NTr")r5r"r"r#rIRsz1TestFSAPIsWithInvalidPath.expect_exact_path_matchN)r6r7r8rxrr0rIr"r"r"r#r{Lsr{c@sJeZdZdZerendZejj	e
dd�ejj	eo4edd�dd���Z
dS)	�
TestNonFSAPISz&Unicode tests for non fs-related APIs.�èz
not supported)r=zsegfaults on PYPY + WINDOWScCs|tjj�}|j|d<|j|d�}tj|j�}|j�}x0|j�D]$\}}t	|t
�sTt�t	|t
�s>t�q>W|d|jksxt�dS)NZ	FUNNY_ARG)�env)rF�environ�copyr0rrJrKrL�itemsrArBrN)r5r~r*rQ�k�vr"r"r#�test_proc_environas

zTestNonFSAPIS.test_proc_environN)r6r7r8rxrrr0rryrzrrrr�r"r"r"r#r|\sr|)-rxrFr&rrC�
contextlibrrJrrrZpsutil._compatrrrrr	r
rrr
rrrrrrrrrrrrrrrrr+r,ryZxdist_grouprzr>r{r|r"r"r"r#�<module>IsZ