o
    US`c                     @   sl   d dl Z d dlZddlmZ ddlmZmZ d dlZe jdkr!e	dZ
G dd dZG d	d
 d
eedZdS )    N   )Stream)ConflictDetectorFinalposixi   c                   @   s>   e Zd ZdefddZedd Zdd Zdd	 Zd
d Z	dS )	_FdHolderfdc                 C   s:   d| _ t|tstd|| _ t|| _t|d d S )Nzfile descriptor must be an intF)r   
isinstanceint	TypeErrorosget_blocking_original_is_blockingset_blockingselfr    r   ;/usr/local/lib/python3.10/dist-packages/trio/_unix_pipes.py__init__%   s   
z_FdHolder.__init__c                 C   s
   | j dkS Nr	   )r   r   r   r   r   closed0   s   
z_FdHolder.closedc                 C   s2   | j rd S | j}d| _t|| j t| d S r   )r   r   r   r   r   closer   r   r   r   
_raw_close4   s   z_FdHolder._raw_closec                 C   s   |    d S N)r   r   r   r   r   __del__C   s   z_FdHolder.__del__c                 C   s$   | j stj| j |   d S d S r   )r   triolowlevelZnotify_closingr   r   r   r   r   r   r   F   s   z_FdHolder.closeN)
__name__
__module____qualname__r   r   propertyr   r   r   r   r   r   r   r   r      s    
r   c                   @   s^   e Zd ZdZdefddZdefddZdd
dZddef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.

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

    Returns:
      A new `FdStream` object.
    r   c                 C   s"   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   r   r   r   r   m   s   

zFdStream.__init__datac                    sH  | j  | jjrtdtj I d H  t|}t|d}d}||k r{||d  D}z|t	
| jj|7 }W n/ tyL   tj| jjI d H  Y n tyg } z|jtjkr_tdd tj|d }~ww W d    n1 srw   Y  ||k s%W d    n1 sw   Y  W d    d S W d    d S 1 sw   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er   r   r   send_allv   s@   

"zFdStream.send_allreturnNc                    s|   | j 0 | jjrtdztj| jjI d H  W n ty+ } ztj	|d }~ww W d    d S 1 s7w   Y  d S )Nr(   )
r%   r$   r   r   r)   r   r/   r   BrokenPipeErrorr3   )r   r8   r   r   r   wait_send_all_might_not_block   s   
"z&FdStream.wait_send_all_might_not_blockc                    s   | j g |d u rt}nt|tstd|dk rtdtj I d H  	 z
t	
| jj|}W n/ tyD   tj| jjI d H  Y n ty_ } z|jtjkrWtdd tj|d }~ww nq&|W  d    S 1 snw   Y  d S )Nzmax_bytes must be integer >= 1r   Tr(   )r&   DEFAULT_RECEIVE_SIZEr
   r   r   
ValueErrorr   r   r*   r   readr$   r   r.   Zwait_readabler0   r1   r2   r)   r3   )r   Z	max_bytesr'   r8   r   r   r   receive_some   s8   
$zFdStream.receive_somec                 C   s   | j   d S r   )r$   r   r   r   r   r   r      s   zFdStream.closec                    s   |    tj I d H  d S r   )r   r   r   r*   r   r   r   r   aclose   s   zFdStream.aclosec                 C   s   | j jS r   )r$   r   r   r   r   r   fileno   s   zFdStream.fileno)r:   Nr   )r   r    r!   __doc__r   r   bytesr9   r<   r@   r   rA   rB   r   r   r   r   r#   L   s     	
r#   )	metaclass)r   r1   _abcr   Z_utilr   r   r   nameImportErrorr=   r   r#   r   r   r   r   <module>   s    
9