a
    qJhD                     @   s4  U d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZ ddlZddlmZmZmZ ejdkrddl	mZ nddlmZ edZed Z ed	Z!ed
edee f dZ"ededee f dZ#ee"e#f Z$ee$ge$f Z%e
Z&e
Z'e
Z(e
Z)e
Z*G dd de+ej,Z-dZ.e)e'ddddZ/edddddde$deee0  e1eeee+e2e3e1df  ee
gee0 f df ee+ e$dddZ4edoddddddddeee0  e1eeee+e2e3e1df  ee
gee0 f df ee+ e%dddZ4dpee$ e
ee$e%f dddZ4e
e1dddZ5e
dddd Z6e
e1dd!d"Z7e
e1dd#d$Z8e&e-d%d&d'Z9e&dd%d(d)Z:ej;d*d+e&ee+ d%d,d-Z<e&ee( dd.d/d0Z=e(dd1d2d3Z>e(dd1d4d5Z?ede
f ee+e
f ej@e*ee+e
f d6d7d8ZAe!ee
 e1e!d9d:d;ZBe(dd1d<d=ZCe(dd1d>d?ZDeE ZFee( eGd@< ej;d*d+eejHejIf e+e0eejejJeeejejJf  df dAdBdCZKee&ee ddDdEdFZLe
e1dGdHdIZMej;d*dJe(e*ddKdLdMZNej;d*dNe(e*ee0 dKdOdPZOej;d*d*dQejee0 dRdSdTZPe
e1dGdUdVZQejedee
 f ej@dWdXdYZRejddZd[d\ZSej4d]eej@ d^d_d`ZTe3e3dadbdcZUej4e3dddedfZVej4e3dddgdhZWej4didjeg e3f dddkdlZXej4didjeg e3f dddmdnZYdS )qzpytest-asyncio implementation.    N)AnyAsyncIterator	AwaitableCallableDictIterableIteratorListOptionalSetTypeVarUnioncastoverload)FunctionItemSession)      )Literal_R)sessionpackagemoduleclassfunction_TSimpleFixtureFunction.)boundFactoryFixtureFunctionc                   @   s   e Zd ZdZdZdS )ModeautostrictN)__name__
__module____qualname__AUTOSTRICT r(   r(   G/var/www/shaz/venv/lib/python3.9/site-packages/pytest_asyncio/plugin.pyr    8   s   r    z'auto' - for automatically handling all async functions by the plugin
'strict' - for autoprocessing disabling (useful if different async frameworks should be tested together, e.g. both pytest-asyncio and pytest-trio are used in the same project)
)parserpluginmanagerreturnc                 C   s2   |  d}|jddd dtd | jdddd d S )	Nasyncioz--asyncio-modeasyncio_modeZMODE)destdefaultmetavarhelpz default value for --asyncio-moder"   )r2   r0   )ZgetgroupZ	addoptionASYNCIO_MODE_HELPZaddini)r*   r+   groupr(   r(   r)   pytest_addoptionE   s    
r5   )scopeparamsautouseidsnamez6Union[_ScopeName, Callable[[str, Config], _ScopeName]])fixture_functionr6   r7   r8   r9   r:   r,   c                C   s   d S Nr(   r;   r6   r7   r8   r9   r:   r(   r(   r)   fixtureU   s    r>   c                C   s   d S r<   r(   r=   r(   r(   r)   r>   f   s    )r;   kwargsr,   c                    sH   | d ur"t |  tj| fi  S ttttd fdd}|S d S )N)r;   r,   c                    s   t | fi  S r<   )r>   )r;   r?   r(   r)   inner   s    zfixture.<locals>.inner)_make_asyncio_fixture_functionpytestr>   	functoolswrapsFixtureFunction)r;   r?   rA   r(   r@   r)   r>   w   s    )objr,   c                 C   s   t | d| } t | ddS )N__func___force_asyncio_fixtureFgetattrrG   r(   r(   r)   _is_asyncio_fixture_function   s    rM   c                 C   s   t | dr| j} d| _d S )NrH   T)hasattrrH   rI   rL   r(   r(   r)   rB      s    
rB   c                 C   s
   t | S )z9Check to see if an object is really an asyncio coroutine.)r-   iscoroutinefunctionrL   r(   r(   r)   _is_coroutine   s    rP   c                 C   s   t | pt| S r<   )rP   inspectisasyncgenfunctionrL   r(   r(   r)   _is_coroutine_or_asyncgen   s    rS   )configr,   c                 C   s$   |  d}|d u r| d}t|S )Nr.   )Z	getoptionZgetinir    )rT   valr(   r(   r)   _get_asyncio_mode   s    

rV   c                 C   s,   |  dd ttdddk r(tdt dS )zInject documentation.markerszQasyncio: mark the test as a coroutine, it will be run using an asyncio event loopversion_tuple)r   r   r   )   zYou're using an outdated version of pytest. Newer releases of pytest-asyncio will not be compatible with this pytest version. Please update pytest to version 7 or later.N)Zaddinivalue_linerK   rC   warningswarnDeprecationWarning)rT   r(   r(   r)   pytest_configure   s    r]   T)tryfirstc                 C   s   t | }d| gS )z$Add asyncio config to pytest header.zasyncio: mode=)rV   )rT   moder(   r(   r)   pytest_report_header   s    r`   )rT   processed_fixturedefsr,   c                 C   s   t | }| jd}|j D ]j}|D ]`}|j}||v s&t|sBq&t|sV|tj	krVq&t
| t| t| t|js|J || q&qd S )NZ
funcmanage)rV   r+   Z
get_pluginZ_arg2fixturedefsvaluesfuncrS   rM   r    r'   rB   _inject_fixture_argnames_synchronize_async_fixtureadd)rT   ra   r.   ZfixturemanagerZfixtures
fixturedefrc   r(   r(   r)   _preprocess_async_fixtures   s     
rh   )rg   r,   c                 C   s<   g }dD ]}|| j vr|| q|r8|  j t|7  _ dS )zY
    Ensures that `request` and `event_loop` are arguments of the specified fixture.
    )request
event_loopN)argnamesappendtuple)rg   Zto_addr:   r(   r(   r)   rd      s    
rd   c                 C   s.   t | jrt|  nt | jr*t|  dS )zS
    Wraps the fixture function of an async fixture in a synchronous function.
    N)rQ   rR   rc   _wrap_asyncgen_fixturerO   _wrap_async_fixture)rg   r(   r(   r)   re      s    
re   )rc   r?   rj   ri   r,   c                 C   s:   t | }| }d|jv r$||d< d|jv r6||d< |S )Nri   rj   )rQ   	signaturecopy
parameters)rc   r?   rj   ri   sigretr(   r(   r)   _add_kwargs   s    


ru   )rc   instanceunittestr,   c                 C   s`   |d ur\| d  }}z| j t| j }}W n ty:   Y n0 |sR|d ur\t||r\||} | S r<   )rH   type__self__AttributeError
isinstance__get__)rc   rv   rw   unboundclsr(   r(   r)   _perhaps_rebind_fixture_func   s    

r   c                    s4   j  t tjttd fdd}|_ d S )Nrj   ri   r?   c                    sb   t |jj}|f i t|| |fdd}d d fdd} | }|| |S )Nc                     s      I d H } | S r<   )	__anext__resgen_objr(   r)   setup  s    zH_wrap_asyncgen_fixture.<locals>._asyncgen_fixture_wrapper.<locals>.setupr,   c                     s"   ddfdd}   |   dS )zYield again, to finalize.Nr   c                     s>   z   I d H  W n ty$   Y n0 d} | d7 } t| d S )Nz$Async generator fixture didn't stop.zYield only once.)r   StopAsyncIteration
ValueError)msgr   r(   r)   async_finalizer  s    ze_wrap_asyncgen_fixture.<locals>._asyncgen_fixture_wrapper.<locals>.finalizer.<locals>.async_finalizer)run_until_complete)r   rj   r   r(   r)   	finalizer  s    
zL_wrap_asyncgen_fixture.<locals>._asyncgen_fixture_wrapper.<locals>.finalizer)r   rv   rw   ru   r   Zaddfinalizer)rj   ri   r?   rc   r   r   resultr>   rg   r   r)   _asyncgen_fixture_wrapper  s    

z9_wrap_asyncgen_fixture.<locals>._asyncgen_fixture_wrapperrc   rD   rE   r-   AbstractEventLoop
SubRequestr   )rg   r   r(   r   r)   rn     s
    rn   c                    s4   j  t tjttd fdd}|_ d S )Nr   c                    s.   t jj fdd} | S )Nc                     s"   f i t  I d H } | S r<   )ru   r   rj   rc   r?   ri   r(   r)   r   =  s    zB_wrap_async_fixture.<locals>._async_fixture_wrapper.<locals>.setup)r   rv   rw   r   )rj   ri   r?   r   r   r   r)   _async_fixture_wrapper5  s
    
z3_wrap_async_fixture.<locals>._async_fixture_wrapperr   )rg   r   r(   r   r)   ro   2  s
    ro   _HOLDER)	collectorr:   rG   r,   c                 C   s   |  |sdS t| jt dS )z,A pytest hook to collect asyncio coroutines.N)Zfuncnamefilterrh   rT   r   )r   r:   rG   r(   r(   r)   pytest_pycollect_makeitemI  s    
r   )r   rT   itemsr,   c                 C   sf   t |tjkrdS dd |D }|D ]<}|j}t|tr>|j}t|sVt|r$t	|r$|
d q$dS )z
    Marks collected async test items as `asyncio` tests.

    The mark is only applied in `AUTO` mode. It is applied to:

      - coroutines
      - staticmethods wrapping coroutines
      - Hypothesis tests wrapping coroutines

    Nc                 s   s   | ]}t |tr|V  qd S r<   )r{   r   ).0itemr(   r(   r)   	<genexpr>e      z0pytest_collection_modifyitems.<locals>.<genexpr>r-   )rV   r    r&   rG   r{   staticmethodrH   rP   _is_hypothesis_test _hypothesis_test_wraps_coroutineZ
add_marker)r   rT   r   Zfunction_itemsZfunction_itemr   r(   r(   r)   pytest_collection_modifyitemsV  s    
r   )r   r,   c                 C   s   t | jjS r<   )rP   
hypothesis
inner_testr   r(   r(   r)   r   s  s    r   )Ztrylast)rg   ri   r,   c                 C   s\   | j dkrXt }z| }W n ty4   d}Y n0 |durF|  | }|| dS )zCalled after fixture teardownrj   N)argnamer-   get_event_loop_policyget_event_loopRuntimeErrorclosenew_event_loopset_event_loop)rg   ri   policyloopZnew_loopr(   r(   r)   pytest_fixture_post_finalizerw  s    

r   )hookwrapperc                 c   s   | j dkrdV }| }t }zPt $ tdt | }W d   n1 sT0    Y  ||urn|	  W n t
y   Y n0 || dS dV  dS )z<Adjust the event loop policy when an event loop is produced.rj   Nignore)r   Z
get_resultr-   r   rZ   catch_warningssimplefilterr\   r   r   r   r   )rg   ri   outcomer   r   Zold_loopr(   r(   r)   pytest_fixture_setup  s    

&
r   )r^   r   )
pyfuncitemr,   c                 c   sj   |  d}|dur`| j}ttj|d }t| jrNt| | jjj	|d| jj_	nt| | j|d| _dV  dS )z
    Pytest hook called before a test case is run.

    Wraps marked tests in a synchronous function
    where the wrapped test coroutine is executed in an event loop.
    r-   Nrj   )_loop)
get_closest_markerfuncargsr   r-   r   r   rG   wrap_in_syncr   r   )r   markerr   r   r(   r(   r)   pytest_pyfunc_call  s     

r   c                 C   s   t | ddS )Nis_hypothesis_testFrJ   r   r(   r(   r)   r     s    r   )r   rc   r   c                    s<   t dd}|dur|t fdd}|_|S )zZReturn a sync wrapper around an async function executing it in the
    current event loop._raw_test_funcNc                     s   | i |}t |s4td d d S tj| d}z | W n, ty|   |	 rv|
 sv|   Y n0 d S )Nz	The test z is marked with '@pytest.mark.asyncio' but it is not an async function. Please remove asyncio marker. If the test is not marked explicitly, check for global markers applied via 'pytestmark'.)r   )rQ   isawaitabler[   rC   ZPytestWarningr-   Zensure_futurer   BaseExceptiondoneZ	cancelled	exception)argsr?   corotaskr   rc   r   r(   r)   rA     s    

	zwrap_in_sync.<locals>.inner)rK   rD   rE   r   )r   rc   r   Zraw_funcrA   r(   r   r)   r     s    r   )r   r,   c                 C   sp   |  d}|d u rd S | j}d|v r.|d |dd t| dd }t|ddslt|ddrltd|   d S )	Nr-   rj   r   rG   r   Fr   zftest function `%r` is using Hypothesis, but pytest-asyncio only works with Hypothesis 3.64.0 or later.)r   fixturenamesremoveinsertrK   rC   Zfail)r   r   r   rG   r(   r(   r)   pytest_runtest_setup  s     

r   zpytest.FixtureRequest)ri   r,   c                 c   s   t   }|V  |  dS )z@Create an instance of the default event loop for each test case.N)r-   r   r   r   )ri   r   r(   r(   r)   rj     s    rj   )socket_typer,   c                 C   sL   t tj| d&}|d | d W  d   S 1 s>0    Y  dS )z<Find an unused localhost port from 1024-65535 and return it.)rx   )z	127.0.0.1r      N)
contextlibclosingsocketbindgetsockname)r   sockr(   r(   r)   _unused_port  s    
r   r   c                   C   s
   t tjS r<   )r   r   SOCK_STREAMr(   r(   r(   r)   unused_tcp_port  s    r   c                   C   s
   t tjS r<   )r   r   
SOCK_DGRAMr(   r(   r(   r)   unused_udp_port  s    r   r   )r6   c                     s   t    fdd} | S )z9A factory function, producing different unused TCP ports.c                     s,   t tj} |  v rt tj} q
 |  | S zReturn an unused port.)r   r   r   rf   portZproducedr(   r)   factory  s
    

z(unused_tcp_port_factory.<locals>.factorysetr   r(   r   r)   unused_tcp_port_factory  s    r   c                     s   t    fdd} | S )z9A factory function, producing different unused UDP ports.c                     s,   t tj} |  v rt tj} q
 |  | S r   )r   r   r   rf   r   r   r(   r)   r   /  s
    

z(unused_udp_port_factory.<locals>.factoryr   r   r(   r   r)   unused_udp_port_factory*  s    r   ).)N)Z__doc__r-   r   enumrD   rQ   r   sysrZ   typingr   r   r   r   r   r   r   r	   r
   r   r   r   r   r   rC   r   r   r   version_infor   typing_extensionsr   Z
_ScopeNamer   r   r   rF   ZFixtureFunctionMarkerZConfigZPytestPluginManagerZ
FixtureDefParserr   strEnumr    r3   r5   objectboolfloatintr>   rM   rB   rP   rS   rV   r]   Zhookimplr`   rh   rd   re   r   ru   r   rn   ro   r   r   __annotations__ModuleClassZ	Collectorr   r   r   r   r   r   r   r   r   rj   r   r   r   r   r   r(   r(   r(   r)   <module>   s  @

 
 





&



,

