
    +h                         d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlZddlmZ ddl	m
Z
 dd	lmZ  e ej                         d
      Zd Z G d dej                        ZdedefdZdedefdZdddedefdZ G d d      Zd Zd Zd Zy)    N)Any)Callable)	Coroutine   )compat)memoized_property   )exc
gr_contextc                 |    t        | t               xs* t        | t        j                  t        j                  f      S N)
isinstance	ExceptionasyncioTimeoutErrorCancelledError)es    T/var/www/html/venv/lib/python3.12/site-packages/sqlalchemy/util/_concurrency_py3k.pyis_exit_exceptionr      s8     !Y'' :	G  '"8"89,     c                       e Zd Zd Zy)_AsyncIoGreenletc                     t         j                   j                  | ||       || _        t        r|j                  | _        y y r   )greenlet__init__driver_has_gr_contextr   )selffnr   s      r   r   z_AsyncIoGreenlet.__init__(   s6    ""4V4$//DO r   N)__name__
__module____qualname__r    r   r   r   r   '   s    0r   r   	awaitablereturnc                     t        j                         }t        |t              st	        j
                  d      |j                  j                  |       S )zAwaits an async function in a sync method.

    The sync method must be inside a :func:`greenlet_spawn` context.
    :func:`await_only` calls cannot be nested.

    :param awaitable: The coroutine to call.

    zjgreenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place?)r   
getcurrentr   r   r
   MissingGreenletr   switch)r$   currents     r   
await_onlyr+   /   sL     !!#Gg/0!!=
 	
 >>  ++r   c                     t        j                         }t        |t              s@t	               }|j                         rt        j                  d      |j                  |       S |j                  j                  |       S )zAwaits an async function in a sync method.

    The sync method must be inside a :func:`greenlet_spawn` context.
    :func:`await_fallback` calls cannot be nested.

    :param awaitable: The coroutine to call.

    zgreenlet_spawn has not been called and asyncio event loop is already running; can't call await_fallback() here. Was IO attempted in an unexpected place?)r   r'   r   r   get_event_loop
is_runningr
   r(   run_until_completer   r)   )r$   r*   loops      r   await_fallbackr1   G   so     !!#Gg/0??%%; 
 &&y11>>  ++r   F)_require_awaitr   c                  K   t        | t        j                               }d}	  |j                  |i |}|j                  s+d}	 | d{   }|j                  |      }|j                  s+|`	|r|st        j                  d      |S 7 ?# t
        $ r$  |j                  t        j                          }Y Xw xY w# |`	w xY ww)aG  Runs a sync function ``fn`` in a new greenlet.

    The sync function can then use :func:`await_only` to wait for async
    functions.

    :param fn: The sync callable to call.
    :param \*args: Positional arguments to pass to the ``fn`` callable.
    :param \*\*kwargs: Keyword arguments to pass to the ``fn`` callable.
    FTNzThe current operation required an async execution but none was detected. This will usually happen when using a non compatible DBAPI driver. Please ensure that an async DBAPI is used.)r   r   r'   r)   deadBaseExceptionthrowsysexc_infor   r
   AwaitRequired)r   r2   argskwargscontextswitch_occurredresultvalues           r   greenlet_spawnr@   _   s      r8#6#6#89G
 O00,,"O
/ % !. ,, NoG
 	

 M# %  8 '7	8 NsR   !C B< B 
B
B B< -C
B *B96B< 8B99B< <C  Cc                   (    e Zd Zed        Zd Zd Zy)AsyncAdaptedLockc                 *    t        j                         S r   )r   Lockr   s    r   mutexzAsyncAdaptedLock.mutex   s     ||~r   c                 L    t        | j                  j                                | S r   )r1   rF   acquirerE   s    r   	__enter__zAsyncAdaptedLock.__enter__   s     	tzz))+,r   c                 8    | j                   j                          y r   )rF   release)r   argkws      r   __exit__zAsyncAdaptedLock.__exit__   s    

r   N)r    r!   r"   r   rF   rI   rN   r#   r   r   rB   rB      s     
r   rB   c                 z    t               }|j                         rt        d      |j                   | |i |      S )for test suite/ util onlyz]for async run coroutine we expect that no greenlet or event loop is running when we start out)r-   r.   r   r/   r   r:   r;   r0   s       r   "_util_async_run_coroutine_functionrR      sE     D0
 	
 ""2t#6v#677r   c                     t               }|j                         s|j                  t        | g|i |      S t	        t        j                         t              sJ  | |i |S )rP   )r-   r.   r/   r@   r   r   r'   r   rQ   s       r   _util_async_runrT      sb     D??&&~b'J4'J6'JKK (--/1ABBB4"6""r   c                      t         j                  r	 t        j                         S t        j                         S # t        $ r% t        j
                         j                         cY S w xY w)z}vendor asyncio.get_event_loop() for python 3.7 and above.

    Python 3.10 deprecates get_event_loop() as a standalone.

    )r   py37r   get_running_loopRuntimeErrorget_event_loop_policyr-   r#   r   r   r-   r-      s\     {{	D++-- %%''  	D002AACC	Ds   : +A('A()r   r7   typingr   r   r   r    r   langhelpersr   r
   hasattrr'   r   r   r   r+   r1   r@   rB   rR   rT   r-   r#   r   r   <module>r^      s     
      * 
 -(--/>0x(( 0,) , ,0,i ,C ,2 ).+++\ "	8	#(r   