
    f                         d Z ddlZddlZddlZddlmZ dad Z G d d      Z e       Z	 G d d      Z
d	 Zd
 Zd ZdedefdZy)a  
Async helper function that are invalid syntax on Python 3.5 and below.

This code is best effort, and may have edge cases not behaving as expected. In
particular it contain a number of heuristics to detect whether code is
effectively async and need to run in an event loop or not.

Some constructs (like top-level `return`, or `yield`) are taken care of
explicitly to actually raise a SyntaxError and stay as close as possible to
Python semantics.
    N)wrapsc                      	 t        j                         S # t        $ r Y nw xY wt        t        j	                         rt        j
                         at        S )aZ  asyncio has deprecated get_event_loop

    Replicate it here, with our desired semantics:

    - always returns a valid, not-closed loop
    - not thread-local like asyncio's,
      because we only want one loop for IPython
    - if called from inside a coroutine (e.g. in ipykernel),
      return the running loop

    .. versionadded:: 8.0
    )asyncioget_running_loopRuntimeError_asyncio_event_loop	is_closednew_event_loop     U/var/www/cvtools/html/venv/lib/python3.12/site-packages/IPython/core/async_helpers.pyget_asyncio_loopr      sS    ''))  	 "&9&C&C&E%446s    	""c                       e Zd Zd Zd Zy)_AsyncIORunnerc                 4    t               j                  |      S )z/
        Handler for asyncio autoawait
        )r   run_until_complete)selfcoros     r   __call__z_AsyncIORunner.__call__4   s      !44T::r   c                      y)Nr   r   r   s    r   __str__z_AsyncIORunner.__str__:   s    r   N)__name__
__module____qualname__r   r   r   r   r   r   r   3   s    ;r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)_AsyncIOProxyz^Proxy-object for an asyncio

    Any coroutine methods will be wrapped in event_loop.run_
    c                      || _         || _        y N)_obj_event_loop)r   obj
event_loops      r   __init__z_AsyncIOProxy.__init__G   s    	%r   c                 "    d| j                   dS )Nz<_AsyncIOProxy(z)>)r    r   s    r   __repr__z_AsyncIOProxy.__repr__K   s     R00r   c                      t         j                  |      t        j                        rt	               fd       }|S S )Nc                  z    t        j                   | i |j                        }t        j                  |      S r   )r   run_coroutine_threadsafer!   wrap_future)argskwargsconcurrent_futureattrr   s      r   _wrappedz+_AsyncIOProxy.__getattr__.<locals>._wrappedS   s=    $+$D$D$)&)4+;+;%! **+<==r   )getattrr    inspectiscoroutinefunctionr   )r   keyr/   r.   s   `  @r   __getattr__z_AsyncIOProxy.__getattr__N   sE    tyy#&&&t, 4[> > OKr   c                 ,    t        | j                        S r   )dirr    r   s    r   __dir__z_AsyncIOProxy.__dir__^   s    499~r   N)r   r   r   __doc__r$   r&   r4   r7   r   r   r   r   r   A   s    
&1 r   r   c                 ,    ddl }|j                  |       S )z%
    handler for curio autoawait
    r   N)curiorun)	coroutiner:   s     r   _curio_runnerr=   b   s     99Yr   c                 4    dd l }d }|j                  ||       S )Nr   c                 "   K   |  d{   S 7 w)z
        We need the dummy no-op async def to protect from
        trio's internal. See https://github.com/python-trio/trio/issues/89
        Nr   )r   s    r   locz_trio_runner.<locals>.locn   s     
 zzs   )trior;   )async_fnrA   r@   s      r   _trio_runnerrC   k   s     88C""r   c                     	 | j                  d       t        dj                  | j                              # t        $ r}|j
                  cY d}~S d}~ww xY w)z
    A runner that does not really allow async execution, and just advance the coroutine.

    See discussion in https://github.com/python-trio/trio/issues/608,

    Credit to Nathaniel Smith
    Nz%{coro_name!r} needs a real async loop)	coro_name)sendr   formatr   StopIterationvalue)r   excs     r   _pseudo_sync_runnerrK   x   sR    
		$
 3::T]]:S
 	
	  yys   8 	AAAAcellreturnc           
          	 t        | ddt        t        dd            }t        j                  |j
                  z  t        j                  k(  S # t        t        f$ r Y yw xY w)a  Detect if a block of code need to be wrapped in an `async def`

    Attempt to parse the block of code, it it compile we're fine.
    Otherwise we  wrap if and try to compile.

    If it works, assume it should be async. Otherwise Return False.

    Not handled yet: If the block of code has a return statement as the top
    level, it will be seen as async. This is a know limitation.
    z<>execPyCF_ALLOW_TOP_LEVEL_AWAITr   )flagsF)compiler0   astr1   CO_COROUTINEco_flagsSyntaxErrorMemoryError)rL   codes     r   _should_be_asyncrY      s^    $gc3OQT&U
 ##dmm3w7K7KKK% s   AA A A )r8   rS   r   r1   	functoolsr   r   r   r   _asyncio_runnerr   r=   rC   rK   strboolrY   r   r   r   <module>r^      sd   
     :  !" B 
#
&3 4 r   