o
    0׾g(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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,   a/var/www/html/backend_erp/backend_erp_env/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-   r/   M   s   zGreenFileDescriptorIO.isattyc                 C   
   | j d uS N)r"   r%   r,   r,   r-   r)   U      
zGreenFileDescriptorIO.readablec                 C   r1   r2   )r#   r3   r,   r,   r-   r*   X   r4   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OSErrorr3   r,   r,   r-   seekable[   s   
zGreenFileDescriptorIO.seekablec                 C      | j S r2   )r   r3   r,   r,   r-   r&   e   s   zGreenFileDescriptorIO.filenoc                 C   r;   r2   )_closedr3   r,   r,   r-   closedh   s   zGreenFileDescriptorIO.closedc                 C   sH   | j }| j}| j}d  | _ | _ | _|||ft| j| j| j| j d S r2   )	r"   r#   r   cancel_waits_close_and_thenr
   $_GreenFileDescriptorIO__finish_closer   r   _keep_alive)r%   
read_eventwrite_eventr   r,   r,   r-   __destroy_eventsl   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   r@   r3   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$   )r6   r$   hasattr)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   r9   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r2   )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)rP   len	TypeErrorarray
isinstance)r%   brT   rL   errrY   r,   r,   r-   readinto   s   zGreenFileDescriptorIO.readintoc              
   C   rI   )NrQ   Tr   )	r#   r   r   r   r9   rK   r   r   r   )r%   r[   rM   r,   r,   r-   rQ      rO   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   )	r6   r7   r   IOErrorr9   r	   rK   sysexc_info)r%   offsetwhencerM   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   r3   r,   r,   r-   __repr__   s   zGreenFileDescriptorIO.__repr__)T)r   )re   
__module____qualname__r"   r#   r<   r5   r@   r   r/   r)   r*   r:   r&   propertyr=   rE   r$   staticmethodr?   rP   rU   r]   rQ   rc   rg   r,   r,   r,   r-   r      s0    	
$



r   c                   @   s   e Zd ZdS )GreenFileDescriptorIOWriteallN)re   rh   ri   r,   r,   r,   r-   rl      s    rl   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&   r@   r   AttributeError)r%   fileior(   rawr&   r,   r,   r-   rp      s   

z GreenOpenDescriptor._do_open_rawc                 C   s<   ||us|  |sJ |jtu rt|_|S t| ||}|S r2   )_raw_object_is_new	__class__r   rl   r   _make_atomic_write)r%   resultrs   r,   r,   r-   rv      s   
z&GreenOpenDescriptor._make_atomic_writeN)re   rh   ri   rp   rv   r,   r,   r,   r-   rm      s    rm   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 r2   )rm   r   r   
opened_rawrr   )r%   rK   kwargs
descriptorr,   r,   r-   r   J  s   zFileObjectPosix.__init__c                 C   s(   z|   | j   W d | _d S d | _w r2   )r$   rr   )r%   fobjr(   r,   r,   r-   	_do_closeP  s   zFileObjectPosix._do_closeN)re   rh   ri   __doc__r   default_bufsizer   r}   r,   r,   r,   r-   rx     s
    Erx   )
__future__r   r   r6   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   rl   rm   rx   r,   r,   r,   r-   <module>   s6     A#