o
    !Qd                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ej	Z	ej
Z
ejZG d	d
 d
ejejZG dd dZdddejfddZdddejdddZdS ))create_subprocess_execcreate_subprocess_shell    N   )events)	protocols)streams)tasks)loggerc                       sX   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Z  ZS )SubprocessStreamProtocolz0Like StreamReaderProtocol, but for a subprocess.c                    sH   t  j|d || _d  | _ | _| _d | _d| _g | _| j	
 | _d S )N)loopF)super__init___limitstdinstdoutstderr
_transport_process_exited	_pipe_fds_loopcreate_future_stdin_closed)selflimitr   	__class__ -/RAG/Python-3.10.13/Lib/asyncio/subprocess.pyr      s   z!SubprocessStreamProtocol.__init__c                 C   sn   | j jg}| jd ur|d| j | jd ur!|d| j | jd ur/|d| j dd|S )Nzstdin=zstdout=zstderr=z<{}> )r   __name__r   appendr   r   formatjoin)r   infor   r   r   __repr__   s   



z!SubprocessStreamProtocol.__repr__c                 C   s   || _ |d}|d ur"tj| j| jd| _| j| | j	d |d}|d urAtj| j| jd| _
| j
| | j	d |d}|d urWtj|| d | jd| _d S d S )Nr   r   r      r   )protocolreaderr   )r   get_pipe_transportr   StreamReaderr   r   r   set_transportr   r    r   StreamWriterr   )r   	transportZstdout_transportZstderr_transportZstdin_transportr   r   r   connection_made(   s.   


z(SubprocessStreamProtocol.connection_madec                 C   s>   |dkr| j }n
|dkr| j}nd }|d ur|| d S d S Nr   r&   )r   r   	feed_data)r   fddatar(   r   r   r   pipe_data_received@   s   z+SubprocessStreamProtocol.pipe_data_receivedc                 C   s   |dkr(| j }|d ur|  | | |d u r | jd  d S | j| d S |dkr0| j}n
|dkr8| j}nd }|d urL|d u rG|  n|| || j	v rW| j	
| |   d S )Nr   r   r&   )r   closeconnection_lostr   
set_resultset_exceptionr   r   feed_eofr   remove_maybe_close_transport)r   r1   excpiper(   r   r   r   pipe_connection_lostJ   s,   



z-SubprocessStreamProtocol.pipe_connection_lostc                 C   s   d| _ |   d S )NT)r   r:   r   r   r   r   process_exitede   s   z'SubprocessStreamProtocol.process_exitedc                 C   s0   t | jdkr| jr| j  d | _d S d S d S )Nr   )lenr   r   r   r4   r>   r   r   r   r:   i   s   

z/SubprocessStreamProtocol._maybe_close_transportc                 C   s   || j u r| jS d S N)r   r   )r   streamr   r   r   _get_close_waitern   s   
z*SubprocessStreamProtocol._get_close_waiter)r   
__module____qualname____doc__r   r$   r.   r3   r=   r?   r:   rC   __classcell__r   r   r   r   r
      s    	

r
   c                   @   sj   e Zd Zdd Zdd Zedd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd ZdddZdS )Processc                 C   s8   || _ || _|| _|j| _|j| _|j| _| | _d S rA   )r   	_protocolr   r   r   r   get_pidpid)r   r-   r'   r   r   r   r   r   t   s   zProcess.__init__c                 C   s   d| j j d| j dS )N<r   >)r   r   rK   r>   r   r   r   r$   }   s   zProcess.__repr__c                 C   s
   | j  S rA   )r   get_returncoder>   r   r   r   
returncode   s   
zProcess.returncodec                    s   | j  I dH S )z?Wait until the process exit and return the process return code.N)r   _waitr>   r   r   r   wait   s   zProcess.waitc                 C   s   | j | d S rA   )r   send_signal)r   signalr   r   r   rR      s   zProcess.send_signalc                 C      | j   d S rA   )r   	terminater>   r   r   r   rU         zProcess.terminatec                 C   rT   rA   )r   killr>   r   r   r   rW      rV   zProcess.killc              
      s   | j  }| j| |rtd| t| z
| j I d H  W n tt	fy> } z|r4td| | W Y d }~nd }~ww |rGtd|  | j
  d S )Nz%%r communicate: feed stdin (%s bytes)z%r communicate: stdin got %rz%r communicate: close stdin)r   	get_debugr   writer	   debugr@   drainBrokenPipeErrorConnectionResetErrorr4   )r   inputrZ   r;   r   r   r   _feed_stdin   s"   

zProcess._feed_stdinc                    s   d S rA   r   r>   r   r   r   _noop   s   zProcess._noopc                    s   | j |}|dkr| j}n	|dksJ | j}| j r,|dkr#dnd}td| | | I d H }| j rG|dkr>dnd}td| | |	  |S )Nr&   r   r   r   z%r communicate: read %sz%r communicate: close %s)
r   r)   r   r   r   rX   r	   rZ   readr4   )r   r1   r-   rB   nameoutputr   r   r   _read_stream   s   

zProcess._read_streamNc                    s   |d ur|  |}n|  }| jd ur| d}n|  }| jd ur)| d}n|  }t|||I d H \}}}|  I d H  ||fS r/   )r_   r`   r   rd   r   r   gatherrQ   )r   r^   r   r   r   r   r   r   communicate   s   

zProcess.communicaterA   )r   rD   rE   r   r$   propertyrO   rQ   rR   rU   rW   r_   r`   rd   rf   r   r   r   r   rH   s   s    	
rH   c           	         sJ   t   fdd}j|| f|||d|I d H \}}t||S )Nc                         t  dS Nr%   r
   r   r%   r   r   <lambda>       z)create_subprocess_shell.<locals>.<lambda>r   r   r   )r   get_running_loopsubprocess_shellrH   )	cmdr   r   r   r   kwdsprotocol_factoryr-   r'   r   r%   r   r      s   r   )r   r   r   r   c          
         sP   t   fdd}j|| g|R |||d|I d H \}}	t||	S )Nc                      rh   ri   rj   r   r%   r   r   rk      rl   z(create_subprocess_exec.<locals>.<lambda>rm   )r   rn   subprocess_execrH   )
programr   r   r   r   argsrq   rr   r-   r'   r   r%   r   r      s   r   )__all__
subprocess r   r   r   r   logr	   PIPESTDOUTDEVNULLFlowControlMixinSubprocessProtocolr
   rH   _DEFAULT_LIMITr   r   r   r   r   r   <module>   s(    bU
