o
    U˟iC2                     @   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 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)_close)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namefileio_modemoder   can_read	can_writer   hubloopio_read_watcher_write_watcherclose)selffilenoopen_descriptorclosefdreadablewritable
io_watcher r-   _/var/www/apps/myagent/mysuperagent/venv/lib/python3.10/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.   r0   N   s   zGreenFileDescriptorIO.isattyc                 C   
   | j d uS N)r#   r&   r-   r-   r.   r*   V      
zGreenFileDescriptorIO.readablec                 C   r2   r3   )r$   r4   r-   r-   r.   r+   Y   r5   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OSErrorr4   r-   r-   r.   seekable\   s   
zGreenFileDescriptorIO.seekablec                 C      | j S r3   )r   r4   r-   r-   r.   r'   f   s   zGreenFileDescriptorIO.filenoc                 C   r<   r3   )_closedr4   r-   r-   r.   closedi   s   zGreenFileDescriptorIO.closedc                 C   sH   | j }| j}| j}d  | _ | _ | _|||ft| j| j| j| j d S r3   )	r#   r$   r    cancel_waits_close_and_thenr
   $_GreenFileDescriptorIO__finish_closer   r   _keep_alive)r&   
read_eventwrite_eventr    r-   r-   r.   __destroy_eventsm   s   z&GreenFileDescriptorIO.__destroy_eventsc              	   C   sB   | j rd S |   d| _ z|   W d  | _| _d S d  | _| _w )NT)r=   flush&_GreenFileDescriptorIO__destroy_eventsr   rA   r4   r-   r-   r.   r%   |   s   
zGreenFileDescriptorIO.closec              	   C   s@   z| rt | W t|dr|  d S d S t|dr|  w w )Nr%   )r   hasattrr%   )r)   r'   
keep_aliver-   r-   r.   __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   r:   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r3   )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)rQ   len	TypeErrorarray
isinstance)r&   brU   rM   errrZ   r-   r-   r.   readinto   s   zGreenFileDescriptorIO.readintoc              
   C   rJ   )NrR   Tr   )	r$   r   r   r   r:   rL   r   r   r    )r&   r\   rN   r-   r-   r.   rR      rP   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   )	r7   r8   r   IOErrorr:   r	   rL   sysexc_info)r&   offsetwhencerN   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   r4   r-   r-   r.   __repr__   s   zGreenFileDescriptorIO.__repr__)T)r   )rf   
__module____qualname__r#   r$   r=   r6   rA   r   r0   r*   r+   r;   r'   propertyr>   rF   r%   staticmethodr@   rQ   rV   r^   rR   rd   rh   r-   r-   r-   r.   r      s0    	
$



r   c                   @   s   e Zd ZdS )GreenFileDescriptorIOWriteallN)rf   ri   rj   r-   r-   r-   r.   rm      s    rm   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)
is_fdr   _fobjr)   r   _do_open_rawr'   rA   r   AttributeError)r&   fileior)   rawr'   r-   r-   r.   rq      s   

z GreenOpenDescriptor._do_open_rawc                 C   s<   ||us|  |sJ |jtu rt|_|S t| ||}|S r3   )_raw_object_is_new	__class__r   rm   r   _make_atomic_write)r&   resultrt   r-   r-   r.   rw      s   
z&GreenOpenDescriptor._make_atomic_writeN)rf   ri   rj   rq   rw   r-   r-   r-   r.   rn      s    rn   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 r3   )rn   r   r   
opened_rawrs   )r&   rL   kwargs
descriptorr-   r-   r.   r   K  s   zFileObjectPosix.__init__c                 C   s(   z|   | j   W d | _d S d | _w r3   )r%   rs   )r&   fobjr)   r-   r-   r.   	_do_closeQ  s   zFileObjectPosix._do_closeN)rf   ri   rj   __doc__r   default_bufsizer   r~   r-   r-   r-   r.   ry     s
    Ery   ) 
__future__r   r   r7   r`   r"   r   r   r   r   r   gevent._compatr	   gevent._fileobjectcommonr
   r   r   r   gevent._hub_primitivesr   
gevent.hubr   	gevent.osr   r   r   r   r   r   rm   rn   ry   r-   r-   r-   r.   <module>   s8     A#