a
    i3e1                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZdZ	dd Z
dd Zdd Zejd	Zd
d ZdZdZdd Ze ZejejfddZefddZdd Zdd Zdd Zzd dlT W n ey   Y n0 zd dlmZ W n ey   Y n0 dS )    Ni @ c                 C   s   t dd|  } | S )zStrip the stderr of a Python process from potential debug output
    emitted by the interpreter.

    This will typically be run on the result of the communicate() method
    of a subprocess.Popen object.
    s   \[\d+ refs, \d+ blocks\]\r?\n?    )resubstrip)stderr r   G/home/pi/bot/my_env/lib/python3.9/site-packages/asyncio/test_support.pystrip_python_stderr   s    r	   c           
      O   s(  d|v r| d}n| }tjddg}|r@tjdkr@|d n|sN|d tj }| dd rhi }|| |	| t
j|t
jt
jt
j|d}z,| \}}W t
  |j  |j  nt
  |j  |j  0 |j}	t|}|	r| s|	s| std	|	|d
df |	||fS )NZ
__isolatedz-Xfaulthandler)      z-Iz-EZ
__cleanenv)stdinstdoutr   envz-Process return code is %d, stderr follows:
%sasciiignore)popsys
executableversion_infoappendosenvironcopyupdateextend
subprocessPopenPIPEcommunicate_cleanupr   closer   
returncoder	   AssertionErrordecode)
Zexpected_successargsenv_varsisolatedZcmd_liner   pouterrrcr   r   r   _assert_python%   sD    






r,   c                  O   s   t dg| R i |S )ay  
    Assert that running the interpreter with `args` and optional environment
    variables `env_vars` succeeds (rc == 0) and return a (return code, stdout,
    stderr) tuple.

    If the __cleanenv keyword is set, env_vars is used a fresh environment.

    Python is started in isolated mode (command line option -I),
    except if the __isolated keyword is set to False.
    T)r,   )r%   r&   r   r   r   assert_python_okM   s    r-   javac                   C   s*   t   trtd t   t   dS )a  Force as many objects as possible to be collected.

    In non-CPython implementations of Python, this is needed because timely
    deallocation is not guaranteed by the garbage collector.  (Even in CPython
    this can be the case in case of reference cycles.)  This means that __del__
    methods may be called later than expected and weakrefs may remain alive for
    longer than expected.  This function tries its best to force all garbage
    objects to disappear.
    g?N)gcZcollect	is_jythontimesleepr   r   r   r   
gc_collect]   s
    

r3   z	127.0.0.1z::1c                  C   st   t jrpd} zVz2t  t jt j} | tdf W W | r<|   dS  tyP   Y n0 W | rp|   n| rn|   0 dS )z+Check whether IPv6 is enabled on this host.Nr   TF)sockethas_ipv6AF_INET6SOCK_STREAMbindHOSTv6r!   OSError)sockr   r   r   _is_ipv6_enabledr   s     

r<   c                 C   s"   t  | |}t|}|  ~|S )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:
    http://msdn2.microsoft.com/en-us/library/ms740621(VS.85).aspx)

    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.
    )r4   	bind_portr!   )familysocktypeZtempsockportr   r   r   find_unused_port   s
    8rA   c                 C   s   | j tjkr| jtjkrttdr>| tjtjdkr>t	dttdrz$| tjtj
}|dkrjt	dW n ty~   Y n0 ttdr| tjtjd | |df |  d }|S )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_REUSEADDR   zHtests 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   )r>   r4   AF_INETtyper7   hasattr
getsockopt
SOL_SOCKETrB   Z
TestFailedrD   r:   
setsockoptrE   r8   getsockname)r;   hostZreuser@   r   r   r   r=      s     


r=   c                     s    fdd}|S )zDecorator raising SkipTest if the OS is Mac OS X and the OS X
    version if less than min_version.

    For example, @requires_mac_ver(10, 5) raises SkipTest if the OS X version
    is lesser than 10.5.
    c                    s"   t   fdd}|_|S )Nc                     sz   t jdkrlt d }zttt|d}W n ty@   Y n,0 |k rldtt	}t
d||f  | i |S )Ndarwinr   .z&Mac OS X %s or higher required, not %s)r   platformmac_vertuplemapintsplit
ValueErrorjoinstrunittestSkipTestr%   kwZversion_txtversionZmin_version_txt)funcmin_versionr   r   wrapper   s    
z4requires_mac_ver.<locals>.decorator.<locals>.wrapper	functoolswrapsr_   r^   r`   r_   r^   r   	decorator   s    z#requires_mac_ver.<locals>.decoratorr   )r_   rg   r   re   r   requires_mac_ver   s    rh   c                    s    fdd}|S )zDecorator raising SkipTest if the OS is `sysname` and the version is
    less than `min_version`.

    For example, @_requires_unix_version('FreeBSD', (7, 2)) raises SkipTest if
    the FreeBSD version is less than 7.2.
    c                    s$   t   fdd}|_|S )Nc                     s   t  krxt  ddd }zttt|d}W n tyJ   Y n.0 |k rxdtt	}t
d||f  | i |S )N-rC   r   rO   z(%s version %s or higher required, not %s)rP   systemreleaserU   rR   rS   rT   rV   rW   rX   rY   rZ   r[   )r^   r_   sysnamer   r   r`     s    z:_requires_unix_version.<locals>.decorator.<locals>.wrapperra   rd   r_   rl   rf   r   rg   
  s    z)_requires_unix_version.<locals>.decoratorr   )rl   r_   rg   r   rm   r   _requires_unix_version  s    rn   c                  G   s
   t d| S )zDecorator raising SkipTest if the OS is FreeBSD and the FreeBSD version
    is less than `min_version`.

    For example, @requires_freebsd_version(7, 2) raises SkipTest if the FreeBSD
    version is less than 7.2.
    ZFreeBSD)rn   re   r   r   r   requires_freebsd_version  s    ro   )*)r-   )rb   r/   r   rP   r   r4   r   r   r1   ZPIPE_MAX_SIZEr	   r,   r-   
startswithr0   r3   ZHOSTr9   r<   ZIPV6_ENABLEDrF   r7   rA   r=   rh   rn   ro   Ztest.supportImportErrorZtest.script_helperr   r   r   r   <module>   s>   (>&
