o
    +i(                     @  s  U d dl mZ d dlZd dlZd dlmZmZmZ d dlm	Z	m
Z
 d dlmZmZmZ d dlmZ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mZmZmZ d	dlmZ d	dl m!Z! ej"dk rld dl#m$Z$ da%de&d< da'de&d< d a(dSddZ)e
dTdd Z*dUd$d%Z+dVd(d)Z,ej-d*d+dWd0d1Z.ej-d*d2dXd7d8Z/ej-d*d2dYd;d<Z0ej1d=e d>dZd?d@Z2ej1d[dAdBZ3ej1d\dCdDZ4G dEdF dFZ5ej1dGdHd]dIdJZ6ej1dGdHd]dKdLZ7ej1d^dOdPZ8ej1d_dQdRZ9dS )`    )annotationsN)Callable	GeneratorIterator)	ExitStackcontextmanager)isasyncgenfunctioniscoroutinefunctionismethod)Anycast)
SubRequest)Exit   )get_available_backends)current_async_libraryget_async_backendreset_current_async_libraryset_current_async_library)iterate_exceptions)
TestRunner)      )ExceptionGroupzTestRunner | None_current_runnerzExitStack | None_runner_stackbackendobjectreturntuple[str, dict[str, Any]]c                 C  sf   t | tr	| i fS t | tr/t| dkr/t | d tr/t | d tr/ttttttf f | S td)N   r   r   z@anyio_backend must be either a string or tuple of (string, dict))
isinstancestrtuplelendictr   r   	TypeError)r    r'   F/var/www/html/venv/lib/python3.10/site-packages/anyio/pytest_plugin.pyextract_backend_and_options    s   
r)   backend_namer"   backend_optionsdict[str, Any]Iterator[TestRunner]c                 c  s    t d u r't| }t at d u rt| }tt| |pi }t|	|a t
d7 a
zt V  W t
d8 a
t
sFtd us<J t  d  aa d S d S t
d8 a
t
s\td usTJ t  d  aa w Nr   )r   r   r   r   r   r   callbackr   enter_contextcreate_test_runner_runner_leasesclose)r*   r+   asynclibtokenr'   r'   r(   
get_runner*   s2   

r6   parserpytest.ParserNonec                 C  s   | j dddd d S )N
anyio_modestrictz-AnyIO plugin mode (either "strict" or "auto"))defaulthelp)addini)r7   r'   r'   r(   pytest_addoptionH   s
   
r?   configpytest.Configc                 C  sV   |  dd | ddkr%| jdr'| ddkr)| tdd d S d S d S d S )	NmarkerszManyio: mark the (coroutine function) test to be run asynchronously via anyio.r:   autoasyncioasyncio_modezlAnyIO auto mode has been enabled together with pytest-asyncio auto mode. This may cause unexpected behavior.r   )addinivalue_linegetinipluginmanager
has_pluginissue_config_time_warningpytestPytestConfigWarning)r@   r'   r'   r(   pytest_configureP   s    
rM   T)hookwrapper
fixturedefr   requestGenerator[Any]c                 #  s    d fdd}| j  t st rNd|jv rN|| _ | j}d| jv  s.|  jd	7  _d| jv  s<|  jd
7  _z
d V W  | _ || _S  | _ || _w d V S )Nanyio_backendr   rP   r   kwargsr   c                 ;  s    |j rt rt jt|j u r j|j }n }t| \}}r(| |d< r.||d< t||&}t|rB|	||E d H  n|
||V  W d    d S W d    d S 1 s\w   Y  d S )NrR   rP   )instancer
   type__self____func____get__r)   r6   r   run_asyncgen_fixturerun_fixture)rR   rP   rS   
local_funcr*   r+   runnerfunchas_backend_arghas_request_argr'   r(   wrappere   s(   "z%pytest_fixture_setup.<locals>.wrapperrR   rP   )rR   r   rP   r   rS   r   r   r   )r^   r   r	   fixturenamesargnames)rO   rP   ra   original_argnamer'   r]   r(   pytest_fixture_setupc   s$   
rg   )tryfirst	collectorpytest.Module | pytest.Classnameobjc                 C  s   |  ||r?t|dr|jjn|}t|rA| jddk}| d}t|dd}|s5|s5t	dd |D rCt
jd	| d S d S d S d S )
N
hypothesisr:   rC   anyio
pytestmarkr'   c                 s  s    | ]}|j d kV  qdS )rn   N)rk   ).0markerr'   r'   r(   	<genexpr>   s    z,pytest_pycollect_makeitem.<locals>.<genexpr>rR   )istestfunctionhasattrrm   
inner_testr	   r@   rG   get_closest_markergetattranyrK   markusefixtures)ri   rk   rl   
inner_funcanyio_auto_moderq   own_markersr'   r'   r(   pytest_pycollect_makeitem   s    
r~   
pyfuncitembool | Nonec                   s  d fdd}| j d}|rt|\ t| jdr3| jjjj|jkr1tr1|| jj_d S t| jr| j fd	d
| j	j
D }t 2}z	|| j| W n tyt } zt|D ]}t|tttfrn||qa d }~ww W d    dS 1 sw   Y  dS d S )NrS   r   r   r9   c                    s<   t  }||  W d    d S 1 sw   Y  d S N)r6   run_test)rS   r\   )r*   r+   original_funcr'   r(   run_with_hypothesis   s   "z/pytest_pyfunc_call.<locals>.run_with_hypothesisrR   rm   c                   s   i | ]}| | qS r'   r'   )rp   arg)funcargsr'   r(   
<dictcomp>   s    z&pytest_pyfunc_call.<locals>.<dictcomp>T)rS   r   r   r9   )r   getr)   rt   rl   rm   ru   __qualname__r	   _fixtureinfore   r6   r   r   r   r!   r   KeyboardInterrupt
SystemExit)r   r   r   testargsr\   excgrpexcr'   )r*   r+   r   r   r(   pytest_pyfunc_call   s<   





r   module)scopeparamsc                 C     | j S r   )paramrc   r'   r'   r(   rR         rR   c                 C  s   t | tr| S | d S )Nr   r!   r"   rb   r'   r'   r(   anyio_backend_name      
r   c                 C  s   t | tri S | d S r.   r   rb   r'   r'   r(   anyio_backend_options   r   r   c                   @  s4   e Zd ZdZdddZeddd	ZddddZd
S )FreePortFactoryaO  
    Manages port generation based on specified socket kind, ensuring no duplicate
    ports are generated.

    This class provides functionality for generating available free ports on the
    system. It is initialized with a specific socket kind and can generate ports
    for given address families while avoiding reuse of previously generated ports.

    Users should not instantiate this class directly, but use the
    ``free_tcp_port_factory`` and ``free_udp_port_factory`` fixtures instead. For simple
    uses cases, ``free_tcp_port`` and ``free_udp_port`` can be used instead.
    kindsocket.SocketKindr   r9   c                 C  s   || _ tt  | _d S r   )_kindsetint
_generated)selfr   r'   r'   r(   __init__   s   zFreePortFactory.__init__c                 C  r   )z
        The type of socket connection (e.g., :data:`~socket.SOCK_STREAM` or
        :data:`~socket.SOCK_DGRAM`) used to bind for checking port availability

        )r   )r   r'   r'   r(   r      s   zFreePortFactory.kindNfamilysocket.AddressFamily | Noner   c              
   C  s   |dur|g}nt jg}t jr|t j 	 d}t P}|D ]1}|t  || j}|t jkr1dnd}z	|||f W n
 t	yF   Y  nw |sO|
 d }q|| jvrd| j| |W  d   S W d   n1 snw   Y  q)z
        Return an unbound port for the given address family.

        :param family: if omitted, both IPv4 and IPv6 addresses will be tried
        :return: a port number

        NTr   z::1z	127.0.0.1r   )socketAF_INEThas_ipv6appendAF_INET6r   r0   r   bindOSErrorgetsocknamer   add)r   r   familiesportstacksockaddrr'   r'   r(   __call__   s4   
 zFreePortFactory.__call__)r   r   r   r9   )r   r   r   )r   r   r   r   )__name__
__module__r   __doc__r   propertyr   r   r'   r'   r'   r(   r      s    
r   session)r   c                   C  
   t tjS r   )r   r   SOCK_STREAMr'   r'   r'   r(   free_tcp_port_factory     
r   c                   C  r   r   )r   r   
SOCK_DGRAMr'   r'   r'   r(   free_udp_port_factory"  r   r   Callable[[], int]r   c                 C     |  S r   r'   )r   r'   r'   r(   free_tcp_port'  r   r   c                 C  r   r   r'   )r   r'   r'   r(   free_udp_port,  r   r   )r   r   r   r   )r*   r"   r+   r,   r   r-   )r7   r8   r   r9   )r@   rA   r   r9   )rO   r   rP   r   r   rQ   )ri   rj   rk   r"   rl   r   r   r9   )r   r   r   r   )rP   r   r   r   )rR   r   r   r"   )rR   r   r   r,   )r   r   )r   r   r   r   )r   r   r   r   ):
__future__r   r   syscollections.abcr   r   r   
contextlibr   r   inspectr   r	   r
   typingr   r   rK   _pytest.fixturesr   _pytest.outcomesr    r   _core._eventloopr   r   r   r   _core._exceptionsr   abcr   version_infoexceptiongroupr   r   __annotations__r   r2   r)   r6   r?   rM   hookimplrg   r~   r   fixturerR   r   r   r   r   r   r   r   r'   r'   r'   r(   <module>   sZ    






0
$
=
