Viewing File: /opt/hc_python/lib/python3.12/site-packages/psutil/tests/__pycache__/test_unicode.cpython-312.pyc

�

h0i�)���dZddlZddlZddlZddlmZddlZddlmZddlmZddlm	Z	ddlm
Z
ddlmZdd	lmZdd
l
mZddl
mZddl
mZdd
l
mZddl
mZddl
mZddl
mZddl
mZddl
mZddl
mZddl
mZddl
mZddl
mZddl
mZddl
mZddl
mZddl
mZddl
mZddl
m Z ddl
m!Z!ddl
m"Z"d�Z#Gd �d!e�Z$ejJjMd"�#�ejJjOed$�%�Gd&�d'e$���Z(ejJjOed(�%�Gd)�d*e(��Z)Gd+�d,e$�Z*y)-aYNotes 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
||dz�t|dz�	|�t
|�t|�y#ttf$rY|�t
|�t|�ywxYw#|�t
|�t|�wxYw)z`Return True if both the fs and the subprocess module can
    deal with a unicode file name.
    N��suffix)�cmdz-2TF)	rrrr�shutil�copyfiler�UnicodeEncodeError�OSError)r"�sproc�testfns   �J/opt/hc_python/lib64/python3.12/site-packages/psutil/tests/test_unicode.py�try_unicoder+hs���
�E�
�v�
&�F�
��F���f���6�(�+��������
�.��F�T�M�"�����e���F���
��(������e���F����
���e���F��s$�A
A4�4B�B!�B�B!�!B;c�6��eZdZdZe�fd��Z�fd�Z�xZS)�BaseUnicodeTestNc����t�|��d|_d|_|j�Nt|j�sd|_yt
|j��|_t|j�yy)NFTr!)�super�
setUpClass�
skip_tests�
funky_name�funky_suffixr+rr)�cls�	__class__s �r*r0zBaseUnicodeTest.setUpClass�s`���
�������������'��s�/�/�0�!%���!+�3�3C�3C�!D����c�n�n�-�(�c�d��t�|��|jrtjd�Sy)Nzcan't handle unicode str)r/�setUpr1r�skip)�selfr5s �r*r8zBaseUnicodeTest.setUp�s(���
��
���?�?��;�;�9�:�:�r6)�__name__�
__module__�__qualname__r3�classmethodr0r8�
__classcell__)r5s@r*r-r-�s$����L��	.��	.�;�;r6r-�serial)�namezASCII fs��reasonc�>�eZdZdZeZd�Zd�Zd�Zd�Z	d�Z
ejjexred��ejjexsed	��d
���Zejjed��ejjed��d
���Zejjed��ejjed��e�d����Zd�Zejjed��d��Zy)�
TestFSAPIsz1Test FS APIs with a funky, valid, UTF8 path name.c���tj�5tjd�|jt	j
d�vcddd�S#1swYyxYw)N�ignore�.)�warnings�catch_warnings�simplefilterr2�os�listdir�r:s r*�expect_exact_path_matchz"TestFSAPIs.expect_exact_path_match�s<��
�
$�
$�
&��!�!�(�+��?�?�b�j�j��o�5�'�
&�
&�s�6A�Ac�~�|jddg}|j|�}tj|j�}|j�}t
|t�sJ�|j�rLtjj|�tjj|j�k(sJ�yy�Nz-cz2import time; [time.sleep(0.1) for x in range(100)])r2r�psutil�Process�pid�exe�
isinstance�strrOrL�path�normcase)r:r#�subp�prUs     r*�
test_proc_exezTestFSAPIs.test_proc_exe�s����O�O��@�
��
�!�!�#�&���N�N�4�8�8�$���e�e�g���#�s�#�#�#��'�'�)��7�7�#�#�C�(�B�G�G�,<�,<�T�_�_�,M�M�M�M�*r6c�@�|jddg}|j|�}tj|j�j�}t
|t�sJ�|j�r/|tjj|j�k(sJ�yyrQ)r2rrRrSrTrArVrWrOrLrX�basename)r:r#rZrAs    r*�test_proc_namezTestFSAPIs.test_proc_name�s����O�O��@�
��
�!�!�#�&���~�~�d�h�h�'�,�,�.���$��$�$�$��'�'�)��2�7�7�+�+�D�O�O�<�<�<�<�*r6c��|jddg}|j|�}tj|j�}|j�}|D]}t
|t�r�J�|j�r||k(sJ�yyrQ)	r2rrRrSrT�cmdlinerVrWrO)r:r#rZr[ra�parts      r*�test_proc_cmdlinezTestFSAPIs.test_proc_cmdline�s|���O�O��@�
��
�!�!�#�&���N�N�4�8�8�$���)�)�+���D��d�C�(�(�(���'�'�)��c�>�!�>�*r6c�\�|jdz}|jt|�t|�t	|�5tj�}|j�}ddd�tj�t�sJ�|j�r|k(sJ�yy#1swY�BxYw�N�2)r2�
addCleanuprrrrRrS�cwdrVrWrO)r:�dnamer[rhs    r*�
test_proc_cwdzTestFSAPIs.test_proc_cwd�s������#�%������U�+��5��
�5�\���� �A��%�%�'�C���!�%�%�'�3�'�'�'��'�'�)��%�<��<�*�	�\�s�%B"�"B+zfails on PYPY + WINDOWSrBzbroken on NETBSD or OPENBSDc�6�tj�}t|j��}t	|j
d�5t|j��}ddd�|z
j
�j}t|t�sJ�tr|stjd�S|j�rLtjj|�tjj|j
�k(sJ�yy#1swY��xYw)N�rbzopen_files on BSD is broken)rRrS�set�
open_files�openr2�poprXrVrWrrr9rOrLrY)r:r[�start�newrXs     r*�test_proc_open_fileszTestFSAPIs.test_proc_open_files�s���

�N�N����A�L�L�N�#��
�$�/�/�4�
(��a�l�l�n�%�C�)��e�� � �"�'�'���$��$�$�$��t��;�;�<�=�=��'�'�)��7�7�#�#�D�)�R�W�W�-=�-=�d�o�o�-N�N�N�N�*�)�
(�s�D�Dz
POSIX onlyzcan't list UNIX socketsc��|j|j��}t|�}t|�5t	j
�j
d�d}t|jt�sJ�|js*tr$trtjd�cddd�S|j|k(sJ�	ddd�y#1swYyxYw)Nr!�unixrzunreliable on OSX)rr3rrrRrS�net_connectionsrV�laddrrWrrrr9)r:rA�sock�conns    r*�test_proc_net_connectionsz$TestFSAPIs.test_proc_net_connections�s���
���d�&7�&7��8����%��
�T�]��>�>�#�3�3�F�;�A�>�D��d�j�j�#�.�.�.��:�:�%�J��{�{�#6�7�	�]�
�:�:��%�%�%��]�]�s�A/C�,C�Cc�0�d�}|j|j��}t|�}t|�5t	j
d��}||�}t
|jt�sJ�|j|k(sJ�	ddd�y#1swYyxYw)Nc��|D]B}tjj|j�j	t
�s�@|cSt
d��)Nzconnection not found)rLrXr^rw�
startswithr�
ValueError)�consrys  r*�	find_sockz2TestFSAPIs.test_net_connections.<locals>.find_sock�s@�����7�7�#�#�D�J�J�/�:�:�=�I��K���3�4�4r6r!ru)�kind)	rr3rrrRrvrVrwrW)r:r�rArxrrys      r*�test_net_connectionszTestFSAPIs.test_net_connections�sy��	5����d�&7�&7��8����%��
�T�]��)�)�v�6�D��T�?�D��d�j�j�#�.�.�.��:�:��%�%�%�	�]�]�s�AB�Bc��|jdz}|jt|�t|�t	j
|�yre)r2rgrrrR�
disk_usage)r:ris  r*�test_disk_usagezTestFSAPIs.test_disk_usages4�����#�%������U�+��5�����%� r6�
not supportedc�n�t|j��5}d�}tj�j	�D�cgc]}||j
���}}|D�cgc]
}t|vs�|��}}||�|vsJ�|D]}t|t�r�J�	ddd�ycc}wcc}w#1swYyxYw)Nr!c�z�tjjtjj|��S�N)rLrX�realpathrY)r[s r*�normpathz-TestFSAPIs.test_memory_maps.<locals>.normpaths&���w�w�'�'����(8�(8��(;�<�<r6)	rr3rRrS�memory_mapsrXrrVrW)r:�
funky_pathr��x�libpathsrXs      r*�test_memory_mapszTestFSAPIs.test_memory_mapss���
 ��(9�(9�
:�j�
=�+1�.�.�*:�*F�*F�*H��*H�Q����� �*H�
��$,�B�8�a�}��/A��8�H�B��J�'�8�3�3�3� ��!�$��,�,�,�!�;�
:��
��C�;�
:�s4�(B+�B!�B+�
B&�,B&�0#B+�B+�!
B+�+B4N)r;r<r=�__doc__rr3rOr\r_rcrjr�mark�skipifrr
rrrsr	rrzrr�r�rr��r6r*rErE�sZ��<�!�L�6�N�
=�"�	 ��[�[����(��1J��K��[�[�����'�"?���O��L�O��[�[���E�	�,��7��[�[���$�$�-F���&��8�&��[�[���E�	�,��7��[�[���$�$�-F�����
&���8�

&�!��[�[���O�+�O��D�
-�E�
-r6rEzunreliable on CIc��eZdZdZeZd�Zy)�TestFSAPIsWithInvalidPathz-Test FS APIs with a funky, invalid path name.c��tSr�rrNs r*rOz1TestFSAPIsWithInvalidPath.expect_exact_path_match(s
���y�r6N)r;r<r=r�rr3rOr�r6r*r�r�"s��7�)�L�r6r�c��eZdZdZeZejje	d��ejje
xred��d���Zy)�
TestNonFSAPISz&Unicode tests for non fs-related APIs.r�rBzsegfaults on PYPY + WINDOWSc��tjj�}|j|d<|j	|��}tj|j�}|j�}|j�D])\}}t|t�sJ�t|t�r�)J�|d|jk(sJ�y)N�	FUNNY_ARG)�env)rL�environ�copyr3rrRrSrT�itemsrVrW)r:r�r(r[�k�vs      r*�test_proc_environzTestNonFSAPIS.test_proc_environ6s���
�j�j�o�o����,�,��K���"�"�s�"�+���N�N�5�9�9�%���i�i�k���I�I�K�D�A�q��a��%�%�%��a��%�%�%� ��;��4�#4�#4�4�4�4r6N)
r;r<r=r�rr3rr�r�r
rr
r�r�r6r*r�r�1sV��0�!�L��[�[���K����@��[�[����(��1N��O�5�P�A�5r6r�)+r�rLr$rI�
contextlibrrRrrrrr	r
�psutil.testsrrr
rrrrrrrrrrrrrrrrrrr+r-r��xdist_groupr�rEr�r�r�r6r*�<module>r�s��<�|
�
���
�������!�#�$�(�1�/��&�'�'�)��,�&�#��#�$�.�&�"��6;�n�;�*�����h��'������H�Z��0�E-��E-�1�(�E-�P�����J�'9��:��
��;��5�O�5r6
Back to Directory File Manager