o
    ;'hŸ$  ã                   @  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Zd dlmZ d dlmZ d	d
lmZmZ d	dlmZ d	dlmZ ejdk rfd dl m!Z! da"de#d< da$de#d< d a%dLdd„Z&e
dMdd„ƒZ'dNd#d$„Z(ej)d%d&�dOd*d+„ƒZ*ej)d%d,�dPd0d1„ƒZ+ej)d%d,�dQd4d5„ƒZ,ej-d6eƒ d7�dRd8d9„ƒZ.ej-dSd:d;„ƒZ/ej-dTd<d=„ƒZ0G d>d?„ d?ƒZ1ej-d@dA�dUdBdC„ƒZ2ej-d@dA�dUdDdE„ƒZ3ej-dVdHdI„ƒZ4ej-dWdJdK„ƒZ5dS )Xé    )ÚannotationsN)ÚCallableÚ	GeneratorÚIterator)Ú	ExitStackÚcontextmanager)ÚisasyncgenfunctionÚiscoroutinefunctionÚismethod)ÚAnyÚcast)Ú
SubRequest)ÚExité   )Úget_all_backendsÚget_async_backend)Ú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$   úIC:\Users\shubh\Videos\agent\nova\Lib\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j d ¡d u r"tj | ¡}t tjj	|¡ |p%i }t 
| |¡¡a td7 azt V  W td8 atsMtd usCJ ‚t ¡  d  aa d S d S td8 atsctd us[J ‚t ¡  d  aa w ©Nr   )r   r   r   r   ÚsniffioZcurrent_async_library_cvarÚgetÚsetÚcallbackÚresetÚenter_contextZcreate_test_runnerÚ_runner_leasesÚclose)r'   r(   ZasynclibÚtokenr$   r$   r%   Ú
get_runner%   s2   €ÿýÿ
r5   Úconfigr   ÚNonec                 C  s   |   dd¡ d S )NÚmarkerszManyio: mark the (coroutine function) test to be run asynchronously via anyio.)Zaddinivalue_line)r6   r$   r$   r%   Úpytest_configureC   s   þr9   T)ZhookwrapperÚ
fixturedefÚ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Úargsr   Úanyio_backendr;   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 )Nr>   r;   )Úinstancer
   ÚtypeÚ__self__Ú__func__Ú__get__r&   r5   r   Zrun_asyncgen_fixtureZrun_fixture)r>   r;   r=   r?   Z
local_funcr'   r(   Úrunner©ÚfuncZhas_backend_argZhas_request_argr$   r%   ÚwrapperL   s(   €ÿþü"þz%pytest_fixture_setup.<locals>.wrapper©r>   ©r;   )
r=   r   r>   r   r;   r   r?   r   r   r   )rG   r   r	   ZfixturenamesÚargnames)r:   r;   rH   Zoriginal_argnamer$   rF   r%   Úpytest_fixture_setupJ   s$   €
ÿrL   )ZtryfirstÚ	collectorÚnameÚobjc                 C  sv   |   ||¡r5t|dƒr|jjn|}t|ƒr7|  d¡}t|ddƒ}|s+tdd„ |D ƒƒr9tj	 
d¡|ƒ d S d S d S d S )NÚ
hypothesisÚanyioZ
pytestmarkr$   c                 s  s   � | ]}|j d kV  qdS )rQ   N)rN   )Ú.0Úmarkerr$   r$   r%   Ú	<genexpr>„   s   € z,pytest_pycollect_makeitem.<locals>.<genexpr>r>   )ZistestfunctionÚhasattrrP   Ú
inner_testr	   Zget_closest_markerÚgetattrÚanyÚpytestÚmarkZusefixtures)rM   rN   rO   Z
inner_funcrS   Zown_markersr$   r$   r%   Úpytest_pycollect_makeitem}   s   
úr[   Ú
pyfuncitemúbool | Nonec                   s  d‡ ‡‡fdd„}| j  d¡}|r‡t|ƒ\‰ ‰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 s€w   Y  dS d S )Nr?   r   r   r7   c                    s<   t ˆ ˆƒ�}| ˆ| ¡ W d   ƒ d S 1 sw   Y  d S ©N)r5   Úrun_test)r?   rE   )r'   r(   Úoriginal_funcr$   r%   Úrun_with_hypothesisŠ   s   "ÿz/pytest_pyfunc_call.<locals>.run_with_hypothesisr>   rP   c                   s   i | ]}|ˆ | “qS r$   r$   )rR   Úarg)Úfuncargsr$   r%   Ú
<dictcomp>�   s    z&pytest_pyfunc_call.<locals>.<dictcomp>T)r?   r   r   r7   )rc   r-   r&   rU   rO   rP   rV   Ú__qualname__r	   Z_fixtureinforK   r5   r_   r   r   r   r   ÚKeyboardInterruptÚ
SystemExit)r\   ra   r   ZtestargsrE   ZexcgrpÚexcr$   )r'   r(   rc   r`   r%   Úpytest_pyfunc_callˆ   s<   


ÿ€ûÿ
þ
ö
ri   Úmodule)ÚscopeÚparamsc                 C  ó   | j S r^   )ÚparamrJ   r$   r$   r%   r>   ­   ó   r>   c                 C  s   t | tƒr| S | d S )Nr   ©r   r   rI   r$   r$   r%   Úanyio_backend_name²   ó   
rq   c                 C  s   t | tƒri S | d S r+   rp   rI   r$   r$   r%   Úanyio_backend_optionsº   rr   rs   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   r7   c                 C  s   || _ tt ƒ | _d S r^   )Ú_kindr.   ÚintÚ
_generated)Úselfru   r$   r$   r%   Ú__init__Ð   s   zFreePortFactory.__init__c                 C  rm   )z¦
        The type of socket connection (e.g., :data:`~socket.SOCK_STREAM` or
        :data:`~socket.SOCK_DGRAM`) used to bind for checking port availability

        )rw   )rz   r$   r$   r%   ru   Ô   s   zFreePortFactory.kindNÚfamilyúsocket.AddressFamily | Nonerx   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   r1   rw   ÚbindÚOSErrorÚgetsocknamery   Úadd)rz   r|   ZfamiliesÚportÚstackÚsockÚaddrr$   r$   r%   Ú__call__Ý   s4   ÿ€
ò€ þzFreePortFactory.__call__)ru   rv   r   r7   )r   rv   r^   )r|   r}   r   rx   )Ú__name__Ú
__module__re   Ú__doc__r{   Úpropertyru   r‹   r$   r$   r$   r%   rt   Â   s    
rt   Úsession)rk   c                   C  ó
   t tjƒS r^   )rt   r~   ÚSOCK_STREAMr$   r$   r$   r%   Úfree_tcp_port_factoryÿ   ó   
r“   c                   C  r‘   r^   )rt   r~   Ú
SOCK_DGRAMr$   r$   r$   r%   Úfree_udp_port_factory  r”   r–   úCallable[[], int]rx   c                 C  ó   | ƒ S r^   r$   )r“   r$   r$   r%   Úfree_tcp_port	  ro   r™   c                 C  r˜   r^   r$   )r–   r$   r$   r%   Úfree_udp_port  ro   rš   )r   r   r   r   )r'   r   r(   r)   r   r*   )r6   r   r   r7   )r:   r   r;   r   r   r<   )rM   r   rN   r   rO   r   r   r7   )r\   r   r   r]   )r;   r   r   r   )r>   r   r   r   )r>   r   r   r)   )r   rt   )r“   r—   r   rx   )r–   r—   r   rx   )6Ú
__future__r   r~   ÚsysÚcollections.abcr   r   r   Ú
contextlibr   r   Úinspectr   r	   r
   Útypingr   r   rY   r,   Z_pytest.fixturesr   Z_pytest.outcomesr   Z_core._eventloopr   r   Z_core._exceptionsr   Úabcr   Úversion_infoZexceptiongroupr   r   Ú__annotations__r   r2   r&   r5   r9   ZhookimplrL   r[   ri   Zfixturer>   rq   rs   rt   r“   r–   r™   rš   r$   r$   r$   r%   Ú<module>   sX    





2

$
=
