
    e!                       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dAd)ZdBd+Z dBd,Z!d-d.dCd5Z" G d6 d7          Z#dDd9Z$dDd:Z%dEd<Z&dS )F    )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dS )greenletbooldeadzOptional[Context]
gr_contextfnCallable[..., Any]driverc                    d S N selfr   r   s      Alib/python3.11/site-packages/sqlalchemy/util/_concurrency_py3k.py__init__zgreenlet.__init__$   s    C    argr   returnc                    d S r   r   )r   r#   s     r    throwzgreenlet.throw'       4r"   valuec                    d S r   r   )r   r(   s     r    switchzgreenlet.switch*   r'   r"   Nr   r   r   r   )r#   r   r$   r   )r(   r   r$   r   )__name__
__module____qualname____annotations__r!   r&   r*   r   r"   r    r   r       sg         


%%%%	 	 	 		 	 	 		 	 	 	 	 	r"   r   r$   c                     d S r   r   r   r"   r    
getcurrentr1   -   s    r"   )r1   )r   r   eBaseExceptionr   c                z    t          | t                     p%t          | t          j        t          j        f          S r   )
isinstance	ExceptionasyncioTimeoutErrorCancelledError)r2   s    r    is_exit_exceptionr:   ;   s:     !Y''' :	G '"89, , r"   c                  "    e Zd ZU ded<   d	dZdS )
_AsyncIoGreenletr   r   r   r   r   r   c                j    t          j        | ||           || _        t          r|j        | _        d S d S r   )r   r!   r   _has_gr_contextr   r   s      r    r!   z_AsyncIoGreenlet.__init__K   s>    $F+++ 	0$/DOOO	0 	0r"   Nr+   )r,   r-   r.   r/   r!   r   r"   r    r<   r<   H   s3         JJJ0 0 0 0 0 0r"   r<   _T_coT)	covariant	awaitableAwaitable[_T_co]%TypeGuard[Coroutine[Any, Any, _T_co]]c                    d S r   r   rA   s    r    iscoroutinerF   V   s	     	r"   Awaitable[Any]Nonec                P    t          |           r|                                  d S d S r   )rF   closerE   s    r    _safe_cancel_awaitablerK   _   s4     9  r"   c                 H    t                      } t          | t                    S r   )r1   r5   r<   )currents    r    in_greenletrN   f   s    llGg/000r"   Awaitable[_T]c                    t                      }t          |t                    s#t          |            t	          j        d          |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?)r1   r5   r<   rK   r   MissingGreenletr   r*   )rA   rM   s     r    
await_onlyrR   k   s`     llGg/00 
y)))!=
 
 	
 >  +++r"   c                0   t                      }t          |t                    sZt                      }|                                r#t          |            t          j        d          |                    |           S |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?)r1   r5   r<   get_event_loop
is_runningrK   r   rQ   run_until_completer   r*   )rA   rM   loops      r    await_fallbackrX      s      llGg/00 
2?? 	"9---%;  
 &&y111>  +++r"   F)_require_awaitr   Callable[..., _T]argsr   rY   kwargsc               Z  K   t          | t                                }d}	  |j        |i |}|j        sSd}	 | d{V }|                    |          }n+# t          $ r  |j        t          j                     }Y nw xY w|j        S|`n# |`w xY w|r|st          j
        d          |S )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<   r1   r*   r   r3   r&   sysexc_infor   r   AwaitRequired)r   rY   r[   r\   contextswitch_occurredresultr(   s           r    greenlet_spawnrd      s     " r:<<00G
 O000, 	/"O
/ % !.. ! 8 8 8 '7	8 , 	/ NNGN 
o 
G
 
 	

 Ms.   B A B %A?<B >A??
B Bc                  6    e Zd Zedd            ZddZdd
ZdS )AsyncAdaptedLockr$   asyncio.Lockc                (    t          j                    S r   )r7   Lockr   s    r    mutexzAsyncAdaptedLock.mutex   s     |~~r"   r   c                N    t          | j                                                  S r   )rX   rk   acquirerj   s    r    	__enter__zAsyncAdaptedLock.__enter__   s      dj0022333r"   r#   r   kwrH   c                8    | j                                          d S r   )rk   release)r   r#   ro   s      r    __exit__zAsyncAdaptedLock.__exit__   s    
r"   N)r$   rg   r$   r   )r#   r   ro   r   r$   rH   )r,   r-   r.   r   rk   rn   rr   r   r"   r    rf   rf      s[           
4 4 4 4
     r"   rf   'Callable[..., Coroutine[Any, Any, Any]]c                    t                      }|                                rt          d          |                     | |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)rT   rU   r6   rV   r   r[   r\   rW   s       r    "_util_async_run_coroutine_functionrx      s]    
 D 
0
 
 	
 ""22t#6v#6#6777r"   c                    t                      }|                                s#|                    t          | g|R i |          S t	          t                      t                    sJ  | |i |S )rv   )rT   rU   rV   rd   r5   r1   r<   rw   s       r    _util_async_runrz      s    
 D?? #&&~b'J4'J'J'J6'J'JKKK *,,(899999r4"6"""r"   asyncio.AbstractEventLoopc                     	 t          j                    S # t          $ r Y nw xY wt          j                                                    S )z}vendor asyncio.get_event_loop() for python 3.7 and above.

    Python 3.10 deprecates get_event_loop() as a standalone.

    )r7   get_running_loopRuntimeErrorget_event_loop_policyrT   r   r"   r    rT   rT     sS    ')))    (**99;;;s    
"")r$   r   )r2   r3   r$   r   )rA   rB   r$   rC   )rA   rG   r$   rH   rs   )rA   rO   r$   r   )
r   rZ   r[   r   rY   r   r\   r   r$   r   )r   rt   r[   r   r\   r   r$   r   )r$   r{   )'
__future__r   r7   contextvarsr   r^   typingr   r   r   r   r	   r
   r   langhelpersr    r   util.typingr   r   r   r   r1   hasattrr>   r:   r<   r?   rF   rK   rN   rR   rX   rd   rf   rx   rz   rT   r   r"   r    <module>r      s   # " " " " "        



                                                  * * * * * *       " " " " " " # # # # # #WT]]	 "    8        $#####!!!!!! '**,,55   0 0 0 0 0x 0 0 0 	4((( &     %K   1 1 1 1
, , , ,4, , , ,F !/ / / / / /d        8 8 8 8# # # #< < < < < <r"   