U
    g                      @  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rfd dlmZ erxejdksxtejd	kre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                   @  sb   e Zd ZU ded< dddddZeddd	d
ZddddZddddZddddZ	dS )	_FdHolderintfdNoner   returnc                 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   4/tmp/pip-unpacked-wheel-ks04xdmi/trio/_unix_pipes.py__init__0   s    
z_FdHolder.__init__boolr   c                 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 r&   )r#   triolowlevelZnotify_closingr   r%   r"   r   r   r   r$   Q   s    z_FdHolder.closeN)
__name__
__module____qualname____annotations__r   propertyr#   r%   r'   r$   r   r   r   r   r      s   
r   c                   @  sz   e Zd ZdZdddddZdddd	d
ZddddZddddddZddddZddddZ	d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   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__bytes)datar   c                   s   | j  | jjrtdtj I d H  t|}t|}d}||k r||d  }z|t	
| jj|7 }W nl tk
r   tj| jjI d H  Y nD tk
r } z&|jtjkrtdd ntj|W 5 d }~X Y nX W 5 Q R X q@W 5 Q R X W 5 Q R X d S )Nfile was already closedr   )r1   r0   r#   r(   ClosedResourceErrorr)   
checkpointlen
memoryviewr   writer   BlockingIOErrorwait_writableOSErrorerrnoEBADFBrokenResourceError)r   r4   lengthviewsent	remaininger   r   r   send_all   s*    

zFdStream.send_allr    c                   sn   | j ^ | jjrtdztj| jjI d H  W n* tk
r^ } ztj	|W 5 d }~X Y nX W 5 Q R X 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_blockNz
int | None)	max_bytesr   c                   s   | j  |d krt}n"t|ts(td|dk r8tdtj I d H  zt	
| jj|}W q tk
r   tj| jjI d H  Y qH tk
r } z&|jtjkrtdd ntj|W 5 d }~X Y qHX qqH|W  5 Q R  S Q R X d S )Nzmax_bytes must be integer >= 1r   r5   )r2   r   r   r   r   
ValueErrorr(   r)   r7   r   readr0   r   r;   Zwait_readabler=   r>   r?   r6   r@   )r   rI   r4   excr   r   r   receive_some   s,    
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.fileno)N)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AssertionErrornameImportErrorr   r-   r   r/   r   r   r   r   <module>   s"    
;