ó
ÐH/\c           @@  sZ  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 m Z d  d l m Z d  d l m Z d  d l m	 Z	 d  d l m
 Z
 d  d	 l m Z d  d
 l m Z d  d l m Z d  d l m Z d  d l m Z d  d l m Z d  d l m Z d  d l m Z d e	 f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   i    (   t   absolute_importN(   t   BufferedReader(   t   BufferedWriter(   t   BytesIO(   t   DEFAULT_BUFFER_SIZE(   t	   RawIOBase(   t   UnsupportedOperation(   t   reraise(   t   cancel_wait_ex(   t   FileObjectBase(   t   get_hub(   t   _read(   t   _write(   t   ignored_errors(   t   make_nonblockingt   GreenFileDescriptorIOc           B@  s¤   e  Z d Z d Z e Z d Z d  e d „ Z	 d „  Z
 d „  Z d „  Z d „  Z e d „  ƒ Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d d „ Z RS(   t   rc         C@  s±   t  j |  ƒ | |  _ | |  _ t | ƒ d | k } d | k } t ƒ  |  _ |  j j j } y: | rz | | d ƒ |  _	 n  | r• | | d ƒ |  _
 n  Wn |  j ƒ  ‚  n Xd  S(   NR   t   wi   i   (   R   t   __init__t   _closefdt   _filenoR   R
   t   hubt   loopt   iot   _read_eventt   _write_eventt   close(   t   selft   filenot   modet   closefdt   readablet   writablet
   io_watcher(    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyR   "   s     		

c         C@  s   |  j  d  k	 S(   N(   R   t   None(   R   (    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyR   D   s    c         C@  s   |  j  d  k	 S(   N(   R   R"   (   R   (    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyR    G   s    c         C@  s\   |  j  d  k rU y t j |  j d t j ƒ Wn t k
 rH t |  _  qU Xt |  _  n  |  j  S(   Ni    (	   t	   _seekableR"   t   ost   lseekR   t   SEEK_CURt   OSErrort   Falset   True(   R   (    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyt   seekableJ   s    c         C@  s   |  j  S(   N(   R   (   R   (    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyR   T   s    c         C@  s   |  j  S(   N(   t   _closed(   R   (    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyt   closedW   s    c         C@  sz   |  j  } |  j } |  j } d  |  _ |  _  |  _ | d  k	 rT | j | t t ƒ n  | d  k	 rv | j | t t ƒ n  d  S(   N(   R   R   R   R"   t   cancel_waitR   R)   (   R   t
   read_eventt   write_eventR   (    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyt   __destroy_events[   s    			c         C@  sY   |  j  r d  S|  j ƒ  t |  _  |  j ƒ  |  j } |  j rU d  |  _ t j | ƒ n  d  S(   N(	   R+   t   flushR)   t&   _GreenFileDescriptorIO__destroy_eventsR   R   R"   R$   R   (   R   R   (    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyR   f   s    	
	
			c         C@  s‹   |  j  d  k r t d ƒ ‚ n  xf t r† y t |  j | ƒ SWn2 t t f k
 ro } | j d t	 k rp ‚  qp n X|  j
 j |  j  ƒ q! Wd  S(   Nt   readi    (   R   R"   R   R)   R   R   t   IOErrorR'   t   argsR   R   t   wait(   R   t   nt   ex(    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyt   __readz   s    	
c         C@  sF   t  ƒ  } x0 t r; |  j t ƒ } | s+ Pn  | j | ƒ q W| j ƒ  S(   N(   R   R)   t   _GreenFileDescriptorIO__readR   t   writet   getvalue(   R   t   rett   data(    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyt   readall…   s    		c         C@  s†   |  j  t | ƒ ƒ } t | ƒ } y | | | *WnP t k
 r } d d  l } t | | j ƒ sh | ‚ n  | j d | ƒ | | *n X| S(   Ni    t   b(   R:   t   lent	   TypeErrort   arrayt
   isinstance(   R   R@   R>   R7   t   errRC   (    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyt   readintoŽ   s    	c         C@  s‹   |  j  d  k r t d ƒ ‚ n  xf t r† y t |  j | ƒ SWn2 t t f k
 ro } | j d t	 k rp ‚  qp n X|  j
 j |  j  ƒ q! Wd  S(   NR;   i    (   R   R"   R   R)   R   R   R4   R'   R5   R   R   R6   (   R   R@   R8   (    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyR;   š   s    	
i    c         C@  sj   y t  j |  j | | ƒ SWnI t k
 r0 ‚  n6 t k
 re } t t t | j Œ  t j ƒ  d ƒ n Xd  S(   Ni   (	   R$   R%   R   R4   R'   R   R5   t   syst   exc_info(   R   t   offsett   whenceR8   (    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyt   seek¥   s    N(   t   __name__t
   __module__R"   R   R   R(   R+   R#   R)   R   R   R    R*   R   t   propertyR,   R2   R   R:   R?   RF   R;   RK   (    (    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyR      s"   "			
								t   FlushingBufferedWriterc           B@  s   e  Z d  „  Z RS(   c         C@  s    t  j |  | ƒ } |  j ƒ  | S(   N(   R   R;   R1   (   R   R@   R=   (    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyR;   ´   s    
(   RL   RM   R;   (    (    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyRO   ²   s   t   FileObjectPosixc           B@  s;   e  Z d  Z e j Z d d e d „ Z d „  Z d „  Z	 RS(   sØ	  
    A file-like object that operates on non-blocking files but
    provides a synchronous, cooperative interface.

    .. caution::
         This object is only effective wrapping files that can be used meaningfully
         with :func:`select.select` such as sockets and pipes.

         In general, on most platforms, operations on regular files
         (e.g., ``open('a_file.txt')``) are considered non-blocking
         already, even though they can take some time to complete as
         data is copied to the kernel and flushed to disk: this time
         is relatively bounded compared to sockets or pipes, though.
         A :func:`~os.read` or :func:`~os.write` call on such a file
         will still effectively block for some small period of time.
         Therefore, wrapping this class around a regular file is
         unlikely to make IO gevent-friendly: reading or writing large
         amounts of data could still block the event loop.

         If you'll be working with regular files and doing IO in large
         chunks, you may consider using
         :class:`~gevent.fileobject.FileObjectThread` or
         :func:`~gevent.os.tp_read` and :func:`~gevent.os.tp_write` to bypass this
         concern.

    .. note::
         Random read/write (e.g., ``mode='rwb'``) is not supported.
         For that, use :class:`io.BufferedRWPair` around two instance of this
         class.

    .. tip::
         Although this object provides a :meth:`fileno` method and so
         can itself be passed to :func:`fcntl.fcntl`, setting the
         :data:`os.O_NONBLOCK` flag will have no effect (reads will
         still block the greenlet, although other greenlets can run).
         However, removing that flag *will cause this object to no
         longer be cooperative* (other greenlets will no longer run).

         You can use the internal ``fileio`` attribute of this object
         (a :class:`io.RawIOBase`) to perform non-blocking byte reads.
         Note, however, that once you begin directly using this
         attribute, the results from using methods of *this* object
         are undefined, especially in text mode. (See :issue:`222`.)

    .. versionchanged:: 1.1
       Now uses the :mod:`io` package internally. Under Python 2, previously
       used the undocumented class :class:`socket._fileobject`. This provides
       better file-like semantics (and portability to Python 3).
    .. versionchanged:: 1.2a1
       Document the ``fileio`` attribute for non-blocking reads.
    t   rbiÿÿÿÿc   
      @  s  t  | t ƒ r | } d	 } n | j ƒ  } t  | t ƒ sL t d | ƒ ‚ n  | } | p[ d j d d ƒ } d | k rÜ t |  _ t t	 k rÇ d | k rÇ d |  _
 d d	 l ‰  ‡  f d
 †  } | |  _ n  | j d d ƒ } n	 t |  _ | j d d ƒ } t | ƒ d k r+| d k r+t d | f ƒ ‚ n  | |  _ | d k  sL| d k rX|  j } n | d k rmd } n  | d k r‚t } n0 | d k s”t ‚ t } |  j d k r²t } n  | |  _ t | | d | ƒ|  _ | |  j | ƒ }	 t t |  ƒ j |	 | ƒ d	 S(   så  
        :param fobj: Either an integer fileno, or an object supporting the
            usual :meth:`socket.fileno` method. The file *will* be
            put in non-blocking mode using :func:`gevent.os.make_nonblocking`.
        :keyword str mode: The manner of access to the file, one of "rb", "rU" or "wb"
            (where the "b" or "U" can be omitted).
            If "U" is part of the mode, universal newlines will be used. On Python 2,
            if 't' is not in the mode, this will result in returning byte (native) strings;
            putting 't'  in the mode will return text strings. This may cause
            :exc:`UnicodeDecodeError` to be raised.
        :keyword int bufsize: If given, the size of the buffer to use. The default
            value means to use a platform-specific default
            Other values are interpreted as for the :mod:`io` package.
            Buffering is ignored in text mode.

        .. versionchanged:: 1.3a1

           On Python 2, enabling universal newlines no longer forces unicode
           IO.

        .. versionchanged:: 1.2a1

           A bufsize of 0 in write mode is no longer forced to be 1.
           Instead, the underlying buffer is flushed after every write
           operation to simulate a bufsize of 0. In gevent 1.0, a
           bufsize of 0 was flushed when a newline was written, while
           in gevent 1.1 it was flushed when more than one byte was
           written. Note that this may have performance impacts.
        s   fileno must be int: %rRQ   R@   t    t   Ut   ts   latin-1i    Nc         @  s8   ˆ  j  j d ƒ r4 ˆ j ˆ  ƒ ‡  f d †  ƒ } | Sˆ  S(   NR3   c          @  s1   ˆ  |  | Ž  } t  | t ƒ s$ t ‚ | j d ƒ S(   Ns   latin-1(   RD   t   unicodet   AssertionErrort   encode(   R5   t   kwargst   result(   t   m(    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyt   wrapped(  s    (   RL   t
   startswitht   wraps(   RZ   R[   (   t	   functools(   RZ   s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyt   wrap_method&  s    i   t   rws%   mode can only be [rb, rU, wb], not %rR   R   R   (   RD   t   intR"   R   RB   t   replaceR)   t
   _translatet   bytest   strt   _translate_encodingR^   t   _wrap_methodR(   RA   t
   ValueErrort   _orig_bufsizet   default_bufsizeR   RV   R   RO   t   _fobjR   t   fileiot   superRP   R   (
   R   t   fobjR   t   bufsizeR   R   t	   orig_modeR_   t   IOFamilyt   buffered_fobj(    (   R^   s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyR   ò   sF    										c         C@  s5   z | j  ƒ  |  j j  ƒ  Wd  d  |  _ d  |  _ Xd  S(   N(   R   Rl   R"   Rk   (   R   Rn   R   (    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyt	   _do_closeZ  s
    
	c         C@  s   |  j  S(   N(   t   _io(   R   (    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyt   __iter__d  s    (
   RL   RM   t   __doc__R   R   Rj   R)   R   Rs   Ru   (    (    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyRP   º   s
   3	h	
(   t
   __future__R    R$   RG   R   R   R   R   R   R   R   t   gevent._compatR   t   gevent._fileobjectcommonR   R	   t
   gevent.hubR
   t	   gevent.osR   R   R   R   R   RO   RP   (    (    (    s6   lib/python2.7/site-packages/gevent/_fileobjectposix.pyt   <module>   s(   œ