
    Xhz)                        d Z ddlZddlZddlZddlm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 Z# G d  d!e      Z$ejJ                  jM                  d"#      ejJ                  jO                  ed$%       G d& d'e$                    Z(ejJ                  jO                  ed(%       G d) d*e(             Z) G d+ d,e$      Z*y)-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                 |   d}t        |       }	 t        |       t        |       t        |g      }t	        j
                  ||dz          t        |dz          	 |t        |       t        |       y# t        t        f$ r Y |t        |       t        |       yw xY w# |t        |       t        |       w xY w)z`Return 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      M/var/www/html/myenv/lib/python3.12/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
A4 4BB! BB! !B;c                   6     e Zd ZdZe fd       Z fdZ xZS )BaseUnicodeTestNc                     t         |           d| _        d | _        | j                  Nt        | j                        sd| _        y t        | j                        | _        t        | j                         y y )NFTr!   )super
setUpClass
skip_tests
funky_namefunky_suffixr+   r   r   )cls	__class__s    r*   r0   zBaseUnicodeTest.setUpClass   s`    's//0!%!+33C3C!Dcnn- (    c                 d    t         |           | j                  rt        j                  d      y )Nzcan't handle unicode str)r/   setUpr1   r   skip)selfr5   s    r*   r8   zBaseUnicodeTest.setUp   s(    ??++899 r6   )__name__
__module____qualname__r3   classmethodr0   r8   __classcell__)r5   s   @r*   r-   r-      s$    L	. 	.: :r6   r-   serial)namezASCII fsreasonc                      e Zd ZdZeZd Zd Zd Zd Z	d Z
ej                  j                  exr ed      ej                  j                  exs ed	      d
               Zej                  j                  e d      d        Zej                  j                  e d      ej                  j                  e d       e       d                      Zd Zej                  j                  e d      d        Zy)
TestFSAPIsz1Test FS APIs with a funky, valid, UTF8 path name.c                     t        j                         5  t        j                  d       | j                  t	        j
                  d      v cd d d        S # 1 sw Y   y xY w)Nignore.)warningscatch_warningssimplefilterr2   oslistdirr:   s    r*   expect_exact_path_matchz"TestFSAPIs.expect_exact_path_match   sE    $$& 	6!!(+??bjjo5	6 	6 	6s   6AAc                 ~   | j                   ddg}| j                  |      }t        j                  |j                        }|j                         }t        |t              sJ | j                         rLt        j                  j                  |      t        j                  j                  | j                         k(  sJ y y Nz-cz2import time; [time.sleep(0.1) for x in range(100)])r2   r   psutilProcesspidexe
isinstancestrrO   rL   pathnormcase)r:   r#   subpprU   s        r*   test_proc_exezTestFSAPIs.test_proc_exe   s    OO@

 !!#&NN488$eeg#s###'')77##C(BGG,<,<T__,MMMM *r6   c                 @   | j                   ddg}| j                  |      }t        j                  |j                        j                         }t        |t              sJ | j                         r/|t        j                  j                  | j                         k(  sJ y y rQ   )r2   r   rR   rS   rT   rA   rV   rW   rO   rL   rX   basename)r:   r#   rZ   rA   s       r*   test_proc_namezTestFSAPIs.test_proc_name   s    OO@

 !!#&~~dhh',,.$$$$'')277++DOO<<<< *r6   c                    | j                   ddg}| j                  |      }t        j                  |j                        }|j                         }|D ]  }t        |t              rJ  | j                         r||k(  sJ y y rQ   )	r2   r   rR   rS   rT   cmdlinerV   rW   rO   )r:   r#   rZ   r[   ra   parts         r*   test_proc_cmdlinezTestFSAPIs.test_proc_cmdline   s    OO@

 !!#&NN488$))+ 	)DdC(((	)'')c>!> *r6   c                 \   | j                   dz   }| j                  t        |       t        |       t	        |      5  t        j                         }|j                         }d d d        t        j                         t              sJ | j                         r|k(  sJ y y # 1 sw Y   BxY wN2)r2   
addCleanupr   r   r   rR   rS   cwdrV   rW   rO   )r:   dnamer[   rh   s       r*   test_proc_cwdzTestFSAPIs.test_proc_cwd   s    #%U+55\ 	 A%%'C	 !%%'3''''')%<< *		 	s   %B""B+zfails on PYPY + WINDOWSrB   zbroken on NETBSD or OPENBSDc                 6   t        j                         }t        |j                               }t	        | j
                  d      5  t        |j                               }d d d        |z
  j                         j                  }t        |t              sJ t        r|st        j                  d      | j                         rLt        j                  j                  |      t        j                  j                  | j
                        k(  sJ y y # 1 sw Y   xY w)Nrbzopen_files on BSD is broken)rR   rS   set
open_filesopenr2   poprX   rV   rW   r   r   r9   rO   rL   rY   )r:   r[   startnewrX   s        r*   test_proc_open_fileszTestFSAPIs.test_proc_open_files   s    
 NNALLN#$//4( 	&alln%C	&e  "''$$$$t++;<<'')77##D)RWW-=-=doo-NNNN *	& 	&s   DDz
POSIX onlyc                    | j                  | j                        }t        |      }t        |      5  t	        j
                         j                  d      d   }t        |j                  t              sJ |j                  s!t        rt        rt        j                  d      |j                  |k(  sJ 	 d d d        y # 1 sw Y   y xY w)Nr!   unixr   zunreliable on OSX)r   r3   r   r   rR   rS   net_connectionsrV   laddrrW   r   r   r   r9   )r:   rA   sockconns       r*   test_proc_net_connectionsz$TestFSAPIs.test_proc_net_connections   s    d&7&78%T] 	&>>#33F;A>Ddjj#...::%Jkk"566::%%%	& 	& 	&s   BB>>Czcan't list UNIX socketsc                 0   d }| j                  | j                        }t        |      }t        |      5  t	        j
                  d      } ||      }t        |j                  t              sJ |j                  |k(  sJ 	 d d d        y # 1 sw Y   y xY w)Nc                     | D ]B  }t         j                  j                  |j                        j	                  t
              s@|c S  t        d      )Nzconnection not found)rL   rX   r^   rw   
startswithr   
ValueError)consry   s     r*   	find_sockz2TestFSAPIs.test_net_connections.<locals>.find_sock   sE      77##DJJ/::=IK  344r6   r!   ru   )kind)	r   r3   r   r   rR   rv   rV   rw   rW   )r:   r   rA   rx   r   ry   s         r*   test_net_connectionszTestFSAPIs.test_net_connections   s    	5 d&7&78%T] 	&))v6DT?Ddjj#...::%%%		& 	& 	&s   ABBc                     | j                   dz   }| j                  t        |       t        |       t	        j
                  |       y re   )r2   rg   r   r   rR   
disk_usage)r:   ri   s     r*   test_disk_usagezTestFSAPIs.test_disk_usage  s4    #%U+5% r6   not supportedc                 n   t        | j                        5 }d }t        j                         j	                         D cg c]  } ||j
                         }}|D cg c]  }t        |v s| }} ||      |v sJ |D ]  }t        |t              rJ  	 d d d        y c c}w c c}w # 1 sw Y   y xY w)Nr!   c                 z    t         j                  j                  t         j                  j                  |             S N)rL   rX   realpathrY   )r[   s    r*   normpathz-TestFSAPIs.test_memory_maps.<locals>.normpath  s&    ww''(8(8(;<<r6   )	r   r3   rR   rS   memory_mapsrX   r   rV   rW   )r:   
funky_pathr   xlibpathsrX   s         r*   test_memory_mapszTestFSAPIs.test_memory_maps  s     (9(9: 	-j= +1..*:*F*F*H%& H  $,Ba}/ABHBJ'8333  -!$,,,-	- 	-
 C	- 	-s4   (B+B!B+B&,B&0#B+B+!
B++B4N)r;   r<   r=   __doc__r   r3   rO   r\   r_   rc   rj   r   markskipifr   r
   r   r   rs   r	   rz   r   r   r   r   r   r    r6   r*   rE   rE      s5    <!L6N
="	  [[(1JK[['"?  O LO [[E	,7& 8& [[E	,7[[$$-F   &  8
&! [[O+OD- E-r6   rE   zunreliable on CIc                       e Zd ZdZeZd Zy)TestFSAPIsWithInvalidPathz-Test FS APIs with a funky, invalid path name.c                     t          S r   r   rN   s    r*   rO   z1TestFSAPIsWithInvalidPath.expect_exact_path_match%  s
    yr6   N)r;   r<   r=   r   r   r3   rO   r   r6   r*   r   r     s    7)Lr6   r   c                       e Zd ZdZeZej                  j                  e	 d      ej                  j                  e
xr ed      d               Zy)TestNonFSAPISz&Unicode tests for non fs-related APIs.r   rB   zsegfaults on PYPY + WINDOWSc                    t         j                  j                         }| j                  |d<   | j	                  |      }t        j                  |j                        }|j                         }|j                         D ])  \  }}t        |t              sJ t        |t              r)J  |d   | j                  k(  sJ y )N	FUNNY_ARG)env)rL   environcopyr3   r   rR   rS   rT   itemsrV   rW   )r:   r   r(   r[   kvs         r*   test_proc_environzTestNonFSAPIS.test_proc_environ3  s    
 jjoo,,K""s"+NN599%iikIIK 	&DAqa%%%a%%%	& ;4#4#4444r6   N)r;   r<   r=   r   r   r3   r   r   r   r   r   r
   r   r   r6   r*   r   r   .  sV    0!L[[K@[[(1NO5 P A5r6   r   )+r   rL   r$   rI   
contextlibr   rR   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   rE   r   r   r   r6   r*   <module>r      s  <| 
           ! # $ ( 1 /  & ' ' )  , & #  # $ . & "6:n :* h'HZ0B- B- 1 (B-J J'9:
  ;5O 5r6   