
    h#                    V   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 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d0dZ G d de      Z  edd !      Z!er		 	 	 	 d1d"Z"nejD                  Z"d2d#Z#d3d$Z$d4d%Z%d4d&Z&d'd(	 	 	 	 	 	 	 	 	 d5d)Z' G d* d+      Z(d6d,Z)eserejT                  Z+y G d- d.      Z+y)7    )annotationsN)Context)Any)	Awaitable)Callable)	Coroutine)Optional)TYPE_CHECKING)TypeVar)Union   )memoized_property   )exc)py311)Literal)Protocol)Self)	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      l/var/www/html/retail-simulation-api/retail/lib/python3.12/site-packages/sqlalchemy/util/_concurrency_py3k.py__init__zgreenlet.__init__(   s    c    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    
%%I		r%   r   c                      y r   r   r   r%   r#   
getcurrentr4   0   s    #r%   )r4   )r   r   c                |    t        | t               xs* t        | t        j                  t        j                  f      S r   )
isinstance	ExceptionasyncioTimeoutErrorCancelledError)es    r#   is_exit_exceptionr<   =   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__M   s0    $F+$//DO r%   Nr-   )r/   r0   r1   r2   r$   r   r%   r#   r>   r>   J   s    
J0r%   r>   _T_coT)	covariantc                     y r   r   	awaitables    r#   iscoroutinerF   X   s    03r%   c                <    t        |       r| j                          y y r   )rF   closerD   s    r#   _safe_cancel_awaitablerI   `   s     9 r%   c                 6    t               } t        | t              S r   )r4   r6   r>   )currents    r#   in_greenletrL   g   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?)r4   r6   r>   rI   r   MissingGreenletr"   r,   )rE   rK   s     r#   
await_onlyrO   l   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?)r4   r6   r>   get_event_loop
is_runningrI   r   rN   run_until_completer"   r,   )rE   rK   loops      r#   await_fallbackrU      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>   r4   r,   r   BaseExceptionr(   sysexc_infor"   r   AwaitRequired)r!   rV   argskwargscontextswitch_occurredresultr+   s           r#   greenlet_spawnra      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   )r8   Lockr    s    r#   mutexzAsyncAdaptedLock.mutex   s     ||~r%   c                H    t        | j                  j                               S r   )rU   rg   acquirerf   s    r#   	__enter__zAsyncAdaptedLock.__enter__   s     djj00233r%   c                8    | j                   j                          y r   )rg   release)r    r'   kws      r#   __exit__zAsyncAdaptedLock.__exit__   s    

r%   N)r.   zasyncio.Lockr.   r   )r'   r   rm   r   r.   None)r/   r0   r1   r   rg   rj   rn   r   r%   r#   rc   rc      s     
4
r%   rc   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.

    )r8   get_running_loopRuntimeErrorget_event_loop_policyrQ   r   r%   r#   rQ   rQ      sB    ''))  ((*99;;s    	""c                  T    e Zd ZU dZded<   ddZddZddZddZddZ	dd	Z
dd
Zy)_Runnerz#Runner implementation for test onlyz6Union[None, asyncio.AbstractEventLoop, Literal[False]]_loopc                    d | _         y r   )rw   rf   s    r#   r$   z_Runner.__init__   s	    DJr%   c                &    | j                          | S r   )
_lazy_initrf   s    r#   rj   z_Runner.__enter__  s    OOKr%   c                $    | j                          y r   )rH   )r    exc_typeexc_valexc_tbs       r#   rn   z_Runner.__exit__  s    JJLr%   c                   | j                   rV	 | j                   j                  | j                   j                                | j                   j                          d| _         y y # | j                   j                          d| _         w xY w)NF)rw   rS   shutdown_asyncgensrH   rf   s    r#   rH   z_Runner.close	  si    zz'JJ11

557 JJ$$&!&DJ  JJ$$&!&DJs   3A$ $#Bc                V    | j                          | j                  sJ | j                  S )zReturn embedded event loop.)rz   rw   rf   s    r#   get_loopz_Runner.get_loop  s"    OO:::::r%   c                t    | j                          | j                  sJ | j                  j                  |      S r   )rz   rw   rS   )r    coros     r#   runz_Runner.run  s-    OO:::::0066r%   c                    | j                   du rt        d      | j                   t        j                         | _         y y )NFzRunner is closed)rw   rs   r8   new_event_looprf   s    r#   rz   z_Runner._lazy_init  s9    zzU""#566zz!$335
 "r%   N)r.   rp   )r.   r   )r|   r   r}   r   r~   r   r.   rp   r.   zasyncio.AbstractEventLoop)r   zCoroutine[Any, Any, _T]r.   r   )r/   r0   r1   __doc__r2   r$   rj   rn   rH   r   r   rz   r   r%   r#   rv   rv      s.    1EE				'		7
	6r%   rv   )r.   r   )r;   rX   r.   r   )rE   zAwaitable[_T_co]r.   z%TypeGuard[Coroutine[Any, Any, _T_co]])rE   zAwaitable[Any]r.   rp   ro   )rE   zAwaitable[_T]r.   r   )
r!   zCallable[..., _T]r\   r   rV   r   r]   r   r.   r   r   ),
__future__r   r8   contextvarsr   rY   typingr   r   r   r   r	   r
   r   r   langhelpersr    r   utilr   util.typingr   r   r   r   r   r   r4   hasattrr@   r<   r>   rA   rF   rI   rL   rO   rU   ra   rc   rQ   Runnerrv   r   r%   r#   <module>r      s9   #   
           *   ! "  #T]	
8 
 & $! *,50x 0 	4(4#4	.4
 %%K1
,4,F !/// / 	/
 /d  < nnG(6 (6r%   