
    ;^h(                     ^   S r SSKrSSKrSSKrSSKJr  SSKrSSKJr  SSKJr  SSKJ	r	  SSK
Jr  SSK
Jr  SS	K
Jr  SS
K
Jr  SSK
Jr  SSK
Jr  SSK
Jr  SSK
Jr  SSK
Jr  SSK
Jr  SSK
Jr  SSK
Jr  SSK
Jr  SSK
Jr  SSK
Jr  SSK
Jr  SSK
Jr  SSK
Jr  SSK
Jr  SSK
Jr  SSK
Jr  S r  " S S\5      r!\RD                  RG                  SS 9\RD                  RI                  \S!S"9 " S# S$\!5      5       5       r%\RD                  RI                  \S%S"9 " S& S'\%5      5       r& " S( S)\!5      r'g)*aY  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:
    * sys.getfilesystemencodeerrors() or "surrogatescape" on POSIX and
      "replace" on Windows.

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)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)	terminatec                 x   Sn[        U S9n [        U5        [        U5        [        U/S9n[        R
                  " X"S-   5        [        US-   5         Ub  [        U5        [        U5        g! [        [        4 a     Ub  [        U5        [        U5        gf = f! Ub  [        U5        [        U5        f = f)zXReturn True if both the fs and the subprocess module can
deal with a unicode file name.
Nsuffix)cmdz-2TF)	r   r   r   r   shutilcopyfiler   UnicodeEncodeErrorOSError)r   sproctestfns      K/var/www/html/env/lib/python3.13/site-packages/psutil/tests/test_unicode.pytry_unicoder'   e   s     Ev&FFfF8,.FTM" eF (  eF
 eFs$   AA/ /B?B BB B9c                   B   ^  \ rS rSrSr\U 4S j5       rU 4S jrSrU =r	$ )BaseUnicodeTest   Nc                    > [         TU ]  5         SU l        S U l        U R                  bP  [        U R                  5      (       d  SU l        g [        U R                  S9U l        [        U R                  5        g g )NFTr   )super
setUpClass
skip_tests
funky_namefunky_suffixr'   r   r   )cls	__class__s    r&   r-   BaseUnicodeTest.setUpClass   sc    's//00!%!+33C3C!Dcnn- (    c                 p   > [         TU ]  5         U R                  (       a  [        R                  " S5      eg )Nzcan't handle unicode str)r,   setUpr.   r   skip)selfr2   s    r&   r6   BaseUnicodeTest.setUp   s(    ??++899 r4    )
__name__
__module____qualname____firstlineno__r0   classmethodr-   r6   __static_attributes____classcell__)r2   s   @r&   r)   r)      s$    L	. 	.: :r4   r)   serial)namezASCII fsreasonc                      \ rS rSrSr\rS rS rS r	S r
S r\R                  R                  \=(       a    \SS	9S
 5       r\R                  R                  \(       + SS	9S 5       r\R                  R                  \(       + SS	9\R                  R                  \(       + SS	9\" 5       S 5       5       5       rS r\R                  R                  \(       + SS	9\R                  R                  \SS	9S 5       5       rSrg)
TestFSAPIs   z1Test FS APIs with a funky, valid, UTF8 path name.c                     [         R                  " 5          [         R                  " S5        U R                  [        R
                  " S5      ;   sS S S 5        $ ! , (       d  f       g = f)Nignore.)warningscatch_warningssimplefilterr/   oslistdirr8   s    r&   expect_exact_path_match"TestFSAPIs.expect_exact_path_match   s<    $$&!!(+??bjjo5 '&&s   9A
A'c                    U R                   SS/nU R                  U5      n[        R                  " UR                  5      nUR                  5       n[        U[        5      (       d   eU R                  5       (       aM  [        R                  R                  U5      [        R                  R                  U R                   5      :X  d   eg g Nz-cz2import time; [time.sleep(0.1) for x in range(100)])r/   r   psutilProcesspidexe
isinstancestrrR   rO   pathnormcase)r8   r   subpprY   s        r&   test_proc_exeTestFSAPIs.test_proc_exe   s    OO@

 ""3'NN488$eeg#s####''))77##C(BGG,<,<T__,MMMM *r4   c                 X   U R                   SS/nU R                  U5      n[        R                  " UR                  5      R                  5       n[        U[        5      (       d   eU R                  5       (       a0  U[        R                  R                  U R                   5      :X  d   eg g rU   )r/   r   rV   rW   rX   rC   rZ   r[   rR   rO   r\   basename)r8   r   r^   rC   s       r&   test_proc_nameTestFSAPIs.test_proc_name   s    OO@

 ""3'~~dhh',,.$$$$$''))277++DOO<<<< *r4   c                    U R                   SS/nU R                  U5      n[        R                  " UR                  5      nUR                  5       nU H  n[        U[        5      (       a  M   e   U R                  5       (       a  XA:X  d   eg g rU   )	r/   r   rV   rW   rX   cmdlinerZ   r[   rR   )r8   r   r^   r_   rg   parts         r&   test_proc_cmdlineTestFSAPIs.test_proc_cmdline   s    OO@

 ""3'NN488$))+DdC(((( ''))>!> *r4   c                 ~   U R                   S-   nU R                  [        U5        [        U5        [	        U5         [
        R                  " 5       nUR                  5       nS S S 5        [        WR                  5       [        5      (       d   eU R                  5       (       a	  WU:X  d   eg g ! , (       d  f       NR= fN2)r/   
addCleanupr   r   r   rV   rW   cwdrZ   r[   rR   )r8   dnamer_   ro   s       r&   test_proc_cwdTestFSAPIs.test_proc_cwd   s    #%U+55\ A%%'C  !%%'3''''''))%<< *	 \s   &B..
B<zfails on PYPY + WINDOWSrD   c                 n   [         R                  " 5       n[        UR                  5       5      n[	        U R
                  S5         [        UR                  5       5      nS S S 5        WU-
  R                  5       R                  n[        U[        5      (       d   e[        (       a  U(       d  [        R                  " S5      eU R                  5       (       aM  [        R                  R                  U5      [        R                  R                  U R
                  5      :X  d   eg g ! , (       d  f       N= f)Nrbzopen_files on BSD is broken)rV   rW   set
open_filesopenr/   popr\   rZ   r[   r   r   r7   rR   rO   r]   )r8   r_   startnewr\   s        r&   test_proc_open_filesTestFSAPIs.test_proc_open_files   s    NNALLN#$//4(alln%C )e  "''$$$$$3t++;<<''))77##D)RWW-=-=doo-NNNN * )(s   D&&
D4z
POSIX onlyc                 L   U R                  U R                  S9n[        U5      n[        U5         [        R
                  " 5       R                  S5      S   n[        UR                  [        5      (       d   eUR                  U:X  d   e S S S 5        g ! , (       d  f       g = f)Nr   unixr   )
r   r0   r   r   rV   rW   net_connectionsrZ   laddrr[   )r8   rC   sockconns       r&   test_proc_net_connections$TestFSAPIs.test_proc_net_connections   sx    d&7&78%T]>>#33F;A>Ddjj#....::%%% ]]s   AB
B#zcan't list UNIX socketsc                 <   S nU R                  U R                  S9n[        U5      n[        U5         [        R
                  " SS9nU" U5      n[        UR                  [        5      (       d   eUR                  U:X  d   e S S S 5        g ! , (       d  f       g = f)Nc                     U  HH  n[         R                  R                  UR                  5      R	                  [
        5      (       d  MF  Us  $    [        S5      e)Nzconnection not found)rO   r\   rc   r   
startswithr   
ValueError)consr   s     r&   	find_sock2TestFSAPIs.test_net_connections.<locals>.find_sock   sC    77##DJJ/::=IIK  344r4   r   r~   )kind)	r   r0   r   r   rV   r   rZ   r   r[   )r8   r   rC   r   r   r   s         r&   test_net_connectionsTestFSAPIs.test_net_connections   s|    	5 d&7&78%T]))v6DT?Ddjj#....::%%%	 ]]s   AB
Bc                     U R                   S-   nU R                  [        U5        [        U5        [        R
                  " U5        g rl   )r/   rn   r   r   rV   
disk_usage)r8   rp   s     r&   test_disk_usageTestFSAPIs.test_disk_usage   s4    #%U+5% r4   not supportedzunstable on PYPYc                    [        U R                  S9 nS n[        R                  " 5       R	                  5        Vs/ s H  o2" UR
                  5      PM     nnU Vs/ s H  n[        U;   d  M  UPM     nnU" U5      U;   d   eU H  n[        U[        5      (       a  M   e   S S S 5        g s  snf s  snf ! , (       d  f       g = f)Nr   c                 z    [         R                  R                  [         R                  R                  U 5      5      $ )N)rO   r\   realpathr]   )r_   s    r&   normpath-TestFSAPIs.test_memory_maps.<locals>.normpath  s&    ww''(8(8(;<<r4   )	r   r0   rV   rW   memory_mapsr\   r   rZ   r[   )r8   
funky_pathr   xlibpathsr\   s         r&   test_memory_mapsTestFSAPIs.test_memory_maps  s     !(9(9:j= +1..*:*F*F*H*HQ *H   $,B8a}/A8HBJ'8333 !$,,,, ! ;:
 C ;:s4   )B9B/B9B40B46(B9"B9/
B99
Cr:   N)r;   r<   r=   r>   __doc__r   r0   rR   r`   rd   ri   rq   r   markskipifr   r   r{   r   r   r   r   r   r   r
   r   r@   r:   r4   r&   rG   rG      s+    <!L6N
="	  [[(1JKO LO [[E	,7& 8& [[E	,7[[$$-F   &  8
&! [[O+OD[[%78- 9 E-r4   rG   zunreliable on CIc                   "    \ rS rSrSr\rS rSrg)TestFSAPIsWithInvalidPathi  z-Test FS APIs with a funky, invalid path name.c                     g)NTr:   rQ   s    r&   rR   1TestFSAPIsWithInvalidPath.expect_exact_path_match  s    r4   r:   N)	r;   r<   r=   r>   r   r   r0   rR   r@   r:   r4   r&   r   r     s    7)Lr4   r   c                       \ rS rSrSr\r\R                  R                  \
(       + SS9\R                  R                  \=(       a    \SS9S 5       5       rSrg)	TestNonFSAPISi'  z&Unicode tests for non fs-related APIs.r   rD   zsegfaults on PYPY + WINDOWSc                    [         R                  R                  5       nU R                  US'   U R	                  US9n[
        R                  " UR                  5      nUR                  5       nUR                  5        H3  u  pE[        U[        5      (       d   e[        U[        5      (       a  M3   e   US   U R                  :X  d   eg )N	FUNNY_ARG)env)rO   environcopyr0   r   rV   rW   rX   itemsrZ   r[   )r8   r   r$   r_   kvs         r&   test_proc_environTestNonFSAPIS.test_proc_environ,  s    
 jjoo,,K###,NN599%iikIIKDAa%%%%a%%%%   ;4#4#4444r4   r:   N)r;   r<   r=   r>   r   r   r0   r   r   r   r	   r   r   r   r@   r:   r4   r&   r   r   '  sV    0!L[[K@[[(1NO5 P A5r4   r   )(r   rO   r    rL   
contextlibr   rV   r   r   r   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   xdist_groupr   rG   r   r   r:   r4   r&   <module>r      s   <| 
        ! # $ ( 1 /  & ' ' )  , & #  # $ . ' "6:n :* h'HZ0~- ~- 1 (~-B J'9:
  ;5O 5r4   