a
    xd                     @   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rBe	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ed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   H/var/www/html/Ranjet/env/lib/python3.9/site-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 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                   @   sb   e Zd ZdZedddZedddZdd	d
dZde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.

    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                    sB  | 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y   tj| jjI d H  Y nF ty } z.|jtjkrtdd ntj|W Y d }~n
d }~0 0 W d    qB1 s0    Y  qBW d    n1 s0    Y  W d    n1 s40    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_allN)returnc                    s   | j j | jjrtdztj| jjI d H  W n, ty` } ztj	|W Y d }~n
d }~0 0 W d    n1 sv0    Y  d S )Nr)   )
r&   r%   r   r   r*   r   r0   r	   BrokenPipeErrorr4   )r   r9   r   r   r   wait_send_all_might_not_block   s    
z&FdStream.wait_send_all_might_not_blockc                    s   | j  |d u rt}n"t|ts(td|dk r8tdtj I d H  zt	
| jj|}W q ty   tj| jjI d H  Y qH ty } z.|jtjkrtdd ntj|W Y d }~qHd }~0 0 qqH|W  d    S 1 s0    Y  d S )Nzmax_bytes must be integer >= 1r   r)   )r'   DEFAULT_RECEIVE_SIZEr   r   r   
ValueErrorr   r   r+   r   readr%   r	   r/   Zwait_readabler1   r2   r3   r*   r4   )r   Z	max_bytesr(   r9   r   r   r   receive_some   s,    
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)N)r    r!   r"   __doc__r   r   bytesr:   r=   rA   r   rB   rC   r   r   r   r   r$   L   s    	r$   )	metaclass)r   r2   _abcr   Z_utilr   r   r   nameImportErrorr>   r   r$   r   r   r   r   <module>   s   
9