ó
Gq]c           @   s¾  d  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 d! „  Z d" „  Z' e rPe" j( d# ƒ d$ j) d# d% ƒ Z* n
 e" d$ Z* d& e+ f d' „  ƒ  YZ, e% j- e oe$ d( ƒ e% j- e o”e$ d) ƒ e% j- e d* ƒ e% j- e' e# ƒ d+ ƒ d, e, e% j. f d- „  ƒ  Yƒ ƒ ƒ ƒ Z/ e% j- e oöe$ d( ƒ e% j- e oe$ d) ƒ e% j- e' e* ƒ d. ƒ d/ e, e% j. f d0 „  ƒ  Yƒ ƒ ƒ Z0 e% j- e d1 ƒ d2 e% j. f d3 „  ƒ  Yƒ Z1 d4 e% j. f d5 „  ƒ  YZ2 e3 d6 k rºd d7 l4 m5 Z5 e5 e6 ƒ n  d S(8   só  
Notes about unicode handling in psutil
======================================

In psutil these are the 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

For a detailed explanation of how psutil handles unicode see:
- https://github.com/giampaolo/psutil/issues/1040
- http://psutil.readthedocs.io/#unicode
iÿÿÿÿN(   t   closing(   t   BSD(   t   MACOS(   t   OPENBSD(   t   POSIX(   t   WINDOWS(   t   PY3(   t   u(   t   APPVEYOR(   t   ASCII_FS(   t   bind_unix_socket(   t   chdir(   t   copyload_shared_lib(   t
   create_exe(   t   get_test_subprocess(   t   HAS_CONNECTIONS_UNIX(   t   HAS_ENVIRON(   t   HAS_MEMORY_MAPS(   t   mock(   t   PYPY(   t   reap_children(   t
   safe_mkdir(   t   safe_rmpath(   t   skip_on_access_denied(   t   TESTFILE_PREFIX(   t   TESTFN(   t   TESTFN_UNICODE(   t   TRAVIS(   t   unittest(   t   unix_socket_pathc         C   sC   t  r5 y t |  ƒ SWq? t k
 r1 t j ƒ  q? Xn
 t |  ƒ Sd  S(   N(   R   t   _safe_rmpatht   WindowsErrort	   tracebackt	   print_exc(   t   path(    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyR   \   s    c         C   s^   t  r
 t SzE y( t |  ƒ t |  ƒ t d |  g ƒ Wn t k
 rI t SXt SWd t ƒ  Xd S(   s`   Return True if both the fs and the subprocess module can
    deal with a unicode file name.
    t   cmdN(   R   t   TrueR   R   R   t   UnicodeEncodeErrort   FalseR   (   t   name(    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt   subprocess_supports_unicodep   s     

t   utf8s   fÀ€t   surrogateescapet   _BaseFSAPIsTestsc           B   s÷   e  Z d Z e d  „  ƒ Z e d „  ƒ Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z e j e d	 ƒ d
 „  ƒ Z e j e d	 ƒ e j e d ƒ e ƒ  d „  ƒ ƒ ƒ Z d „  Z e j e d ƒ e j e d ƒ d „  ƒ ƒ Z RS(   c         C   s   t  |  j ƒ t |  j ƒ d  S(   N(   R   t
   funky_nameR   (   t   cls(    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt
   setUpClass’   s    c         C   s   t  ƒ  t |  j ƒ d  S(   N(   R   R   R,   (   R-   (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt   tearDownClass—   s    c         C   s   t  ƒ  d  S(   N(   R   (   t   self(    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt   tearDownœ   s    c         C   s   t  d ƒ ‚ d  S(   Ns   must be implemented in subclass(   t   NotImplementedError(   R0   (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt   expect_exact_path_matchŸ   s    c         C   si   t  d |  j g ƒ } t j | j ƒ } | j ƒ  } |  j | t ƒ |  j ƒ  re |  j	 | |  j ƒ n  d  S(   NR#   (
   R   R,   t   psutilt   Processt   pidt   exet   assertIsInstancet   strR3   t   assertEqual(   R0   t   subpt   pR7   (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt   test_proc_exe¢   s    c         C   sÌ   t  d |  j g ƒ } t rr t j d d t j t j ƒ  ƒ ƒ. } t j	 | j
 ƒ j ƒ  } | j si t ‚ Wd  QXn t j	 | j
 ƒ j ƒ  } |  j | t ƒ |  j ƒ  rÈ |  j | t j j |  j ƒ ƒ n  d  S(   NR#   s    psutil._psplatform.cext.proc_exet   side_effect(   R   R,   R   R   t   patchR4   t   AccessDeniedt   ost   getpidR5   R6   R'   t   calledt   AssertionErrorR8   R9   R3   R:   R"   t   basename(   R0   R;   t   mR'   (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt   test_proc_nameª   s    c         C   s}   t  d |  j g ƒ } t j | j ƒ } | j ƒ  } x | D] } |  j | t ƒ q: W|  j ƒ  ry |  j	 | |  j g ƒ n  d  S(   NR#   (
   R   R,   R4   R5   R6   t   cmdlineR8   R9   R3   R:   (   R0   R;   R<   RH   t   part(    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt   test_proc_cmdlineº   s    c         C   s‹   |  j  d } |  j t | ƒ t | ƒ t | ƒ  t j ƒ  } | j ƒ  } Wd  QX|  j | j ƒ  t	 ƒ |  j
 ƒ  r‡ |  j | | ƒ n  d  S(   Nt   2(   R,   t
   addCleanupR   R   R   R4   R5   t   cwdR8   R9   R3   R:   (   R0   t   dnameR<   RM   (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt   test_proc_cwdÃ   s    
c         C   sÄ   t  j ƒ  } t | j ƒ  ƒ } t |  j d ƒ  t | j ƒ  ƒ } Wd  QX| | j ƒ  j } |  j | t	 ƒ t
 r† | r† |  j d ƒ S|  j ƒ  rÀ |  j t j j | ƒ t j j |  j ƒ ƒ n  d  S(   Nt   rbs   open_files on BSD is broken(   R4   R5   t   sett
   open_filest   openR,   t   popR"   R8   R9   R   t   skipTestR3   R:   RA   t   normcase(   R0   R<   t   startt   newR"   (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt   test_proc_open_filesÎ   s    s
   POSIX onlyc         C   sË   t  j j |  j ƒ } t d | ƒ ¡ } y t | ƒ } Wn, t k
 re t rS ‚  qf t j	 d ƒ ‚ n Xt
 | ƒ M t j ƒ  j d ƒ d } |  j | j t ƒ t s» |  j | j | ƒ n  Wd  QXWd  QXd  S(   Nt   suffixs   not supportedt   unixi    (   RA   R"   RE   R,   R   R
   R%   R   R   t   SkipTestR    R4   R5   t   connectionsR8   t   laddrR9   R   R:   (   R0   RZ   R'   t   sockt   conn(    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt   test_proc_connectionsÜ   s    s   can't list UNIX socketsc         C   sÙ   d „  } t  j j |  j ƒ } t d | ƒ ¦ } y t | ƒ } Wn, t k
 rn t r\ ‚  qo t j	 d ƒ ‚ n Xt
 | ƒ R t j d d ƒ } t sÉ | | ƒ } |  j | j t ƒ |  j | j | ƒ n  Wd  QXWd  QXd  S(   Nc         S   sC   x0 |  D]( } t  j j | j ƒ j t ƒ r | Sq Wt d ƒ ‚ d  S(   Ns   connection not found(   RA   R"   RE   R^   t
   startswithR   t
   ValueError(   t   consR`   (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt	   find_sockò   s    RZ   s   not supportedt   kindR[   (   RA   R"   RE   R,   R   R
   R%   R   R   R\   R    R4   t   net_connectionsR   R8   R^   R9   R:   (   R0   Re   RZ   R'   R_   Rd   R`   (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt   test_net_connectionsî   s    	c         C   s8   |  j  d } |  j t | ƒ t | ƒ t j | ƒ d  S(   NRK   (   R,   RL   R   R   R4   t
   disk_usage(   R0   RN   (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt   test_disk_usage	  s    
s   not supporteds&   ctypes does not support unicode on PY2c         C   s²   t  d |  j ƒ š } d „  } g  t j ƒ  j ƒ  D] } | | j ƒ ^ q1 } g  | D] } t | k rS | ^ qS } |  j | | ƒ | ƒ x | D] } |  j | t	 ƒ qŽ WWd  QXd  S(   Nt
   dst_prefixc         S   s   t  j j t  j j |  ƒ ƒ S(   N(   RA   R"   t   realpathRV   (   R<   (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt   normpath  s    (
   R   R,   R4   R5   t   memory_mapsR"   R   t   assertInR8   R9   (   R0   t
   funky_pathRm   t   xt   libpathsR"   (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt   test_memory_maps  s    	+%N(   t   __name__t
   __module__t   NoneR,   t   classmethodR.   R/   R1   R3   R=   RG   RJ   RO   RY   R   t   skipIfR   Ra   R   R   Rh   Rj   R   R   Rs   (    (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyR+      s"   									s   unreliable on PYPY + TRAVISs   unreliable on TRAVISs   ASCII fss"   subprocess can't deal with unicodet
   TestFSAPIsc           B   s#   e  Z d  Z e Z e d „  ƒ Z RS(   s1   Test FS APIs with a funky, valid, UTF8 path name.c         C   sh   t  r
 t St |  j t ƒ r" d n	 t d ƒ } t j ƒ  ( t j d ƒ |  j t	 j
 | ƒ k SWd  QXd  S(   Nt   .t   ignore(   R   R$   t
   isinstanceR,   R9   R   t   warningst   catch_warningst   simplefilterRA   t   listdir(   R-   t   here(    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyR3   *  s    $(   Rt   Ru   t   __doc__R   R,   Rw   R3   (    (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyRy   !  s   s*   subprocess can't deal with invalid unicodet   TestFSAPIsWithInvalidPathc           B   s#   e  Z d  Z e Z e d „  ƒ Z RS(   s-   Test FS APIs with a funky, invalid path name.c         C   s   t  S(   N(   R$   (   R-   (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyR3   ?  s    (   Rt   Ru   R‚   t   INVALID_NAMER,   Rw   R3   (    (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyRƒ   7  s   s   WINDOWS onlyt   TestWinProcessNamec           B   s   e  Z d  „  Z RS(   c         C   s\   t  j d d t j t j ƒ  ƒ ƒ2 } |  j t j ƒ  j ƒ  t	 ƒ | j
 sR t ‚ Wd  QXd  S(   Ns    psutil._psplatform.cext.proc_exeR>   (   R   R?   R4   R@   RA   RB   R8   R5   R'   R9   RC   RD   (   R0   RF   (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt   test_name_typeH  s    (   Rt   Ru   R†   (    (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyR…   E  s   t   TestNonFSAPISc           B   s3   e  Z d  Z d „  Z e j e d ƒ d „  ƒ Z RS(   s&   Unicode tests for non fs-related APIs.c         C   s   t  ƒ  d  S(   N(   R   (   R0   (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyR1   Z  s    s   not supportedc         C   s­   t  j j ƒ  } t r t n d } | | d <t d | ƒ } t j | j ƒ } | j ƒ  } x: | j	 ƒ  D], \ } } |  j
 | t ƒ |  j
 | t ƒ qe W|  j | d | ƒ d  S(   Ns   Ã¨t	   FUNNY_ARGt   env(   RA   t   environt   copyR   R   R   R4   R5   R6   t   itemsR8   R9   R:   (   R0   R‰   t	   funky_strt   sprocR<   t   kt   v(    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt   test_proc_environ]  s    
(   Rt   Ru   R‚   R1   R   Rx   R   R‘   (    (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyR‡   W  s   	t   __main__(   t   run(7   R‚   RA   R    R}   t
   contextlibR    R4   R   R   R   R   R   t   psutil._compatR   R   t   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(   t   encodet   decodeR„   t   objectR+   Rx   t   TestCaseRy   Rƒ   R…   R‡   Rt   t   psutil.tests.runnerR“   t   __file__(    (    (    s8   lib/python2.7/site-packages/psutil/tests/test_unicode.pyt   <module>5   st   		
’	%	"
