
    gs                      H   d Z ddlZddlmZ ddlZddlZddlZddlmZ ddl	Z	ddl	m
Z
mZmZmZmZ  e	j                  d      Z G d de      Zej&                  Zej&                  efZd	e
d
efdZ G d dej.                        Z e       Zde
de
d
efdZ e       Zd!dZddded
dfdZddded
dfdZ dddeee!   ee   eejD                     f   d
dfdZ#e	jH                  dddedgdf   d
dfd       Z%e	jH                  dddedgdf   d
dfd       Z%ddded   d
dfd Z%y)"a  Utilities for working with ``Future`` objects.

Tornado previously provided its own ``Future`` class, but now uses
`asyncio.Future`. This module contains utility functions for working
with `asyncio.Future` in a way that is backwards-compatible with
Tornado's old ``Future`` implementation.

While this module is an important part of Tornado's internal
implementation, applications rarely need to interact with it
directly.

    N)futures)app_log)AnyCallableOptionalTupleUnion_Tc                       e Zd Zy)ReturnValueIgnoredErrorN)__name__
__module____qualname__     G/var/www/openai/venv/lib/python3.12/site-packages/tornado/concurrent.pyr   r   *   s    r   r   xreturnc                 "    t        | t              S N)
isinstanceFUTURES)r   s    r   	is_futurer   4   s    a!!r   c                   v    e Zd Zdedef   dededdfdZej                  dk\  rdd	e	d
e	ddfdZ
ydd	e	ddfdZ
y)DummyExecutorfn.argskwargsr   futures.Future[_T]c                     t        j                         }	 t        | ||i |       |S # t        $ r" t	        |t        j                                Y |S w xY wr   )r   Future"future_set_result_unless_cancelled	Exceptionfuture_set_exc_infosysexc_info)selfr   r   r   futures        r   submitzDummyExecutor.submit9   sV     !	8.vr47J67JK   	87	8s   * 'AA)   	   waitcancel_futuresNc                      y r   r   )r'   r,   r-   s      r   shutdownzDummyExecutor.shutdownE       r   c                      y r   r   )r'   r,   s     r   r/   zDummyExecutor.shutdownJ   r0   r   )TF)T)r   r   r   r   r
   r   r)   r%   version_infoboolr/   r   r   r   r   r   8   sm    37#,/;>	 6!	 	d 	t 	
	 	 	r   r   r   r   c                      dt         dt         dt        f   ffd}| rrt        d      t        |       dk(  r || d         S t        |       dk7  rt        dt        |             |S )	aE  Decorator to run a synchronous method asynchronously on an executor.

    Returns a future.

    The executor to be used is determined by the ``executor``
    attributes of ``self``. To use a different attribute name, pass a
    keyword argument to the decorator::

        @run_on_executor(executor='_thread_pool')
        def foo(self):
            pass

    This decorator should not be confused with the similarly-named
    `.IOLoop.run_in_executor`. In general, using ``run_in_executor``
    when *calling* a blocking method is recommended instead of using
    this decorator when *defining* a method. If compatibility with older
    versions of Tornado is required, consider defining an executor
    and using ``executor.submit()`` at the call site.

    .. versionchanged:: 4.2
       Added keyword arguments to use alternative attributes.

    .. versionchanged:: 5.0
       Always uses the current IOLoop instead of ``self.io_loop``.

    .. versionchanged:: 5.1
       Returns a `.Future` compatible with ``await`` instead of a
       `concurrent.futures.Future`.

    .. deprecated:: 5.1

       The ``callback`` argument is deprecated and will be removed in
       6.0. The decorator itself is discouraged in new code but will
       not be removed in 6.0.

    .. versionchanged:: 6.0

       The ``callback`` argument was removed.
    r   r   .c           	           j                  dd      t        j                         dt        dt        dt        dt        f fd       }|S )Nexecutorr'   r   r   r   c                 v    t               } t        |       j                  | g|i |}t        ||       |S r   )r!   getattrr)   chain_future)r'   r   r   async_futureconc_futurer6   r   s        r   wrapperzCrun_on_executor.<locals>.run_on_executor_decorator.<locals>.wrapper   s?    !8L8'$188TSDSFSKl3r   )get	functoolswrapsr   r!   )r   r<   r6   r   s   ` @r   run_on_executor_decoratorz2run_on_executor.<locals>.run_on_executor_decorator|   sQ    ::j*5			 # 	 c 	 S 	 V 	  
	  r   z*cannot combine positional and keyword args   r   zexpected 1 argument, got %d)r   r!   
ValueErrorlen)r   r   r@   s    ` r   run_on_executorrD   Q   sp    V
h 
8CK3H 
 EFF
4yA~(a11	Ta6D	BB$$r   
Future[_T]c                     dfd}t        | t              rt        | |       yddlm} |j                         j                  | |       y)aj  Chain two futures together so that when one completes, so does the other.

    The result (success or failure) of ``a`` will be copied to ``b``, unless
    ``b`` has already been completed or cancelled by the time ``a`` finishes.

    .. versionchanged:: 5.0

       Now accepts both Tornado/asyncio `Future` objects and
       `concurrent.futures.Future`.

    Nc                    j                         ry t        | d      r+| j                         t        | j                                y | j	                         }|j                  |       y j                  | j                                y )Nr&   )donehasattrr&   r$   	exceptionset_exception
set_resultresult)aa_excbs     r   copyzchain_future.<locals>.copy   sc    6681j!ajjl&>1::<0KKME &QXXZ(r   r   )IOLoop)rN   rE   r   N)r   r!   future_add_done_callbacktornado.iolooprR   current
add_future)rN   rP   rQ   rR   s    `  r   r9   r9      s:    
) !V D) 	*##At,r   r(   z%Union[futures.Future[_T], Future[_T]]valuec                 H    | j                         s| j                  |       yy)zSet the given ``value`` as the `Future`'s result, if not cancelled.

    Avoids ``asyncio.InvalidStateError`` when calling ``set_result()`` on
    a cancelled `asyncio.Future`.

    .. versionadded:: 5.0
    N)	cancelledrL   )r(   rW   s     r   r"   r"      s#     %  r   excc                 v    | j                         s| j                  |       yt        j                  d|       y)a  Set the given ``exc`` as the `Future`'s exception.

    If the Future is already canceled, logs the exception instead. If
    this logging is not desired, the caller should explicitly check
    the state of the Future and call ``Future.set_exception`` instead of
    this wrapper.

    Avoids ``asyncio.InvalidStateError`` when calling ``set_exception()`` on
    a cancelled `asyncio.Future`.

    .. versionadded:: 6.0

    z$Exception after Future was cancelled)r&   N)rY   rK   r   error)r(   rZ   s     r   %future_set_exception_unless_cancelledr]      s-      S!<sKr   r&   c                 B    |d   t        d      t        | |d          y)a  Set the given ``exc_info`` as the `Future`'s exception.

    Understands both `asyncio.Future` and the extensions in older
    versions of Tornado to enable better tracebacks on Python 2.

    .. versionadded:: 5.0

    .. versionchanged:: 6.0

       If the future is already cancelled, this function is a no-op.
       (previously ``asyncio.InvalidStateError`` would be raised)

    rA   Nz,future_set_exc_info called with no exception)r#   r]   )r(   r&   s     r   r$   r$      s(    & {FGG)&(1+>r   r   callbackc                      y r   r   r(   r_   s     r   rS   rS           	r   c                      y r   r   ra   s     r   rS   rS      rb   r   ).Nc                 X    | j                         r	 ||        y| j                  |       y)aL  Arrange to call ``callback`` when ``future`` is complete.

    ``callback`` is invoked with one argument, the ``future``.

    If ``future`` is already done, ``callback`` is invoked immediately.
    This may differ from the behavior of ``Future.add_done_callback``,
    which makes no such guarantee.

    .. versionadded:: 5.0
    N)rH   add_done_callbackra   s     r   rS   rS      s#     {{}  *r   )rN   rE   rP   rE   r   N)&__doc__asyncio
concurrentr   r>   r%   typestornado.logr   typingr   r   r   r   r	   TypeVarr
   r#   r   r!   r   r3   r   Executorr   dummy_executorrD   object
_NO_RESULTr9   r"   BaseExceptionr]   typeTracebackTyper$   overloadrS   r   r   r   <module>ru      s      
    8 8V^^D	i 	
 
>>6
"" " "G$$ , =%3 =%# =%( =%@ X
-D!3!<>!	!L3L:GL	L,?3?/%:M:M1NN?
 
?0 	 	,46J5KT5Q,R			 	 		$,l^T-A$B			 	+3+?G	?R+	+r   