
    i'_                     >   d Z ddlmZmZmZ 	 ddlmZ n# e$ r dZY nw xY 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e          Z G d de          Z G d dej                  Z G d dej                  Z G d de          Z G d dej                  Z G d de          Z  G d de ej                  Z! G d de          Z" G d de          Z# G d d e          Z$ G d! d"e$          Z% G 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 ZdS )cancel_wait_exc                 H    t                               | t          d           d S )Nz.File descriptor was closed in another greenletIOError__init__r   selfs    C/usr/local/lib/python3.11/dist-packages/gevent/_fileobjectcommon.pyr   zcancel_wait_ex.__init__   s2    C	E 	E 	E 	E 	E    N__name__
__module____qualname__r    r   r   r   r      s(        E E E E Er   r   c                       e Zd Zd ZdS )FileObjectClosedc                 H    t                               | t          d           d S )Nz+Bad file descriptor (FileObject was closed)r   r   s    r   r   zFileObjectClosed.__init__   s2    @	B 	B 	B 	B 	Br   Nr   r   r   r   r   r      s(        B B B B Br   r   c                   :    e Zd ZdZdZd Zd Zd	dZd Z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                 N    t           j                            | |dd |           d S )Nlatin-1)encodingnewlineline_buffering)ioTextIOWrapperr   )r   fobjr&   s      r   r   z%UniversalNewlineBytesWrapper.__init__,   s=    
!!$y*.1? 	" 	A 	A 	A 	A 	Ar   c                 `    t          j        j        | g|R i |}|                    d          S Nr#   )r'   r(   readencode)r   argskwargsresults       r   r,   z!UniversalNewlineBytesWrapper.read2   s7    !&t=d===f==}}Y'''r   c                 l    t           j                            | |          }|                    d          S r+   )r'   r(   readliner-   )r   limitr0   s      r   r3   z%UniversalNewlineBytesWrapper.readline6   s,    !**477}}Y'''r   c                     | S Nr   r   s    r   __iter__z%UniversalNewlineBytesWrapper.__iter__:   s	     r   c                 @    |                                  }|st          |S r6   r3   StopIterationr   lines     r   __next__z%UniversalNewlineBytesWrapper.__next__@   "    }} 	 r   )r1   )r   r   r   __doc__moder   r,   r3   r7   r=   nextr   r   r   r!   r!   $   sy          DA A A( ( (( ( ( (     DDDr   r!   c                       e Zd Zd ZdS )FlushingBufferedWriterc                 n    t           j                            | |          }|                                  |S r6   )r'   BufferedWriterwriteflush)r   brets      r   rF   zFlushingBufferedWriter.writeK   s,    %%dA..


r   Nr   r   r   rF   r   r   r   rC   rC   I   s#            r   rC   c                        e Zd Z fdZ xZS )WriteallMixinc                     t          t          |           j        }t          |          }|r-t          |          } ||          }||k    rn||d         }|-|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)superrL   rF   len)r   valuerF   totallw	__class__s         r   writeallzWriteallMixin.writeallS   sl     mT**0E

 	E

AeAAvv!""IE  	 r   )r   r   r   rU   __classcell__)rT   s   @r   rL   rL   Q   s8                r   rL   c                       e Zd ZdZdZdS )FileIOz8A subclass that we can dynamically assign __class__ for.r   N)r   r   r   r?   	__slots__r   r   r   rX   rX   j   s        BBIIIr   rX   c                       e Zd Zd ZdS )WriteIsWriteallMixinc                 ,    |                      |          S r6   )rU   )r   rP   s     r   rF   zWriteIsWriteallMixin.writeq   s    }}U###r   NrJ   r   r   r   r[   r[   o   s#        $ $ $ $ $r   r[   c                       e Zd ZdS )WriteallFileION)r   r   r   r   r   r   r^   r^   u   s        Dr   r^   c                       e Zd ZdZed             Z	 	 	 	 ddZej        Z	dZ
dZd Zd Zd	 Zd
 Zd Zed             Zed             Zd Zd Z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           
      X    ||t          d| d|d|d|          |||S ||n|S )NzCannot specify both =z and )	TypeError)	pref_namepreferred_valold_nameold_valdefaults        r   _collapse_argzOpenDescriptor._collapse_arg   s`    
 $)<)		===''     W_N - 9}}wFr   rNFc                    |                      d|	d|d          }	~|                      d|d|d          }~t          |d          sxt          |t                    st	          |          }t          |t
          t          ft          z             st          d|z            t          |t
          t          f          rd}	t          |t
                    st          d	|z            t          |t                    st          d
|z            |'t          |t
                    st          d|z            |'t          |t
                    st          d|z            t          |          }|t          d          z
  s t          |          t          |          k    rt          d	|z            d|v }d|v }d|v }d|v }d|v }d|v }d|v }d|v }|p|p|p|}|r|rt          d          d}|r|rt          d          ||z   |z   |z   dk    rt          d          |s|s|s|st          d          |r|t          d          |r|t          d          |r|t          d          |r&|dk    r dd l}|                    dt          d            || _        |rdpd!|rdpd!z   |rdpd!z   |rdpd!z   |rdpd!z   | _        | j        |rdnd!z   |rdnd!z   | _        || _        || _        || _        || _        || _        || _        || _        || _        |p|| _        | j         o| j         o| o| | _        || _        || _        || _        || _        || _        |	| _         |
| _!        d S )"NclosefdcloseT	bufferingbufsizer1   filenozinvalid file: %rzinvalid mode: %rzinvalid buffering: %rzinvalid encoding: %rzinvalid errors: %rzaxrwb+tUxrj   rS   a+trH   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    )"ri   hasattr
isinstancer	   r   strbytesrc   setrO   
ValueErrorwarningswarnRuntimeWarning_fobjfileio_moder@   creatingreadingwriting	appendingupdatingtextbinary	can_writecan_readnative	universalrn   r$   errorsr%   rl   atomic_write)r   r)   r@   ro   rm   r$   r   r%   rn   rl   r   modesr   r   r   r   r   r   r   r   r   r   s                         r   r   zOpenDescriptor.__init__   sj    $$Y%NN&&{Iy'SUVV	tX&& 	dM22 $d||dS%L=$@AA ; 2T 9:::$e-- $$$ 	7.5666)]33 	A3i?@@@
8S(A(A2X=>>>j&=&=069:::D		3z??" 	8c$ii#e**&<&</$6777%<,,5L	%<e|5L	@@9@	 	 Y !WXXX G 	HF 	HFGGGg')3a77HIII 	PG 	Pw 	P) 	PNOOO 	Nh*LMMM 	Lf(JKKK 	Lg)JKKK 	-i1nnOOOMM G(!- - - 
##3$"&3$"&  S&B( C%2	' 	 $t(;<v@USUV	 " 	"+8	M ,$+o ,,%+ 	 #" (r   c                 6    t          | j        t                    S r6   )rz   r   r	   r   s    r   is_fdzOpenDescriptor.is_fd  s    $*m444r   c                     | j         J|                                 }	 |                     |          | _         n#  |                                  xY w| j         S )z9
        Return the :meth:`wrapped` file object.
        )_opened
opened_raw_OpenDescriptor__wrappedrm   r   raws     r   openedzOpenDescriptor.opened  sX     <//##C#~~c22 		|s	   8 Ac                     | j         |uS r6   r   r   s     r   _raw_object_is_newz!OpenDescriptor._raw_object_is_new  s    z$$r   c                 P    | j         |                                 | _         | j         S r6   )_opened_raw_do_open_rawr   s    r   r   zOpenDescriptor.opened_raw  s(    ##0022Dr   c                 z    t          | j        d          r| j        S t          | j        | j        | j                  S )Nrp   )ry   r   rX   r   rl   r   s    r   r   zOpenDescriptor._do_open_raw"  s9    4:x(( 	:
 dj$"2DLAAAr   c                     t          | t          j        t          j        f          pt	          | d          o| j        d uS )Nbuffer)rz   r'   BufferedIOBase
TextIOBasery   r   )streams    r   is_bufferedzOpenDescriptor.is_buffered+  s?     v 12=ABB I))Gfm4.G	
r   c                     | j         }	 t          j        |                                          j        }|dk    r|}n# t
          t          f$ r Y nw xY w|S )Nrv   )default_buffer_sizeosfstatrp   
st_blksizeOSErrorAttributeError)clsr   r0   bss       r   buffer_size_for_streamz%OpenDescriptor.buffer_size_for_stream3  sh    (	&--//**5B Avv	 ( 	 	 	D	
 s   += AAc                    | j         rt          j        }nM| j        s| j        s| j        rt          j        }n+| j        rt          j        }nt          d| j
        z            	  |||          }n# t          $ r |}Y nw xY w|S )Nzunknown mode: %r)r   r'   BufferedRandomr   r   r   rE   r   BufferedReaderr~   r@   r   )r   r   rn   Bufferr0   s        r   
__bufferedzOpenDescriptor.__buffered?  s    = 	=&FF] 	=dl 	=dn 	=&FF\ 	=&FF/$);<<<	VFI..FF 	 	 	 FFF	
 s   #A0 0A?>A?c                     ||us|                      |          r-|j        t          u rt          |_        nt	          d|z            |S )Nz[Don't know how to make %s have atomic write. Please open a gevent issue with your use-case.)r   rT   rX   r^   NotImplementedError)r   r0   r   s      r   _make_atomic_writez!OpenDescriptor._make_atomic_writeR  sb      7 7 < <6))#1  )E   r   c                     | j         r)t          |t          j                  rt	          d          |}| j        }d}|dk    s|dk     r|                                rd}d}|dk     r|                     |          }|dk     rt	          d          |dk    r+|                     |          s| 	                    ||          }| j         sAt          |t          j                  s't          j
        || j        | j        | j        |          }||us|                     |          r%	 | j        |_        n# t           t"          f$ r Y nw xY w| j        r@|                     |          s+t          |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 streamFrv   r   r1   Tzinvalid buffering size)r   rz   r'   r   r~   rn   isattyr   r   _OpenDescriptor__bufferedr(   r$   r   r%   r   r@   r   rc   r   r[   r   )r   r   r0   rn   r&   s        r   	__wrappedzOpenDescriptor.__wrappedl  s   
 ; 	U:c2=99 	U STTTN	>>Y]]szz||]I!Nq==33F;;Iq==5666>>$"2"26":":> __VY77F{ 	: c2=11 : )&$-dl*8: :  7 7 < <"i"I.     !	:((00	: #6+?@@	: ,,VS99Fs   #D0 0EE)	rj   NNNNNNNF)r   r   r   r?   staticmethodri   r   r'   DEFAULT_BUFFER_SIZEr   r   r   r   r   r   r   r   r   classmethodr   r   r   r   r   r   r   r`   r`   y   s*        & G G \G <@59)-#f) f) f) f)P 0GK5 5 5  % % %     
B B B 
 
 \
 	 	 [	  &  49 9 9 9 9r   r`   c                   (    e Zd ZdZd Zd Zd ZeZdS )	_ClosedIO)namec                 B    	 |j         | _         d S # t          $ r Y d S w xY wr6   )r   r   )r   io_objs     r   r   z_ClosedIO.__init__  s6    	DIII 	 	 	DD	s    
c                 *    |dk    rt           t          )Nr   )r   r   r   r   s     r   __getattr__z_ClosedIO.__getattr__  s    6>>  r   c                     dS )NFr   r   s    r   __bool__z_ClosedIO.__bool__  s    ur   N)r   r   r   rY   r   r   r   __nonzero__r   r   r   r   r     sG        
 I      KKKr   r   c                       e Zd ZdZdZdZd Z ed d           Zd Z	d Z
ed	             Zd
 Zd Zd Zd Zd Zd Zd Zd Zd ZeZd ZeZdS )FileObjectBasez
    Internal base class to ensure a level of consistency
    between :class:`~.FileObjectPosix`, :class:`~.FileObjectThread`
    and :class:`~.FileObjectBlock`.
    )rG   rp   writablereadableseekseekabletellr,   r3   	readlinesread1readintorF   rU   
writelinestruncateNc                 x    |                                 | _        |j        | _        |                                  d S r6   )r   _iorl   _close_do_delegate_methods)r   
descriptors     r   r   zFileObjectBase.__init__  s9    $$&& !(!!#####r   c                     | j         S r6   )r   )ss    r   <lambda>zFileObjectBase.<lambda>  s    AE r   c                 L    t          | d|          p|                                 S )Nr   )setattrr   )r   nvs     r   r   zFileObjectBase.<lambda>  s#    5" 5 5 Q9O9O9Q9Q r   c                    | j         D ]}t          | j        |d           }t          t	          |           |          }|r'|s%t          | ||                     |                     ^t          | |          r|st          | |           d S r6   )_delegate_methodsgetattrr   ry   typer   _wrap_methoddelattr)r   	meth_namemethimplemented_by_classs       r   r   z#FileObjectBase._do_delegate_methods  s    / 	) 	)I48Y55D#*4::y#A#A  )0 )i):):4)@)@AAAAy)) )2F )i(((	) 	)r   c                     |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   methods     r   r   zFileObjectBase._wrap_method  s	    
 r   c                 6    t          | j        t                    S )zTrue if the file is closed)rz   r   r   r   s    r   closedzFileObjectBase.closed  s     $(I...r   c                 d   t          | j        t                    rd S | j        }t          | j                  | _        	 |                     || j                   d }| j        }| j        D ]}|                    |d            d S # d }| j        }| j        D ]}|                    |d            w xY wr6   )rz   r   r   	_do_closer   __dict__r   pop)r   r)   dr   s       r   rm   zFileObjectBase.close  s    dh	** 	FxTX&&	'NN4---D A!3 ' '	i&&&&' '	 D A!3 ' '	i&&&&'s   B +B/c                     t                      r6   )r   r   r)   rl   s      r   r   zFileObjectBase._do_close  s    !###r   c                 ,    t          | j        |          S r6   )r   r   r   s     r   r   zFileObjectBase.__getattr__   s    tx&&&r   c                     d| j         j        t          |           | j        rdnd| j        |                                 fz  S )Nz<%s at 0x%x %s_fobj=%r%s>r   rx   )rT   r   idr   r'   _extra_reprr   s    r   __repr__zFileObjectBase.__repr__#  sG    *N#tHH+HHG.
 
 	
r   c                     dS )Nrx   r   r   s    r   r   zFileObjectBase._extra_repr,  s    rr   c                     | S r6   r   r   s    r   	__enter__zFileObjectBase.__enter__/      r   c                 .    |                                   d S r6   rm   )r   r.   s     r   __exit__zFileObjectBase.__exit__2      

r   c                     | S r6   r   r   s    r   r7   zFileObjectBase.__iter__5  r   r   c                 @    |                                  }|st          |S r6   r9   r;   s     r   r=   zFileObjectBase.__next__8  r>   r   c                     dS )NTr   r   s    r   r   zFileObjectBase.__bool__@  s    tr   )r   r   r   r?   r   r   r   propertyr'   r   r   r   rm   r   r   r   r   r   r  r7   r=   rA   r   r   r   r   r   r   r     s5        8 C$ $ $ 
// RQ
S 
SB) ) )   / / X/' ' ' $ $ $' ' '
 
 
           D   KKKr   r   c                       e Zd ZdZd Zd ZdS )FileObjectBlockz
    FileObjectBlock()

    A simple synchronous wrapper around a file object.

    Adds no concurrency or gevent compatibility.
    c                 \    t          |g|R i |}t                              | |           d S r6   )r`   r   r   )r   r)   r.   r/   r   s        r   r   zFileObjectBlock.__init__O  s;    #D:4:::6::
j11111r   c                 .    |                                  d S r6   r  r   s      r   r   zFileObjectBlock._do_closeS  r  r   N)r   r   r   r?   r   r   r   r   r   r	  r	  F  s<         2 2 2    r   r	  c                   0    e Zd ZdZd Zd Zd Zd Z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                    |                     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                  z            |
                                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   r`   get_hubr  r  r   r   ry   rc   r   r   _FileObjectThread__io_holderr   r   )r   r.   r/   r  r  r   s         r   r   zFileObjectThread.__init__g  s     zz&$''ZZd33
#T4V44
$<		(<	9!DII 	)&((DIty+.. 	YETYWXXX&--//0j11111r   c                 f   d | j         d<   	 | j        5  | j                            |j                   d d d            n# 1 swxY w Y   |r.|fd}~| j                            |          }~|rt          |  d S d S d S # |r-|fd}~| j                            |          }~|rt          |  w w w xY w)Nr   c                 ~    	 |                                   n#  t          j                    cY d } S xY w	 d } d S # d } w xY wr6   )rm   sysexc_infor   s    r   rm   z)FileObjectThread._do_close.<locals>.close  sR    %."|~~-- $ &
 !%s    8 08 8 <)r  r  r  applyrG   r
   )r   r)   rl   rm   r  s        r   r   zFileObjectThread._do_close  sZ   "	' 2 2%%dj1112 2 2 2 2 2 2 2 2 2 2 2 2 2 2  ' !% % % % % ?0077 'X&&&&-' '*' '+  ' !% % % % % ?0077 'X&&&-'*'s+   A>  ?A> AA> AA> >2B0c                 X    t                               |            | j        | j        d<   d S Nr   )r   r   r   r  r   s    r   r   z%FileObjectThread._do_delegate_methods  s+    ++D111"hr   c                     d| j         S )Nz threadpool=)r  r   s    r   r   zFileObjectThread._extra_repr  s     #'??44r   c                 |    | j         | j        | j        t          j                  fd            }|S )Nc                      d         t           5                      | |          cd d d            S # 1 swxY w Y   d S r  )r   r  )r.   r/   	io_holderr  r   r  s     r   thread_methodz4FileObjectThread._wrap_method.<locals>.thread_method  s    |# '& > >!''f==> > > > > > > > > > > > > > > > > >s   7;;)r  r  r  	functoolswraps)r   r   r  r  r  r  s    ` @@@r   r   zFileObjectThread._wrap_method  sc     $	y_
		 	 	> 	> 	> 	> 	> 	> 	> 
!	 	> r   N)	r   r   r   r?   r   r   r   r   r   r   r   r   r  r  W  si         2 2 28' ' '<' ' '5 5 5    r   r  )'r?   
__future__r   r   r   errnor   ImportErrorr'   r  r  r   
gevent.hubr   r  gevent._compatr	   r
   r   gevent.lockr   r   r   r   r   r(   r!   rE   rC   objectrL   rX   r[   r^   r`   r   r   r	  r  r   r   r   <module>r(     s]    A @ @ @ @ @ @ @ @ @   EEE 
			     



 				 1 1 1 1 1 1 ( ( ( ( ( ( " " " " " " ! ! ! ! ! ! 1 1 1 1 1 1 1 1E E E E EW E E EB B B B Bw B B B" " " " "2#3 " " "J    R.       F   2    RY   
$ $ $ $ $= $ $ $	 	 	 	 	)29 	 	 	l l l l lV l l l^	       0C C C C CV C C CL    n   "b b b b b~ b b b b bs    