
    Xh(                     p   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 Z  G d de      Z!ejD                  jG                  d       ejD                  jI                  ed!"       G d# d$e!                    Z%ejD                  jI                  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)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                 |   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      e/var/www/html/cjf29f/d23bf782/d32178f2/venv/lib/python3.12/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
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&   r,   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+   setUpr-   r   skip)selfr1   s    r&   r4   zBaseUnicodeTest.setUp   s(    ??++899 r2   )__name__
__module____qualname__r/   classmethodr,   r4   __classcell__)r1   s   @r&   r)   r)      s$    L	. 	.: :r2   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      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      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simplefilterr.   oslistdirr6   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)])r.   r   psutilProcesspidexe
isinstancestrrK   rH   pathnormcase)r6   r   subpprQ   s        r&   test_proc_exezTestFSAPIs.test_proc_exe   s    OO@

 ""3'NN488$eeg#s###'')77##C(BGG,<,<T__,MMMM *r2   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 rM   )r.   r   rN   rO   rP   r=   rR   rS   rK   rH   rT   basename)r6   r   rV   r=   s       r&   test_proc_namezTestFSAPIs.test_proc_name   s    OO@

 ""3'~~dhh',,.$$$$'')277++DOO<<<< *r2   c                    | j                   ddg}| j                  |      }t        j                  |j                        }|j                         }|D ]  }t        |t              rJ  | j                         r||k(  sJ y y rM   )	r.   r   rN   rO   rP   cmdlinerR   rS   rK   )r6   r   rV   rW   r]   parts         r&   test_proc_cmdlinezTestFSAPIs.test_proc_cmdline   s    OO@

 ""3'NN488$))+ 	)DdC(((	)'')c>!> *r2   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)r.   
addCleanupr   r   r   rN   rO   cwdrR   rS   rK   )r6   dnamerW   rd   s       r&   test_proc_cwdzTestFSAPIs.test_proc_cwd   s    #%U+55\ 	 A%%'C	 !%%'3''''')%<< *		 	s   %B""B+zfails on PYPY + WINDOWSr>   c                 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)rN   rO   set
open_filesopenr.   poprT   rR   rS   r   r   r5   rK   rH   rU   )r6   rW   startnewrT   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                  |k(  sJ 	 d d d        y # 1 sw Y   y xY w)Nr   unixr   )
r   r/   r   r   rN   rO   net_connectionsrR   laddrrS   )r6   r=   sockconns       r&   test_proc_net_connectionsz$TestFSAPIs.test_proc_net_connections   s    d&7&78%T] 	&>>#33F;A>Ddjj#...::%%%	& 	& 	&s   ABB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)rH   rT   rZ   rs   
startswithr   
ValueError)consru   s     r&   	find_sockz2TestFSAPIs.test_net_connections.<locals>.find_sock   sE      77##DJJ/::=IK  344r2   r   rq   )kind)	r   r/   r   r   rN   rr   rR   rs   rS   )r6   r|   r=   rt   r{   ru   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 ra   )r.   rc   r   r   rN   
disk_usage)r6   re   s     r&   test_disk_usagezTestFSAPIs.test_disk_usage   s4    #%U+5% r2   not supportedzunstable on PYPYc                 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)rH   rT   realpathrU   )rW   s    r&   normpathz-TestFSAPIs.test_memory_maps.<locals>.normpath  s&    ww''(8(8(;<<r2   )	r   r/   rN   rO   memory_mapsrT   r   rR   rS   )r6   
funky_pathr   xlibpathsrT   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)r7   r8   r9   __doc__r   r/   rK   rX   r[   r_   rf   r   markskipifr   r   ro   r   rv   r   r   r~   r   r
   r    r2   r&   rA   rA      s+    <!L6N
="	  [[(1JKO LO [[E	,7& 8& [[E	,7[[$$-F   &  8
&! [[O+OD[[%78- 9 E-r2   rA   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                      y)NTr   rJ   s    r&   rK   z1TestFSAPIsWithInvalidPath.expect_exact_path_match  s    r2   N)r7   r8   r9   r   r   r/   rK   r   r2   r&   r   r     s    7)Lr2   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   r>   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)rH   environcopyr/   r   rN   rO   rP   itemsrR   rS   )r6   r   r$   rW   kvs         r&   test_proc_environzTestNonFSAPIS.test_proc_environ,  s    
 jjoo,,K###,NN599%iikIIK 	&DAqa%%%a%%%	& ;4#4#4444r2   N)r7   r8   r9   r   r   r/   r   r   r   r	   r   r   r   r   r2   r&   r   r   '  sV    0!L[[K@[[(1NO5 P A5r2   r   )(r   rH   r    rE   
contextlibr   rN   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   rA   r   r   r   r2   r&   <module>r      s   <| 
        ! # $ ( 1 /  & ' ' )  , & #  # $ . ' "6:n :* h'HZ0~- ~- 1 (~-B J'9:
  ;5O 5r2   