
    iC2                     l   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e	          Z G d dee          Z G d de          Z G 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                       e Zd ZdZdZdZdZdZddZd Z	d Z
d Zd Zd	 Zed
             Zd Zd Zed             Zd Zd Zd Zd ZddZd ZdS )GreenFileDescriptorIONFTc                 x   t          j        |            || _        || _        || _        |j        | _        t          |           |j        }|j	        }t                      | _        | j        j        j        }	 |r ||d          | _        |r ||d          | _        d S d S #  |                                   xY w)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_watchers          B/usr/local/lib/python3.11/dist-packages/gevent/_fileobjectposix.pyr   zGreenFileDescriptorIO.__init__*   s    4   	#/	   "+",99X]%
	 ;%/Z%:%:" <&0j&;&;###< <	 JJLLLs   8&B" "B9c                     t          | j        dd          }	 |                                |                                 S # |                                 w xY w)NrF)r   r   isattyr(   )r)   fs     r0   r3   zGreenFileDescriptorIO.isattyN   sF    4<e,,	88::GGIIIIAGGIIIIs   A   Ac                     | j         d uS N)r&   r)   s    r0   r-   zGreenFileDescriptorIO.readableV   s    !--    c                     | j         d uS r6   )r'   r7   s    r0   r.   zGreenFileDescriptorIO.writableY   s    "$..r8   c                     | j         E	 t          j        | j        dt          j                   d| _         n# t
          $ r
 d| _         Y nw xY w| j         S )Nr   TF)	_seekableoslseekr   SEEK_CUROSErrorr7   s    r0   seekablezGreenFileDescriptorIO.seekable\   sd    >!&q"+666 "&  ' ' '!&' ~s   %6 A
	A
c                     | j         S r6   )r   r7   s    r0   r*   zGreenFileDescriptorIO.filenof   s
    |r8   c                     | j         S r6   )_closedr7   s    r0   closedzGreenFileDescriptorIO.closedi   s
    |r8   c                     | j         }| j        }| j        }d x| _        x| _         | _        |                    ||ft          | j        | j        | j        | j                   d S r6   )	r&   r'   r#   cancel_waits_close_and_thenr   $_GreenFileDescriptorIO__finish_closer   r   _keep_alive)r)   
read_eventwrite_eventr#   s       r0   __destroy_eventsz&GreenFileDescriptorIO.__destroy_eventsm   ss    '
)h>BBB4%(;''%ML	
 	
 	
 	
 	
r8   c                     | j         rd S |                                  d| _         	 |                                  d x| _        | _        d S # d x| _        | _        w xY w)NT)rC   flush&_GreenFileDescriptorIO__destroy_eventsr   rH   r7   s    r0   r(   zGreenFileDescriptorIO.close|   sn    < 	F

 	3!!###.22DL4+++d2DL4+2222s   A
 
Ac                     	 | rt          |           t          |d          r|                                 d S d S # t          |d          r|                                 w w xY w)Nr(   )r   hasattrr(   )r,   r*   
keep_alives      r0   __finish_closez$GreenFileDescriptorIO.__finish_close   s}    	# vz7++ #  """""# #wz7++ #  """"#s	   ; 'A"c                     | j         t          d          	 	 t          | j        |          S # t          $ r}|j        d         t          vr Y d }~nd }~ww xY wt          | j         d d | j                   _)Nreadr   r   )	r&   r	   r   r   r?   argsr   r   r#   )r)   nexs      r0   __readzGreenFileDescriptorIO.__read   s    %&v...	FT\1---   71:^33 43333 D.dDHEEE	F   . 
AAAc                     t                      }	 	 |                     t                    }n# t          $ r d }Y nw xY w|sn|                    |           G|                                S r6   )r   _GreenFileDescriptorIO__readr   r   writegetvalue)r)   retdatas      r0   readallzGreenFileDescriptorIO.readall   s    ii
	{{#677!    	
  IIdOOO
	 ||~~s   , ;;c                    |                      t          |                    }t          |          }	 ||d |<   nM# t          $ r@}dd l}t	          ||j                  s||                    d|          |d |<   Y d }~nd }~ww xY w|S )Nr      b)r[   len	TypeErrorarray
isinstance)r)   br_   rV   errre   s         r0   readintozGreenFileDescriptorIO.readinto   s    {{3q66""II	,AbqbEE 	, 	, 	,LLLa-- 	KKd++AbqbEEEEEE		,
 s   ; 
B6B  Bc                     | j         t          d          	 	 t          | j        |          S # t          $ r}|j        d         t          vr Y d }~nd }~ww xY wt          | j         d d | j                   _)Nr\   Tr   )	r'   r	   r   r   r?   rU   r   r   r#   )r)   rg   rW   s      r0   r\   zGreenFileDescriptorIO.write   s    &&w///	GdlA...   71:^33 43333 D/tTXFFF	GrY   r   c                     	 t          j        | j        ||          S # t          $ r  t          $ rD}t          t          t          |j         t          j                    d                    Y d }~d S d }~ww xY w)Nr   )	r<   r=   r   IOErrorr?   r
   rU   sysexc_info)r)   offsetwhencerW   s       r0   seekzGreenFileDescriptorIO.seek   s    		C8DL&&999 	 	 	 	C 	C 	C
 GWbg.q0ABBBBBBBBB	Cs    A59A00A5c                 f    dt          |           j        t          |           | j        | j        fz  S )Nz<%s at 0x%x fileno=%s mode=%r>)type__name__idr   r    r7   s    r0   __repr__zGreenFileDescriptorIO.__repr__   s0    /JJD4<3
 
 	
r8   )T)r   )rt   
__module____qualname__r&   r'   rC   r;   rH   r   r3   r-   r.   r@   r*   propertyrD   rN   r(   staticmethodrG   r[   r`   ri   r\   rq   rv    r8   r0   r   r      sL        MNGIK" " " "H  . . ./ / /       X
 
 

3 
3 
3 # # \#	F 	F 	F  
 
 
	G 	G 	G
C 
C 
C 
C
 
 
 
 
r8   r   c                       e Zd ZdS )GreenFileDescriptorIOWriteallN)rt   rw   rx   r{   r8   r0   r}   r}      s        Dr8   r}   c                       e Zd Zd Zd ZdS )GreenOpenDescriptorc                 .   |                                  rt          | j        | | j                  }ncd}t	          j        |           }|                                }t          || |          }||_        	 |j        |_        n# t          $ r |`Y nw xY w|S )N)r,   F)
is_fdr   _fobjr,   r   _do_open_rawr*   rH   r   AttributeError)r)   fileior,   rawr*   s        r0   r   z GreenOpenDescriptor._do_open_raw   s    ::<< 	 *4:tT\RRRFF
 G -d33CZZ\\F*64IIIF!$F !h!      KKK s   6B BBc                     ||us|                      |          sJ |j        t          u rt          |_        nt	          j        | ||          }|S r6   )_raw_object_is_new	__class__r   r}   r   _make_atomic_write)r)   resultr   s      r0   r   z&GreenOpenDescriptor._make_atomic_write   s[     S  D$;$;C$@$@   444<F#6tVSIIFr8   N)rt   rw   rx   r   r   r{   r8   r0   r   r      s2          *	 	 	 	 	r8   r   c                   "    e Zd ZdZeZd Z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                 z    t          |i |}t          j        | |           |                                | _        d S r6   )r   r   r   
opened_rawr   )r)   rU   kwargs
descriptors       r0   r   zFileObjectPosix.__init__K  s>    ($9&99
j111 ++--r8   c                     	 |                                  | j                                          d | _        d S # d | _        w xY wr6   )r(   r   )r)   fobjr,   s      r0   	_do_closezFileObjectPosix._do_closeQ  sH    	JJLLL KDKKK$DKs	   -8 	AN)rt   rw   rx   __doc__r   default_bufsizer   r   r{   r8   r0   r   r     sF        C CJ *O. . .    r8   r   ) 
__future__r   r   r<   rm   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   r}   r   r   r{   r8   r0   <module>r      s9   & & & & & & % % % % % % 				 



       " " " " " "             # # # # # # " " " " " " 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 9 9 9 9 9 9 2 2 2 2 2 2                         $ $ $ $ $ $ & & & & & &}
 }
 }
 }
 }
I }
 }
 }
@	 	 	 	 	$8$9	 	 	
         .      FU U U U Un U U U U Ur8   