U
    e
                     @  sn  U d dl mZ d dlmZ d dlmZmZ d dlmZm	Z	 d dl
mZmZmZmZ d dlZd dlZddlmZmZ dd	lmZ dad
ed< daded< d adddddZeddddddZdddddZdddddd Zejd!d"ddddd#d$d%Zejd!d"dd&d'd(d)Z ej!d*e d+ddd,d-d.Z"ej!ddd/d0d1Z#ej!ddd/d2d3Z$dS )4    )annotations)Iterator)	ExitStackcontextmanager)isasyncgenfunctioniscoroutinefunction)AnyDictTuplecastN   )get_all_backendsget_async_backend)
TestRunnerzTestRunner | None_current_runnerzExitStack | None_runner_stackobjectztuple[str, dict[str, Any]])backendreturnc                 C  sj   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d S )N   r   r   z@anyio_backend must be either a string or tuple of (string, dict))

isinstancestrtuplelendictr   r
   r	   r   	TypeError)r    r   7/tmp/pip-unpacked-wheel-7mhetrzx/anyio/pytest_plugin.pyextract_backend_and_options   s    
r   r   zdict[str, Any]zIterator[TestRunner])backend_namebackend_optionsr   c                 c  s   t d krZt| }t atjd d krBtj| }ttjj	| |pHi }t
||a td7 az
t V  W 5 td8 atstd k	stt  d  aa X d S Nr   )r   r   r   r   sniffioZcurrent_async_library_cvargetsetcallbackresetenter_contextZcreate_test_runner_runner_leasesAssertionErrorclose)r   r    Zasynclibtokenr   r   r   
get_runner   s$    
r,   r   None)configr   c                 C  s   |  dd d S )NmarkerszManyio: mark the (coroutine function) test to be run asynchronously via anyio.)Zaddinivalue_line)r.   r   r   r   pytest_configure;   s    r0   )
fixturedefrequestr   c                   sT    fdd}| j  t s$t rPd|jkrPd| jk|| _ sP|  jd7  _d S )Nc              	   ?  s\   t | \}}r| |d< t||0}t r@| |E d H  n| |V  W 5 Q R X d S )Nanyio_backend)r   r,   r   Zrun_asyncgen_fixtureZrun_fixture)r3   argskwargsr   r    runnerfuncZhas_backend_argr   r   wrapperD   s    z%pytest_fixture_setup.<locals>.wrapperr3   r3   )r8   r   r   Zfixturenamesargnames)r1   r2   r9   r   r7   r   pytest_fixture_setupC   s    

r<   T)Ztryfirst)	collectornameobjr   c                 C  sj   |  ||rft|dr|jjn|}t|rf| d}t|dd}|sVtdd |D rftj	
d| d S )N
hypothesisanyioZ
pytestmarkr   c                 s  s   | ]}|j d kV  qdS )rA   N)r>   ).0markerr   r   r   	<genexpr>a   s     z,pytest_pycollect_makeitem.<locals>.<genexpr>r3   )Zistestfunctionhasattrr@   
inner_testr   Zget_closest_markergetattranypytestmarkZusefixtures)r=   r>   r?   Z
inner_funcrC   Zown_markersr   r   r   pytest_pycollect_makeitemZ   s    
rK   zbool | None)
pyfuncitemr   c              	     s   ddd fdd}| j d}|rt|\ t| jdrl| jjjj|jkrhtrh|| jj_d S t| jr| j fdd	| j	j
D }t }|| j| W 5 Q R X d
S d S )Nr   r-   )r5   r   c               	     s&   t  }||  W 5 Q R X d S N)r,   run_test)r5   r6   )r   r    original_funcr   r   run_with_hypothesisg   s    z/pytest_pyfunc_call.<locals>.run_with_hypothesisr3   r@   c                   s   i | ]}| | qS r   r   )rB   arg)funcargsr   r   
<dictcomp>z   s      z&pytest_pyfunc_call.<locals>.<dictcomp>T)rR   r#   r   rE   r?   r@   rF   __qualname__r   Z_fixtureinfor;   r,   rN   )rL   rP   r   Ztestargsr6   r   )r   r    rR   rO   r   pytest_pyfunc_calle   s"    


rU   module)Zscopeparams)r2   r   c                 C  s   | j S rM   )param)r2   r   r   r   r3      s    r3   )r3   r   c                 C  s   t | tr| S | d S d S )Nr   r   r   r:   r   r   r   anyio_backend_name   s    
rZ   c                 C  s   t | tri S | d S d S r!   rY   r:   r   r   r   anyio_backend_options   s    
r[   )%
__future__r   collections.abcr   
contextlibr   r   inspectr   r   typingr   r	   r
   r   rI   r"   Z_core._eventloopr   r   abcr   r   __annotations__r   r(   r   r,   r0   r<   ZhookimplrK   rU   Zfixturer3   rZ   r[   r   r   r   r   <module>   s6    



