Format: application/x-python-code
⬇ Download File (4.8 KB)
   1  o
   2  
   3  ��h��@s�dZddlZddlZddlZddlmZdadd�ZGdd�d�Ze�Z	Gdd	�d	�Z
   4  d
   5  d�Zdd
   6  �Zdd�Z
   7  dedefdd�ZdS)a�
   8  Async helper function that are invalid syntax on Python 3.5 and below.
   9  
  10  This code is best effort, and may have edge cases not behaving as expected. In
  11  particular it contain a number of heuristics to detect whether code is
  12  effectively async and need to run in an event loop or not.
  13  
  14  Some constructs (like top-level `return`, or `yield`) are taken care of
  15  explicitly to actually raise a SyntaxError and stay as close as possible to
  16  Python semantics.
  17  �N)�wrapscCs:zt��WStyYnwtdust��rt��atS)aZasyncio has deprecated get_event_loop
  18  
  19      Replicate it here, with our desired semantics:
  20  
  21      - always returns a valid, not-closed loop
  22      - not thread-local like asyncio's,
  23        because we only want one loop for IPython
  24      - if called from inside a coroutine (e.g. in ipykernel),
  25        return the running loop
  26  
  27      .. versionadded:: 8.0
  28      N)�asyncio�get_running_loop�RuntimeError�_asyncio_event_loop�	is_closed�new_event_loop�r	r	�Pc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/core/async_helpers.py�get_asyncio_loops
  29  �	rc@seZdZdd�Zdd�ZdS)�_AsyncIORunnercCst��|�S)z/
  30          Handler for asyncio autoawait
  31          )r�run_until_complete)�self�coror	r	r
  32  �__call__3sz_AsyncIORunner.__call__cCsdS)Nrr	�rr	r	r
  33  �__str__9sz_AsyncIORunner.__str__N)�__name__�
  34  __module__�__qualname__rrr	r	r	r
  35  r2src@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
  36  S)�
  37  _AsyncIOProxyz^Proxy-object for an asyncio
  38  
  39      Any coroutine methods will be wrapped in event_loop.run_
  40      cCs||_||_dS�N)�_obj�_event_loop)r�objZ
  41  event_loopr	r	r
  42  �__init__Fs
  43  z_AsyncIOProxy.__init__cCsd|j�d�S)Nz<_AsyncIOProxy(z)>)rrr	r	r
  44  �__repr__Jsz_AsyncIOProxy.__repr__cs4t�j|��t���rt����fdd��}|S�S)Ncs"t��|i|���j�}t�|�Sr)r�run_coroutine_threadsafer�wrap_future)�args�kwargsZconcurrent_future��attrrr	r
  45  �_wrappedRs�
  46  z+_AsyncIOProxy.__getattr__.<locals>._wrapped)�getattrr�inspect�iscoroutinefunctionr)r�keyr#r	r!r
  47  �__getattr__Ms
  48  z_AsyncIOProxy.__getattr__cCs
  49  t|j�Sr)�dirrrr	r	r
  50  �__dir__]s
  51  z_AsyncIOProxy.__dir__N)rrr�__doc__rrr(r*r	r	r	r
  52  r@srcCsddl}|�|�S)z%
  53      handler for curio autoawait
  54      rN)�curio�run)�	coroutiner,r	r	r
  55  �
  56  _curio_runneras
  57  r/cCsddl}dd�}|�||�S)Nrc�s�|IdHS)z�
  58          We need the dummy no-op async def to protect from
  59          trio's internal. See https://github.com/python-trio/trio/issues/89
  60          Nr	)rr	r	r
  61  �locms�
  62  z_trio_runner.<locals>.loc)�trior-)Zasync_fnr1r0r	r	r
  63  �_trio_runnerjsr2c
  64  CsLz|�d�Wnty}z	|jWYd}~Sd}~wwtdj|jd���)z�
  65      A runner that does not really allow async execution, and just advance the coroutine.
  66  
  67      See discussion in https://github.com/python-trio/trio/issues/608,
  68  
  69      Credit to Nathaniel Smith
  70      Nz%{coro_name!r} needs a real async loop)�	coro_name)�send�
  71  StopIteration�valuer�formatr)r�excr	r	r
  72  �_pseudo_sync_runnerws���r9�cell�returnc	CsFzt|ddttdd�d�}tj|j@tjkWSttfy"YdSw)a�Detect if a block of code need to be wrapped in an `async def`
  73  
  74      Attempt to parse the block of code, it it compile we're fine.
  75      Otherwise we  wrap if and try to compile.
  76  
  77      If it works, assume it should be async. Otherwise Return False.
  78  
  79      Not handled yet: If the block of code has a return statement as the top
  80      level, it will be seen as async. This is a know limitation.
  81      z<>�exec�PyCF_ALLOW_TOP_LEVEL_AWAITr)�flagsF)�compiler$�astr%�CO_COROUTINE�co_flags�SyntaxError�MemoryError)r:�coder	r	r
  82  �_should_be_async�s��rF)r+r@rr%�	functoolsrrrrZ_asyncio_runnerrr/r2r9�str�boolrFr	r	r	r
  83  �<module>s!	
  84