
    9i)                        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SKJ r   SSKJ!r!  SSKJ"r"  S r# " S  S!\5      r$\RJ                  RM                  S"S#9\RJ                  RO                  \S$S%9 " S& S'\$5      5       5       r(\RJ                  RO                  \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MACOS)NETBSD)OPENBSD)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_subproc)	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      Y/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/psutil/tests/test_unicode.pytry_unicoder+   h   s     Ev&FFf6(+.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*   r1   BaseUnicodeTest.setUpClass   sc    's//00!%!+33C3C!Dcnn- (    c                 p   > [         TU ]  5         U R                  (       a  [        R                  " S5      $ g )Nzcan't handle unicode str)r0   setUpr2   r   skip)selfr6   s    r*   r:   BaseUnicodeTest.setUp   s(    ??;;9:: r8    )
__name__
__module____qualname____firstlineno__r4   classmethodr1   r:   __static_attributes____classcell__)r6   s   @r*   r-   r-      s$    L	. 	.; ;r8   r-   serial)namezASCII fsreasonc                   X   \ rS rSrSr\rS rS rS r	S r
S r\R                  R                  \=(       a    \SS	9\R                  R                  \=(       d    \S
S	9S 5       5       r\R                  R                  \(       + SS	9\R                  R                  \(       + SS	9S 5       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S 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simplefilterr3   oslistdirr<   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)])r3   r   psutilProcesspidexe
isinstancestrrV   rS   pathnormcase)r<   r#   subppr]   s        r*   test_proc_exeTestFSAPIs.test_proc_exe   s    OO@

 !!#&NN488$eeg#s####''))77##C(BGG,<,<T__,MMMM *r8   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 rY   )r3   r   rZ   r[   r\   rG   r^   r_   rV   rS   r`   basename)r<   r#   rb   rG   s       r*   test_proc_nameTestFSAPIs.test_proc_name   s    OO@

 !!#&~~dhh',,.$$$$$''))277++DOO<<<< *r8   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 rY   )	r3   r   rZ   r[   r\   cmdliner^   r_   rV   )r<   r#   rb   rc   rk   parts         r*   test_proc_cmdlineTestFSAPIs.test_proc_cmdline   s    OO@

 !!#&NN488$))+DdC(((( ''))>!> *r8   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)r3   
addCleanupr   r   r   rZ   r[   cwdr^   r_   rV   )r<   dnamerc   rs   s       r*   test_proc_cwdTestFSAPIs.test_proc_cwd   s    #%U+55\ A%%'C  !%%'3''''''))%<< *	 \s   &B..
B<zfails on PYPY + WINDOWSrH   zbroken on NETBSD or OPENBSDc                 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      $ 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)rZ   r[   set
open_filesopenr3   popr`   r^   r_   r   r   r;   rV   rS   ra   )r<   rc   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 onlyzcan't list UNIX socketsc                    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                  (       d5  [        (       a*  [        (       a  [        R                  " S5      sS S S 5        $ UR                  U:X  d   e S S S 5        g ! , (       d  f       g = f)Nr!   unixr   zunreliable on OSX)r   r4   r   r   rZ   r[   net_connectionsr^   laddrr_   r   r   r   r;   )r<   rG   sockconns       r*   test_proc_net_connections$TestFSAPIs.test_proc_net_connections   s    
 d&7&78%T]>>#33F;A>Ddjj#....::%%JJ{{#67	 ]
 ::%%% ]]s   BC?C
C)c                 <   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)rS   r`   rg   r   
startswithr   
ValueError)consr   s     r*   	find_sock2TestFSAPIs.test_net_connections.<locals>.find_sock   sC    77##DJJ/::=IIK  344r8   r!   r   )kind)	r   r4   r   r   rZ   r   r^   r   r_   )r<   r   rG   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 rp   )r3   rr   r   r   rZ   
disk_usage)r<   rt   s     r*   test_disk_usageTestFSAPIs.test_disk_usage  s4    #%U+5% r8   not supportedc                    [        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)rS   r`   realpathra   )rc   s    r*   normpath-TestFSAPIs.test_memory_maps.<locals>.normpath  s&    ww''(8(8(;<<r8   )	r   r4   rZ   r[   memory_mapsr`   r   r^   r_   )r<   
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@   rA   rB   __doc__r   r4   rV   rd   rh   rm   ru   r   markskipifr   r
   r   r   r   r	   r   r   r   r   r   r   r   rD   r>   r8   r*   rK   rK      sZ    <!L6N
="	  [[(1JK[['"?  O LO [[E	,7[[$$-F  & 8& [[E	,7[[$$-F   &  8
&! [[O+OD- E-r8   rK   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                     [         (       + $ r   r   rU   s    r*   rV   1TestFSAPIsWithInvalidPath.expect_exact_path_match(  s
    yr8   r>   N)	r?   r@   rA   rB   r   r   r4   rV   rD   r>   r8   r*   r   r   "  s    7)Lr8   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)	TestNonFSAPISi1  z&Unicode tests for non fs-related APIs.r   rH   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)rS   environcopyr4   r   rZ   r[   r\   itemsr^   r_   )r<   r   r(   rc   kvs         r*   test_proc_environTestNonFSAPIS.test_proc_environ6  s    
 jjoo,,K""s"+NN599%iikIIKDAa%%%%a%%%%   ;4#4#4444r8   r>   N)r?   r@   rA   rB   r   r   r4   r   r   r   r   r   r
   r   rD   r>   r8   r*   r   r   1  sV    0!L[[K@[[(1NO5 P A5r8   r   )+r   rS   r$   rP   
contextlibr   rZ   r   r   r   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   rK   r   r   r>   r8   r*   <module>r      s  <| 
           ! # $ ( 1 /  & ' ' )  , & #  # $ . & "6;n ;* h'HZ0E- E- 1 (E-P J'9:
  ;5O 5r8   