
[c           @` s  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 Z d d l Z d d l Z d d l m Z d d l m Z d d l m Z m Z d d l m Z m Z m Z d d l m Z d d	 l m Z y d d
 l m Z Wn e k
 r-e Z n Xy d d l  Z  Wn e k
 rWe Z  n Xe j! e j" f Z# e$ e d  re# e j% f 7Z# n  e j& e j' e j( e j) f Z* e$ e d  re* e j+ e j, e j- f 7Z* n  e j. d k re* e j/ f 7Z* n  e j0 f Z1 e$ e d  r(e1 e j2 f 7Z1 n  e j. j3 d  Z4 d e5 f d     YZ6 d e7 f d     YZ8 d e7 f d     YZ9 d e: f d     YZ; d e: f d     YZ< d e< f d     YZ= d e= f d     YZ> d e< f d     YZ? d    Z@ d S(!   sB  Utility classes to write to and read from non-blocking files and sockets.

Contents:

* `BaseIOStream`: Generic interface for reading and writing.
* `IOStream`: Implementation of BaseIOStream using non-blocking sockets.
* `SSLIOStream`: SSL-aware version of IOStream.
* `PipeIOStream`: Pipe-based IOStream implementation.
i    (   t   absolute_importt   divisiont   print_functionN(   t   Future(   t   ioloop(   t   gen_logt   app_log(   t   ssl_wrap_sockett   _client_ssl_defaultst   _server_ssl_defaults(   t   stack_context(   t   errno_from_exception(   t   _set_nonblockingt   WSAEWOULDBLOCKt   WSAECONNRESETt   darwint   WSAEINPROGRESSt   wint   StreamClosedErrorc           B` s   e  Z d  Z d d  Z RS(   s  Exception raised by `IOStream` methods when the stream is closed.

    Note that the close callback is scheduled to run *after* other
    callbacks on the stream (to allow for buffered data to be processed),
    so you may see this error before you see the close callback.

    The ``real_error`` attribute contains the underlying error that caused
    the stream to close (if any).

    .. versionchanged:: 4.3
       Added the ``real_error`` attribute.
    c         C` s#   t  t |   j d  | |  _ d  S(   Ns   Stream is closed(   t   superR   t   __init__t
   real_error(   t   selfR   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR   f   s    N(   t   __name__t
   __module__t   __doc__t   NoneR   (    (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR   Y   s   t   UnsatisfiableReadErrorc           B` s   e  Z d  Z RS(   s   Exception raised when a read cannot be satisfied.

    Raised by ``read_until`` and ``read_until_regex`` with a ``max_bytes``
    argument.
    (   R   R   R   (    (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR   k   s   t   StreamBufferFullErrorc           B` s   e  Z d  Z RS(   sD   Exception raised by `IOStream` methods when the buffer is full.
    (   R   R   R   (    (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR   t   s   t   _StreamBufferc           B` sA   e  Z d  Z d   Z d   Z d Z d   Z d   Z d   Z RS(   sh   
    A specialized buffer that tries to avoid copies when large pieces
    of data are encountered.
    c         C` s%   t  j   |  _ d |  _ d |  _ d  S(   Ni    (   t   collectionst   dequet   _bufferst
   _first_post   _size(   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR      s    	c         C` s   |  j  S(   N(   R"   (   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   __len__   s    i   c         C` s   t  |  } | |  j k rR t | t  s9 t |  } n  |  j j t | f  n~ | d k r |  j r |  j d \ } } | p t  |  |  j k } n t } | r |  j j t t |  f  q | | 7} n  |  j	 | 7_	 d S(   sX   
        Append the given piece of data (should be a buffer-compatible object).
        i    iN(
   t   lent   _large_buf_thresholdt
   isinstancet
   memoryviewR    t   appendt   Truet   Falset	   bytearrayR"   (   R   t   datat   sizet
   is_memviewt   bt   new_buf(    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR(      s    	c         C` s{   | d k s t   y |  j d \ } } Wn t k
 rC t d  SX|  j } | rb | | | | !St |  | | | !Sd S(   sq   
        Get a view over at most ``size`` bytes (possibly fewer) at the
        current buffer position.
        i    t    N(   t   AssertionErrorR    t
   IndexErrorR'   R!   (   R   R-   R.   R/   t   pos(    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   peek   s    	c         C` s$  d | k  o |  j  k n s% t  |  j  | 8_  |  j } |  j } x | r| d k r| d \ } } t |  | | } | d k r | j   | t |  | 8} d } qI | r | | 7} d } qI | | 7} t |  d | k r | | 4d } n  d } qI W| d k st  | |  _ d S(   sH   
        Advance the current buffer position by ``size`` bytes.
        i    i   N(   R"   R2   R!   R    R$   t   popleft(   R   R-   R4   t   bufferst   is_largeR/   t   b_remain(    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   advance   s*    %		
	
	
	
(	   R   R   R   R   R#   R%   R(   R5   R:   (    (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR   y   s   				t   BaseIOStreamc           B` s  e  Z d  Z d& d& d& d  Z d   Z d   Z d   Z d   Z d   Z	 d& d& d  Z
 d& d& d  Z d& d& e d	  Z d& e d
  Z d& d& d  Z d& d  Z d   Z e d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z  d   Z! d   Z" d   Z# d    Z$ d!   Z% d"   Z& d#   Z' d$   Z( d%   Z) RS('   s]  A utility class to write to and read from a non-blocking file or socket.

    We support a non-blocking ``write()`` and a family of ``read_*()`` methods.
    All of the methods take an optional ``callback`` argument and return a
    `.Future` only if no callback is given.  When the operation completes,
    the callback will be run or the `.Future` will resolve with the data
    read (or ``None`` for ``write()``).  All outstanding ``Futures`` will
    resolve with a `StreamClosedError` when the stream is closed; users
    of the callback interface will be notified via
    `.BaseIOStream.set_close_callback` instead.

    When a stream is closed due to an error, the IOStream's ``error``
    attribute contains the exception object.

    Subclasses must implement `fileno`, `close_fd`, `write_to_fd`,
    `read_from_fd`, and optionally `get_fd_error`.
    c         C` sU  t  j j   |  _ | p d |  _ t | p- d |  j d  |  _ | |  _ d |  _	 t
   |  _ d |  _ d |  _ t |  _ d |  _ t   |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ t |  _ t |  _ d |  _ d |  _ d |  _ d |  _ t j    |  _! d |  _" d |  _# d |  _$ d |  _% t |  _& d |  _' d |  _( t |  _) d S(   s  `BaseIOStream` constructor.

        :arg max_buffer_size: Maximum amount of incoming data to buffer;
            defaults to 100MB.
        :arg read_chunk_size: Amount of data to read at one time from the
            underlying transport; defaults to 64KB.
        :arg max_write_buffer_size: Amount of outgoing data to buffer;
            defaults to unlimited.

        .. versionchanged:: 4.0
           Add the ``max_write_buffer_size`` parameter.  Changed default
           ``read_chunk_size`` to 64KB.
        .. versionchanged:: 5.0
           The ``io_loop`` argument (deprecated since version 4.1) has been
           removed.
        i  @i   i   i    N(*   R   t   IOLoopt   currentt   io_loopt   max_buffer_sizet   mint   read_chunk_sizet   max_write_buffer_sizeR   t   errorR+   t   _read_buffert   _read_buffer_post   _read_buffer_sizeR*   t   _user_read_buffert   _after_user_read_bufferR   t   _write_buffert   _total_write_indext   _total_write_done_indext   _read_delimitert   _read_regext   _read_max_bytest   _read_bytest   _read_partialt   _read_until_closet   _read_callbackt   _read_futuret   _streaming_callbackt   _write_callbackR   R   t   _write_futurest   _close_callbackt   _connect_callbackt   _connect_futuret   _ssl_connect_futuret   _connectingt   _statet   _pending_callbackst   _closed(   R   R?   RA   RB   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR      sB    																									c         C` s   t     d S(   s,   Returns the file descriptor for this stream.N(   t   NotImplementedError(   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   fileno  s    c         C` s   t     d S(   s   Closes the file underlying this stream.

        ``close_fd`` is called by `BaseIOStream` and should not be called
        elsewhere; other users should call `close` instead.
        N(   R_   (   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   close_fd#  s    c         C` s   t     d S(   si   Attempts to write ``data`` to the underlying file.

        Returns the number of bytes written.
        N(   R_   (   R   R,   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   write_to_fd+  s    c         C` s   t     d S(   s  Attempts to read from the underlying file.

        Reads up to ``len(buf)`` bytes, storing them in the buffer.
        Returns the number of bytes read. Returns None if there was
        nothing to read (the socket returned `~errno.EWOULDBLOCK` or
        equivalent), and zero on EOF.

        .. versionchanged:: 5.0

           Interface redesigned to take a buffer and return a number
           of bytes instead of a freshly-allocated object.
        N(   R_   (   R   t   buf(    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   read_from_fd2  s    c         C` s   d S(   s>  Returns information about any error on the underlying file.

        This method is called after the `.IOLoop` has signaled an error on the
        file descriptor, and should return an Exception (such as `socket.error`
        with additional information, or None if no such information is
        available.
        N(   R   (   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   get_fd_errorA  s    c         C` s   |  j  |  } t j |  |  _ | |  _ y |  j   Wn] t k
 ro } t j d |  |  j	 d |  | S| d k	 r | j d    n    n X| S(   s,  Asynchronously read until we have matched the given regex.

        The result includes the data that matches the regex and anything
        that came before it.  If a callback is given, it will be run
        with the data as an argument; if not, this method returns a
        `.Future`.

        If ``max_bytes`` is not None, the connection will be closed
        if more than ``max_bytes`` bytes have been read and the regex is
        not satisfied.

        .. versionchanged:: 4.0
            Added the ``max_bytes`` argument.  The ``callback`` argument is
            now optional and a `.Future` will be returned if it is omitted.

        .. deprecated:: 5.1

           The ``callback`` argument is deprecated and will be removed
           in Tornado 6.0. Use the returned `.Future` instead.

        s*   Unsatisfiable read, closing connection: %st   exc_infoc         S` s
   |  j    S(   N(   t	   exception(   t   f(    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   <lambda>o  s    N(   t   _set_read_callbackt   ret   compileRM   RN   t   _try_inline_readR   R   t   infot   closeR   t   add_done_callback(   R   t   regext   callbackt	   max_bytest   futuret   e(    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   read_until_regexK  s    	c         C` s   |  j  |  } | |  _ | |  _ y |  j   Wn] t k
 rf } t j d |  |  j d |  | S| d k	 r | j	 d    n    n X| S(   s  Asynchronously read until we have found the given delimiter.

        The result includes all the data read including the delimiter.
        If a callback is given, it will be run with the data as an argument;
        if not, this method returns a `.Future`.

        If ``max_bytes`` is not None, the connection will be closed
        if more than ``max_bytes`` bytes have been read and the delimiter
        is not found.

        .. versionchanged:: 4.0
            Added the ``max_bytes`` argument.  The ``callback`` argument is
            now optional and a `.Future` will be returned if it is omitted.

        .. deprecated:: 5.1

           The ``callback`` argument is deprecated and will be removed
           in Tornado 6.0. Use the returned `.Future` instead.
        s*   Unsatisfiable read, closing connection: %sRf   c         S` s
   |  j    S(   N(   Rg   (   Rh   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRi     s    N(
   Rj   RL   RN   Rm   R   R   Rn   Ro   R   Rp   (   R   t	   delimiterRr   Rs   Rt   Ru   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt
   read_untils  s    		c         C` s   |  j  |  } t | t j  s' t  | |  _ | |  _ | d k	 rj t j	 d t
  t j |  |  _ n  y |  j   Wn) | d k	 r | j d    n    n X| S(   s  Asynchronously read a number of bytes.

        If a ``streaming_callback`` is given, it will be called with chunks
        of data as they become available, and the final result will be empty.
        Otherwise, the result is all the data that was read.
        If a callback is given, it will be run with the data as an argument;
        if not, this method returns a `.Future`.

        If ``partial`` is true, the callback is run as soon as we have
        any bytes to return (but never more than ``num_bytes``)

        .. versionchanged:: 4.0
            Added the ``partial`` argument.  The callback argument is now
            optional and a `.Future` will be returned if it is omitted.

        .. deprecated:: 5.1

           The ``callback`` and ``streaming_callback`` arguments are
           deprecated and will be removed in Tornado 6.0. Use the
           returned `.Future` (and ``partial=True`` for
           ``streaming_callback``) instead.

        s5   streaming_callback is deprecated, use partial insteadc         S` s
   |  j    S(   N(   Rg   (   Rh   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRi     s    N(   Rj   R&   t   numberst   IntegralR2   RO   RP   R   t   warningst   warnt   DeprecationWarningR
   t   wrapRT   Rm   Rp   (   R   t	   num_bytesRr   t   streaming_callbackt   partialRt   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt
   read_bytes  s    			c         C` s  |  j  |  } |  j } t |  } | | k rp |  j | } t |  j  |  j | !| (|  j | 4|  j |  _ n) | d k r t |  j  |  j | | *n  t |  _ | |  _ d |  _ | |  _ | |  _	 | |  _
 y |  j   Wn) | d k	 r| j d    n    n X| S(   s  Asynchronously read a number of bytes.

        ``buf`` must be a writable buffer into which data will be read.
        If a callback is given, it will be run with the number of read
        bytes as an argument; if not, this method returns a `.Future`.

        If ``partial`` is true, the callback is run as soon as any bytes
        have been read.  Otherwise, it is run when the ``buf`` has been
        entirely filled with read data.

        .. versionadded:: 5.0

        .. deprecated:: 5.1

           The ``callback`` argument is deprecated and will be removed
           in Tornado 6.0. Use the returned `.Future` instead.

        i    c         S` s
   |  j    S(   N(   Rg   (   Rh   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRi     s    N(   Rj   RF   R$   RE   R'   RD   RH   R)   RG   RO   RP   Rm   R   Rp   (   R   Rc   Rr   R   Rt   t   available_bytest   nt   end(    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt	   read_into  s.    	
						c         C` s   |  j  |  } | d k	 r@ t j d t  t j |  |  _ n  |  j   r |  j d k	 rq |  j	 |  j
 t  n  |  j	 |  j
 t  | St |  _ y |  j   Wn) | d k	 r | j d    n    n X| S(   s  Asynchronously reads all data from the socket until it is closed.

        If a ``streaming_callback`` is given, it will be called with chunks
        of data as they become available, and the final result will be empty.
        Otherwise, the result is all the data that was read.
        If a callback is given, it will be run with the data as an argument;
        if not, this method returns a `.Future`.

        Note that if a ``streaming_callback`` is used, data will be
        read from the socket as quickly as it becomes available; there
        is no way to apply backpressure or cancel the reads. If flow
        control or cancellation are desired, use a loop with
        `read_bytes(partial=True) <.read_bytes>` instead.

        .. versionchanged:: 4.0
            The callback argument is now optional and a `.Future` will
            be returned if it is omitted.

        .. deprecated:: 5.1

           The ``callback`` and ``streaming_callback`` arguments are
           deprecated and will be removed in Tornado 6.0. Use the
           returned `.Future` (and `read_bytes` with ``partial=True``
           for ``streaming_callback``) instead.

        sF   streaming_callback is deprecated, use read_bytes(partial=True) insteadc         S` s
   |  j    S(   N(   Rg   (   Rh   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRi     s    N(   Rj   R   R{   R|   R}   R
   R~   RT   t   closedt   _run_read_callbackRF   R)   R*   RQ   Rm   Rp   (   R   Rr   R   Rt   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   read_until_close  s$    		c         C` s$  |  j    | rx |  j d k	 rP t |  j  t |  |  j k rP t d   n  |  j j |  |  j t |  7_ n  | d k	 r t j	 d t
  t j |  |  _ d } n2 t   } | j d    |  j j |  j | f  |  j s |  j   |  j r|  j |  j j  n  |  j   n  | S(   s  Asynchronously write the given data to this stream.

        If ``callback`` is given, we call it when all of the buffered write
        data has been successfully written to the stream. If there was
        previously buffered write data and an old write callback, that
        callback is simply overwritten with this new callback.

        If no ``callback`` is given, this method returns a `.Future` that
        resolves (with a result of ``None``) when the write has been
        completed.

        The ``data`` argument may be of type `bytes` or `memoryview`.

        .. versionchanged:: 4.0
            Now returns a `.Future` if no callback is given.

        .. versionchanged:: 4.5
            Added support for `memoryview` arguments.

        .. deprecated:: 5.1

           The ``callback`` argument is deprecated and will be removed
           in Tornado 6.0. Use the returned `.Future` instead.

        s!   Reached maximum write buffer sizes<   callback argument is deprecated, use returned Future insteadc         S` s
   |  j    S(   N(   Rg   (   Rh   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRi   H  s    N(   t   _check_closedRB   R   R$   RI   R   R(   RJ   R{   R|   R}   R
   R~   RU   R   Rp   RV   R[   t   _handle_writet   _add_io_stateR>   t   WRITEt   _maybe_add_error_listener(   R   R,   Rr   Rt   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   write   s*    
"				
	c         C` s    t  j |  |  _ |  j   d S(   s  Call the given callback when the stream is closed.

        This mostly is not necessary for applications that use the
        `.Future` interface; all outstanding ``Futures`` will resolve
        with a `StreamClosedError` when the stream is closed. However,
        it is still useful as a way to signal that the stream has been
        closed while no other read or write is in progress.

        Unlike other callback-based interfaces, ``set_close_callback``
        will not be removed in Tornado 6.0.
        N(   R
   R~   RW   R   (   R   Rr   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   set_close_callbackQ  s    c         C` s"  |  j    s| rw t | t  r1 | d |  _ qw t | t  rL | |  _ qw t j   } t |  rw | d |  _ qw n  |  j r |  j	 d k	 r |  j r |  j |  j t  n  t |  _ |  j |  j t  n  |  j d k	 r |  j j |  j    d |  _ n  |  j   t |  _ n  |  j   d S(   s   Close this stream.

        If ``exc_info`` is true, set the ``error`` attribute to the current
        exception from `sys.exc_info` (or if ``exc_info`` is a tuple,
        use that instead of `sys.exc_info`).
        i   N(   R   R&   t   tupleRC   t   BaseExceptiont   sysRf   t   anyRQ   RT   R   RF   R   R)   R*   R\   R>   t   remove_handlerR`   Ra   R^   t   _maybe_run_close_callback(   R   Rf   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRo   `  s*    			
c         C` sZ  |  j    rV|  j d k rVg  } |  j d  k	 rL | j |  j  d  |  _ n  | g  |  j D] \ } } | ^ qY 7} |  j j   |  j d  k	 r | j |  j  d  |  _ n  |  j d  k	 r | j |  j  d  |  _ n  x1 | D]) } | j	 t
 d |  j   | j   q W|  j d  k	 r:|  j } d  |  _ |  j |  n  d  |  _ |  _ d  |  _ n  d  S(   Ni    R   (   R   R]   RS   R   R(   RV   t   clearRY   RZ   t   set_exceptionR   RC   Rg   RW   t   _run_callbackRR   RU   RI   (   R   t   futurest   _Rt   t   cb(    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR   ~  s,    &		c         C` s   |  j  d k	 p |  j d k	 S(   s9   Returns true if we are currently reading from the stream.N(   RR   R   RS   (   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   reading  s    c         C` s   t  |  j  S(   s7   Returns true if we are currently writing to the stream.(   t   boolRI   (   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   writing  s    c         C` s   |  j  S(   s+   Returns true if the stream has been closed.(   R^   (   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s    c         C` s   d S(   s  Sets the no-delay flag for this stream.

        By default, data written to TCP streams may be held for a time
        to make the most efficient use of bandwidth (according to
        Nagle's algorithm).  The no-delay flag requests that data be
        written as soon as possible, even if doing so would consume
        additional bandwidth.

        This flag is currently defined only for TCP-based ``IOStreams``.

        .. versionadded:: 3.1
        N(    (   R   t   value(    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   set_nodelay  s    c         C` s  |  j    r  t j d |  d  Sy|  j r9 |  j   n  |  j    rI d  S| |  j j @rf |  j   n  |  j    rv d  S| |  j j @r |  j	   n  |  j    r d  S| |  j j
 @r |  j   |  _ |  j j |  j  d  S|  j j
 } |  j   r| |  j j O} n  |  j   r#| |  j j O} n  | |  j j
 k rW|  j d k rW| |  j j O} n  | |  j k r|  j d  k	 st d   | |  _ |  j j |  j   |  j  n  Wnl t k
 r} t j d |  |  j d |  n9 t k
 r} t j d d t |  j d |    n Xd  S(   Ns   Got events for closed stream %si    s4   shouldn't happen: _handle_events without self._states*   Unsatisfiable read, closing connection: %sRf   s'   Uncaught exception, closing connection.(   R   R   t   warningR[   t   _handle_connectR>   t   READt   _handle_readR   R   t   ERRORRe   RC   t   add_callbackRo   R   R   RF   R\   R   R2   t   update_handlerR`   R   Rn   t	   ExceptionR)   (   R   t   fdt   eventst   stateRu   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   _handle_events  sP    	!		#c         ` sK       f d   } t  j   $  j d 7_  j j |  Wd  QXd  S(   Nc          ` so    j  d 8_  zN y      SWn9 t k
 r[ }  t j d d t  j d |     n XWd   j   Xd  S(   Ni   s'   Uncaught exception, closing connection.Rf   (   R]   R   R   RC   R)   Ro   R   (   Ru   (   t   argsRr   R   (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   wrapper  s     i   (   R
   t   NullContextR]   R>   R   (   R   Rr   R   R   (    (   R   Rr   R   s/   lib/python2.7/site-packages/tornado/iostream.pyR     s    c         C` s  z|  j  d  k	 r |  j  } n6 |  j d  k	 r9 |  j } n |  j   rN d  } n d } d } |  j d 7_ x |  j   s |  j   d k r Pn  |  j   | d  k	 r |  j | k r Pn  |  j | k rl |  j	   } | d  k	 r | S|  j d } ql ql W|  j	   SWd  |  j d 8_ Xd  S(   Ni    i   i   (
   RO   R   RN   R   R]   R   t   _read_to_buffert   _run_streaming_callbackRF   t   _find_read_pos(   R   t   target_bytest   next_find_posR4   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   _read_to_buffer_loop  s0    	
c         C` s   y |  j    } WnH t k
 r&   n5 t k
 rZ } t j d |  |  j d |  d  SX| d  k	 rx |  j |  d  S|  j   d  S(   Ns   error on read: %sRf   (	   R   R   R   R   R   Ro   R   t   _read_from_bufferR   (   R   R4   Ru   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR   K  s    c         C` sz   |  j  d  k s t d   |  j d  k s6 t d   | d  k	 rg t j d t  t j |  |  _  n t	   |  _ |  j S(   Ns   Already readings5   callbacks are deprecated, use returned Future instead(
   RR   R   R2   RS   R{   R|   R}   R
   R~   R   (   R   Rr   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRj   Z  s    	c         C` s  |  j  rT |  j p t   |  _ d  |  _ d |  _ t |  j  |  _ t |  _  | } n |  j	 |  } | ru |  j
 } n\ |  j } d  |  _ |  _
 |  j d  k	 r | d  k s t  |  j } d  |  _ | j |  n  | d  k	 r|  j d  k s | s t  |  j | |  n
 |  j   d  S(   Ni    (   RG   RH   R+   RD   R   RE   R$   RF   R*   t   _consumeRT   RR   RS   R2   t
   set_resultR   R   (   R   R-   t	   streamingt   resultRr   Rt   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR   e  s*    								c         C` s   |  j    |  j   } | d k	 r3 |  j |  d S|  j   y |  j   } Wn t k
 rm |  j     n X| d k	 r |  j |  d S|  j   r |  j   n |  j	 t
 j j  d S(   s  Attempt to complete the current read operation from buffered data.

        If the read can be completed without blocking, schedules the
        read callback on the next IOLoop iteration; otherwise starts
        listening for reads on the socket.
        N(   R   R   R   R   R   R   R   R   R   R   R   R<   R   (   R   R4   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRm     s"    


c         C` sm  z'x t  r yD |  j r1 t |  j  |  j } n t |  j  } |  j |  } Wnp t j	 t
 t f k
 r } t |  t j k r q n  |  j |  r |  j d |  d S|  j d |    n XPq W| d k r d S| d k r |  j   d S|  j s|  j t |  |  7_ n  |  j | 7_ Wd d } X|  j |  j k rit j	 d  |  j   t d   n  | S(   s  Reads from the socket and appends the result to the read buffer.

        Returns the number of bytes read.  Returns 0 if there is nothing
        to read (i.e. the read returns EWOULDBLOCK or equivalent).  On
        error closes the socket and raises an exception.
        Rf   Ni    s    Reached maximum read buffer size(   R)   RG   R'   RD   RF   R+   RA   Rd   t   socketRC   t   IOErrort   OSErrorR   t   errnot   EINTRt   _is_connresetRo   R   R?   R   R   (   R   Rc   t
   bytes_readRu   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s<    		
	
c         C` sk   |  j  d  k	 rg |  j rg |  j } |  j d  k	 rT t |  j |  } |  j | 8_ n  |  j | t  n  d  S(   N(   RT   R   RF   RO   R@   R   R)   (   R   t   bytes_to_consume(    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s    	c         C` s4   d |  _ |  _ |  _ t |  _ |  j | t  d S(   s   Attempts to complete the currently-pending read from the buffer.

        The argument is either a position in the read buffer or None,
        as returned by _find_read_pos.
        N(   R   RO   RL   RM   R*   RP   R   (   R   R4   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s    	c         C` sq  |  j  d k	 rR |  j |  j  k s9 |  j rR |  j d k rR t |  j  |  j  } | S|  j d k	 r |  j rm|  j j |  j |  j  } | d k r | |  j 8} t	 |  j  } |  j
 |  j | |  | | S|  j
 |  j |  j  qmn |  j d k	 rm|  j rm|  j j |  j |  j  } | d k	 rQ| j   |  j } |  j
 |  j |  | S|  j
 |  j |  j  qmn  d S(   s   Attempts to find a position in the read buffer that satisfies
        the currently-pending read.

        Returns a position in the buffer if the current read can be satisfied,
        or None if it cannot.
        i    iN(   RO   R   RF   RP   R@   RL   RD   t   findRE   R$   t   _check_max_bytesRM   t   searchR   (   R   R   t   loct   delimiter_lent   m(    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s6    			c         C` s>   |  j  d  k	 r: | |  j  k r: t d | |  j  f   n  d  S(   Ns&   delimiter %r not found within %d bytes(   RN   R   R   (   R   Rw   R-   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s
    c         C` s  x
t  rt |  j  } | s" Pn  | d k s4 t  y] t rF d } n  |  j |  j j |   } | d k rq Pn  |  j j |  |  j | 7_ Wq t	 j
 t t f k
 r} | j d t k r Pq	|  j |  s t j d |  j   |  n  |  j d |  d  Sq Xq WxM |  j r\|  j d \ } } | |  j k r?Pn  |  j j   | j d   qWt |  j  s|  j r|  j } d  |  _ |  j |  qn  d  S(   Ni    i   i   s   Write error on %s: %sRf   i   (   R)   R$   RI   R2   t   _WINDOWSRb   R5   R:   RK   R   RC   R   R   R   t   _ERRNO_WOULDBLOCKR   R   R   R`   Ro   RV   R6   R   R   RU   R   (   R   R-   R   Ru   t   indexRt   Rr   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s@    						c         C` s   | d k r d S| |  j  k s% t  t |  j  |  j |  j | !j   } |  j | 7_ |  j  | 8_  |  j |  j  k r |  j |  j 4d |  _ n  | S(   Ni    R1   (   RF   R2   R'   RD   RE   t   tobytes(   R   R   R/   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR   C  s    c         C` s%   |  j    r! t d |  j   n  d  S(   NR   (   R   R   RC   (   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR   V  s    c         C` s   |  j  d k r d  S|  j d  k s7 |  j t j j k r |  j   rP |  j   q |  j d k r |  j	 d  k	 r |  j
 t j j  q n  d  S(   Ni    (   R]   R\   R   R   R<   R   R   R   RF   RW   R   R   (   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR   Z  s    $c         C` s   |  j    r d S|  j d k rj t j j | B|  _ t j   ' |  j j	 |  j
   |  j |  j  Wd QXn< |  j | @s |  j | B|  _ |  j j |  j
   |  j  n  d S(   sJ  Adds `state` (IOLoop.{READ,WRITE} flags) to our event handler.

        Implementation notes: Reads and writes have a fast path and a
        slow path.  The fast path reads synchronously from socket
        buffers, while the slow path uses `_add_io_state` to schedule
        an IOLoop callback.  Note that in both cases, the callback is
        run asynchronously with `_run_callback`.

        To detect closed connections, we must have called
        `_add_io_state` at some point, but we want to delay this as
        much as possible so we don't have to set an `IOLoop.ERROR`
        listener that will be overwritten by the next slow-path
        operation.  As long as there are callbacks scheduled for
        fast-path ops, those callbacks may do more reads.
        If a sequence of fast-path ops do not end in a slow-path op,
        (e.g. for an @asynchronous long-poll request), we must add
        the error handler.  This is done in `_run_callback` and `write`
        (since the write callback is optional so we can have a
        fast-path write with no `_run_callback`)
        N(   R   R\   R   R   R<   R   R
   R   R>   t   add_handlerR`   R   R   (   R   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR   j  s    	"c         C` s(   t  | t j t f  o' t |  t k S(   sb   Return true if exc is ECONNRESET or equivalent.

        May be overridden in subclasses.
        (   R&   R   RC   R   R   t   _ERRNO_CONNRESET(   R   t   exc(    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s    N(*   R   R   R   R   R   R`   Ra   Rb   Rd   Re   Rv   Rx   R*   R   R   R   R   R   Ro   R   R   R   R   R   R   R   R   R   Rj   R   Rm   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR;      sP   9					
($(2.1							5	#	<				!	.		
	+		.				!t   IOStreamc           B` st   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d d d  Z
 d d d  Z d	   Z d
   Z RS(   s  Socket-based `IOStream` implementation.

    This class supports the read and write methods from `BaseIOStream`
    plus a `connect` method.

    The ``socket`` parameter may either be connected or unconnected.
    For server operations the socket is the result of calling
    `socket.accept <socket.socket.accept>`.  For client operations the
    socket is created with `socket.socket`, and may either be
    connected before passing it to the `IOStream` or connected with
    `IOStream.connect`.

    A very simple (and broken) HTTP client using this class:

    .. testcode::

        import tornado.ioloop
        import tornado.iostream
        import socket

        async def main():
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
            stream = tornado.iostream.IOStream(s)
            await stream.connect(("friendfeed.com", 80))
            await stream.write(b"GET / HTTP/1.0\r\nHost: friendfeed.com\r\n\r\n")
            header_data = await stream.read_until(b"\r\n\r\n")
            headers = {}
            for line in header_data.split(b"\r\n"):
                parts = line.split(b":")
                if len(parts) == 2:
                    headers[parts[0].strip()] = parts[1].strip()
            body_data = await stream.read_bytes(int(headers[b"Content-Length"]))
            print(body_data)
            stream.close()

        if __name__ == '__main__':
            tornado.ioloop.IOLoop.current().run_sync(main)
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
            stream = tornado.iostream.IOStream(s)
            stream.connect(("friendfeed.com", 80), send_request)
            tornado.ioloop.IOLoop.current().start()

    .. testoutput::
       :hide:

    c         O` s6   | |  _  |  j  j t  t t |   j | |   d  S(   N(   R   t   setblockingR*   R   R   R   (   R   R   R   t   kwargs(    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s    	c         C` s   |  j  S(   N(   R   (   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR`     s    c         C` s   |  j  j   d  |  _  d  S(   N(   R   Ro   R   (   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRa     s    c         C` s4   |  j  j t  j t  j  } t  j | t j |   S(   N(   R   t
   getsockoptt
   SOL_SOCKETt   SO_ERRORRC   t   ost   strerror(   R   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRe     s    c         C` sY   zK y |  j  j |  SWn0 t  j k
 rI } | j d t k rC d  S  n XWd  d  } Xd  S(   Ni    (   R   t	   recv_intoRC   R   R   R   (   R   Rc   Ru   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRd     s     c         C` s   z |  j  j |  SWd  ~ Xd  S(   N(   R   t   send(   R   R,   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRb     s    c         C` s   t  |  _ | d k	 r@ t j d t  t j |  |  _ d } n t	   } |  _
 y |  j j |  Wny t j k
 r } t |  t k r t |  t k r | d k r t j d |  j j   |  n  |  j d |  | Sn X|  j |  j j  | S(   s  Connects the socket to a remote address without blocking.

        May only be called if the socket passed to the constructor was
        not previously connected.  The address parameter is in the
        same format as for `socket.connect <socket.socket.connect>` for
        the type of socket passed to the IOStream constructor,
        e.g. an ``(ip, port)`` tuple.  Hostnames are accepted here,
        but will be resolved synchronously and block the IOLoop.
        If you have a hostname instead of an IP address, the `.TCPClient`
        class is recommended instead of calling this method directly.
        `.TCPClient` will do asynchronous DNS resolution and handle
        both IPv4 and IPv6.

        If ``callback`` is specified, it will be called with no
        arguments when the connection is completed; if not this method
        returns a `.Future` (whose result after a successful
        connection will be the stream itself).

        In SSL mode, the ``server_hostname`` parameter will be used
        for certificate validation (unless disabled in the
        ``ssl_options``) and SNI (if supported; requires Python
        2.7.9+).

        Note that it is safe to call `IOStream.write
        <BaseIOStream.write>` while the connection is pending, in
        which case the data will be written as soon as the connection
        is ready.  Calling `IOStream` read methods before the socket is
        connected works on some platforms but is non-portable.

        .. versionchanged:: 4.0
            If no callback is given, returns a `.Future`.

        .. versionchanged:: 4.2
           SSL certificates are validated by default; pass
           ``ssl_options=dict(cert_reqs=ssl.CERT_NONE)`` or a
           suitably-configured `ssl.SSLContext` to the
           `SSLIOStream` constructor to disable.

        .. deprecated:: 5.1

           The ``callback`` argument is deprecated and will be removed
           in Tornado 6.0. Use the returned `.Future` instead.

        s<   callback argument is deprecated, use returned Future insteads   Connect error on fd %s: %sRf   N(   R)   R[   R   R{   R|   R}   R
   R~   RX   R   RY   R   t   connectRC   R   t   _ERRNO_INPROGRESSR   R   R   R`   Ro   R   R>   R   (   R   t   addressRr   t   server_hostnameRt   Ru   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s&    -				c      	   ` sP  |  j  sZ |  j sZ |  j sZ |  j sZ |  j sZ |  j sZ |  j sZ |  j sZ |  j sZ |  j	 ri t
 d   n  | d k r | r t } q t } n  |  j } |  j j |  d |  _ t | | d | d | d t } |  j  d |  _ t     t | d |      f d   }  j |     f d    _ |  j  _ |  j  _   S(	   s  Convert this `IOStream` to an `SSLIOStream`.

        This enables protocols that begin in clear-text mode and
        switch to SSL after some initial negotiation (such as the
        ``STARTTLS`` extension to SMTP and IMAP).

        This method cannot be used if there are outstanding reads
        or writes on the stream, or if there is any data in the
        IOStream's buffer (data in the operating system's socket
        buffer is allowed).  This means it must generally be used
        immediately after reading or writing the last clear-text
        data.  It can also be used immediately after connecting,
        before any reads or writes.

        The ``ssl_options`` argument may be either an `ssl.SSLContext`
        object or a dictionary of keyword arguments for the
        `ssl.wrap_socket` function.  The ``server_hostname`` argument
        will be used for certificate validation unless disabled
        in the ``ssl_options``.

        This method returns a `.Future` whose result is the new
        `SSLIOStream`.  After this method has been called,
        any other operation on the original stream is undefined.

        If a close callback is defined on this stream, it will be
        transferred to the new stream.

        .. versionadded:: 4.0

        .. versionchanged:: 4.2
           SSL certificates are validated by default; pass
           ``ssl_options=dict(cert_reqs=ssl.CERT_NONE)`` or a
           suitably-configured `ssl.SSLContext` to disable.
        s+   IOStream is not idle; cannot convert to SSLR   t   server_sidet   do_handshake_on_connectt   ssl_optionsc           ` sB     j    s(   j  j p! t    n   d  k	 r>    n  d  S(   N(   t   doneR   RC   R   R   (    (   Rt   t   orig_close_callbackt
   ssl_stream(    s/   lib/python2.7/site-packages/tornado/iostream.pyt   close_callbackp  s    
c           ` s     j    S(   N(   R   (    (   Rt   R   (    s/   lib/python2.7/site-packages/tornado/iostream.pyRi     s    N(   RR   RS   RU   RV   RX   RY   R]   R^   RD   RI   t
   ValueErrorR   R	   R   R   R>   R   R   R*   RW   R   t   SSLIOStreamR   t   _ssl_connect_callbackR?   RA   (   R   R   R   R   R   R   (    (   Rt   R   R   s/   lib/python2.7/site-packages/tornado/iostream.pyt	   start_tls0  s6    #								c         C` s2  y |  j  j t  j t  j  } Wn4 t  j k
 rU } t |  t j k rV d } qV n X| d k r t  j | t j	 |   |  _ |  j
 d  k r t j d |  j  j   t j |  n  |  j   d  S|  j d  k	 r |  j } d  |  _ |  j |  n  |  j
 d  k	 r%|  j
 } d  |  _
 | j |   n  t |  _ d  S(   Ni    s   Connect error on fd %s: %s(   R   R   R   R   RC   R   R   t   ENOPROTOOPTR   R   RY   R   R   R   R`   t	   errorcodeRo   RX   R   R   R*   R[   (   R   t   errRu   Rr   Rt   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s*    	
				c         C` s   |  j  d  k	 r |  j  j t  j t  j f k r y, |  j  j t  j t  j | rQ d n d  Wq t  j k
 r } | j	 t	 j
 k r |  j |  r   q q Xn  d  S(   Ni   i    (   R   R   t   familyt   AF_INETt   AF_INET6t
   setsockoptt   IPPROTO_TCPt   TCP_NODELAYRC   R   t   EINVALR   (   R   R   Ru   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s    "N(   R   R   R   R   R`   Ra   Re   Rd   Rb   R   R   R   R   R   (    (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s   .						IT	R   c           B` s   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d d d	  Z d
   Z d d  Z d   Z d   Z d   Z RS(   sn  A utility class to write to and read from a non-blocking SSL socket.

    If the socket passed to the constructor is already connected,
    it should be wrapped with::

        ssl.wrap_socket(sock, do_handshake_on_connect=False, **kwargs)

    before constructing the `SSLIOStream`.  Unconnected sockets will be
    wrapped when `IOStream.connect` is finished.
    c         O` s   | j  d t  |  _ t t |   j | |   t |  _ t |  _	 t |  _
 d |  _ d |  _ y |  j j   Wn t j k
 r n X|  j |  j j  d S(   s   The ``ssl_options`` keyword argument may either be an
        `ssl.SSLContext` object or a dictionary of keywords arguments
        for `ssl.wrap_socket`
        R   N(   t   popR   t   _ssl_optionsR   R   R   R)   t   _ssl_acceptingR*   t   _handshake_readingt   _handshake_writingR   R   t   _server_hostnameR   t   getpeernameRC   R   R>   R   (   R   R   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s    					c         C` s   |  j  p t t |   j   S(   N(   R   R   R   R   (   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s    c         C` s   |  j  p t t |   j   S(   N(   R   R   R   R   (   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s    c         C` s  y# t  |  _ t  |  _ |  j j   Wnut j k
 r%} | j d t j k r[ t	 |  _ d  S| j d t j
 k r~ t	 |  _ d  S| j d t j t j f k r |  j d |  S| j d t j k ry |  j j   } Wn t k
 r d } n Xt j d |  j j   | |  |  j d |  S  n t j k
 r{} |  j |  se| j d t j t j f k ru|  j d |  S  nY t k
 r} |  j d |  SXt  |  _ |  j |  j j    s|  j   d  S|  j   d  S(   Ni    Rf   s   (not connected)s   SSL Error on %s %s: %s(   R*   R   R   R   t   do_handshaket   sslt   SSLErrorR   t   SSL_ERROR_WANT_READR)   t   SSL_ERROR_WANT_WRITEt   SSL_ERROR_EOFt   SSL_ERROR_ZERO_RETURNRo   t   SSL_ERROR_SSLR   R   R   R   R`   RC   R   R   t   EBADFt   ENOTCONNt   AttributeErrorR   t   _verify_certt   getpeercertt   _run_ssl_connect_callback(   R   R   t   peer(    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   _do_ssl_handshake  sF    				
		
c         C` sf   |  j  d  k	 r1 |  j  } d  |  _  |  j |  n  |  j d  k	 rb |  j } d  |  _ | j |   n  d  S(   N(   R   R   R   RZ   R   (   R   Rr   Rt   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR    s    				c         C` s  t  |  j t  r- |  j j d t j  } n$ t  |  j t j  rQ |  j j } n  | t j t j t j	 f k su t
  | t j k s |  j d k r t S|  j j   } | d k r | t j k r t j d  t Sy t j | |  j  Wn( t j k
 r} t j d |  t SXt Sd S(   s  Returns True if peercert is valid according to the configured
        validation mode and hostname.

        The ssl handshake already tested the certificate for a valid
        CA signature; the only thing that remains is to check
        the hostname.
        t	   cert_reqss   No SSL certificate givens   Invalid SSL certificate: %sN(   R&   R   t   dictt   getR   t	   CERT_NONEt
   SSLContextt   verify_modet   CERT_REQUIREDt   CERT_OPTIONALR2   R   R   R)   R   R  R   R   R*   t   match_hostnamet   CertificateError(   R   t   peercertR  t   certRu   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR    s"    $c         C` s.   |  j  r |  j   d  St t |   j   d  S(   N(   R   R
  R   R   R   (   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR   +  s    	
c         C` s.   |  j  r |  j   d  St t |   j   d  S(   N(   R   R
  R   R   R   (   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR   1  s    	
c         C` s>   | |  _  t t |   j |  } | j d    |  j |  S(   Nc         S` s
   |  j    S(   N(   Rg   (   Rh   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRi   ?  s    (   R   R   R   R   Rp   t   wait_for_handshake(   R   R   Rr   R   t   fut(    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR   7  s    	c         C` s   t  t |   j   |  j   r# d  S|  j j |  j  |  j } d  |  _ t	 |  j |  j
 d |  j d t |  _ |  j |  d  S(   NR   R   (   R   R   R   R   R>   R   R   R\   R   R   R   R   R*   R   (   R   t	   old_state(    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR   B  s    			c         C` s   |  j  d k	 s |  j d k	 r- t d   n  | d k	 rd t j d t  t j |  |  _  d } n t	   } |  _ |  j
 s |  j   n  | S(   s  Wait for the initial SSL handshake to complete.

        If a ``callback`` is given, it will be called with no
        arguments once the handshake is complete; otherwise this
        method returns a `.Future` which will resolve to the
        stream itself after the handshake is complete.

        Once the handshake is complete, information such as
        the peer's certificate and NPN/ALPN selections may be
        accessed on ``self.socket``.

        This method is intended for use on server-side streams
        or after using `IOStream.start_tls`; it should not be used
        with `IOStream.connect` (which already waits for the
        handshake to complete). It may only be called once per stream.

        .. versionadded:: 4.2

        .. deprecated:: 5.1

           The ``callback`` argument is deprecated and will be removed
           in Tornado 6.0. Use the returned `.Future` instead.

        s   Already waitings<   callback argument is deprecated, use returned Future insteadN(   R   R   RZ   t   RuntimeErrorR{   R|   R}   R
   R~   R   R   R  (   R   Rr   Rt   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR  Y  s    			c         C` sY   zN y |  j  j |  SWn3 t j k
 rL } | j d t j k rF d S  n XWd  ~ Xd  S(   Ni    (   R   R   R   R   R   R   (   R   R,   Ru   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRb     s     c         C` s   z |  j  r d  Sy |  j j |  SWnb t j k
 rY } | j d t j k rS d  S  n0 t j k
 r } | j d t	 k r d  S  n XWd  d  } Xd  S(   Ni    (
   R   R   R   R   R   R   R   R   RC   R   (   R   Rc   Ru   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRd     s    	c         C` sB   t  | t j  r, | j d t j k r, t St t |   j |  S(   Ni    (	   R&   R   R   R   R   R)   R   R   R   (   R   Ru   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s    (N(   R   R   R   R   R   R   R
  R  R  R   R   R   R   R   R  Rb   Rd   R   (    (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s   
				.	
				'		t   PipeIOStreamc           B` s;   e  Z d  Z d   Z d   Z d   Z d   Z d   Z RS(   s  Pipe-based `IOStream` implementation.

    The constructor takes an integer file descriptor (such as one returned
    by `os.pipe`) rather than an open file object.  Pipes are generally
    one-way, so a `PipeIOStream` can be used for reading or writing but not
    both.
    c         O` sH   | |  _  t j |  j  d  |  _ t |  t t |   j | |   d  S(   Ns   r+(   R   t   iot   FileIOt   _fioR   R   R  R   (   R   R   R   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR     s    	
c         C` s   |  j  S(   N(   R   (   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR`     s    c         C` s   |  j  j   d  S(   N(   R  Ro   (   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRa     s    c         C` s"   z t  j |  j |  SWd  ~ Xd  S(   N(   R   R   R   (   R   R,   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRb     s    c         C` sn   z` y |  j  j |  SWnE t t f k
 r^ } t |  t j k rX |  j d |  d  S  n XWd  d  } Xd  S(   NRf   (	   R  t   readintoR   R   R   R   R  Ro   R   (   R   Rc   Ru   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyRd     s     (   R   R   R   R   R`   Ra   Rb   Rd   (    (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyR    s   				c          C` s   d d  l  }  |  j   S(   Ni    (   t   doctestt   DocTestSuite(   R   (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   doctests  s    (A   R   t
   __future__R    R   R   R   R   R  Ry   R   R   R   Rk   R{   t   tornado.concurrentR   t   tornadoR   t   tornado.logR   R   t   tornado.netutilR   R   R	   R
   t   tornado.utilR   t   tornado.platform.posixR   t   ImportErrorR   R   t   EWOULDBLOCKt   EAGAINR   t   hasattrR   t
   ECONNRESETt   ECONNABORTEDt   EPIPEt	   ETIMEDOUTR   R   t   WSAECONNABORTEDt   WSAETIMEDOUTt   platformt
   EPROTOTYPEt   EINPROGRESSR   R   t
   startswithR   R   R   R   R   R   t   objectR   R;   R   R   R  R"  (    (    (    s/   lib/python2.7/site-packages/tornado/iostream.pyt   <module>   sf   

	Z     +