
    i,                         U d dl mZ d dlZd dlZd dlZd dlmZmZ d dl	Z	ddl
mZ ddlmZmZ erej        dk    sJ dZd	ed
<    G d d          Ze G d de                      ZdS )    )annotationsN)TYPE_CHECKINGFinal   )Stream)ConflictDetectorfinalwin32i   	FinalTypeDEFAULT_RECEIVE_SIZEc                  R    e Zd ZU ded<   ddZedd            ZddZdd	Zdd
Z	dS )	_FdHolderintfdreturnNonec                    d| _         t          |t                    st          d          || _         t	          j        |          | _        t	          j        |d           d S )Nzfile descriptor must be an intF)r   
isinstancer   	TypeErrorosget_blocking_original_is_blockingset_blockingselfr   s     W/home/piyush/.hermes/hermes-agent/venv/lib/python3.11/site-packages/trio/_unix_pipes.py__init__z_FdHolder.__init__(   s]     "c"" 	><===%'_R%8%8"
E"""""    boolc                    | j         dk    S Nr   )r   r   s    r   closedz_FdHolder.closed3   s    w"}r   c                    | j         rd S | j        }d| _        t          j        || j                   t          j        |           d S r"   )r$   r   r   r   r   closer   s     r   
_raw_closez_FdHolder._raw_close7   sG     ; 	FW
D6777
r   c                .    |                                   d S N)r'   r#   s    r   __del__z_FdHolder.__del__F   s    r   c                    | j         s:t          j                            | j                   |                                  d S d S r)   )r$   triolowlevelnotify_closingr   r'   r#   s    r   r&   z_FdHolder.closeI   sD    { 	M((111OO	 	r   Nr   r   r   r   )r   r    r   r   )
__name__
__module____qualname____annotations__r   propertyr$   r'   r*   r&    r   r   r   r      s         $ GGG	# 	# 	# 	#    X           r   r   c                  L    e Zd ZdZddZdd	Zdd
ZdddZddZddZ	ddZ
dS )FdStreama  Represents a stream given the file descriptor to a pipe, TTY, etc.

    *fd* must refer to a file that is open for reading and/or writing and
    supports non-blocking I/O (pipes and TTYs will work, on-disk files probably
    not).  The returned stream takes ownership of the fd, so closing the stream
    will close the fd too.  As with `os.fdopen`, you should not directly use
    an fd after you have wrapped it in a stream using this function.

    To be used as a Trio stream, an open file must be placed in non-blocking
    mode.  Unfortunately, this impacts all I/O that goes through the
    underlying open file, including I/O that uses a different
    file descriptor than the one that was passed to Trio. If other threads
    or processes are using file descriptors that are related through `os.dup`
    or inheritance across `os.fork` to the one that Trio is using, they are
    unlikely to be prepared to have non-blocking I/O semantics suddenly
    thrust upon them.  For example, you can use
    ``FdStream(os.dup(sys.stdin.fileno()))`` to obtain a stream for reading
    from standard input, but it is only safe to do so with heavy caveats: your
    stdin must not be shared by any other processes, and you must not make any
    calls to synchronous methods of `sys.stdin` until the stream returned by
    `FdStream` is closed. See `issue #174
    <https://github.com/python-trio/trio/issues/174>`__ for a discussion of the
    challenges involved in relaxing this restriction.

    .. warning:: one specific consequence of non-blocking mode
      applying to the entire open file description is that when
      your program is run with multiple standard streams connected to
      a TTY (as in a terminal emulator), all of the streams become
      non-blocking when you construct an `FdStream` for any of them.
      For example, if you construct an `FdStream` for standard input,
      you might observe Python loggers begin to fail with
      `BlockingIOError`.

    Args:
      fd (int): The fd to be wrapped.

    Returns:
      A new `FdStream` object.
    r   r   r   r   c                ~    t          |          | _        t          d          | _        t          d          | _        d S )Nz*another task is using this stream for sendz-another task is using this stream for receive)r   
_fd_holderr   _send_conflict_detector_receive_conflict_detectorr   s     r   r   zFdStream.__init__y   sA    #B--'78(
 (
$ +;;+
 +
'''r   databytesc                  K   | j         5  | j        j        rt          j        d          t          j                                         d {V  t          |          }t          |          5 }d}||k     r||d          5 }	 |t          j
        | j        j        |          z  }n# t          $ r2 t          j                            | j        j                   d {V  Y nHt          $ r<}|j        t          j        k    rt          j        d          d t          j        |d }~ww xY wd d d            n# 1 swxY w Y   ||k     d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nfile was already closedr   )r;   r:   r$   r,   ClosedResourceErrorr-   
checkpointlen
memoryviewr   writer   BlockingIOErrorwait_writableOSErrorerrnoEBADFBrokenResourceError)r   r=   lengthviewsent	remaininges          r   send_allzFdStream.send_all   s     ) 	F 	F % J./HIII-**,,,,,,,,,YYFD!! FTVmmdee F	
F BHT_-?$K$KKDD. R R R"&-"="=do>P"Q"QQQQQQQQQQ& F F F w%+55&*&>$='" '"'+!, '+&>A EFF F F F F F F F F F F F F F F VmmF F F F F F F F F F F F F F F	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	Fs   A#E)-E D4"B%$D4%9D%	D4 	D%	)7D 	 D%	%D4(E4D88E;D8<	EE)E	E)E	E))E-0E-c                   K   | j         5  | j        j        rt          j        d          t          j                            | j        j                   d {V  d d d            d S # 1 swxY w Y   d S )Nr@   )r;   r:   r$   r,   rA   r-   rG   r   r#   s    r   wait_send_all_might_not_blockz&FdStream.wait_send_all_might_not_block   s      ) 	B 	B% J./HIII---do.@AAAAAAAAA	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	Bs   AA''A+.A+N	max_bytes
int | Nonec                f  K   | j         5  |t          }n9t          |t                    st	          d          |dk     rt          d          t          j                                         d {V  	 	 t          j
        | j        j        |          }n# t          $ r2 t          j                            | j        j                   d {V  Y nHt          $ r<}|j        t           j        k    rt          j        d          d t          j        |d }~ww xY w|cd d d            S # 1 swxY w Y   d S )Nzmax_bytes must be integer >= 1r   Tr@   )r<   r   r   r   r   
ValueErrorr,   r-   rB   r   readr:   r   rF   wait_readablerH   rI   rJ   rA   rK   )r   rT   r=   excs       r   receive_somezFdStream.receive_some   s     , 	 	 0		!)S11 F#$DEEEq==$%EFFF-**,,,,,,,,,74?#5yAAD  ' J J J-55do6HIIIIIIIIIII @ @ @yEK//"65 #$ #6C?@ 3	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	sB   A)D&4BD&9DD&	D7DDD&&D*-D*c                8    | j                                          d S r)   )r:   r&   r#   s    r   r&   zFdStream.close   s    r   c                z   K   |                                   t          j                                         d {V  d S r)   )r&   r,   r-   rB   r#   s    r   aclosezFdStream.aclose   s?      

m&&(((((((((((r   c                    | j         j        S r)   )r:   r   r#   s    r   filenozFdStream.fileno   s    !!r   r/   )r=   r>   r   r   r0   r)   )rT   rU   r   r>   )r   r   )r1   r2   r3   __doc__r   rQ   rS   r[   r&   r^   r`   r6   r   r   r8   r8   O   s        & &P
 
 
 
F F F F2B B B B    8       ) ) ) )" " " " " "r   r8   )
__future__r   rI   r   systypingr   r   r   r,   _abcr   _utilr   r	   platformr   r4   r   r8   r6   r   r   <module>rh      s   " " " " " " "  				 



 4 4 4 4 4 4 4 4        * * * * * * * * 3CLG3333 #(  ' ' ' '8 8 8 8 8 8 8 8v u" u" u" u" u"v u" u" u" u" u"r   