o
    i1                     @  s(  U d dl mZ d dl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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 m!Z!m"Z"m#Z# ddl$m%Z% ddl&m'Z' ej(dk r~d dl)m*Z* da+de,d< da-de,d< d a.dXddZ/edYd!d"Z0dZd&d'Z1d[d*d+Z2ej3d,d-d\d2d3Z4ej3d,d4d]d9d:Z5d^d=d>Z6ej3d,d4d_dAdBZ7ej8dCe dDd`dEdFZ9ej8dadGdHZ:ej8dbdIdJZ;G dKdL dLZ<ej8d;dMdcdNdOZ=ej8d;dMdcdPdQZ>ej8dddTdUZ?ej8dedVdWZ@dS )f    )annotationsN)Callable	GeneratorIterator)	ExitStackcontextmanager)isasyncgenfunctioniscoroutinefunctionismethod)Anycast)FuncFixtureInfo
SubRequest)Exit)	CallSpec2)Scope   )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*   M/home/kim/smarthome/.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_contextZcreate_test_runner_runner_leasesclose)r-   r.   Zasynclibtokenr*   r*   r+   
get_runner-   s2   

r7   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)Zaddini)r8   r*   r*   r+   pytest_addoptionK   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;   autoZasyncioZasyncio_modezlAnyIO auto mode has been enabled together with pytest-asyncio auto mode. This may cause unexpected behavior.r   )Zaddinivalue_linegetiniZpluginmanagerZ
has_pluginZissue_config_time_warningpytestZPytestConfigWarning)r@   r*   r*   r+   pytest_configureS   s    
rF   T)Z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   rH   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 )NrJ   rH   )instancer
   type__self____func____get__r,   r7   r   Zrun_asyncgen_fixtureZrun_fixture)rJ   rH   rK   Z
local_funcr-   r.   runnerfuncZhas_backend_argZhas_request_argr*   r+   wrapperh   s(   "z%pytest_fixture_setup.<locals>.wrapperrJ   rH   )rJ   r   rH   r   rK   r   r!   r   )rS   r   r	   fixturenamesargnames)rG   rH   rT   Zoriginal_argnamer*   rR   r+   pytest_fixture_setupf   s$   
rY   )Z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   anyioZ
pytestmarkr*   c                 s  s    | ]}|j d kV  qdS )r_   Nr\   ).0markerr*   r*   r+   	<genexpr>   s    z,pytest_pycollect_makeitem.<locals>.<genexpr>rJ   )Zistestfunctionhasattrr^   
inner_testr	   r@   rD   get_closest_markergetattranyrE   markZusefixtures)rZ   r\   r]   Z
inner_funcZanyio_auto_moderb   Zown_markersr*   r*   r+   pytest_pycollect_makeitem   s    
rj   sessionpytest.Sessionc                 C  sl  t tt| jD ]\}}t|tjrt|jr|	dd urd|j
vrg }zdd ttD }W n ty>   t }Y nw tt D ]e\}}d|v r^td|id|idtji|gg d}nti d|id|idtji|gg d}|j}t|j}	d|	vr|	d t|j|j|	|jd}
tjj|j|j d	| d
||j|
|j|jd}|| qD|| j||d < q	d S )Nr_   rJ   c                 S  s   h | ]}|j qS r*   r`   )ra   fr*   r*   r+   	<setcomp>   s    z+pytest_collection_finish.<locals>.<setcomp>
_arg2scope)paramsindicesro   Z_idlistmarks)funcargsrp   rq   Z	arg2scopeZidlistrr   )rX   initialnamesnames_closurename2fixturedefs[])r\   callspecZcallobjZfixtureinfokeywordsoriginalnamer   )reversedlist	enumerateitemsr$   rE   Functionr	   functionrf   rW   dataclassesfieldsr   r)   setr   r   Module_fixtureinforu   appendr   rX   rt   rv   Zfrom_parentparentr{   r]   rz   )rk   iitemZ	new_itemsZ	cs_fieldsZparam_indexr   ry   fiZnew_names_closureZnew_fixtureinfoZnew_itemr*   r*   r+   pytest_collection_finish   sj   


	

	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 )NrK   r   r!   r:   c                    s<   t  }||  W d    d S 1 sw   Y  d S N)r7   run_test)rK   rQ   )r-   r.   original_funcr*   r+   run_with_hypothesis   s   "z/pytest_pyfunc_call.<locals>.run_with_hypothesisrJ   r^   c                   s   i | ]}| | qS r*   r*   )ra   arg)rs   r*   r+   
<dictcomp>   s    z&pytest_pyfunc_call.<locals>.<dictcomp>T)rK   r   r!   r:   )rs   getr,   rd   r]   r^   re   __qualname__r	   r   rX   r7   r   r   r   r$   r   KeyboardInterrupt
SystemExit)r   r   r   ZtestargsrQ   Zexcgrpexcr*   )r-   r.   rs   r   r+   pytest_pyfunc_call   s<   





r   module)scoperp   c                 C     | j S r   )paramrV   r*   r*   r+   rJ        rJ   c                 C  s   t | tr| S | d S )Nr   r$   r%   rU   r*   r*   r+   anyio_backend_name     
r   c                 C  s   t | tri S | d S r1   r   rU   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!   r:   c                 C  s   || _ tt  | _d S r   )_kindr   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_ipv6r   AF_INET6r   r3   r   bindOSErrorgetsocknamer   add)r   r   Zfamiliesportstacksockaddrr*   r*   r+   __call__8  s4   
 zFreePortFactory.__call__)r   r   r!   r:   )r!   r   r   )r   r   r!   r   )__name__
__module__r   __doc__r   propertyr   r   r*   r*   r*   r+   r     s    
r   )r   c                   C  
   t tjS r   )r   r   SOCK_STREAMr*   r*   r*   r+   free_tcp_port_factoryZ     
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_portd  r   r   c                 C  r   r   r*   )r   r*   r*   r+   free_udp_porti  r   r   )r   r    r!   r"   )r-   r%   r.   r/   r!   r0   )r8   r9   r!   r:   )r@   rA   r!   r:   )rG   r   rH   r   r!   rI   )rZ   r[   r\   r%   r]   r    r!   r:   )rk   rl   r!   r:   )r   r   r!   r   )rH   r   r!   r   )rJ   r   r!   r%   )rJ   r   r!   r/   )r!   r   )r   r   r!   r   )r   r   r!   r   )A
__future__r   r   r   syscollections.abcr   r   r   
contextlibr   r   inspectr   r	   r
   typingr   r   rE   Z_pytest.fixturesr   r   Z_pytest.outcomesr   Z_pytest.pythonr   Z_pytest.scoper    r   Z_core._eventloopr   r   r   r   Z_core._exceptionsr   abcr   version_infoZexceptiongroupr   r   __annotations__r   r4   r,   r7   r?   rF   ZhookimplrY   rj   r   r   ZfixturerJ   r   r   r   r   r   r   r   r*   r*   r*   r+   <module>   sb    






0

:$
=
