B
    H/\%                 @   s   d dl mZmZmZ yd dlmZ W n ek
r<   dZY nX d dlZd dlm	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mZ G d
d deZG dd deZG dd deZG dd deZG dd deZdS )    )absolute_importprint_functiondivision)EBADF	   N)TextIOWrapper)_get_hub_noargs)integer_types)reraise)	SemaphoreDummySemaphorec                   s   e Zd Z fddZ  ZS )cancel_wait_exc                s   t t| td d S )Nz.File descriptor was closed in another greenlet)superr   __init__r   )self)	__class__ 7lib/python3.7/site-packages/gevent/_fileobjectcommon.pyr      s    
zcancel_wait_ex.__init__)__name__
__module____qualname__r   __classcell__r   r   )r   r   r      s   r   c                   s   e Zd Z fddZ  ZS )FileObjectClosedc                s   t t| td d S )Nz+Bad file descriptor (FileObject was closed))r   r   r   r   )r   )r   r   r   r      s    
zFileObjectClosed.__init__)r   r   r   r   r   r   r   )r   r   r      s   r   c               @   s   e Zd ZdZdZdZdZdZdd Ze	dd d	d Z
d
d Zdd Zdd Ze	dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS ) FileObjectBasezo
    Internal base class to ensure a level of consistency
    between FileObjectPosix and FileObjectThread
    )flushfilenowritablereadableseekseekabletellreadreadline	readlinesread1write
writelinestruncateFNc             C   s2   || _ || _| jr&| d| j| j n|   dS )z6
        :param io: An io.IOBase-like object.
        N)_io_close
_translatetranslate_newlines_translate_encoding_translate_errors_do_delegate_methods)r   ioclosefdr   r   r   r   E   s
    zFileObjectBase.__init__c             C   s   | j S )N)r(   )sr   r   r   <lambda>V   s    zFileObjectBase.<lambda>c             C   s   t | d|p|  S )Nr(   )setattrr.   )r1   Znvr   r   r   r2   \   s    c             C   sd   x^| j D ]T}t| j|d }tt| |}|rD|sDt| || | qt| |r|st| | qW d S )N)_delegate_methodsgetattrr(   hasattrtyper3   _wrap_methoddelattr)r   Z	meth_namemethZimplemented_by_classr   r   r   r.   ^   s    z#FileObjectBase._do_delegate_methodsc             C   s   |S )z
        Wrap a method we're copying into our dictionary from the underlying
        io object to do something special or different, if necessary.
        r   )r   methodr   r   r   r8   g   s    zFileObjectBase._wrap_methodc             O   s,   t | jf||}|r||_|| _d| _d S )NT)r   r(   moder/   r*   )r   r<   Z	text_argsZtext_kwargswrapperr   r   r   r+   n   s
    z!FileObjectBase.translate_newlinesc             C   s
   | j dkS )zTrue if the file is closedN)r(   )r   r   r   r   closedu   s    zFileObjectBase.closedc             C   s,   | j d krd S | j }d | _ | || j d S )N)r(   	_do_closer)   )r   r/   r   r   r   closez   s
    
zFileObjectBase.closec             C   s
   t  d S )N)NotImplementedError)r   fobjr0   r   r   r   r?      s    zFileObjectBase._do_closec             C   s   | j d krt t| j |S )N)r(   r   r5   )r   namer   r   r   __getattr__   s    
zFileObjectBase.__getattr__c             C   s   d| j j| j|  f S )Nz<%s _fobj=%r%s>)r   r   r/   _extra_repr)r   r   r   r   __repr__   s    zFileObjectBase.__repr__c             C   s   dS )N r   )r   r   r   r   rE      s    zFileObjectBase._extra_reprc             C   s   | S )Nr   )r   r   r   r   	__enter__   s    zFileObjectBase.__enter__c             G   s   |    d S )N)r@   )r   argsr   r   r   __exit__   s    zFileObjectBase.__exit__)r   r   r   __doc__r4   r*   r,   r-   r   propertyr/   r.   r8   r+   r>   r@   r?   rD   rF   rE   rH   rJ   r   r   r   r   r       s&   
	r   c                   s$   e Zd Z fddZdd Z  ZS )FileObjectBlockc                s\   | dd}|r td|  t|trF|s6tdtj|f| }tt| 	|| d S )Nr@   TzUnexpected arguments: %rz6FileObjectBlock does not support close=False on an fd.)
pop	TypeErrorkeys
isinstancer	   osfdopenr   rM   r   )r   rB   rI   kwargsr0   )r   r   r   r      s    
zFileObjectBlock.__init__c             C   s   |   d S )N)r@   )r   rB   r0   r   r   r   r?      s    zFileObjectBlock._do_close)r   r   r   r   r?   r   r   r   )r   r   rM      s   rM   c                   sZ   e Zd ZdZd fdd	Zdd Z fd	d
Zdd Zdd Zdd Z	e	Z
dd Z  ZS )FileObjectThreada  
    A file-like object wrapping another file-like object, performing all blocking
    operations on that object in a background thread.

    .. caution::
        Attempting to change the threadpool or lock of an existing FileObjectThread
        has undefined consequences.

    .. versionchanged:: 1.1b1
       The file object is closed using the threadpool. Note that whether or
       not this action is synchronous or asynchronous is not documented.

    NTc                s   |}|pt  j| _|| _| jdkr,t | _n| js:t | _t| jdsXtdt| j t|t	r|sntd|dkr|dkst
dt|}nt|||}|g| _tt| || dS )a  
        :param fobj: The underlying file-like object to wrap, or an integer fileno
           that will be pass to :func:`os.fdopen` along with *mode* and *bufsize*.
        :keyword bool lock: If True (the default) then all operations will
           be performed one-by-one. Note that this does not guarantee that, if using
           this file object from multiple threads/greenlets, operations will be performed
           in any particular order, only that no two operations will be attempted at the
           same time. You can also pass your own :class:`gevent.lock.Semaphore` to synchronize
           file operations with an external resource.
        :keyword bool close: If True (the default) then when this object is closed,
           the underlying object is closed as well.
        TrH   z'Expected a Semaphore or boolean, got %rz7FileObjectThread does not support close=False on an fd.NrV   z7If you use the default mode, you can't choose a bufsize)get_hub
threadpoollockr   r   r6   rO   r7   rQ   r	   AssertionErrorrR   rS   _FileObjectThread__io_holderr   rU   r   )r   rB   r<   bufsizer@   rX   rY   r0   )r   r   r   r      s$    


zFileObjectThread.__init__c          	   C   sb   d | j d< z$| j | j|j W d Q R X W d |r\|fdd}~| j|}~|r\t|  X d S )Nr   c             S   s,   z y|    W n   t S W d d } X d S )N)r@   sysexc_info)Z_fobjr   r   r   r@      s
    z)FileObjectThread._do_close.<locals>.close)r[   rY   rX   applyr   r
   )r   rB   r0   r@   r^   r   r   r   r?      s    
zFileObjectThread._do_closec                sB   t t|   t| ds2dt| jddkr2| j| _| j| jd< d S )Nr$   rr<   rG   r   )	r   rU   r.   r6   r5   r(   r!   r$   r[   )r   )r   r   r   r.      s    z%FileObjectThread._do_delegate_methodsc             C   s   d| j f S )Nz threadpool=%r)rX   )r   r   r   r   rE      s    zFileObjectThread._extra_reprc             C   s   | S )Nr   )r   r   r   r   __iter__   s    zFileObjectThread.__iter__c             C   s   |   }|r|S td S )N)r"   StopIteration)r   liner   r   r   next  s    zFileObjectThread.nextc                s2   | j  | j| jt fdd}|S )Nc           	      s0    d d krt   | |S Q R X d S )Nr   )r   r_   )rI   rT   )	io_holderrY   r;   rX   r   r   thread_method  s    z4FileObjectThread._wrap_method.<locals>.thread_method)r[   rY   rX   	functoolswraps)r   r;   rf   r   )re   rY   r;   rX   r   r8     s
    
zFileObjectThread._wrap_method)NrV   TNT)r   r   r   rK   r   r?   r.   rE   ra   rd   __next__r8   r   r   r   )r   r   rU      s   #rU   )Z
__future__r   r   r   errnor   ImportErrorrR   r/   r   rg   r]   Z
gevent.hubr   rW   Zgevent._compatr	   r
   Zgevent.lockr   r   IOErrorr   r   objectr   rM   rU   r   r   r   r   <module>   s"   
v