a
    (ñ»j9!  ã                   @  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sxJ ‚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Úwin32i   Ú	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 )Néÿÿÿÿzfile descriptor must be an intF)r   Ú
isinstancer   Ú	TypeErrorÚosZget_blockingÚ_original_is_blockingÚset_blocking©Úselfr   © r   ú6E:\soccer\ml_env\Lib\site-packages\trio/_unix_pipes.pyÚ__init__+   s    
z_FdHolder.__init__Úbool©r   c                 C  s
   | j dkS ©Nr   )r   ©r   r   r   r   Úclosed6   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__I   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"   L   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.

    .. 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   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   |   s    
ÿÿzFdStream.__init__Úbytes)Údatar   c                 Ã  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rÀt d¡d ‚ntj|‚W Y d }~n
d }~0 0 W d   ƒ qB1 sò0    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   r2   ÚlengthÚviewÚsentÚ	remainingÚer   r   r   Úsend_all…   s*    


ÿþzFdStream.send_allr   c                 Ã  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 )Nr3   )
r/   r.   r!   r&   r4   r'   r:   r   ÚBrokenPipeErrorr>   )r   rC   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 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r¬t d¡d ‚ntj|‚W Y d }~qHd }~0 0 qÎqH|W  d   ƒ S 1 sæ0    Y  d S )Nzmax_bytes must be integer >= 1r   r3   )r0   r   r   r   r   Ú
ValueErrorr&   r'   r5   r   Úreadr.   r   r9   Zwait_readabler;   r<   r=   r4   r>   )r   rG   r2   Úexcr   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'   r5   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   rD   rF   rK   r"   rL   rM   r   r   r   r   r-   R   s   (	r-   )Ú
__future__r   r<   r   ÚsysÚtypingr   r&   Ú_abcr   Z_utilr   r   r   r
   Úplatformr   r+   r   r-   r   r   r   r   Ú<module>   s   ;