o
    8e                     @  s   U d dl mZ d dlZd dlZd dlZd dlmZ d dlZddlm	Z	 ddl
mZmZ er3d dlmZ er<ejdks<J ejd	krCed
Zded< G dd dZeG dd de	ZdS )    )annotationsN)TYPE_CHECKING   )Stream)ConflictDetectorfinal)Finalwin32posixi   	FinalTypeDEFAULT_RECEIVE_SIZEc                   @  sL   e Zd ZU ded< dddZeddd	Zdd
dZdddZdddZ	dS )	_FdHolderintfdreturnNonec                 C  s:   d| _ t|tstd|| _ t|| _t|d d S )Nzfile descriptor must be an intF)r   
isinstancer   	TypeErrorosget_blocking_original_is_blockingset_blockingselfr    r   4usr/lib/python3.10/site-packages/trio/_unix_pipes.py__init__0   s   
z_FdHolder.__init__boolc                 C  s
   | j dkS Nr   )r   r   r   r   r   closed;   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_close?   s   z_FdHolder._raw_closec                 C  s   |    d S N)r#   r    r   r   r   __del__N   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"   Q   s   z_FdHolder.closeNr   r   r   r   )r   r   r   r   )
__name__
__module____qualname____annotations__r   propertyr!   r#   r%   r"   r   r   r   r   r      s   
 


r   c                   @  sX   e Zd ZdZdddZdd
dZdddZddddZdddZdddZ	d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   r   r   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   y   s   

zFdStream.__init__databytesc                   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   )r1   r0   r!   r&   ClosedResourceErrorr'   
checkpointlen
memoryviewr   writer   BlockingIOErrorwait_writableOSErrorerrnoEBADFBrokenResourceError)r   r3   lengthviewsent	remaininger   r   r   send_all   s@   

"zFdStream.send_allc                   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 )Nr5   )
r1   r0   r!   r&   r6   r'   r<   r   BrokenPipeErrorr@   )r   rE   r   r   r   wait_send_all_might_not_block   s   
"z&FdStream.wait_send_all_might_not_blockN	max_bytes
int | Nonec                   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   Tr5   )r2   r   r   r   r   
ValueErrorr&   r'   r7   r   readr0   r   r;   Zwait_readabler=   r>   r?   r6   r@   )r   rI   r3   rE   r   r   r   receive_some   s8   
$zFdStream.receive_somec                 C  s   | j   d S r$   )r0   r"   r    r   r   r   r"      s   zFdStream.closec                   s   |    tj I d H  d S r$   )r"   r&   r'   r7   r    r   r   r   aclose   s   zFdStream.aclosec                 C  s   | j jS r$   )r0   r   r    r   r   r   fileno   s   zFdStream.filenor(   )r3   r4   r   r   r)   r$   )rI   rJ   r   r4   )r   r   )r*   r+   r,   __doc__r   rF   rH   rM   r"   rN   rO   r   r   r   r   r/   W   s    
 
	


r/   )
__future__r   r>   r   systypingr   r&   _abcr   Z_utilr   r   r   r   platformnameImportErrorr   r-   r   r/   r   r   r   r   <module>   s"    
;