<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
3
JZ[2                 @   s  d Z ddlZddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! ddlm"Z" ddlm#Z# ddlm$Z$ ddlm%Z% dd lm&Z& ddlZddlZd!d" Zd#d$ Z'ere"j(d%d& j)d%d'Z*ne"d( Z*G d)d* d*e+Z,e%j-e	oe$d+e%j-ed,e%j-e'e# d-G d.d/ d/e,e%j.Z/e%j-e	o*e$d+e%j-e'e* d0G d1d2 d2e,e%j.Z0e%j-e d3G d4d5 d5e%j.Z1G d6d7 d7e%j.Z2e3d8kree4 dS )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             C   s8   t r,yt| S  tk
r(   tj  Y q4X nt| S d S )N)r
   _safe_rmpathZWindowsError	traceback	print_exc)path r$   $/usr/lib64/python3.6/test_unicode.pyr   \   s    r   c             C   sT   t rdS z>y t|  t|  t| gd W n tk
r>   dS X dS W dt  X dS )z`Return True if both the fs and the subprocess module can
    deal with a unicode file name.
    T)cmdFN)r   r   r   r   UnicodeEncodeErrorr   )namer$   r$   r%   subprocess_supports_unicodep   s    r)   utf8s   fsurrogateescapeu   fÀc               @   s   e Zd ZdZedd Zedd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zeje ddd Zeje deje de dd Zdd Zeje deje ddd ZdS ) _BaseFSAPIsTestsNc             C   s   t | j t| j d S )N)r   
funky_namer   )clsr$   r$   r%   
setUpClass   s    
z_BaseFSAPIsTests.setUpClassc             C   s   t   t| j d S )N)r   r   r-   )r.   r$   r$   r%   tearDownClass   s    z_BaseFSAPIsTests.tearDownClassc             C   s
   t   d S )N)r   )selfr$   r$   r%   tearDown   s    z_BaseFSAPIsTests.tearDownc             C   s   t dd S )Nzmust be implemented in subclass)NotImplementedError)r1   r$   r$   r%   expect_exact_path_match   s    z(_BaseFSAPIsTests.expect_exact_path_matchc             C   sH   t | jgd}tj|j}|j }| j|t | j rD| j	|| j d S )N)r&   )
r   r-   psutilProcesspidexeassertIsInstancestrr4   assertEqual)r1   subppr8   r$   r$   r%   test_proc_exe   s    z_BaseFSAPIsTests.test_proc_exec             C   s   t | jgd}trRtjdtjtj d }tj	|j
j }|jsFtW d Q R X ntj	|j
j }| j|t | j r| j|tjj| j d S )N)r&   z psutil._psplatform.cext.proc_exe)side_effect)r   r-   r   r   patchr5   AccessDeniedosgetpidr6   r7   r(   calledAssertionErrorr9   r:   r4   r;   r#   basename)r1   r<   mr(   r$   r$   r%   test_proc_name   s    z_BaseFSAPIsTests.test_proc_namec             C   sX   t | jgd}tj|j}|j }x|D ]}| j|t q(W | j rT| j	|| jg d S )N)r&   )
r   r-   r5   r6   r7   cmdliner9   r:   r4   r;   )r1   r<   r=   rI   partr$   r$   r%   test_proc_cmdline   s    
z"_BaseFSAPIsTests.test_proc_cmdlinec          
   C   sj   | j d }| jt| t| t| tj }|j }W d Q R X | j|j t	 | j
 rf| j|| d S )N2)r-   
addCleanupr   r   r   r5   r6   cwdr9   r:   r4   r;   )r1   dnamer=   rN   r$   r$   r%   test_proc_cwd   s    

z_BaseFSAPIsTests.test_proc_cwdc             C   s   t j }t|j }t| jd t|j }W d Q R X || j j}| j|t	 t
rf| rf| jdS | j r| jtjj|tjj| j d S )Nrbzopen_files on BSD is broken)r5   r6   setZ
open_filesopenr-   popr#   r9   r:   r   ZskipTestr4   r;   rB   normcase)r1   r=   startnewr#   r$   r$   r%   test_proc_open_files   s    

z%_BaseFSAPIsTests.test_proc_open_filesz
POSIX onlyc             C   s   t jj| j}t|d}yt|}W n& tk
rL   tr> n
tj	dY nX t
|8 tj jdd }| j|jt ts| j|j| W d Q R X W d Q R X d S )N)suffixznot supportedunixr   )rB   r#   rF   r-   r   r   r'   r   r   SkipTestr   r5   r6   Zconnectionsr9   laddrr:   r   r;   )r1   rY   r(   sockconnr$   r$   r%   test_proc_connections   s    
z&_BaseFSAPIsTests.test_proc_connectionszcan't list UNIX socketsc             C   s   dd }t jj| j}t|d}yt|}W n& tk
rT   trF n
tj	dY nX t
|: tjdd}ts||}| j|jt | j|j| W d Q R X W d Q R X d S )Nc             S   s2   x$| D ]}t jj|jjtr|S qW tdd S )Nzconnection not found)rB   r#   rF   r\   
startswithr   
ValueError)consr^   r$   r$   r%   	find_sock   s    
z8_BaseFSAPIsTests.test_net_connections.<locals>.find_sock)rY   znot supportedrZ   )Zkind)rB   r#   rF   r-   r   r   r'   r   r   r[   r   r5   Znet_connectionsr   r9   r\   r:   r;   )r1   rc   rY   r(   r]   rb   r^   r$   r$   r%   test_net_connections   s    
z%_BaseFSAPIsTests.test_net_connectionsc             C   s,   | j d }| jt| t| tj| d S )NrL   )r-   rM   r   r   r5   Z
disk_usage)r1   rO   r$   r$   r%   test_disk_usage	  s    
z _BaseFSAPIsTests.test_disk_usageznot supportedz&ctypes does not support unicode on PY2c                sv   t | jd`}dd   fddtj j D }dd |D }| j || x|D ]}| j|t qTW W d Q R X d S )N)Z
dst_prefixc             S   s   t jjt jj| S )N)rB   r#   realpathrU   )r=   r$   r$   r%   normpath  s    z3_BaseFSAPIsTests.test_memory_maps.<locals>.normpathc                s   g | ]} |j qS r$   )r#   ).0x)rg   r$   r%   
<listcomp>  s   z5_BaseFSAPIsTests.test_memory_maps.<locals>.<listcomp>c             S   s   g | ]}t |kr|qS r$   )r   )rh   ri   r$   r$   r%   rj     s    )r   r-   r5   r6   Zmemory_mapsZassertInr9   r:   )r1   Z
funky_pathZlibpathsr#   r$   )rg   r%   test_memory_maps  s    

z!_BaseFSAPIsTests.test_memory_maps)__name__
__module____qualname__r-   classmethodr/   r0   r2   r4   r>   rH   rK   rP   rX   r   skipIfr   r_   r   r   rd   re   r   r   rk   r$   r$   r$   r%   r,      s"   	r,   zunreliable on TRAVISzASCII fsz"subprocess can't deal with unicodec               @   s    e Zd ZdZeZedd ZdS )
TestFSAPIsz1Test FS APIs with a funky, valid, UTF8 path name.c          
   C   sN   t rdS t| jtrdntd}tj  tjd | jtj	|kS Q R X d S )NT.ignore)
r   
isinstancer-   r:   r	   warningscatch_warningssimplefilterrB   listdir)r.   herer$   r$   r%   r4   (  s    

z"TestFSAPIs.expect_exact_path_matchN)rl   rm   rn   __doc__r   r-   ro   r4   r$   r$   r$   r%   rq      s   rq   z*subprocess can't deal with invalid unicodec               @   s    e Zd ZdZeZedd ZdS )TestFSAPIsWithInvalidPathz-Test FS APIs with a funky, invalid path name.c             C   s   dS )NTr$   )r.   r$   r$   r%   r4   <  s    z1TestFSAPIsWithInvalidPath.expect_exact_path_matchN)rl   rm   rn   rz   INVALID_NAMEr-   ro   r4   r$   r$   r$   r%   r{   5  s   r{   zWINDOWS onlyc               @   s   e Zd Zdd ZdS )TestWinProcessNamec          
   C   sF   t jdtjtj d$}| jtj j t	 |j
s8tW d Q R X d S )Nz psutil._psplatform.cext.proc_exe)r?   )r   r@   r5   rA   rB   rC   r9   r6   r(   r:   rD   rE   )r1   rG   r$   r$   r%   test_name_typeE  s    z!TestWinProcessName.test_name_typeN)rl   rm   rn   r~   r$   r$   r$   r%   r}   B  s   r}   c               @   s.   e Zd ZdZdd Zeje ddd ZdS )TestNonFSAPISz&Unicode tests for non fs-related APIs.c             C   s
   t   d S )N)r   )r1   r$   r$   r%   r2   W  s    zTestNonFSAPIS.tearDownznot supportedc             C   s~   t jj }trtnd}||d< t|d}tj|j}|j }x,|j	 D ] \}}| j
|t | j
|t qFW | j|d | d S )N   èZ	FUNNY_ARG)env)rB   environcopyr   r   r   r5   r6   r7   itemsr9   r:   r;   )r1   r   Z	funky_strZsprocr=   kvr$   r$   r%   test_proc_environZ  s    

zTestNonFSAPIS.test_proc_environN)	rl   rm   rn   rz   r2   r   rp   r   r   r$   r$   r$   r%   r   T  s   r   __main__)5rz   rB   r!   ru   
contextlibr   r5   r   r   r   r   r   Zpsutil._compatr   r	   Zpsutil.testsr
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r   r   r   r   r   r   r)   encodedecoder|   objectr,   rp   ZTestCaserq   r{   r}   r   rl   __file__r$   r$   r$   r%   <module>4   sr   
 



