<!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>
B
    Œ=Ý_«0  ã               @   s<  d Z ddl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er¢d!d"„ Zd#d$„ Z'G d%d&„ d&eƒZ(e e& )ed'¡e& )eoÔe d(¡G d)d*„ d*e(ƒƒƒƒZ*e& )ed+¡G d,d-„ d-e*ƒƒZ+G d.d/„ d/e(ƒZ,e-d0kr8dd1l.m/Z/ e/e0ƒ dS )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)Úunittestc             C   s6   ddl m} y|| ƒS  tk
r0   t ¡  Y nX d S )Nr   )r   )Úpsutil.testsr   ZWindowsErrorÚ	tracebackÚ	print_exc)ÚpathZrm© r$   úL/opt/alt/python37/lib64/python3.7/site-packages/psutil/tests/test_unicode.pyr   s   s
    r   c          	   C   sŒ   d}t | d}z^y<t|ƒ t|ƒ t|gd}t ||d ¡ t|d ƒ W n ttfk
rd   dS X dS W d|dk	r~t|ƒ t|ƒ X dS )z`Return True if both the fs and the subprocess module can
    deal with a unicode file name.
    N)Úsuffix)Úcmdz-2FT)	r   r   r   r   ÚshutilÚcopyfileÚUnicodeEncodeErrorÚIOErrorr   )r&   ÚsprocZtestfnr$   r$   r%   Útry_unicode…   s    
r-   c               @   s   e Zd ZdZdd„ ZdS )ÚBaseUnicodeTestNc             C   s"   | j d k	rt| j ƒs|  d¡‚d S )Nzcan't handle unicode str)Úfunky_suffixr-   ÚskipTest)Úselfr$   r$   r%   ÚsetUp£   s    

zBaseUnicodeTest.setUp)Ú__name__Ú
__module__Ú__qualname__r/   r2   r$   r$   r$   r%   r.       s   r.   zASCII fsztoo much trouble on PYPY2c               @   sä   e Zd ZdZeZedd„ ƒZedd„ ƒZdd„ Z	dd	„ Z
d
d„ Zdd„ Zdd„ Ze eoZed¡dd„ ƒZe e d¡dd„ ƒZe e d¡e e d¡eƒ dd„ ƒƒƒZdd„ Ze e d¡e e d¡e ed¡dd„ ƒƒƒZd S )!Ú
TestFSAPIsz1Test FS APIs with a funky, valid, UTF8 path name.c             C   s   t | jd| _t| jƒ d S )N)r&   )r   r/   Ú
funky_namer   )Úclsr$   r$   r%   Ú
setUpClass±   s    zTestFSAPIs.setUpClassc             C   s   t | jƒ d S )N)r   r7   )r8   r$   r$   r%   ÚtearDownClass¶   s    zTestFSAPIs.tearDownClassc          	   C   sF   t | jtƒrdntdƒ}t ¡  t d¡ | jt |¡kS Q R X d S )NÚ.Úignore)	Ú
isinstancer7   Ústrr   ÚwarningsÚcatch_warningsÚsimplefilterÚosÚlistdir)r1   Úherer$   r$   r%   Úexpect_exact_path_matchº   s    

z"TestFSAPIs.expect_exact_path_matchc             C   sZ   | j | jgd}t |j¡}| ¡ }|  |t¡ |  ¡ rV|  	t
j |¡t
j | j¡¡ d S )N)r'   )r   r7   ÚpsutilÚProcessÚpidÚexeÚassertIsInstancer>   rE   ÚassertEqualrB   r#   Únormcase)r1   ÚsubpÚprI   r$   r$   r%   Útest_proc_exeÄ   s    zTestFSAPIs.test_proc_exec             C   sN   | j | jgd}t |j¡ ¡ }|  |t¡ |  ¡ rJ|  	|t
j | j¡¡ d S )N)r'   )r   r7   rF   rG   rH   ÚnamerJ   r>   rE   rK   rB   r#   Úbasename)r1   rM   rP   r$   r$   r%   Útest_proc_nameÍ   s
    zTestFSAPIs.test_proc_namec             C   sZ   | j | jgd}t |j¡}| ¡ }x|D ]}|  |t¡ q*W |  ¡ rV|  	|| jg¡ d S )N)r'   )
r   r7   rF   rG   rH   ÚcmdlinerJ   r>   rE   rK   )r1   rM   rN   rS   Úpartr$   r$   r%   Útest_proc_cmdlineÔ   s    
zTestFSAPIs.test_proc_cmdlinec          	   C   sj   | j d }|  t|¡ t|ƒ t|ƒ t ¡ }| ¡ }W d Q R X |  | ¡ t	¡ |  
¡ rf|  ||¡ d S )NÚ2)r7   Ú
addCleanupr   r   r   rF   rG   ÚcwdrJ   r>   rE   rK   )r1   ÚdnamerN   rX   r$   r$   r%   Útest_proc_cwdÝ   s    

zTestFSAPIs.test_proc_cwdzfails on PYPY + WINDOWSc          	   C   sŽ   t  ¡ }t| ¡ ƒ}t| jdƒ t| ¡ ƒ}W d Q R X ||  ¡ j}|  |t	¡ t
rd|sd|  d¡S |  ¡ rŠ|  tj |¡tj | j¡¡ d S )NÚrbzopen_files on BSD is broken)rF   rG   ÚsetZ
open_filesÚopenr7   Úpopr#   rJ   r>   r   r0   rE   rK   rB   rL   )r1   rN   ÚstartÚnewr#   r$   r$   r%   Útest_proc_open_filesè   s    
zTestFSAPIs.test_proc_open_filesz
POSIX onlyc          	   C   sŒ   | j | jd}yt|ƒ}W n& tk
r@   tr2‚ n
t d¡‚Y nX t|ƒ8 t 	¡  
d¡d }|  |jt¡ ts~|  |j|¡ W d Q R X d S )N)r&   znot supportedÚunixr   )r   r/   r   r*   r   r   ÚSkipTestr   rF   rG   ZconnectionsrJ   Úladdrr>   r   rK   )r1   rP   ÚsockÚconnr$   r$   r%   Útest_proc_connections÷   s    
z TestFSAPIs.test_proc_connectionszcan't list UNIX socketsc          	   C   s–   dd„ }| j | jd}yt|ƒ}W n& tk
rH   tr:‚ n
t d¡‚Y nX t|ƒ: tj	dd}t
sˆ||ƒ}|  |jt¡ |  |j|¡ W d Q R X d S )Nc             S   s2   x$| D ]}t j |j¡ t¡r|S qW tdƒ‚d S )Nzconnection not found)rB   r#   rQ   rd   Ú
startswithr   Ú
ValueError)Úconsrf   r$   r$   r%   Ú	find_sock  s    
z2TestFSAPIs.test_net_connections.<locals>.find_sock)r&   znot supportedrb   )Úkind)r   r/   r   r*   r   r   rc   r   rF   Znet_connectionsr   rJ   rd   r>   rK   )r1   rk   rP   re   rj   rf   r$   r$   r%   Útest_net_connections  s    
zTestFSAPIs.test_net_connectionsc             C   s,   | j d }|  t|¡ t|ƒ t |¡ d S )NrV   )r7   rW   r   r   rF   Ú
disk_usage)r1   rY   r$   r$   r%   Útest_disk_usage"  s    
zTestFSAPIs.test_disk_usageznot supportedz&ctypes does not support unicode on PY2zunstable on PYPYc          	      sv   t | jd`}dd„ ‰ ‡ fdd„t ¡  ¡ D ƒ}dd„ |D ƒ}|  ˆ |ƒ|¡ x|D ]}|  |t¡ qTW W d Q R X d S )N)r&   c             S   s   t j t j | ¡¡S )N)rB   r#   ÚrealpathrL   )rN   r$   r$   r%   Únormpath/  s    z-TestFSAPIs.test_memory_maps.<locals>.normpathc                s   g | ]}ˆ |j ƒ‘qS r$   )r#   )Ú.0Úx)rq   r$   r%   ú
<listcomp>1  s   z/TestFSAPIs.test_memory_maps.<locals>.<listcomp>c             S   s   g | ]}t |kr|‘qS r$   )r   )rr   rs   r$   r$   r%   rt   4  s    )r   r/   rF   rG   Zmemory_mapsZassertInrJ   r>   )r1   Z
funky_pathZlibpathsr#   r$   )rq   r%   Útest_memory_maps(  s    

zTestFSAPIs.test_memory_mapsN)r3   r4   r5   Ú__doc__r   r/   Úclassmethodr9   r:   rE   rO   rR   rU   rZ   r   ÚskipIfr   r   ra   r   rg   r   r   rm   ro   r   r   ru   r$   r$   r$   r%   r6   ©   s$   
		r6   zunreliable on CIc               @   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$   )r8   r$   r$   r%   rE   ?  s    z1TestFSAPIsWithInvalidPath.expect_exact_path_matchN)r3   r4   r5   rv   r   r/   rw   rE   r$   r$   r$   r%   ry   :  s   ry   c               @   sB   e Zd ZdZerendZe e	 d¡e e
o.ed¡dd„ ƒƒZdS )ÚTestNonFSAPISz&Unicode tests for non fs-related APIs.õ   Ã¨znot supportedzsegfaults on PYPY + WINDOWSc             C   sx   t j ¡ }| j|d< | j|d}t |j¡}| ¡ }x,| ¡ D ] \}}|  	|t
¡ |  	|t
¡ q>W |  |d | j¡ d S )NZ	FUNNY_ARG)Úenv)rB   ÚenvironÚcopyr/   r   rF   rG   rH   ÚitemsrJ   r>   rK   )r1   r|   r,   rN   ÚkÚvr$   r$   r%   Útest_proc_environN  s    

zTestNonFSAPIS.test_proc_environN)r3   r4   r5   rv   r   r   r/   r   rx   r   r   r   r‚   r$   r$   r$   r%   rz   J  s   rz   Ú__main__)Úrun_from_name)1rv   rB   r(   r!   r?   Ú
contextlibr   rF   r   r   r   r   Zpsutil._compatr   r   r    r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r-   r.   rx   r6   ry   rz   r3   Zpsutil.tests.runnerr„   Ú__file__r$   r$   r$   r%   Ú<module>J   sd   	
 
