
    1P@i!                        d dl mZ d dlZd dlmZ 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m
Z
 d dlmZ d d	lmZ d d
lmZ ddlmZ ddlmZ ddlmZ ddlmZ  ed      Zej                  r G d de      Zd+dZnd dlmZ d dlmZ  e e       d      Zd,dZ G d de      Z edd      Zer		 	 	 	 d-dZnej:                  Zd.dZd/d Zd0d!Z d0d"Z!d#d$	 	 	 	 	 	 	 	 	 d1d%Z" G d& d'      Z#	 	 	 	 	 	 	 	 d2d(Z$	 	 	 	 	 	 	 	 d2d)Z%d3d*Z&y)4    )annotationsN)Context)Any)	Awaitable)Callable)	Coroutine)Optional)TYPE_CHECKING)TypeVar   )memoized_property   )exc)Protocol)	TypeGuard_Tc                  :    e Zd ZU ded<   ded<   d	dZd
dZddZy)greenletbooldeadzOptional[Context]
gr_contextc                     y N selffndrivers      k/home/www/lebenam.kofcorporation.com/venv/lib/python3.12/site-packages/sqlalchemy/util/_concurrency_py3k.py__init__zgreenlet.__init__$   s        c                     y r   r   )r   args     r   throwzgreenlet.throw'       r!   c                     y r   r   )r   values     r   switchzgreenlet.switch*   r%   r!   Nr   zCallable[..., Any]r   r   )r#   r   returnr   )r'   r   r*   r   )__name__
__module____qualname____annotations__r    r$   r(   r   r!   r   r   r       s    
%%			r!   r   c                      y r   r   r   r!   r   
getcurrentr0   -   s    r!   )r0   )r   r   c                |    t        | t               xs* t        | t        j                  t        j                  f      S r   )
isinstance	ExceptionasyncioTimeoutErrorCancelledError)es    r   is_exit_exceptionr8   ;   s8     !Y'' :	G  '"8"89, r!   c                       e Zd ZU ded<   ddZy)_AsyncIoGreenletr   r   c                p    t        j                  | ||       || _        t        r|j                  | _        y y r   )r   r    r   _has_gr_contextr   r   s      r   r    z_AsyncIoGreenlet.__init__K   s0    $F+$//DO r!   Nr)   )r+   r,   r-   r.   r    r   r!   r   r:   r:   H   s    
J0r!   r:   _T_coT)	covariantc                     y r   r   	awaitables    r   iscoroutinerB   V   s     	r!   c                <    t        |       r| j                          y y r   )rB   closer@   s    r   _safe_cancel_awaitablerE   _   s     9 r!   c                 6    t               } t        | t              S r   )r0   r2   r:   )currents    r   in_greenletrH   f   s    lGg/00r!   c                    t               }t        |t              s t        |        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?)r0   r2   r:   rE   r   MissingGreenletr   r(   )rA   rG   s     r   
await_onlyrK   k   sM     lGg/0y)!!=
 	
 >>  ++r!   c                   t               }t        |t              sKt               }|j	                         r t        |        t        j                  d      |j                  |       S |j                  j                  |       S )a  Awaits 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.

    .. deprecated:: 2.0.24 The ``await_fallback()`` function will be removed
       in SQLAlchemy 2.1.  Use :func:`_util.await_only` instead, running the
       function / program / etc. within a top-level greenlet that is set up
       using :func:`_util.greenlet_spawn`.

    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?)r0   r2   r:   get_event_loop
is_runningrE   r   rJ   run_until_completer   r(   )rA   rG   loops      r   await_fallbackrQ      sp      lGg/0??"9-%%; 
 &&y11>>  ++r!   F)_require_awaitc               v  K   t        | t                     }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:   r0   r(   r   BaseExceptionr$   sysexc_infor   r   AwaitRequired)r   rR   argskwargscontextswitch_occurredresultr'   s           r   greenlet_spawnr]      s     " r:<0G
 O00,,"O
/ % !. ,, NoG
 	

 M# %  8 '7	8 NsQ   B9 B2 B  B B B2 #B9 B *B/,B2 .B//B2 2B66B9c                  .    e Zd Zedd       ZddZddZy)AsyncAdaptedLockc                *    t        j                         S r   )r4   Lockr   s    r   mutexzAsyncAdaptedLock.mutex   s     ||~r!   c                H    t        | j                  j                               S r   )rQ   rc   acquirerb   s    r   	__enter__zAsyncAdaptedLock.__enter__   s     djj00233r!   c                8    | j                   j                          y r   )rc   release)r   r#   kws      r   __exit__zAsyncAdaptedLock.__exit__   s    

r!   N)r*   zasyncio.Lockr*   r   )r#   r   ri   r   r*   None)r+   r,   r-   r   rc   rf   rj   r   r!   r   r_   r_      s     
4
r!   r_   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)rM   rN   r3   rO   r   rX   rY   rP   s       r   "_util_async_run_coroutine_functionrp      sE    
 D0
 	
 ""2t#6v#677r!   c                    t               }|j                         s|j                  t        | g|i |      S t	        t               t              sJ  | |i |S )rn   )rM   rN   rO   r]   r2   r0   r:   ro   s       r   _util_async_runrr      s[    
 D??&&~b'J4'J6'JKK *,(89994"6""r!   c                     	 t        j                         S # t        $ r Y nw xY wt        j                         j	                         S )z}vendor asyncio.get_event_loop() for python 3.7 and above.

    Python 3.10 deprecates get_event_loop() as a standalone.

    )r4   get_running_loopRuntimeErrorget_event_loop_policyrM   r   r!   r   rM   rM     sB    ''))  ((*99;;s    	"")r*   r   )r7   rT   r*   r   )rA   zAwaitable[_T_co]r*   z%TypeGuard[Coroutine[Any, Any, _T_co]])rA   zAwaitable[Any]r*   rl   rk   )rA   zAwaitable[_T]r*   r   )
r   zCallable[..., _T]rX   r   rR   r   rY   r   r*   r   )r   z'Callable[..., Coroutine[Any, Any, Any]]rX   r   rY   r   r*   r   )r*   zasyncio.AbstractEventLoop)'
__future__r   r4   contextvarsr   rU   typingr   r   r   r   r	   r
   r   langhelpersr    r   util.typingr   r   r   r   r0   hasattrr<   r8   r:   r=   rB   rE   rH   rK   rQ   r]   r_   rp   rr   rM   r   r!   r   <module>r~      s]   #   
          *  " #T]	8  $! *,50x 0 	4(#	. %%K1
,4,F !/// / 	/
 /d  8/88;8GJ88#/#8;#GJ##<r!   