U
    ÌZjÆ  ã                   @   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e ¡ d
ƒZdd„ ZG dd„ dejƒZeedœdd„Zeedœdd„Zddœeedœ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Ú
gr_contextc                 C   s   t | tƒ pt | tjtjfƒS ©N)Ú
isinstanceÚ	ExceptionÚasyncioÚTimeoutErrorZCancelledError)Úe© r   úd/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/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r"|j| _d S r   )ÚgreenletÚ__init__ÚdriverÚ_has_gr_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 )zÓAwaits 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   Úcurrentr   r   r   Ú
await_only/   s    

ÿ	r$   c                 C   s@   t  ¡ }t|tƒs4tƒ }| ¡ r*t d¡‚| | ¡S |j	 
| ¡S )z×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.

    z˜greenlet_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#   Úloopr   r   r   Úawait_fallbackG   s    

ÿ
r)   F)Ú_require_await)r   r   c                Ï   sˆ   t | t ¡ ƒ}d}zX|j||Ž}|jshd}z|I dH }W n" tk
rZ   |jt 	¡ Ž }Y q X | |¡}q W 5 |`X |r„|s„t
 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.
    FTNz¶The 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   r"   ZdeadÚBaseExceptionÚthrowÚsysÚexc_infor	   ZAwaitRequired)r   r*   ÚargsÚkwargsÚcontextZswitch_occurredÚresultÚvaluer   r   r   Úgreenlet_spawn_   s"    ÿr4   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)   r8   Úacquirer7   r   r   r   Ú	__enter__”   s    zAsyncAdaptedLock.__enter__c                 O   s   | j  ¡  d S r   )r8   Úrelease)r   ÚargÚkwr   r   r   Ú__exit__š   s    zAsyncAdaptedLock.__exit__N)r   r   r   r   r8   r:   r>   r   r   r   r   r5   �   s   
r5   c                 O   s&   t ƒ }| ¡ rtdƒ‚| | ||Ž¡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/   r0   r(   r   r   r   Ú"_util_async_run_coroutine_functionž   s    ÿrA   c                 O   sD   t ƒ }| ¡ s$| t| f|ž|Ž¡S tt ¡ tƒs6t‚| ||ŽS dS )r?   N)	r%   r&   r'   r4   r   r   r    r   ÚAssertionErrorr@   r   r   r   Ú_util_async_runª   s
    rC   c                   C   s@   t jr4z
t ¡ W S  tk
r0   t ¡  ¡  Y S X nt ¡ S dS )z}vendor asyncio.get_event_loop() for python 3.7 and above.

    Python 3.10 deprecates get_event_loop() as a standalone.

    N)r   Zpy37r   Zget_running_loopÚRuntimeErrorZget_event_loop_policyr%   r   r   r   r   r%   ¶   s    
r%   )r   r-   Útypingr   r   r   r   Ú r   Zlanghelpersr   r	   Úhasattrr    r   r   r   r$   r)   r4   r5   rA   rC   r%   r   r   r   r   Ú<module>   s,   ÿþ.