3
Z_ð]ð  ã            	   @   sø   d dl Zd dlZd dlZd dlZd dlmZ	m
ZmZmZmZmZ d dlmZ d dlmZmZmZmZ d dlmZmZmZmZ d dlm Z  d dl!Z!ddgZ"ej#Z$G d	d
„ d
eƒZ%e&e'ƒ ƒZ(e(j)e&e	eeee%ed�ƒ ee!j*ej+ƒe(ƒZ,ddd„Z-ee-_.dS )é    N)ÚBufferedRandomÚBufferedReaderÚBufferedWriterÚDEFAULT_BUFFER_SIZEÚTextIOWrapperÚIOBase)ÚFunctionType)Ú_operation_on_closed_fileÚgreenpipe_docÚset_nonblockingÚSOCKET_BLOCKING)Únotify_closeÚnotify_openedÚIOClosedÚ
trampoline)Ú	get_errnoÚ_fileobjectÚ	GreenPipec               @   s®   e Zd Zd*dd„Zedd„ ƒZdd	„ Zd
d„ Zdd„ Zdd„ Z	d,dd„Z
dd„ Zdd„ Zdd„ Zd-dd„Zdd„ Zdd„ Zd d!„ Zd/d"d#„Zejfd$d%„Zd&d'„ Zd(d)„ ZdS )0ÚGreenFileIOÚrTNc          	   C   s‚   t |tƒr|}d| | _n<t |tjƒs*t‚t||ƒ�}|j| _tj	|j
ƒ ƒ}W d Q R X t|ƒ || _|| _d| _t| ƒ d | _d S )Nz<fd:%d>F)Ú
isinstanceÚintÚ_nameÚsixÚstring_typesÚAssertionErrorÚopenÚnameÚ_original_osÚdupÚfilenor   Ú_filenoÚ_modeÚ_closedr   Ú	_seekable)Úselfr   ÚmodeÚclosefdÚopenerr    Úfd© r*   ú8/tmp/pip-build-yz_zf6az/eventlet/eventlet/greenio/py3.pyÚ__init__#   s    
zGreenFileIO.__init__c             C   s   | j S )N)r#   )r%   r*   r*   r+   Úclosed4   s    zGreenFileIO.closedc             C   sh   | j d krbytj| jdtjƒ W n: tk
rZ } zt|ƒtjkrHd| _ n‚ W Y d d }~X nX d| _ | j S )Nr   FT)	r$   r   Úlseekr!   ÚSEEK_CURÚIOErrorr   ÚerrnoZESPIPE)r%   Úer*   r*   r+   Úseekable8   s    
zGreenFileIO.seekablec             C   s   d| j kpd| j kS )Nr   ú+)r"   )r%   r*   r*   r+   ÚreadableF   s    zGreenFileIO.readablec             C   s   d| j kpd| j kS )NÚwr4   )r"   )r%   r*   r*   r+   ÚwritableI   s    zGreenFileIO.writablec             C   s   | j S )N)r!   )r%   r*   r*   r+   r    L   s    zGreenFileIO.filenoé   c             C   sp   |dkr| j ƒ S xZytj| j|ƒS  tk
rf } z*t|ƒtkrHt|jŽ ‚| j	| dd� W Y d d }~X qX qW d S )Nr8   T)Úreadéÿÿÿÿ)
Úreadallr   r9   r!   ÚOSErrorr   r   r0   ÚargsÚ_trampoline)r%   Úsizer2   r*   r*   r+   r9   O   s    
zGreenFileIO.readc             C   s„   g }xzy.t j| jtƒ}|dkr(dj|ƒS |j|ƒ W q tk
rz } z*t|ƒtkr\t	|j
Ž ‚| j| dd� W Y d d }~X qX qW d S )Nó    T)r9   )r   r9   r!   r   ÚjoinÚappendr<   r   r   r0   r=   r>   )r%   ÚbufÚchunkr2   r*   r*   r+   r;   [   s    

zGreenFileIO.readallc             C   s*   t |ƒ}| j|ƒ}t |ƒ}||d |…< |S )N)Úlenr9   )r%   ÚbZup_toÚdataÚ
bytes_readr*   r*   r+   Úreadintoh   s
    
zGreenFileIO.readintoc             C   s@   yt j| jƒ ƒS  tk
r: } zt|jŽ ‚W Y d d }~X nX d S )N)r   Úisattyr    r<   r0   r=   )r%   r2   r*   r*   r+   rJ   o   s    zGreenFileIO.isattyFc             C   sF   | j rtƒ ‚yt|||||| jd�S  tk
r@   | jƒ  ‚ Y nX d S )N)r9   ÚwriteÚtimeoutÚtimeout_excZmark_as_closed)r#   r   r   Ú_mark_as_closed)r%   r)   r9   rK   rL   rM   r*   r*   r+   r>   u   s    

zGreenFileIO._trampolinec             C   s
   d| _ dS )z" Mark this socket as being closed TN)r#   )r%   r*   r*   r+   rN   ƒ   s    zGreenFileIO._mark_as_closedc             C   sŽ   t |ƒ}t|ƒ}d}xt||k rˆytj| j||d … ƒ}W nD tk
r| } z(t|ƒtkr`t|j	Ž ‚t
| dd� W Y d d }~X qX ||7 }qW |S )Nr   T)rK   )Ú
memoryviewrE   r   rK   r!   r<   r   r   r0   r=   r   )r%   rG   ÚviewZdatalenÚoffsetZwrittenr2   r*   r*   r+   rK   ‡   s    

zGreenFileIO.writec             C   s@   | j sd| _ tj| jƒ t| jƒ xdD ]}t| |tƒ q(W d S )NTr    ÚflushrJ   Únextr9   rI   ÚreadlineÚ	readlinesÚseekÚtellÚtruncaterK   Ú
xreadlinesÚ__iter__Ú__next__Ú
writelines)r    rR   rJ   rS   r9   rI   rT   rU   rV   rW   rX   rK   rY   rZ   r[   r\   )r#   r   Úcloser!   r   Úsetattrr	   )r%   Úmethodr*   r*   r+   r]   –   s    
  zGreenFileIO.closec             C   sb   |dkr| j ƒ }ytj| j|ƒ}W n, tk
rN } zt|jŽ ‚W Y d d }~X nX | j|ƒ |S d S )Nr8   r:   )rW   r   Ú	ftruncater!   r<   r0   r=   rV   )r%   r?   Úrvr2   r*   r*   r+   rX   ¡   s    
zGreenFileIO.truncatec             C   sB   yt j| j||ƒS  tk
r< } zt|jŽ ‚W Y d d }~X nX d S )N)r   r.   r!   r<   r0   r=   )r%   rQ   Úwhencer2   r*   r*   r+   rV   ¬   s    zGreenFileIO.seekc             C   s   | S )Nr*   )r%   r*   r*   r+   Ú	__enter__²   s    zGreenFileIO.__enter__c             G   s   | j ƒ  d S )N)r]   )r%   r=   r*   r*   r+   Ú__exit__µ   s    zGreenFileIO.__exit__)r   TNr:   )r:   )FFNNr:   )r:   )Ú__name__Ú
__module__Ú__qualname__r,   Úpropertyr-   r3   r5   r7   r    r9   r;   rI   rJ   r>   rN   rK   r]   rX   r   ÚSEEK_SETrV   rc   rd   r*   r*   r*   r+   r   "   s$   



r   )r   r   r   r   ÚFileIOÚosr   r8   Tc       	      C   sN   y| j ƒ }W n tk
r    Y nX tj|ƒ}| jƒ  |} t| |||||||ƒS )N)r    ÚAttributeErrorr   r   r]   Ú_open)	r   r&   Ú	bufferingÚencodingÚerrorsÚnewliner'   r(   r    r*   r*   r+   r   É   s    
r:   )r   r:   NNNTN)/Z_pyioZ_original_pyior1   rk   r   ÚsocketZ_original_socketÚior   Z_OriginalBufferedRandomr   Z_OriginalBufferedReaderr   Z_OriginalBufferedWriterr   r   Z_OriginalTextIOWrapperr   Z_OriginalIOBaseÚtypesr   Zeventlet.greenio.baser	   r
   r   r   Zeventlet.hubsr   r   r   r   Zeventlet.supportr   r   Ú__all__ÚSocketIOr   r   ÚdictÚglobalsZ_open_environmentÚupdateÚget_function_coder   rm   r   Ú__doc__r*   r*   r*   r+   Ú<module>   s6     


 
