o
    =a                  	   @   s"  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jrOzd d
lmZ eejd W n eefyN   dZY nw dZdd ZG dd dejZdedefddZdedefddZdddedefddZG dd dZdd Zdd  Zd!d" ZdS )#    N)Any)Callable)	Coroutine   )compat)memoized_property   )exc)copy_context
gr_contextc                 C   s   t | t pt | tjtjfS N)
isinstance	ExceptionasyncioTimeoutErrorCancelledError)e r   W/var/www/html/gps/gps/lib/python3.10/site-packages/sqlalchemy/util/_concurrency_py3k.pyis_exit_exception   s   r   c                   @   s   e Zd Zdd ZdS )_AsyncIoGreenletc                 C   s.   t j | || || _td urt | _d S d S r   )greenlet__init__driver_copy_contextr   )selffnr   r   r   r   r   )   s
   z_AsyncIoGreenlet.__init__N)__name__
__module____qualname__r   r   r   r   r   r   (   s    r   	awaitablereturnc                 C   s(   t  }t|tstd|j| S )Awaits an async function in a sync method.

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

    :param awaitable: The coroutine to call.

    zfgreenlet_spawn has not been called; can't call await_() here. Was IO attempted in an unexpected place?)r   
getcurrentr   r   r	   MissingGreenletr   switch)r    currentr   r   r   
await_only0   s   

	r'   c                 C   s@   t  }t|tst }| rtd|| S |j	
| S )r"   zgreenlet_spawn has not been called and asyncio event loop is already running; can't call await_() here. Was IO attempted in an unexpected place?)r   r#   r   r   get_event_loop
is_runningr	   r$   run_until_completer   r%   )r    r&   loopr   r   r   await_fallbackH   s   


r,   F)_require_awaitr   c                   s   t | t }d}z1|j|i |}|js8d}z|I dH }W n ty/   |jt  }Y nw ||}|jrW |`	n|`	w |rH|sHt
d|S )aC  Runs a sync function ``fn`` in a new greenlet.

    The sync function can then use :func:`await_` 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   r-   argskwargscontextswitch_occurredresultvaluer   r   r   greenlet_spawn`   s*   
r:   c                   @   s(   e Zd Zedd Zdd Zdd ZdS )AsyncAdaptedLockc                 C   s   t  S r   )r   Lockr   r   r   r   mutex   s   zAsyncAdaptedLock.mutexc                 C   s   t | j  | S r   )r,   r>   acquirer=   r   r   r   	__enter__   s   zAsyncAdaptedLock.__enter__c                 O   s   | j   d S r   )r>   release)r   argkwr   r   r   __exit__   s   zAsyncAdaptedLock.__exit__N)r   r   r   r   r>   r@   rD   r   r   r   r   r;      s
    
r;   c                 O   s*   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)r(   r)   r   r*   r   r4   r5   r+   r   r   r   "_util_async_run_coroutine_function   s   rG   c                 O   sJ   t  }| s|t| g|R i |S tt tsJ | |i |S )rE   )r(   r)   r*   r:   r   r   r#   r   rF   r   r   r   _util_async_run   s
   rH   c                   C   s8   t jrzt W S  ty   t   Y S w t S )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   
r(   )r   r1   typingr   r   r   r    r   langhelpersr   r	   rI   contextvarsr
   r   getattrImportErrorAttributeErrorr   r   r'   r,   r:   r;   rG   rH   r(   r   r   r   r   <module>   s>    
.