ó
¨œž[c           @` sD  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l m Z d d l m Z m Z d d l m Z m Z m Z y d d l m Z Wn e k
 rÙ d Z n Xy d d l Z Wn e k
 rd Z n Xy d d l Z Wn e k
 r-d Z n Xe j ƒ  d k oLe	 j d k Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ  d e f d „  ƒ  YZ! e d k	 r©e j! Z! n  e d k r¾e! Z" n e j! e! f Z" d „  Z# d e f d „  ƒ  YZ$ e$ ƒ  Z% d „  Z& e ƒ  Z' d „  Z( e) d „ Z* d „  Z+ d „  Z, d „  Z- d „  Z. d S(   si  Utilities for working with ``Future`` objects.

``Futures`` are a pattern for concurrent programming introduced in
Python 3.2 in the `concurrent.futures` package, and also adopted (in a
slightly different form) in Python 3.4's `asyncio` package. This
package defines a ``Future`` class that is an alias for `asyncio.Future`
when available, and a compatible implementation for older versions of
Python. It also includes some utility functions for interacting with
``Future`` objects.

While this package is an important part of Tornado's internal
implementation, applications rarely need to interact with it
directly.
i    (   t   absolute_importt   divisiont   print_functionN(   t   app_log(   t   ExceptionStackContextt   wrap(   t   raise_exc_infot   ArgReplacert   is_finalizing(   t   futurest   CPythoni   i   t   ReturnValueIgnoredErrorc           B` s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyR   @   s   t   _TracebackLoggerc           B` s;   e  Z d  Z d Z d „  Z d „  Z d „  Z e d „ Z RS(   s
  Helper to log a traceback upon destruction if not cleared.

    This solves a nasty problem with Futures and Tasks that have an
    exception set: if nobody asks for the exception, the exception is
    never logged.  This violates the Zen of Python: 'Errors should
    never pass silently.  Unless explicitly silenced.'

    However, we don't want to log the exception as soon as
    set_exception() is called: if the calling code is written
    properly, it will get the exception and handle it properly.  But
    we *do* want to log it if result() or exception() was never called
    -- otherwise developers waste a lot of time wondering why their
    buggy code fails silently.

    An earlier attempt added a __del__() method to the Future class
    itself, but this backfired because the presence of __del__()
    prevents garbage collection from breaking cycles.  A way out of
    this catch-22 is to avoid having a __del__() method on the Future
    class itself, but instead to have a reference to a helper object
    with a __del__() method that logs the traceback, where we ensure
    that the helper object doesn't participate in cycles, and only the
    Future has a reference to it.

    The helper object is added when set_exception() is called.  When
    the Future is collected, and the helper is present, the helper
    object is also collected, and its __del__() method will log the
    traceback.  When the Future's result() or exception() method is
    called (and a helper object is present), it removes the the helper
    object, after calling its clear() method to prevent it from
    logging.

    One downside is that we do a fair amount of work to extract the
    traceback from the exception, even when it is never logged.  It
    would seem cheaper to just store the exception object, but that
    references the traceback, which references stack frames, which may
    reference the Future, which references the _TracebackLogger, and
    then the _TracebackLogger would be included in a cycle, which is
    what we're trying to avoid!  As an optimization, we don't
    immediately format the exception; we only do the work when
    activate() is called, which call is delayed until after all the
    Future's callbacks have run.  Since usually a Future has at least
    one callback (typically set by 'yield From') and usually that
    callback extracts the callback, thereby removing the need to
    format the exception.

    PS. I don't claim credit for this solution.  I first heard of it
    in a discussion about closing files when they are collected.
    t   exc_infot   formatted_tbc         C` s   | |  _  d  |  _ d  S(   N(   R   t   NoneR   (   t   selfR   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   __init__{   s    	c         C` s7   |  j  } | d  k	 r3 d  |  _  t j | Œ  |  _ n  d  S(   N(   R   R   t	   tracebackt   format_exceptionR   (   R   R   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   activate   s    		c         C` s   d  |  _ d  |  _ d  S(   N(   R   R   R   (   R   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   clear…   s    	c         C` s<   | ƒ  r8 |  j  r8 t j d d j |  j  ƒ j ƒ  ƒ n  d  S(   Ns(   Future exception was never retrieved: %st    (   R   R   t   errort   joint   rstrip(   R   R   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   __del__‰   s    	(   R   R   (	   R   R   t   __doc__t	   __slots__R   R   R   R   R   (    (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyR   G   s   0			t   Futurec           B` sÜ   e  Z d  Z d „  Z e j d k r8 e j d ƒ d Un	 d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d d „ Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e rÚ e d „ Z n  RS(   s¼  Placeholder for an asynchronous result.

    A ``Future`` encapsulates the result of an asynchronous
    operation.  In synchronous applications ``Futures`` are used
    to wait for the result from a thread or process pool; in
    Tornado they are normally used with `.IOLoop.add_future` or by
    yielding them in a `.gen.coroutine`.

    `tornado.concurrent.Future` is an alias for `asyncio.Future` when
    that package is available (Python 3.4+). Unlike
    `concurrent.futures.Future`, the ``Futures`` used by Tornado and
    `asyncio` are not thread-safe (and therefore faster for use with
    single-threaded event loops).

    In addition to ``exception`` and ``set_exception``, Tornado's
    ``Future`` implementation supports storing an ``exc_info`` triple
    to support better tracebacks on Python 2. To set an ``exc_info``
    triple, use `future_set_exc_info`, and to retrieve one, call
    `result()` (which will raise it).

    .. versionchanged:: 4.0
       `tornado.concurrent.Future` is always a thread-unsafe ``Future``
       with support for the ``exc_info`` methods.  Previously it would
       be an alias for the thread-safe `concurrent.futures.Future`
       if that package was available and fall back to the thread-unsafe
       implementation if it was not.

    .. versionchanged:: 4.1
       If a `.Future` contains an error but that error is never observed
       (by calling ``result()``, ``exception()``, or ``exc_info()``),
       a stack trace will be logged when the `.Future` is garbage collected.
       This normally indicates an error in the application, but in cases
       where it results in undesired logging it may be necessary to
       suppress the logging by ensuring that the exception is observed:
       ``f.add_done_callback(lambda f: f.exception())``.

    .. versionchanged:: 5.0

       This class was previoiusly available under the name
       ``TracebackFuture``. This name, which was deprecated since
       version 4.0, has been removed. When `asyncio` is available
       ``tornado.concurrent.Future`` is now an alias for
       `asyncio.Future`. Like `asyncio.Future`, callbacks are now
       always scheduled on the `.IOLoop` and are never run
       synchronously.

    c         C` s:   t  |  _ d  |  _ d  |  _ t  |  _ d  |  _ g  |  _ d  S(   N(   t   Falset   _doneR   t   _resultt	   _exc_infot   _log_tracebackt
   _tb_loggert
   _callbacks(   R   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyR   ¿   s    					i   sF   
        def __await__(self):
            return (yield self)
        Nc         c` s&   |  V} t  ƒ  } | f | _ | ‚ d  S(   N(   t   StopIterationt   args(   R   t   resultt   e(    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt	   __await__Ò   s    	c         C` s   t  S(   s’   Cancel the operation, if possible.

        Tornado ``Futures`` do not support cancellation, so this method always
        returns False.
        (   R    (   R   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   cancelÚ   s    c         C` s   t  S(   s¡   Returns True if the operation has been cancelled.

        Tornado ``Futures`` do not support cancellation, so this method
        always returns False.
        (   R    (   R   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt	   cancelledâ   s    c         C` s   |  j  S(   s4   Returns True if this operation is currently running.(   R!   (   R   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   runningê   s    c         C` s   |  j  S(   s0   Returns True if the future has finished running.(   R!   (   R   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   doneî   s    c         C` s5   t  |  _ |  j d  k	 r1 |  j j ƒ  d  |  _ n  d  S(   N(   R    R$   R%   R   R   (   R   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   _clear_tb_logò   s    	c         C` s^   |  j  ƒ  |  j d k	 r  |  j S|  j d k	 rM z t |  j ƒ Wd d }  Xn  |  j ƒ  |  j S(   s8  If the operation succeeded, return its result.  If it failed,
        re-raise its exception.

        This method takes a ``timeout`` argument for compatibility with
        `concurrent.futures.Future` but it is an error to call it
        before the `Future` is done, so the ``timeout`` is never used.
        N(   R0   R"   R   R#   R   t   _check_done(   R   t   timeout(    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyR)   ø   s    


c         C` s6   |  j  ƒ  |  j d k	 r$ |  j d S|  j ƒ  d Sd S(   s@  If the operation raised an exception, return the `Exception`
        object.  Otherwise returns None.

        This method takes a ``timeout`` argument for compatibility with
        `concurrent.futures.Future` but it is an error to call it
        before the `Future` is done, so the ``timeout`` is never used.
        i   N(   R0   R#   R   R1   (   R   R2   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt	   exception  s
    

c         C` sF   |  j  r2 d d l m } | j ƒ  j | |  ƒ n |  j j | ƒ d S(   s.  Attaches the given callback to the `Future`.

        It will be invoked with the `Future` as its argument when the Future
        has finished running and its result is available.  In Tornado
        consider using `.IOLoop.add_future` instead of calling
        `add_done_callback` directly.
        i    (   t   IOLoopN(   R!   t   tornado.ioloopR4   t   currentt   add_callbackR&   t   append(   R   t   fnR4   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   add_done_callback  s    	c         C` s   | |  _  |  j ƒ  d S(   s   Sets the result of a ``Future``.

        It is undefined to call any of the ``set`` methods more than once
        on the same object.
        N(   R"   t	   _set_done(   R   R)   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt
   set_result(  s    	c         C` s)   |  j  | j | t | d d ƒ f ƒ d S(   s#   Sets the exception of a ``Future.``t   __traceback__N(   t   set_exc_infot	   __class__t   getattrR   (   R   R3   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   set_exception1  s    c         C` s   |  j  ƒ  |  j S(   se   Returns a tuple in the same format as `sys.exc_info` or None.

        .. versionadded:: 4.0
        (   R0   R#   (   R   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyR   8  s    
c         C` sq   | |  _  t |  _ t s* t | ƒ |  _ n  z |  j ƒ  Wd |  j rc |  j d k	 rc |  j j ƒ  n  X| |  _  d S(   s‚   Sets the exception information of a ``Future.``

        Preserves tracebacks on Python 2.

        .. versionadded:: 4.0
        N(	   R#   t   TrueR$   t   _GC_CYCLE_FINALIZERSR   R%   R;   R   R   (   R   R   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyR>   @  s    		c         C` s   |  j  s t d ƒ ‚ n  d  S(   Ns1   DummyFuture does not support blocking for results(   R!   t	   Exception(   R   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyR1   U  s    	c         C` sb   t  |  _ |  j r^ d d l m } | j ƒ  } x! |  j D] } | j | |  ƒ q8 Wd  |  _ n  d  S(   Ni    (   R4   (   RB   R!   R&   R5   R4   R6   R7   R   (   R   R4   t   loopt   cb(    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyR;   Y  s    		c         C` sO   | ƒ  s |  j  r d  St j |  j Œ  } t j d |  d j | ƒ j ƒ  ƒ d  S(   Ns+   Future %r exception was never retrieved: %sR   (   R$   R   R   R#   R   R   R   R   (   R   R   t   tb(    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyR   f  s
    	(   i   i   (   R   R   R   R   t   syst   version_infot   textwrapt   dedentR+   R,   R-   R.   R/   R0   R   R)   R3   R:   R<   RA   R   R>   R1   R;   RC   R   R   (    (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyR      s,   /															c         C` s   t  |  t ƒ S(   N(   t
   isinstancet   FUTURES(   t   x(    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt	   is_future{  s    t   DummyExecutorc           B` s   e  Z d  „  Z e d „ Z RS(   c         O` sN   t  ƒ  } y t | | | | Ž  ƒ Wn$ t k
 rI t | t j ƒ  ƒ n X| S(   N(   R   t"   future_set_result_unless_cancelledRD   t   future_set_exc_infoRH   R   (   R   R9   R(   t   kwargst   future(    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   submit€  s    	c         C` s   d  S(   N(    (   R   t   wait(    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   shutdownˆ  s    (   R   R   RU   RB   RW   (    (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyRP     s   	c          ` sx   ‡  f d †  } |  r* ˆ  r* t  d ƒ ‚ n  t |  ƒ d k rJ | |  d ƒ St |  ƒ d k rt t  d t |  ƒ ƒ ‚ n  | S(   sI  Decorator to run a synchronous method asynchronously on an executor.

    The decorated method may be called with a ``callback`` keyword
    argument and 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.
    c         ` s7   ˆ j  d d ƒ ‰  t j ˆ ƒ ‡  ‡ f d †  ƒ } | S(   Nt   executorc         ` s•   | j  d d  ƒ ‰  t ƒ  } t |  ˆ ƒ j ˆ |  | | Ž } t | | ƒ ˆ  r‘ t j d t ƒ d d l	 m
 } | j ƒ  j | ‡  f d †  ƒ n  | S(   Nt   callbacksB   callback arguments are deprecated, use the returned Future insteadi    (   R4   c         ` s   ˆ  |  j  ƒ  ƒ S(   N(   R)   (   RT   (   RY   (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   <lambda>Â  s    (   t   popR   R   R@   RU   t   chain_futuret   warningst   warnt   DeprecationWarningR5   R4   R6   t
   add_future(   R   R(   RS   t   async_futuret   conc_futureR4   (   RX   R9   (   RY   s1   lib/python2.7/site-packages/tornado/concurrent.pyt   wrapper·  s    	!	(   t   gett	   functoolst   wraps(   R9   Rc   (   RS   (   RX   R9   s1   lib/python2.7/site-packages/tornado/concurrent.pyt   run_on_executor_decorator´  s    !s*   cannot combine positional and keyword argsi   i    s   expected 1 argument, got %d(   t
   ValueErrort   len(   R(   RS   Rg   (    (   RS   s1   lib/python2.7/site-packages/tornado/concurrent.pyt   run_on_executor  s    %c         C` s    t  j d t ƒ t |  d t ƒS(   sô  Decorator to make a function that returns via callback return a
    `Future`.

    This decorator was provided to ease the transition from
    callback-oriented code to coroutines. It is not recommended for
    new code.

    The wrapped function should take a ``callback`` keyword argument
    and invoke it with one argument when it has finished.  To signal failure,
    the function can simply raise an exception (which will be
    captured by the `.StackContext` and passed along to the ``Future``).

    From the caller's perspective, the callback argument is optional.
    If one is given, it will be invoked when the function is complete
    with ``Future.result()`` as an argument.  If the function fails, the
    callback will not be run and an exception will be raised into the
    surrounding `.StackContext`.

    If no callback is given, the caller should use the ``Future`` to
    wait for the function to complete (perhaps by yielding it in a
    coroutine, or passing it to `.IOLoop.add_future`).

    Usage:

    .. testcode::

        @return_future
        def future_func(arg1, arg2, callback):
            # Do stuff (possibly asynchronous)
            callback(result)

        async def caller():
            await future_func(arg1, arg2)

    ..

    Note that ``@return_future`` and ``@gen.engine`` can be applied to the
    same function, provided ``@return_future`` appears first.  However,
    consider using ``@gen.coroutine`` instead of this combination.

    .. versionchanged:: 5.1

       Now raises a `.DeprecationWarning` if a callback argument is passed to
       the decorated function and deprecation warnings are enabled.

    .. deprecated:: 5.1

       This decorator will be removed in Tornado 6.0. New code should
       use coroutines directly instead of wrapping callback-based code
       with this decorator. Interactions with non-Tornado
       callback-based code should be managed explicitly to avoid
       relying on the `.ExceptionStackContext` built into this
       decorator.
    s4   @return_future is deprecated, use coroutines insteadR^   (   R]   R^   R_   t   _non_deprecated_return_futureRB   (   t   f(    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   return_futureÑ  s    7	c         ` s7   t  ˆ  d ƒ ‰ t j ˆ  ƒ ‡  ‡ ‡ f d †  ƒ } | S(   NRY   c          ` s  t  ƒ  ‰ ˆ j t ‡ f d † |  | ƒ \ ‰  }  } ‡ f d †  } d  } t | d t ƒ} | ^ ˆ ss t | _ n  y. ˆ |  | Ž  } | d  k	 r  t d ƒ ‚ n  Wn t	 j
 ƒ  } ‚  n XWd  QX| d  k	 rÙ ˆ j ƒ  n  ˆ  d  k	 rt j d t ƒ ‡  f d †  } t ˆ t | ƒ ƒ n  ˆ S(   Nc         ` s   t  ˆ  |  ƒ S(   N(   RQ   (   t   value(   RT   (    s1   lib/python2.7/site-packages/tornado/concurrent.pyRZ     s    c         ` s   t  ˆ  |  | | f ƒ t S(   N(   RR   RB   (   t   typRn   RG   (   RT   (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   handle_error  s    t   delay_warningsC   @return_future should not be used with functions that return valuessB   callback arguments are deprecated, use the returned Future insteadc         ` s6   |  j  ƒ  } | t k r" ˆ  ƒ  n ˆ  |  j  ƒ  ƒ d  S(   N(   R)   t
   _NO_RESULT(   RT   R)   (   RY   (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   run_callback@  s    
(   R   t   replaceRr   R   R   RB   R    Rq   R   RH   R   R)   R]   R^   R_   t   future_add_done_callbackR   (   R(   RS   Rp   R   t   escR)   Rs   (   Rl   t   replacerR^   (   RY   RT   s1   lib/python2.7/site-packages/tornado/concurrent.pyRc     s4    		(   R   Re   Rf   (   Rl   R^   Rc   (    (   Rl   Rw   R^   s1   lib/python2.7/site-packages/tornado/concurrent.pyRk     s    $5c         ` s[   ‡  ‡ f d †  } t  ˆ  t ƒ r1 t ˆ  | ƒ n& d d l m } | j ƒ  j ˆ  | ƒ d S(   sj  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`.

    c         ` s˜   |  ˆ  k s t  ‚ ˆ j ƒ  r" d  St ˆ  d ƒ rY ˆ  j ƒ  d  k	 rY t ˆ ˆ  j ƒ  ƒ n; ˆ  j ƒ  d  k	 r ˆ j ˆ  j ƒ  ƒ n ˆ j ˆ  j	 ƒ  ƒ d  S(   NR   (
   t   AssertionErrorR/   t   hasattrR   R   RR   R3   RA   R<   R)   (   RT   (   t   at   b(    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   copyW  s    i    (   R4   N(   RL   R   Ru   R5   R4   R6   R`   (   Rz   R{   R|   R4   (    (   Rz   R{   s1   lib/python2.7/site-packages/tornado/concurrent.pyR\   K  s
    c         C` s    |  j  ƒ  s |  j | ƒ n  d S(   sÈ   Set 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(   R-   R<   (   RT   Rn   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyRQ   j  s    c         C` s4   t  |  d ƒ r |  j | ƒ n |  j | d ƒ d S(   sÄ   Set the given ``exc_info`` as the `Future`'s exception.

    Understands both `asyncio.Future` and Tornado's extensions to
    enable better tracebacks on Python 2.

    .. versionadded:: 5.0
    R>   i   N(   Ry   R>   RA   (   RT   R   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyRR   v  s    c         C` s*   |  j  ƒ  r | |  ƒ n |  j | ƒ d S(   sL  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(   R/   R:   (   RT   RY   (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyRu   †  s    (   i   i   (/   R   t
   __future__R    R   R   Re   t   platformRJ   R   RH   R]   t   tornado.logR   t   tornado.stack_contextR   R   t   tornado.utilR   R   R   t
   concurrentR	   t   ImportErrorR   t   asynciot   typingt   python_implementationRI   RC   RD   R   t   objectR   R   RM   RO   RP   t   dummy_executorRj   Rr   Rm   R    Rk   R\   RQ   RR   Ru   (    (    (    s1   lib/python2.7/site-packages/tornado/concurrent.pyt   <module>   sV   


Hã				?		<>			