U
    g                     @  s   d dl mZ d dlZd dlmZ ddlmZ ddlmZm	Z	 ddl
mZmZmZ ddlmZmZ ejd	ksrerrtd
ZG dd dZeG dd de	ZeG dd deZdS )    )annotationsN)TYPE_CHECKING   )_core)ReceiveStream
SendStream)_handlekernel32raise_winerror)ConflictDetectorfinalwin32i   c                   @  sJ   e Zd ZdddddZedddd	Zddd
dZddddZdS )_HandleHolderintNonehandlereturnc                 C  s.   d| _ t|tstd|| _ t| j  d S )Nzhandle must be an int)r   
isinstancer   	TypeErrorr   Zregister_with_iocpselfr    r   7/tmp/pip-unpacked-wheel-ks04xdmi/trio/_windows_pipes.py__init__   s
    
z_HandleHolder.__init__boolr   c                 C  s
   | j dkS Nr   )r   r   r   r   r   closed   s    z_HandleHolder.closedc                 C  s.   | j r
d S | j}d| _tt|s*t  d S r   )r    r   r	   ZCloseHandler   r
   r   r   r   r   close   s    z_HandleHolder.closec                 C  s   |    d S N)r!   r   r   r   r   __del__'   s    z_HandleHolder.__del__N)__name__
__module____qualname__r   propertyr    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ZddddZdS )PipeSendStreamzdRepresents a send stream over a Windows named pipe that has been
    opened in OVERLAPPED mode.
    r   r   r   c                 C  s   t || _td| _d S Nz)another task is currently using this piper   _handle_holderr   _conflict_detectorr   r   r   r   r   1   s    
zPipeSendStream.__init__bytes)datar   c                   s   | j  | jjrtd|s:t I d H  W 5 Q R  d S zt| jj|I d H }W n* tk
r~ } ztj	|W 5 d }~X Y nX |t
|kstW 5 Q R X d S )Nthis pipe is already closed)r,   r+   r    r   ClosedResourceError
checkpointZwrite_overlappedr   BrokenPipeErrorZBrokenResourceErrorlenAssertionError)r   r.   Zwrittenexr   r   r   send_all7   s    
zPipeSendStream.send_allr   c              	     s6   | j & | jjrtdt I d H  W 5 Q R X d S )NzThis pipe is already closed)r,   r+   r    r   r0   r1   r   r   r   r   wait_send_all_might_not_blockI   s    
z,PipeSendStream.wait_send_all_might_not_blockc                 C  s   | j   d S r"   r+   r!   r   r   r   r   r!   Q   s    zPipeSendStream.closec                   s   |    t I d H  d S r"   r!   r   r1   r   r   r   r   acloseT   s    zPipeSendStream.acloseN)	r$   r%   r&   __doc__r   r6   r7   r!   r:   r   r   r   r   r(   +   s   r(   c                   @  sN   e Zd ZdZdddddZddd	d
ddZddddZddddZdS )PipeReceiveStreamz3Represents a receive stream over an os.pipe object.r   r   r   c                 C  s   t || _td| _d S r)   r*   r   r   r   r   r   ]   s    
zPipeReceiveStream.__init__Nz
int | Noner-   )	max_bytesr   c              
     s   | j  | jjrtd|d kr(t}n"t|ts:td|dk rJt	dt
|}zt| jj|I d H }W nB tk
r   | jjrtdd t I d H  Y W 5 Q R  dS X ||d = |W  5 Q R  S W 5 Q R X d S )Nr/   zmax_bytes must be integer >= 1r   zanother task closed this pipe    )r,   r+   r    r   r0   DEFAULT_RECEIVE_SIZEr   r   r   
ValueError	bytearrayZreadinto_overlappedr   r2   r1   )r   r=   buffersizer   r   r   receive_somec   s4    


zPipeReceiveStream.receive_somer   c                 C  s   | j   d S r"   r8   r   r   r   r   r!      s    zPipeReceiveStream.closec                   s   |    t I d H  d S r"   r9   r   r   r   r   r:      s    zPipeReceiveStream.aclose)N)r$   r%   r&   r;   r   rD   r!   r:   r   r   r   r   r<   Y   s
   (r<   )
__future__r   systypingr    r   _abcr   r   Z_core._windows_cffir   r	   r
   Z_utilr   r   platformr4   r?   r   r(   r<   r   r   r   r   <module>   s   -