3
sY]                 @   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dlZddddd	gZd
Ze	jZe	jZej ZeedrejZnejejdfdd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
    Nwin32z
win32 only
socketpairpipePopenPIPE
PipeHandlei    c       
      C   s  | t jkrd}n| t jkr d}ntd|t jkr:td|dkrJtdt j | ||}z|j|df |jd |j dd	 \}}t j | ||}yP|jd
 y|j	||f W n t
tfk
r   Y nX |jd |j \}}	W n   |j   Y nX W d|j  X ||fS )zA socket pair usable as a self-pipe, for Windows.

        Origin: https://gist.github.com/4325783, by Geert Jansen.
        Public domain.
        z	127.0.0.1z::1z?Only AF_INET and AF_INET6 socket address families are supportedz)Only SOCK_STREAM socket type is supportedr   zOnly protocol zero is supported   N   FT)socketAF_INETZAF_INET6
ValueErrorSOCK_STREAMZbindZlistenZgetsocknameZsetblockingZconnectBlockingIOErrorInterruptedErrorZacceptclose)
ZfamilytypeprotohostZlsockZaddrZportZcsockZssock_ r   &lib/python3.6/asyncio/windows_utils.pyr   %   s8    






FT)duplex
overlappedbufsizec             C   s"  t jdtj ttf 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j||tjd||tjtj}	tj||dtjtj|tj}
tj|	dd}|jd |	|
fS    |	dk	rtj|	 |
dk	rtj|
  Y nX dS )zELike os.pipe() but with overlapped support and using handles not fds.z\\.\pipe\python-pipe-%d-%d-)prefixr   r   NT)r   )tempfileZmktemp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ovr   r   r   r   S   s@    








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__   s    zPipeHandle.__init__c             C   s*   | j d k	rd| j  }nd}d| jj|f S )Nz	handle=%rclosedz<%s %s>)r#   	__class____name__)r$   r%   r   r   r   __repr__   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 operatioon on closed pipe)r#   r   )r$   r   r   r   fileno   s    
zPipeHandle.fileno)r!   c            C   s   | j d k	r|| j  d | _ d S )N)r#   )r$   r!   r   r   r   r      s    

zPipeHandle.closec             C   s*   | j d k	r&tjd|  t| d | j  d S )Nzunclosed %r)source)r#   warningswarnResourceWarningr   )r$   r   r   r   __del__   s    
zPipeHandle.__del__c             C   s   | S )Nr   )r$   r   r   r   	__enter__   s    zPipeHandle.__enter__c             C   s   | j   d S )N)r   )r$   tvtbr   r   r   __exit__   s    zPipeHandle.__exit__N)r)   
__module____qualname____doc__r&   r*   propertyr%   r+   r    r!   r   r0   r1   r5   r   r   r   r   r      s   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  |j d st|j dddks$td  } }}d  }	 }
}|tkrdtd	dd\}}	tj|tj}n|}|tkrtd
d\}
}tj|d}n|}|tkrtdd\}}tj|d}n|tkr|}n|}zy t	 j
|f|||d| W n8   x(|	|
|fD ]}|d k	 rtj|  qW  Y n>X |	d k	r6t|	| _|
d k	rJt|
| _|d k	r^t|| _W d |tkrvtj| |tkrtj| |tkrtj| X d S )NZuniversal_newlinesr   r   FT)r   r   )r   )stdinstdoutstderr)FT)TF)TF)getAssertionErrorr   r   msvcrtZopen_osfhandler   O_RDONLYSTDOUTsuperr&   r    r!   r   r:   r;   r<   r   )r$   argsr:   r;   r<   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)   r6   r7   r8   r&   __classcell__r   r   )r(   r   r      s   )TT)r8   sysplatformImportErrorr    	itertoolsr?   r   r
   
subprocessr   r-   __all__ZBUFSIZEr   rA   countr   hasattrr   r   r   r   r   r   r   r   r   r   <module>   s,   

.0-