
    oi                         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     b/home/ubuntu/docker-apps/notebooks/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__3   s      !44T::r   c                      y)Nr   r   r   s    r   __str__z_AsyncIORunner.__str__9   s    r   N)__name__
__module____qualname__r   r   r   r   r   r   r   2   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__F   s    	%r   c                 "    d| j                   dS )Nz<_AsyncIOProxy(z)>)r    r   s    r   __repr__z_AsyncIOProxy.__repr__J   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>._wrappedR   s=    $+$D$D$)&)4+;+;%! **+<==r   )getattrr    inspectiscoroutinefunctionr   )r   keyr/   r.   s   `  @r   __getattr__z_AsyncIOProxy.__getattr__M   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   @   s    
&1 r   r   c                 ,    ddl }|j                  |       S )z%
    handler for curio autoawait
    r   N)curiorun)	coroutiner:   s     r   _curio_runnerr=   a   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>.locm   s     
 zzs   )trior;   )async_fnrA   r@   s      r   _trio_runnerrC   j   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   w   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        t        f$ r Y yw xY w)zDetect if a block of code needs to be wrapped in an `async def`

    If the code block has a top-level return statement or is otherwise
    invalid, `False` will be returned.
    z<>execPyCF_ALLOW_TOP_LEVEL_AWAITr   )flagsF)	compiler0   astr1   CO_COROUTINEco_flagsSyntaxError
ValueErrorMemoryError)rL   codes     r   _should_be_asyncrZ      s`    $gc3OQT&U
 ##dmm3w7K7KKK[1 s   AA A%$A%)r8   rS   r   r1   	functoolsr   r   r   r   _asyncio_runnerr   r=   rC   rK   strboolrZ   r   r   r   <module>r_      sd   
     :  !" B 
#
&3 4 r   