o
    í6Wj)5  ã                   @  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*   úb/home/esfera/Documents/content_generation/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   €
ÿýÿ
r9   Ú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)r:   r*   r*   r+   Úpytest_addoptionK   s
   
ýrB   Ú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)rC   r*   r*   r+   Úpytest_configureS   s    þ
ÿÿûürP   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   rS   r   Úkwargsr!   c                 ;  sÖ   � |j rtˆ ƒrtˆ jƒt|j ƒu rˆ j |j ¡}nˆ }t| ƒ\}}ˆr(| |d< ˆr.||d< t||ƒ�.}| ¡ r<t	dƒ‚t
|ƒrJ| ||¡E d H  n| ||¡V  W d   ƒ d S W d   ƒ d S 1 sdw   Y  d S )NrU   rS   z�Cannot schedule a coroutine in the test runner while another is already running; likely caused by request.getfixturevalue() on an async fixture.)Úinstancer
   ÚtypeÚ__self__Ú__func__Ú__get__r,   r9   Ú
is_runningÚRuntimeErrorr   Úrun_asyncgen_fixtureÚrun_fixture)rU   rS   rV   Ú
local_funcr-   r.   Úrunner©ÚfuncÚhas_backend_argÚhas_request_argr*   r+   Úwrapperh   s0   €ÿþÿð"òz%pytest_fixture_setup.<locals>.wrapper©rU   ©rS   )rU   r   rS   r   rV   r   r!   r   )rc   r   r	   ÚfixturenamesÚargnames)rR   rS   rf   Úoriginal_argnamer*   rb   r+   Úpytest_fixture_setupf   s$   €&
ÿrl   )Ú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=   rF   ÚanyioÚ
pytestmarkr*   c                 s  s   � | ]}|j d kV  qdS )rs   N©rp   )Ú.0Úmarkerr*   r*   r+   Ú	<genexpr>°   s   € z,pytest_pycollect_makeitem.<locals>.<genexpr>rU   )ÚistestfunctionÚhasattrrr   Ú
inner_testr	   rC   rJ   Úget_closest_markerÚgetattrÚanyrN   ÚmarkÚusefixtures)rn   rp   rq   Ú
inner_funcÚanyio_auto_moderw   Úown_markersr*   r*   r+   Úpytest_pycollect_makeitem£   s    
ÿþýõr„   Úsessionúpytest.Sessionc                 C  sl  t tt| jƒƒƒD ]ª\}}t|tjƒr³t|jƒr³| 	d¡d ur³d|j
vr³g }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 )Nrs   rU   c                 S  s   h | ]}|j ’qS r*   ru   )rv   Úfr*   r*   r+   Ú	<setcomp>¿   s    z+pytest_collection_finish.<locals>.<setcomp>Ú
_arg2scope)ÚparamsÚindicesr‰   Ú_idlistÚmarks)ÚfuncargsrŠ   r‹   Ú	arg2scopeÚidlistr�   )rj   ÚinitialnamesÚnames_closureÚname2fixturedefsú[ú])rp   ÚcallspecÚcallobjÚfixtureinfoÚkeywordsÚoriginalnamer   )ÚreversedÚlistÚ	enumerateÚitemsr$   rN   ÚFunctionr	   Úfunctionr|   ri   ÚdataclassesÚfieldsr   r)   Úsetr   r   ÚModuleÚ_fixtureinfor’   Úappendr   rj   r‘   r“   Úfrom_parentÚparentrš   rq   r™   )r…   ÚiÚitemÚ	new_itemsÚ	cs_fieldsÚparam_indexr   r–   ÚfiÚnew_names_closureÚnew_fixtureinfoÚnew_itemr*   r*   r+   Úpytest_collection_finishµ   sj   
ÿþ

ÿûú	

üù	€Ê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 )NrV   r   r!   r<   c                    s<   t ˆ ˆƒ�}| ˆ| ¡ W d   ƒ d S 1 sw   Y  d S ©N)r9   Úrun_test)rV   ra   )r-   r.   Úoriginal_funcr*   r+   Úrun_with_hypothesisñ   s   "ÿz/pytest_pyfunc_call.<locals>.run_with_hypothesisrU   rr   c                   s   i | ]}|ˆ | “qS r*   r*   )rv   Úarg)rŽ   r*   r+   Ú
<dictcomp>  s    z&pytest_pyfunc_call.<locals>.<dictcomp>T)rV   r   r!   r<   )rŽ   Úgetr,   rz   rq   rr   r{   Ú__qualname__r	   r¥   rj   r9   r¶   r   r   r$   r   ÚKeyboardInterruptÚ
SystemExit)r³   r¸   r   Útestargsra   ÚexcgrpÚexcr*   )r-   r.   rŽ   r·   r+   Úpytest_pyfunc_callï   s<   


ÿ€ûÿ
þ
ö
rÂ   Úmodule)ÚscoperŠ   c                 C  ó   | j S rµ   )Úparamrh   r*   r*   r+   rU     ó   rU   c                 C  s   t | tƒr| S | d S )Nr   ©r$   r%   rg   r*   r*   r+   Úanyio_backend_name  ó   
rÉ   c                 C  s   t | tƒri S | d S r1   rÈ   rg   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__7  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Ô   ÚfamiliesÚportÚstackÚsockÚaddrr*   r*   r+   Ú__call__D  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_factoryf  ó   
rê   c                   C  rè   rµ   )rÌ   rÖ   Ú
SOCK_DGRAMr*   r*   r*   r+   Úfree_udp_port_factoryk  rë   rí   úCallable[[], int]rÐ   c                 C  ó   | ƒ S rµ   r*   )rê   r*   r*   r+   Úfree_tcp_portp  rÇ   rð   c                 C  rï   rµ   r*   )rí   r*   r*   r+   Úfree_udp_portu  rÇ   rñ   )r   r    r!   r"   )r-   r%   r.   r/   r!   r0   )r:   r;   r!   r<   )rC   rD   r!   r<   )rR   r   rS   r   r!   rT   )rn   ro   rp   r%   rq   r    r!   r<   )r…   r†   r!   r<   )r³   r   r!   r´   )rS   r   r!   r   )rU   r   r!   r%   )rU   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   rN   Ú_pytest.fixturesr   r   Ú_pytest.outcomesr   Ú_pytest.pythonr   Ú_pytest.scoper   Ú r   Ú_core._eventloopr   r   r   r   Ú_core._exceptionsr   Úabcr   Úversion_infoÚexceptiongroupr   r   Ú__annotations__r   r5   r,   r9   rB   rP   Úhookimplrl   r„   r²   rÂ   ÚfixturerU   rÉ   rË   rÌ   rê   rí   rð   rñ   r*   r*   r*   r+   Ú<module>   sb    






<

:$
=
