o
    0׾g'_                     @   s~  d Z ddlmZmZmZ zddlmZ W n ey   dZY nw ddlZddl	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mZ G dd deZG dd deZG dd dejZG dd dejZG dd deZG dd dejZG dd deZ G dd de ej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 )%z
gevent internals.
    )absolute_importprint_functiondivision)EBADF	   N)_get_hub_noargs)integer_types)reraise)fspath)	SemaphoreDummySemaphorec                   @      e Zd Zdd ZdS )cancel_wait_exc                 C      t | td d S )Nz.File descriptor was closed in another greenletIOError__init__r   self r   b/var/www/html/backend_erp/backend_erp_env/lib/python3.10/site-packages/gevent/_fileobjectcommon.pyr         zcancel_wait_ex.__init__N__name__
__module____qualname__r   r   r   r   r   r          r   c                   @   r   )FileObjectClosedc                 C   r   )Nz+Bad file descriptor (FileObject was closed)r   r   r   r   r   r      r   zFileObjectClosed.__init__Nr   r   r   r   r   r      r   r   c                   @   sB   e Zd ZdZdZdd Zdd Zddd	Zd
d Zdd Z	e	Z
dS )UniversalNewlineBytesWrapperz
    Uses TextWrapper to decode universal newlines, but returns the
    results as bytes.

    This is for Python 2 where the 'rU' mode did that.
    Nc                 C   s   t jj| |dd |d d S )Nlatin-1)encodingnewlineline_buffering)ioTextIOWrapperr   )r   fobjr"   r   r   r   r   ,   s   
z%UniversalNewlineBytesWrapper.__init__c                 O   s$   t jj| g|R i |}|dS Nr   )r#   r$   readencode)r   argskwargsresultr   r   r   r'   2   s   
z!UniversalNewlineBytesWrapper.readc                 C   s   t j| |}|dS r&   )r#   r$   readliner(   )r   limitr+   r   r   r   r-   6   s   
z%UniversalNewlineBytesWrapper.readlinec                 C      | S Nr   r   r   r   r   __iter__:   s   z%UniversalNewlineBytesWrapper.__iter__c                 C      |   }|st|S r0   r-   StopIterationr   liner   r   r   __next__@      z%UniversalNewlineBytesWrapper.__next__)r,   )r   r   r   __doc__moder   r'   r-   r1   r7   nextr   r   r   r   r   $   s    
r   c                   @   r   )FlushingBufferedWriterc                 C   s   t j| |}|   |S r0   )r#   BufferedWriterwriteflush)r   bretr   r   r   r>   K   s   zFlushingBufferedWriter.writeNr   r   r   r>   r   r   r   r   r<   I   r   r<   c                       s   e Zd Z fddZ  ZS )WriteallMixinc                    sJ   t t| j}t|}|r#t|}||}||kr	 |S ||d }|s|S )z
        Similar to :meth:`socket.socket.sendall`, ensures that all the contents of
        *value* have been written (though not necessarily flushed) before returning.

        Returns the length of *value*.

        .. versionadded:: 20.12.0
        N)superrC   r>   len)r   valuer>   totallw	__class__r   r   writeallS   s   zWriteallMixin.writeall)r   r   r   rL   __classcell__r   r   rJ   r   rC   Q   s    rC   c                   @   s   e Zd ZdZdZdS )FileIOz8A subclass that we can dynamically assign __class__ for.r   N)r   r   r   r9   	__slots__r   r   r   r   rN   j   s    rN   c                   @   r   )WriteIsWriteallMixinc                 C   s
   |  |S r0   )rL   )r   rF   r   r   r   r>   q      
zWriteIsWriteallMixin.writeNrB   r   r   r   r   rP   o   r   rP   c                   @   s   e Zd ZdS )WriteallFileION)r   r   r   r   r   r   r   rR   u   s    rR   c                   @   s   e Zd ZdZedd Z				dddZejZ	dZ
dZd	d
 Zdd Zdd Zdd Zdd Zedd Zedd Zdd Zdd Zdd ZdS )OpenDescriptora_  
    Interprets the arguments to `open`. Internal use only.

    Originally based on code in the stdlib's _pyio.py (Python implementation of
    the :mod:`io` module), but modified for gevent:

    - Native strings are returned on Python 2 when neither
      'b' nor 't' are in the mode string and no encoding is specified.
    - Universal newlines work in that mode.
    - Allows externally unbuffered text IO.

    :keyword bool atomic_write: If true, then if the opened, wrapped, stream
        is unbuffered (meaning that ``write`` can produce short writes and the return
        value needs to be checked), then the implementation will be adjusted so that
        ``write`` behaves like Python 2 on a built-in file object and writes the
        entire value. Only set this on Python 2; the only intended user is
        :class:`gevent.subprocess.Popen`.
    c                 C   sH   |d ur|d urt d| |||f |d u r|d u r|S |d ur"|S |S )Nz#Cannot specify both %s=%s and %s=%s)	TypeError)	pref_namepreferred_valold_nameold_valdefaultr   r   r   _collapse_arg   s   zOpenDescriptor._collapse_argrNFc                 C   s   |  d|	d|d}	~|  d|d|d}~t|ds:t|ts"t|}t|ttft s1td| t|ttfr:d}	t|tsEtd	| t|tsPtd
| |d ur_t|ts_td| |d urnt|tsntd| t|}|td st	|t	|krt
d	| d|v }d|v }d|v }d|v }d|v }d|v }d|v }d|v }|p|p|p|}|r|rt
dd}|r|rt
d|| | | dkrt
d|s|s|s|st
d|r|d urt
d|r|d urt
d|r|d urt
d|r|dkrdd l}|dtd  || _|rdpd!|rdpd! |r#dp$d! |r+dp,d! |r3dp4d! | _| j|r>dnd! |rEdnd! | _|| _|| _|| _|| _|| _|| _|| _|| _|pe|| _| j ow| j ow| ow| | _|| _|| _|| _|| _|| _|	| _ |
| _!d S )"NclosefdcloseT	bufferingbufsizer,   filenozinvalid file: %rzinvalid mode: %rzinvalid buffering: %rzinvalid encoding: %rzinvalid errors: %rzaxrwb+tUxr[   rI   a+tr@   Uz4mode U cannot be combined with 'x', 'w', 'a', or '+'z'can't have text and binary mode at once   z)can't have read/write/append mode at oncez/must have exactly one of read/write/append modez-binary mode doesn't take an encoding argumentz+binary mode doesn't take an errors argumentz+binary mode doesn't take a newline argumentr   zaline buffering (buffering=1) isn't supported in binary mode, the default buffer size will be used    )"rZ   hasattr
isinstancer   r
   strbytesrT   setrE   
ValueErrorwarningswarnRuntimeWarning_fobjfileio_moder:   creatingreadingwriting	appendingupdatingtextbinary	can_writecan_readnative	universalr^   r    errorsr!   r\   atomic_write)r   r%   r:   r_   r]   r    r   r!   r^   r\   r   modesrt   ru   rv   rw   rx   ry   rz   r~   r{   ro   r   r   r   r      s   



$
zOpenDescriptor.__init__c                 C      t | jtS r0   )rj   rr   r   r   r   r   r   is_fd     zOpenDescriptor.is_fdc                 C   s>   | j du r|  }z
| || _ W | j S    |   | j S )z9
        Return the :meth:`wrapped` file object.
        N)_opened
opened_raw_OpenDescriptor__wrappedr]   r   rawr   r   r   opened  s   
zOpenDescriptor.openedc                 C   s
   | j |uS r0   rr   r   r   r   r   _raw_object_is_new  rQ   z!OpenDescriptor._raw_object_is_newc                 C   s   | j d u r
|  | _ | j S r0   )_opened_raw_do_open_rawr   r   r   r   r     s   

zOpenDescriptor.opened_rawc                 C   s$   t | jdr	| jS t| j| j| jS )Nr`   )ri   rr   rN   rs   r\   r   r   r   r   r   "  s   zOpenDescriptor._do_open_rawc                 C   s&   t | tjtjfpt| do| jd uS )Nbuffer)rj   r#   BufferedIOBase
TextIOBaseri   r   )streamr   r   r   is_buffered+  s   zOpenDescriptor.is_bufferedc              	   C   sD   | j }z
t| j}W n ttfy   Y |S w |dkr |}|S )Nrf   )default_buffer_sizeosfstatr`   
st_blksizeOSErrorAttributeError)clsr   r+   bsr   r   r   buffer_size_for_stream3  s   z%OpenDescriptor.buffer_size_for_streamc                 C   sn   | j rtj}n| js| js| jrtj}n| jrtj}nt	d| j
 z|||}W |S  ty6   |}Y |S w )Nzunknown mode: %r)rx   r#   BufferedRandomrt   rv   rw   r=   ru   BufferedReaderrn   r:   r   )r   r   r^   Bufferr+   r   r   r   
__buffered?  s   zOpenDescriptor.__bufferedc                 C   s6   ||us	|  |r|jtu rt|_|S td| |S )Nz[Don't know how to make %s have atomic write. Please open a gevent issue with your use-case.)r   rK   rN   rR   NotImplementedError)r   r+   r   r   r   r   _make_atomic_writeR  s   
z!OpenDescriptor._make_atomic_writec              	   C   s  | j rt|tjrtd|}| j}d}|dks |dk r$| r$d}d}|dk r-| |}|dk r5td|dkrD| |sD| 	||}| j sYt|tjsYt
|| j| j| j|}||usb| |rtz| j|_W n ttfys   Y nw | jr| |st|ts| ||}|S )z
        Wraps the raw IO object (`RawIOBase` or `io.TextIOBase`) in
        buffers, text decoding, and newline handling.
        z4Unable to perform binary IO on top of text IO streamFrf   r   r,   Tzinvalid buffering size)rz   rj   r#   r   rn   r^   isattyr   r   _OpenDescriptor__bufferedr$   r    r   r!   r   r:   r   rT   r   rP   r   )r   r   r+   r^   r"   r   r   r   	__wrappedl  sB   
zOpenDescriptor.__wrapped)	r[   NNNNNNNF)r   r   r   r9   staticmethodrZ   r   r#   DEFAULT_BUFFER_SIZEr   r   r   r   r   r   r   r   r   classmethodr   r   r   r   r   r   r   r   rS   y   s0    

h	

rS   c                   @   s,   e Zd ZdZdd Zdd Zdd ZeZdS )		_ClosedIO)namec                 C   s$   z|j | _ W d S  ty   Y d S w r0   )r   r   )r   io_objr   r   r   r     s
   z_ClosedIO.__init__c                 C   s   |dkrt t)Nr   )r   r   r   r   r   r   r   __getattr__  s   z_ClosedIO.__getattr__c                 C      dS )NFr   r   r   r   r   __bool__     z_ClosedIO.__bool__N)r   r   r   rO   r   r   r   __nonzero__r   r   r   r   r     s    r   c                   @   s   e Zd ZdZdZdZdd Zedd 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d Zdd  ZeZd!d" ZeZdS )#FileObjectBasez
    Internal base class to ensure a level of consistency
    between :class:`~.FileObjectPosix`, :class:`~.FileObjectThread`
    and :class:`~.FileObjectBlock`.
    )r?   r`   writablereadableseekseekabletellr'   r-   	readlinesread1readintor>   rL   
writelinestruncateNc                 C   s   |  | _|j| _|   d S r0   )r   _ior\   _close_do_delegate_methods)r   
descriptorr   r   r   r     s   
zFileObjectBase.__init__c                 C   s   | j S r0   )r   )sr   r   r   <lambda>  s    zFileObjectBase.<lambda>c                 C   s   t | d|p	|  S )Nr   )setattrr   )r   nvr   r   r   r     s    c                 C   s`   | j D ]*}t| j|d }tt| |}|r!|s!t| || | qt| |r-|s-t| | qd S r0   )_delegate_methodsgetattrr   ri   typer   _wrap_methoddelattr)r   	meth_namemeth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   r     s   zFileObjectBase._wrap_methodc                 C   r   )zTrue if the file is closed)rj   r   r   r   r   r   r   closed  s   zFileObjectBase.closedc                 C   s~   t | jtrd S | j}t| j| _z| || j W d }| j}| jD ]}||d  q"d S d }| j}| jD ]}||d  q5w r0   )rj   r   r   	_do_closer   __dict__r   pop)r   r%   dr   r   r   r   r]     s    

zFileObjectBase.closec                 C   s   t  r0   )r   r   r%   r\   r   r   r   r     s   zFileObjectBase._do_closec                 C   s   t | j|S r0   )r   r   r   r   r   r   r      r   zFileObjectBase.__getattr__c                 C   s*   d| j jt| | jrdnd| j|  f S )Nz<%s at 0x%x %s_fobj=%r%s>r   rh   )rK   r   idr   r#   _extra_reprr   r   r   r   __repr__#  s   zFileObjectBase.__repr__c                 C   r   )Nrh   r   r   r   r   r   r   ,  r   zFileObjectBase._extra_reprc                 C   r/   r0   r   r   r   r   r   	__enter__/  r   zFileObjectBase.__enter__c                 G   s   |    d S r0   r]   )r   r)   r   r   r   __exit__2  r   zFileObjectBase.__exit__c                 C   r/   r0   r   r   r   r   r   r1   5  r   zFileObjectBase.__iter__c                 C   r2   r0   r3   r5   r   r   r   r7   8  r8   zFileObjectBase.__next__c                 C   r   )NTr   r   r   r   r   r   @  r   zFileObjectBase.__bool__)r   r   r   r9   r   r   r   propertyr#   r   r   r   r]   r   r   r   r   r   r   r1   r7   r;   r   r   r   r   r   r   r     s0    		
	r   c                   @   s    e Zd ZdZdd Zdd ZdS )FileObjectBlockz
    FileObjectBlock()

    A simple synchronous wrapper around a file object.

    Adds no concurrency or gevent compatibility.
    c                 O   s&   t |g|R i |}t| | d S r0   )rS   r   r   )r   r%   r)   r*   r   r   r   r   r   O  s   zFileObjectBlock.__init__c                 C   s   |   d S r0   r   r   r   r   r   r   S  r   zFileObjectBlock._do_closeN)r   r   r   r9   r   r   r   r   r   r   r   F  s    r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )FileObjectThreada  
    FileObjectThread()

    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.
    c                 O   s   | dd}| dd}t|i |}|pt j| _|| _| jdu r't | _n| js.t | _t| jds=tdt	| j |
 g| _t| | dS )a  
        :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 closefd: If True (the default) then when this object is closed,
           the underlying object is closed as well. If *fobj* is a path, then
           *closefd* must be True.
        lockT
threadpoolNr   z'Expected a Semaphore or boolean, got %r)r   rS   get_hubr   r   r   r   ri   rT   r   r   _FileObjectThread__io_holderr   r   )r   r)   r*   r   r   r   r   r   r   r   g  s   

zFileObjectThread.__init__c              	   C   s   d | j d< z7| j | j|j W d    n1 sw   Y  W |r9|fdd}~| j|}~|r;t|  d S d S d S |rT|fdd}~| j|}~|rUt|  w w w )Nr   c                 S   s8   zz|    W n   t  Y W d } S W d } d S d } w r0   )r]   sysexc_infor   r   r   r   r]     s   z)FileObjectThread._do_close.<locals>.close)r   r   r   applyr?   r	   )r   r%   r\   r]   r   r   r   r   r     s0   

zFileObjectThread._do_closec                 C   s   t |  | j| jd< d S Nr   )r   r   r   r   r   r   r   r   r     s   
z%FileObjectThread._do_delegate_methodsc                 C   s   d| j f S )Nz threadpool=%r)r   r   r   r   r   r     r   zFileObjectThread._extra_reprc                    s2   | j  | j| jt fdd}|S )Nc                     sF    d d u rt  | |W  d    S 1 sw   Y  d S r   )r   r   )r)   r*   	io_holderr   r   r   r   r   thread_method  s
   $z4FileObjectThread._wrap_method.<locals>.thread_method)r   r   r   	functoolswraps)r   r   r   r   r   r   r     s   	zFileObjectThread._wrap_methodN)	r   r   r   r9   r   r   r   r   r   r   r   r   r   r   W  s    r   )'r9   
__future__r   r   r   errnor   ImportErrorr#   r   r   r   
gevent.hubr   r   gevent._compatr   r	   r
   gevent.lockr   r   r   r   r   r$   r   r=   r<   objectrC   rN   rP   rR   rS   r   r   r   r   r   r   r   r   <module>   s@    %  1 