U
    dd                     @   s  U d dl mZ d dlmZmZ d dlmZmZmZm	Z	m
Z
mZmZ d dlZd dlZd dlmZ ddlmZmZ ddlmZ erd d	lmZ dae
e ed
< eeeeeef f dddZeeeeef e	eedf dddZdddddZeeddddZej ddeeeddddZ!ej ddee
e" dddZ#ej$e d eed!d"d#Z%ej$eed$d%d&Z&ej$eeeef d$d'd(Z'dS ))    )contextmanager)isasyncgenfunctioniscoroutinefunction)TYPE_CHECKINGAnyDict	GeneratorOptionalTuplecastN)FixtureRequest   )get_all_backendsget_asynclib)
TestRunner)Config_current_runner)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-yaxr6kle/anyio/pytest_plugin.pyextract_backend_and_options   s    
r   )backend_namebackend_optionsr   c              	   c   s   t rt V  d S t| }d }tjd d kr6tj| }z.|p>i }|jf |}|a |V  W 5 Q R X W 5 d a |rztj| X d S N)r   r   sniffioZcurrent_async_library_cvargetsetresetr   )r   r    Zasynclibtokenrunnerr   r   r   
get_runner   s    r(   r   )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_configure6   s    r+   )
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)r.   argskwargsr   r    r'   funcZhas_backend_argr   r   wrapper?   s    z%pytest_fixture_setup.<locals>.wrapperr.   r.   )r2   r   r   Zfixturenamesargnames)r,   r-   r3   r   r1   r   pytest_fixture_setup>   s    

r6   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 )r;   N)r8   ).0markerr   r   r   	<genexpr>\   s     z,pytest_pycollect_makeitem.<locals>.<genexpr>r.   )Zistestfunctionhasattrr:   
inner_testr   Zget_closest_markergetattranypytestmarkZusefixtures)r7   r8   r9   Z
inner_funcr=   Zown_markersr   r   r   pytest_pycollect_makeitemU   s    
rE   )
pyfuncitemr   c              	      s   t 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 )	N)r0   r   c               	      s&   t  }||  W 5 Q R X d S r!   )r(   run_test)r0   r'   )r   r    original_funcr   r   run_with_hypothesisb   s    z/pytest_pyfunc_call.<locals>.run_with_hypothesisr.   r:   c                    s   i | ]}| | qS r   r   )r<   arg)funcargsr   r   
<dictcomp>u   s      z&pytest_pyfunc_call.<locals>.<dictcomp>T)r   rK   r#   r   r?   r9   r:   r@   __qualname__r   Z_fixtureinfor5   r(   rG   )rF   rI   r   Ztestargsr'   r   )r   r    rK   rH   r   pytest_pyfunc_call`   s"    


rN   )params)r-   r   c                 C   s   | j S r!   )param)r-   r   r   r   r.   ~   s    r.   )r.   r   c                 C   s   t | tr| S | d S d S )Nr   r   r   r4   r   r   r   anyio_backend_name   s    
rR   c                 C   s   t | tri S | d S d S )Nr   rQ   r4   r   r   r   anyio_backend_options   s    
rS   )(
contextlibr   inspectr   r   typingr   r   r   r   r	   r
   r   rC   r"   Z_pytest.fixturesr   Z_core._eventloopr   r   abcr   Z_pytest.configr   r   __annotations__objectr   r   r(   r+   r6   ZhookimplrE   boolrN   Zfixturer.   rR   rS   r   r   r   r   <module>   s:    $ 
 



