B
     S]                 @   s   d Z ddlZejdkredddlZddlZddlZddlZddlZddl	Z	ddl
Z
dZdZejZejZe Zdded	d
dZG dd dZG dd dejZdS )z)Various Windows specific bits and pieces.    NZwin32z
win32 only)pipePopenPIPE
PipeHandlei    F)TT)duplex
overlappedbufsizec          
   C   s"  t jdt ttd}| r>tj}tj	tj
B }|| }}ntj}tj
}d| }}|tjO }|d rp|tjO }|d rtj}nd}d }	}
yZt||tjd||tjtj}	t||dtjtj|tj}
tj|	dd}|d |	|
fS    |	dk	rt|	 |
dk	rt|
  Y nX dS )zELike os.pipe() but with overlapped support and using handles not fds.z\\.\pipe\python-pipe-{:d}-{:d}-)prefixr      NT)r   )tempfileZmktempformatosgetpidnext_mmap_counter_winapiZPIPE_ACCESS_DUPLEXZGENERIC_READZGENERIC_WRITEZPIPE_ACCESS_INBOUNDZFILE_FLAG_FIRST_PIPE_INSTANCEZFILE_FLAG_OVERLAPPEDZCreateNamedPipeZ	PIPE_WAITZNMPWAIT_WAIT_FOREVERZNULLZ
CreateFileZOPEN_EXISTINGZConnectNamedPipeZGetOverlappedResultCloseHandle)r   r   r   ZaddressZopenmodeaccessZobsizeZibsizeZflags_and_attribsZh1Zh2Zov r   &lib/python3.7/asyncio/windows_utils.pyr       sB    








r   c               @   s\   e Zd ZdZdd Zdd Zedd Zdd	 Ze	j
d
ddZdd Zdd Zdd ZdS )r   zWrapper for an overlapped pipe handle which is vaguely file-object like.

    The IOCP event loop can use these instead of socket objects.
    c             C   s
   || _ d S )N)_handle)selfhandler   r   r   __init__V   s    zPipeHandle.__init__c             C   s2   | j d k	rd| j }nd}d| jj d| dS )Nzhandle=closed< >)r   	__class____name__)r   r   r   r   r   __repr__Y   s    
zPipeHandle.__repr__c             C   s   | j S )N)r   )r   r   r   r   r   `   s    zPipeHandle.handlec             C   s   | j d krtd| j S )NzI/O operation on closed pipe)r   
ValueError)r   r   r   r   filenod   s    
zPipeHandle.fileno)r   c            C   s   | j d k	r|| j  d | _ d S )N)r   )r   r   r   r   r   closei   s    

zPipeHandle.closec             C   s,   | j d k	r(tjd| t| d |   d S )Nz	unclosed )source)r   warningswarnResourceWarningr#   )r   r   r   r   __del__n   s    
zPipeHandle.__del__c             C   s   | S )Nr   )r   r   r   r   	__enter__t   s    zPipeHandle.__enter__c             C   s   |    d S )N)r#   )r   tvtbr   r   r   __exit__w   s    zPipeHandle.__exit__N)r   
__module____qualname____doc__r   r    propertyr   r"   r   r   r#   r(   r)   r-   r   r   r   r   r   Q   s   r   c                   s"   e Zd ZdZd fdd	Z  ZS )r   zReplacement for subprocess.Popen using overlapped pipe handles.

    The stdin, stdout, stderr are None or instances of PipeHandle.
    Nc          	      s  | drt| dddks"td  } }}d  }	 }
}|tkrbtddd\}}	t|tj}n|}|tkrtdd\}
}t|d}n|}|tkrtdd\}}t|d}n|tkr|}n|}zy t	 j
|f|||d	| W n4   x$|	|
|fD ]}|d k	rt| qW  Y n>X |	d k	r0t|	| _|
d k	rDt|
| _|d k	rXt|| _W d |tkrpt| |tkrt| |tkrt| X d S )
NZuniversal_newlinesr   r   )FTT)r   r   )TF)r   )stdinstdoutstderr)getAssertionErrorr   r   msvcrtZopen_osfhandler   O_RDONLYSTDOUTsuperr   r   r   r   r2   r3   r4   r#   )r   argsr2   r3   r4   kwdsZ	stdin_rfdZ
stdout_wfdZ
stderr_wfdZstdin_whZ	stdout_rhZ	stderr_rhZstdin_rhZ	stdout_whZ	stderr_whh)r   r   r   r      sL    









zPopen.__init__)NNN)r   r.   r/   r0   r   __classcell__r   r   )r   r   r   ~   s   r   )r0   sysplatformImportErrorr   	itertoolsr7   r   
subprocessr   r%   __all__ZBUFSIZEr   r9   countr   r   r   r   r   r   r   r   <module>   s$   
1-