%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3.12/test/support/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3.12/test/support/__pycache__/socket_helper.cpython-312.pyc

�

���f�5��@�ddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
ddl	mZdZdZ
dZe
jZej"ej$fd	�Zefd
�Zd�Zd�Ze�Zdad
�Zd�Ze�Zej:edd�d��Zd�ZiZ d�Z!d�Z"d�Z#y)�N�)�support�)�warnings_helper�	localhostz	127.0.0.1z::1c�x�tj||�5}t|�}ddd�~|S#1swY~SxYw)aReturns an unused port that should be suitable for binding.  This is
    achieved by creating a temporary socket with the same family and type as
    the 'sock' parameter (default is AF_INET, SOCK_STREAM), and binding it to
    the specified host address (defaults to 0.0.0.0) with the port set to 0,
    eliciting an unused ephemeral port from the OS.  The temporary socket is
    then closed and deleted, and the ephemeral port is returned.

    Either this method or bind_port() should be used for any tests where a
    server socket needs to be bound to a particular port for the duration of
    the test.  Which one to use depends on whether the calling code is creating
    a python socket, or if an unused port needs to be provided in a constructor
    or passed to an external program (i.e. the -accept argument to openssl's
    s_server mode).  Always prefer bind_port() over find_unused_port() where
    possible.  Hard coded ports should *NEVER* be used.  As soon as a server
    socket is bound to a hard coded port, the ability to run multiple instances
    of the test simultaneously on the same host is compromised, which makes the
    test a ticking time bomb in a buildbot environment. On Unix buildbots, this
    may simply manifest as a failed test, which can be recovered from without
    intervention in most cases, but on Windows, the entire python process can
    completely and utterly wedge, requiring someone to log in to the buildbot
    and manually kill the affected process.

    (This is easy to reproduce on Windows, unfortunately, and can be traced to
    the SO_REUSEADDR socket option having different semantics on Windows versus
    Unix/Linux.  On Unix, you can't have two AF_INET SOCK_STREAM sockets bind,
    listen and then accept connections on identical host/ports.  An EADDRINUSE
    OSError will be raised at some point (depending on the platform and
    the order bind and listen were called on each socket).

    However, on Windows, if SO_REUSEADDR is set on the sockets, no EADDRINUSE
    will ever be raised when attempting to bind two identical host/ports. When
    accept() is called on each socket, the second caller's process will steal
    the port from the first caller, leaving them both in an awkwardly wedged
    state where they'll no longer respond to any signals or graceful kills, and
    must be forcibly killed via OpenProcess()/TerminateProcess().

    The solution on Windows is to use the SO_EXCLUSIVEADDRUSE socket option
    instead of SO_REUSEADDR, which effectively affords the same semantics as
    SO_REUSEADDR on Unix.  Given the propensity of Unix developers in the Open
    Source world compared to Windows ones, this is a common mistake.  A quick
    look over OpenSSL's 0.9.8g source shows that they use SO_REUSEADDR when
    openssl.exe is called with the 's_server' option, for example. See
    http://bugs.python.org/issue2550 for more info.  The following site also
    has a very thorough description about the implications of both REUSEADDR
    and EXCLUSIVEADDRUSE on Windows:
    https://learn.microsoft.com/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse

    XXX: although this approach is a vast improvement on previous attempts to
    elicit unused ports, it rests heavily on the assumption that the ephemeral
    port returned to us by the OS won't immediately be dished back out to some
    other process when we close and delete our temporary socket but before our
    calling code has a chance to bind the returned port.  We can deal with this
    issue if/when we come across it.
    N)�socket�	bind_port)�family�socktype�tempsock�ports    �1/usr/lib/python3.12/test/support/socket_helper.py�find_unused_portrs?��p
���v�x�	(�#�H���"��#���K�#���K�s�.�9c��|jtjk(�r
|jtjk(r�ttd�rF|j
tjtj�dk(rtjd��ttd�rH	|j
tjtj�dk(rtjd��	ttd�r/|jtjtjd�|j|df�|j!�d}|S#t$rY�rwxYw)a%Bind the socket to a free port and return the port number.  Relies on
    ephemeral ports in order to ensure we are using an unbound port.  This is
    important as many tests may be running simultaneously, especially in a
    buildbot environment.  This method raises an exception if the sock.family
    is AF_INET and sock.type is SOCK_STREAM, *and* the socket has SO_REUSEADDR
    or SO_REUSEPORT set on it.  Tests should *never* set these socket options
    for TCP/IP sockets.  The only case for setting these options is testing
    multicasting via multiple UDP sockets.

    Additionally, if the SO_EXCLUSIVEADDRUSE socket option is available (i.e.
    on Windows), it will be set on the socket.  This will prevent anyone else
    from bind()'ing to our host/port for the duration of the test.
    �SO_REUSEADDRrzHtests should never set the SO_REUSEADDR socket option on TCP/IP sockets!�SO_REUSEPORTzHtests should never set the SO_REUSEPORT socket option on TCP/IP sockets!�SO_EXCLUSIVEADDRUSEr)rr	�AF_INET�type�SOCK_STREAM�hasattr�
getsockopt�
SOL_SOCKETrr�
TestFailedr�OSError�
setsockoptr�bind�getsockname)�sock�hostrs   rr
r
Rs���{�{�f�n�n�$����f�6H�6H�)H��6�>�*����v�0�0�&�2E�2E�F�!�K��(�(�*;�<�<��6�>�*�	
��?�?�6�#4�#4�f�6I�6I�J�a�O�!�,�,�.?�@�@�P��6�0�1��O�O�F�-�-�v�/I�/I�1�M��I�I�t�Q�i������a� �D��K���
��	
�s�#AE�	E�Ec���|jtjk(sJ�	|j|�y#t$r&|j�t
jd��wxYw)zBBind a unix socket, raising SkipTest if PermissionError is raised.zcannot bind AF_UNIX socketsN)rr	�AF_UNIXr�PermissionError�close�unittest�SkipTest)r �addrs  r�bind_unix_socketr)ysR���;�;�&�.�.�(�(�(�?��	�	�$����?��
�
����� =�>�>�?�s	�3�/A"c�\�tjrad}	tjtjtj�}|j	t
df�	|r|j
�yyy#t$rYnwxYw	|r|j
�y#|r|j
�wwxYw)z+Check whether IPv6 is enabled on this host.NrTF)r	�has_ipv6�AF_INET6rr�HOSTv6r%r)r s r�_is_ipv6_enabledr.�s���
�����	��=�=����&�2D�2D�E�D��I�I�v�q�k�"����
�
������	��	�����
�
������
�
���s$�A	A3�3	A?�<B�>A?�?B�B+c���ttd�stjd�|�St�Vddlm}m}|dz}tjtj�5}	|j|�da||�	ddd�tr$dtz}tj|�|�S|S#t$r}|aYd}~�Md}~wwxYw#||�wxYw#1swY�ZxYw)	zCDecorator for tests requiring a functional bind() for unix sockets.r#zNo UNIX SocketsNr)�TESTFN�unlink�can_bind_unix_socketFz%Requires a functional unix bind(): %s)rr	r&�skip�_bind_nix_socket_error�	os_helperr0r1r#rr)�testr0r1�pathr �e�msgs       r�skip_unless_bind_unix_socketr:�s����6�9�%�/�x�}�}�.�/��5�5��%�-��.�.��
�]�]�6�>�>�
*�	�d�
��	�	�$��).�&��t��	��5�8N�N��!�x�}�}�S�!�$�'�'�����
+�)*�&��
+���t���	�	�sB�"C�$B4�7C�4	C	�=C�?C�C	�	C�
C�C�C"c��tjg}ttd�r|jtj�ttd�r|jtj
�ttd�r|jtj�ts|jtj�|S)zr
    Get the different socket error numbers ('errno') which can be received
    when a connection is refused.
    �ENETUNREACH�
EADDRNOTAVAIL�EHOSTUNREACH)	�errno�ECONNREFUSEDr�appendr<r=r>�IPV6_ENABLED�EAFNOSUPPORT)�errorss r�get_socket_conn_refused_errsrE�s���
� � �
!�F��u�m�$��
�
�e�'�'�(��u�o�&�	�
�
�e�)�)�*��u�n�%��
�
�e�(�(�)���
�
�e�(�(�)��M��)�timeout�errnosc	#����
��K�tjd�}ddl�|turtj
}gd�}gd�}t	jd|z��
|�g��sH|D��cgc]\}}tt||���c}}�|D��cgc]\}}tt||���c}}���
��fd�}tj�}		|�tj|�d��	tj|	�ycc}}wcc}}w#|j$rG}
tjr/tjj!�
j"ddz��
|
�d}
~
wt$$rk}
	|
j"}t'|�d	k\rt)|dt$�r|d}
n(t'|�d
k\rt)|d	t$�r|d	}
nn�\||
��d}
~
wwxYw#tj|	�wxYw�w)z�Return a context manager that raises ResourceDenied when various issues
    with the internet connection manifest themselves as exceptions.�nntplibrN))r@�o)�
ECONNRESET�h)r>�q)r<�e)�	ETIMEDOUT�n)r=�c))�	EAI_AGAIN���)�EAI_FAIL���)�
EAI_NONAME���)�
EAI_NODATA���)�
WSANO_DATAi�*zResource %r is not availablec�
��t|dd�}t|t�s�t|tj�r|�vs�t|�j
j�rd|jcxkrdksPnt|�j
j�r*d|jvs d|jvsd|jvs|�vrBtjs/tjj�jddz��|�y)	Nr?i�iW�ConnectionRefusedError�TimeoutError�EOFErrorr�
)�getattr�
isinstancer_r	�gaierror�error�	HTTPError�code�URLError�reasonr�verbose�sys�stderr�write�args)�err�n�captured_errnos�denied�
gai_errnos�urllibs  ����r�filter_errorz(transient_internet.<locals>.filter_error�s�����C��$�'���s�L�)�
��V�_�_�
-�!�z�/�
��V�\�\�3�3�
4�
�C�H�H�
#��
#�
��V�\�\�2�2�
3�+�s�z�z�9�!�S�Z�Z�/�����+�
�� ��?�?��
�
� � ����Q��$�!6�7��c�!�
!rFrarr)r�import_deprecated�urllib.error�_NOT_SETr�INTERNET_TIMEOUT�ResourceDeniedrbr?r	�getdefaulttimeout�setdefaulttimeout�NNTPTemporaryErrorrjrkrlrmrnr�lenrc)�
resource_namerHrIrK�default_errnos�default_gai_errnos�name�numru�old_timeoutro�arqrrrsrts            @@@@r�transient_internetr��s�������/�/�	�:�G���(���*�*��	�N����
#�
#�$B�]�$R�
S�F��O��J��.<�>�*��c�#�5�$��4�>��*<�=�%�4���f�d�C�0�=�
�
"��*�*�,�K�.����$�$�W�-�
�0	� � ��-��a>��=��.�%�%���?�?��J�J���V�[�[��^�d�2�3��#���������A��1�v��{�z�!�A�$��8���d���Q��1���A�a�D�'�!:���d����	�S��
����&	� � ��-�s\�AG&�#C5�>	G&�C;�" G&�D�"G&�G	�AE�G	�A&G�G	�	G�G#�#G&c�b�tjddtjj��S)z�
    Create a UNIX domain name: socket.bind() argument of a AF_UNIX socket.

    Return a path relative to the current directory to get a short path
    (around 27 ASCII characters).
    �test_python_z.sock)�prefix�suffix�dir)�tempfile�mktemp�osr7�curdirrGrFr�create_unix_domain_namer�s$���?�?�.��!�w�w�~�~�/�/rFc	�6�	t|S#t$rYnwxYwdd|g}tj|tjtj
d��}|jr=tjdj|��d|j���dt|<y|j}	t|j��}nL#t$r@}tjddj|��d	|�d
|���dt|<Yd}~yd}~wwxYw|t|<|S)z!Get a sysctl value as an integer.�sysctlz-nT)�stdoutrl�text� z command failed with exit code NzFailed to parse z command output z: )�
_sysctl_cache�KeyError�
subprocess�run�PIPE�STDOUT�
returncoder�
print_warning�joinr��int�strip�	Exception)r��cmd�proc�output�value�excs      r�_get_sysctlr�$s ��
��T�"�"���
��
���T�4�
 �C��>�>�#�!+���!+�!2�!2�#�%�D����������#�� 1�2+�+/�?�?�*;�=�	>�#�
�d���
�[�[�F���F�L�L�N�#�������� 0����#��0A�B0�06�z��C�7�D�	E�#�
�d������ �M�$���Ls$��	��*C�	D
�
6D�D
c�h�tjjd�sytd�}|�y|dk7S)N�freebsdFznet.inet.tcp.blackholer)rk�platform�
startswithr�)r�s r�
tcp_blackholer�Gs6���<�<�"�"�9�-��
�0�1�E��}���Q�J�rFc�N�tjt�d�}||�S)z4Decorator skipping test if TCP blackhole is enabled.z8TCP blackhole is enabled (sysctl net.inet.tcp.blackhole))r&�skipIfr�)r6�skip_ifs  r�skip_if_tcp_blackholer�Ss$���o�o���B��G��4�=�rF)$�
contextlibr?�os.pathr�r	rkr�r�r&�rr�HOST�HOSTv4r-�is_wasi�has_gethostnamerrrr
r)r.rBr4r:rE�objectrx�contextmanagerr�r�r�r�r�r�rGrFr�<module>r�s������
�
��������	��	���o�o�%��#�N�N�V�5G�5G�;�z�%�N?�
� �!�����.�*�8�����19�"�O.��O.�d/��
� �F	�rF

Zerion Mini Shell 1.0