o
    ‡e(2  ã                   @   s.  d dl m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	 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 G dd„ de	ƒZG dd„ deeƒZG dd„ deƒZG dd„ deƒZdS )é    )Úabsolute_import)Úprint_functionN)ÚBytesIO)ÚDEFAULT_BUFFER_SIZE)ÚFileIO)Ú	RawIOBase)ÚUnsupportedOperation)Úreraise)Úcancel_wait_ex)ÚFileObjectBase)ÚOpenDescriptor)ÚWriteIsWriteallMixin)Úwait_on_watcher)Úget_hub)Ú_read)Ú_write)Úignored_errors)Úmake_nonblockingc                   @   s¬   e Zd ZdZdZdZdZdZd%dd„Zdd„ Z	dd	„ Z
d
d„ Zdd„ Zdd„ Ze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!d"„Zd#d$„ ZdS )'ÚGreenFileDescriptorIONFTc                 C   sˆ   t  | ¡ || _|| _|| _|j| _t|ƒ |j}|j	}t
ƒ | _| jjj}z|r.||dƒ| _|r9||dƒ| _W d S W d S    |  ¡  ‚ )Né   é   )r   Ú__init__Ú_closefdÚ_filenoÚnameZfileio_modeÚmoder   Zcan_readZ	can_writer   ÚhubÚloopÚioÚ_read_watcherÚ_write_watcherÚclose)ÚselfÚfilenoZopen_descriptorÚclosefdÚreadableÚwritableZ
io_watcher© r'   úGD:\Projects\ConvertPro\env\Lib\site-packages\gevent/_fileobjectposix.pyr   )   s&   

ÿzGreenFileDescriptorIO.__init__c                 C   s,   t | jddƒ}z	| ¡ W | ¡  S | ¡  w )NÚrF)r   r   Úisattyr!   )r"   Úfr'   r'   r(   r*   M   s   zGreenFileDescriptorIO.isattyc                 C   ó
   | j d uS ©N)r   ©r"   r'   r'   r(   r%   U   ó   
zGreenFileDescriptorIO.readablec                 C   r,   r-   )r    r.   r'   r'   r(   r&   X   r/   zGreenFileDescriptorIO.writablec                 C   sJ   | j d u r"zt | jdtj¡ W n ty   d| _ Y | j S w d| _ | j S )Nr   FT)Ú	_seekableÚosÚlseekr   ÚSEEK_CURÚOSErrorr.   r'   r'   r(   Úseekable[   s   
üzGreenFileDescriptorIO.seekablec                 C   ó   | j S r-   )r   r.   r'   r'   r(   r#   e   s   zGreenFileDescriptorIO.filenoc                 C   r6   r-   )Ú_closedr.   r'   r'   r(   Úclosedh   s   zGreenFileDescriptorIO.closedc                 C   sH   | j }| j}| j}d  | _ | _ | _| ||ft| j| j| j| j¡ d S r-   )	r   r    r   Zcancel_waits_close_and_thenr
   Ú$_GreenFileDescriptorIO__finish_closer   r   Ú_keep_alive)r"   Z
read_eventZwrite_eventr   r'   r'   r(   Z__destroy_eventsl   s   úz&GreenFileDescriptorIO.__destroy_eventsc              	   C   sB   | j rd S |  ¡  d| _ z|  ¡  W d  | _| _d S d  | _| _w )NT)r7   ÚflushÚ&_GreenFileDescriptorIO__destroy_eventsr   r:   r.   r'   r'   r(   r!   {   s   
zGreenFileDescriptorIO.closec              	   C   sB   z| rt  |¡ W t|dƒr| ¡  d S d S t|dƒr | ¡  w w )Nr!   )r1   r!   Úhasattr)r$   r#   Ú
keep_aliver'   r'   r(   Z__finish_close‡   s   
€
ÿ
ÿz$GreenFileDescriptorIO.__finish_closec              
   C   ól   | j d u r	tdƒ‚	 zt| j|ƒW S  ty+ } z|jd tvr!‚ W Y d }~nd }~ww t| j d d | jƒ q
)NÚreadr   r   )	r   r   r   r   r4   Úargsr   r   r   )r"   ÚnÚexr'   r'   r(   Ú__read—   ó   
ÿ€ÿúzGreenFileDescriptorIO.__readc                 C   sH   t ƒ }	 z|  t¡}W n ty   d }Y nw |s	 | ¡ S | |¡ qr-   )r   Ú_GreenFileDescriptorIO__readr   r
   ÚwriteÚgetvalue)r"   ÚretÚdatar'   r'   r(   Úreadall¢   s   ü
ÿözGreenFileDescriptorIO.readallc              
   C   s|   |   t|ƒ¡}t|ƒ}z	||d |…< W |S  ty= } zdd l}t||jƒs(|‚| d|¡|d |…< W Y d }~|S d }~ww )Nr   ó   b)rF   ÚlenÚ	TypeErrorÚarrayÚ
isinstance)r"   ÚbrJ   rB   ÚerrrO   r'   r'   r(   Úreadinto±   s   û€ûzGreenFileDescriptorIO.readintoc              
   C   r?   )NrG   Tr   )	r    r   r   r   r4   rA   r   r   r   )r"   rQ   rC   r'   r'   r(   rG   ½   rE   zGreenFileDescriptorIO.writer   c              
   C   sb   z	t  | j||¡W S  ty   ‚  ty0 } zttt|jŽ t ¡ d ƒ W Y d }~d S d }~ww )Nr   )	r1   r2   r   ÚIOErrorr4   r	   rA   ÚsysÚexc_info)r"   ÚoffsetÚwhencerC   r'   r'   r(   ÚseekÈ   s   (€ûzGreenFileDescriptorIO.seekc                 C   s   dt | ƒjt| ƒ| j| jf S )Nz<%s at 0x%x fileno=%s mode=%r>)ÚtypeÚ__name__Úidr   r   r.   r'   r'   r(   Ú__repr__Ô   s   ÿzGreenFileDescriptorIO.__repr__)T)r   )r[   Ú
__module__Ú__qualname__r   r    r7   r0   r:   r   r*   r%   r&   r5   r#   Úpropertyr8   r<   r!   Ústaticmethodr9   rF   rK   rS   rG   rY   r]   r'   r'   r'   r(   r      s0    	
$



r   c                   @   s   e Zd ZdS )ÚGreenFileDescriptorIOWriteallN)r[   r^   r_   r'   r'   r'   r(   rb   Ú   s    rb   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚGreenOpenDescriptorc                 C   sp   |   ¡ rt| j| | jd}|S d}t | ¡}| ¡ }t|| |d}||_z|j|_W |S  t	y7   |`Y |S w )N)r$   F)
Zis_fdr   Z_fobjr$   r   Ú_do_open_rawr#   r:   r   ÚAttributeError)r"   Úfileior$   Úrawr#   r'   r'   r(   rd   á   s   ô

þþz GreenOpenDescriptor._do_open_rawc                 C   s<   ||us|   |¡sJ ‚|jtu rt|_|S t | ||¡}|S r-   )Z_raw_object_is_newÚ	__class__r   rb   r   Ú_make_atomic_write)r"   Úresultrg   r'   r'   r(   ri   ö   s   
ÿz&GreenOpenDescriptor._make_atomic_writeN)r[   r^   r_   rd   ri   r'   r'   r'   r(   rc   ß   s    rc   c                   @   s$   e Zd ZdZeZdd„ Zdd„ ZdS )ÚFileObjectPosixaª  
    FileObjectPosix()

    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.

    .. 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.
    .. 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.
    .. versionchanged:: 1.3a1
        On Python 2, enabling universal newlines no longer forces unicode
        IO.
    .. versionchanged:: 1.5
       The default value for *mode* was changed from ``rb`` to ``r``. This is consistent
       with :func:`open`, :func:`io.open`, and :class:`~.FileObjectThread`, which is the
       default ``FileObject`` on some platforms.
    .. versionchanged:: 1.5
       Stop forcing buffering. Previously, given a ``buffering=0`` argument,
       *buffering* would be set to 1, and ``buffering=1`` would be forced to
       the default buffer size. This was a workaround for a long-standing concurrency
       issue. Now the *buffering* argument is interpreted as intended.
    c                 O   s(   t |i |¤Ž}t | |¡ | ¡ | _d S r-   )rc   r   r   Z
opened_rawrf   )r"   rA   ÚkwargsZ
descriptorr'   r'   r(   r   J  s   zFileObjectPosix.__init__c                 C   s(   z|  ¡  | j  ¡  W d | _d S d | _w r-   )r!   rf   )r"   Zfobjr$   r'   r'   r(   Ú	_do_closeP  s   zFileObjectPosix._do_closeN)r[   r^   r_   Ú__doc__r   Zdefault_bufsizer   rm   r'   r'   r'   r(   rk     s
    Erk   )Ú
__future__r   r   r1   rU   r   r   r   r   r   r   Zgevent._compatr	   Zgevent._fileobjectcommonr
   r   r   r   Zgevent._hub_primitivesr   Z
gevent.hubr   Z	gevent.osr   r   r   r   r   rb   rc   rk   r'   r'   r'   r(   Ú<module>   s6     Aÿ#