B
    +d                 @   s   d 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yddlm	Z	m
Z
 W n$ ek
rt   ddl	m	Z	m
Z
 Y nX ddlmZmZ ddlmZ ddlmZ G dd	 d	eZdS )
zJProvides an interface like pexpect.spawn interface using subprocess.Popen
    N)QueueEmpty   )	SpawnBasePY3)EOF)string_typesc            	       sl   e Zd Zd fdd	ZdZdd	 Zd
d Zdd Zdd Zdd Z	dddZ
dd Zdd Zdd Z  ZS )
PopenSpawn     Nstrictc          	      s  t t| j||||||	d |d kr4tjd| _n| tj| _tdt	j
t	jt	j
||
|d}tjdkrt	 }| jt	jO  _||d< t	j|d< t|trtjdkrtj|tjdkd	}t	j|f|| _| jj| _d
| _|  | _t | _tj| j d| _!| j!"d | j!#  d S )N)timeoutmaxreadsearchwindowsizelogfileencodingcodec_errorsasciir   )bufsizestdinstderrstdoutcwd
preexec_fnenvwin32startupinfocreationflagsposix)r   F)targetT)$superr	   __init__oslinesepencodeZcrlfZstring_typedict
subprocessPIPESTDOUTsysplatformZSTARTUPINFOZdwFlagsZSTARTF_USESHOWWINDOWZCREATE_NEW_PROCESS_GROUP
isinstancer   shlexsplitnamePopenprocpidclosed_bufr   _read_queue	threadingThread_read_incomingZ_read_thread	setDaemonstart)selfcmdr   r   r   r   r   r   r   r   r   kwargsr   )	__class__ `/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/pexpect/popen_spawn.pyr!      s0    
	



zPopenSpawn.__init__Fc             C   s   | j }| jr8|r*||d  | _ |d | S d| _td|dkrH| j}n|d krTd}t }xnt | |k r|rt||k ry| j }W n t	k
r   P Y q^X |d krd| _P || j
j|dd7 }q^W |d | ||d   }| _ | |d |S )NTzEnd Of File (EOF).g    .AF)finalread)r3   _read_reached_eofZflag_eofr   r   timelenr4   
get_nowaitr   _decoderdecode_log)r:   sizer   buft0incomingrr>   r>   r?   read_nonblockingA   s0    "zPopenSpawn.read_nonblockingc          
   C   sz   | j j }xhd}yt|d}W n. tk
rP } z| |d W dd}~X Y nX |sf| jd dS | j| qW dS )z6Run in a thread to move output from a pipe to a queue.    i   rB   N)	r0   r   filenor"   rB   OSErrorrI   r4   put)r:   rQ   rK   er>   r>   r?   r7   d   s    zPopenSpawn._read_incomingc             C   s   |  | dS )zHThis is similar to send() except that there is no return value.
        N)send)r:   sr>   r>   r?   writeu   s    zPopenSpawn.writec             C   s   x|D ]}|  | qW dS )zThis calls write() for each element in the sequence.

        The sequence can be any iterable object producing strings, typically a
        list of strings. This does not add line separators. There is no return
        value.
        N)rU   )r:   sequencerV   r>   r>   r?   
writelinesz   s    
zPopenSpawn.writelinesc             C   sR   |  |}| |d | jj|dd}tr8| jj|S | jj| t|S dS )zZSend data to the subprocess' stdin.

        Returns the number of bytes written.
        rU   F)rA   N)	Z_coerce_send_stringrI   _encoderr$   r   r0   r   rW   rE   )r:   rV   br>   r>   r?   rU      s    
zPopenSpawn.send c             C   s   |  |}||  | j S )zWraps send(), sending string ``s`` to child process, with os.linesep
        automatically appended. Returns number of bytes written. )rU   r#   )r:   rV   nr>   r>   r?   sendline   s    
zPopenSpawn.sendlinec             C   s8   | j  }|dkr || _d| _nd| _| | _d| _|S )zKWait for the subprocess to finish.

        Returns the exit code.
        r   NT)r0   waitZ
exitstatusZsignalstatusZ
terminated)r:   statusr>   r>   r?   r_      s    
zPopenSpawn.waitc             C   sT   t jdkr@|tjtjgkr"tj}n|tjtjgkr:tj}ntj}t	| j
j| dS )z}Sends a Unix signal to the subprocess.

        Use constants from the :mod:`signal` module to specify which signal.
        r   N)r)   r*   signalSIGINTZCTRL_C_EVENTZSIGBREAKZCTRL_BREAK_EVENTSIGTERMr"   killr0   r1   )r:   sigr>   r>   r?   rd      s    
zPopenSpawn.killc             C   s   | j j  dS )z+Closes the stdin pipe from the writing end.N)r0   r   close)r:   r>   r>   r?   sendeof   s    zPopenSpawn.sendeof)	r
   r   NNNNNr   N)r\   )__name__
__module____qualname__r!   rC   rO   r7   rW   rY   rU   r^   r_   rd   rg   __classcell__r>   r>   )r=   r?   r	      s     (#

r	   )__doc__r"   r5   r&   r)   rD   ra   r,   queuer   r   ImportErrorZ	spawnbaser   r   
exceptionsr   utilsr   r	   r>   r>   r>   r?   <module>   s   