a
    zelF                    @   s  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lZd dl	Z	d dlm
Z d dlZzd dlZW n ey   dZY n0 zd dlZW n ey   dZY n0 g dZzd dlZd dlZdZW n. ey   dZd dlZd dlZd dlZY n`0 d dlmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) e*g d G dd	 d	e+Z,G d
d de,Z-G dd de,Z.erG dd dZ/G dd de0Z1n&e2eddZ3e4edrej5Z6nej7Z6erdZ8dd Z9ng Z8dd Z9dZ:dZ;dZ<dd Z=dd Z>ddd d!Z?d"d# Z@ddd$d%ZAG d&d' d'eBZCddddd(d)d*ZDd+d, ZEd-d. ZFd/d0 ZGd1d2 ZHeH ZIG d3d4 d4eBZJdS )5    N)	monotonic)PopenPIPESTDOUTcall
check_callgetstatusoutput	getoutputcheck_outputrunCalledProcessErrorDEVNULLSubprocessErrorTimeoutExpiredCompletedProcessTF)CREATE_NEW_CONSOLECREATE_NEW_PROCESS_GROUPSTD_INPUT_HANDLESTD_OUTPUT_HANDLESTD_ERROR_HANDLESW_HIDESTARTF_USESTDHANDLESSTARTF_USESHOWWINDOWABOVE_NORMAL_PRIORITY_CLASSBELOW_NORMAL_PRIORITY_CLASSHIGH_PRIORITY_CLASSIDLE_PRIORITY_CLASSNORMAL_PRIORITY_CLASSREALTIME_PRIORITY_CLASSCREATE_NO_WINDOWDETACHED_PROCESSCREATE_DEFAULT_ERROR_MODECREATE_BREAKAWAY_FROM_JOB)r   r   r   r   r   r   r   r   STARTUPINFOr   r   r   r   r   r   r   r    r!   r"   c                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r'   r'    /usr/lib/python3.9/subprocess.pyr   i       r   c                   @   s8   e Zd Zd	ddZdd Zedd Zejdd ZdS )
r   Nc                 C   s   || _ || _|| _|| _d S N)
returncodecmdoutputstderr)selfr+   r,   r-   r.   r'   r'   r(   __init__s   s    zCalledProcessError.__init__c                 C   sf   | j rR| j dk rRzd| jt| j  f W S  tyN   d| j| j  f  Y S 0 nd| j| j f S d S )Nr   zCommand '%s' died with %r.z)Command '%s' died with unknown signal %d.z.Command '%s' returned non-zero exit status %d.)r+   r,   signalSignals
ValueErrorr/   r'   r'   r(   __str__y   s    
zCalledProcessError.__str__c                 C   s   | j S r*   r-   r4   r'   r'   r(   stdout   s    zCalledProcessError.stdoutc                 C   s
   || _ d S r*   r6   r/   valuer'   r'   r(   r7      s    )NNr$   r%   r&   r0   r5   propertyr7   setterr'   r'   r'   r(   r   l   s   

r   c                   @   s8   e Zd Zd	ddZdd Zedd Zejdd ZdS )
r   Nc                 C   s   || _ || _|| _|| _d S r*   )r,   timeoutr-   r.   )r/   r,   r=   r-   r.   r'   r'   r(   r0      s    zTimeoutExpired.__init__c                 C   s   d| j | jf S )Nz'Command '%s' timed out after %s seconds)r,   r=   r4   r'   r'   r(   r5      s    
zTimeoutExpired.__str__c                 C   s   | j S r*   r6   r4   r'   r'   r(   r7      s    zTimeoutExpired.stdoutc                 C   s
   || _ d S r*   r6   r8   r'   r'   r(   r7      s    )NNr:   r'   r'   r'   r(   r      s   

r   c                   @   s,   e Zd ZdddddddddZdd ZdS )r#   r   NdwFlags	hStdInput
hStdOutput	hStdErrorwShowWindowlpAttributeListc                C   s0   || _ || _|| _|| _|| _|p(dg i| _d S )Nhandle_listr>   )r/   r?   r@   rA   rB   rC   rD   r'   r'   r(   r0      s    zSTARTUPINFO.__init__c                 C   s@   | j  }d|v r"t|d |d< t| j| j| j| j| j|dS )NrE   r>   )	rD   copylistr#   r?   r@   rA   rB   rC   )r/   Z	attr_listr'   r'   r(   rF      s    
zSTARTUPINFO.copy)r$   r%   r&   r0   rF   r'   r'   r'   r(   r#      s   	r#   c                   @   s2   e Zd ZdZejfddZdd Zdd ZeZ	dS )	HandleFc                 C   s   | j sd| _ ||  d S )NT)closed)r/   CloseHandler'   r'   r(   Close   s    zHandle.Closec                 C   s    | j sd| _ t| S tdd S )NTzalready closed)rI   intr3   r4   r'   r'   r(   Detach   s    zHandle.Detachc                 C   s   d| j jt| f S )Nz%s(%d))	__class__r$   rL   r4   r'   r'   r(   __repr__   s    zHandle.__repr__N)
r$   r%   r&   rI   _winapirJ   rK   rM   rO   __del__r'   r'   r'   r(   rH      s
   rH   PIPE_BUFi   PollSelectorc                   C   s   d S r*   r'   r'   r'   r'   r(   _cleanup   s    rT   c               	   C   sZ   t d u rd S t d d  D ]<} | jtjd}|d urzt |  W q tyR   Y q0 qd S )N
_deadstate)_active_internal_pollsysmaxsizeremover3   )instresr'   r'   r(   rT      s    c                  C   s*   g } t jj}|dkr&| dd|   | S )Nr   -O)rY   flagsoptimizeappend)argsr9   r'   r'   r(   "_optim_args_from_interpreter_flags  s
    rg   c                  C   sV  ddddddd} t  }|  D ].\}}ttj|}|dkr |d	||   q tjjrd|d
 n$tjjrv|d tjjr|d tj	d d  }tjj
}ttdi }d|v }|dkr|d n|r|d |r|d |D ]}|d|  q|r
|d dD ]B}||v r|| }	|	du r4|}
nd||	f }
|d|
f q|S )NdBSvbq)debugdont_write_bytecodeno_siteverbosebytes_warningquietr   ra   z-Iz-Ez-s	_xoptionsdev   zerror::BytesWarningzdefault::BytesWarningdefaultz-W)-Xru   )faulthandlertracemalloc
importtimeshowrefcountutf8	oldparserTz%s=%srx   )rg   itemsgetattrrY   rc   re   isolatedignore_environmentno_user_sitewarnoptionsrr   r[   extend)flag_opt_maprf   flagoptrk   warnoptsrr   xoptionsdev_moder9   argr'   r'   r(   _args_from_interpreter_flags  sP    






r   r=   c                 O   sf   t |i |D}z|j| dW W  d    S    |   Y n0 W d    n1 sX0    Y  d S )Nr   )r   waitkill)r=   	popenargskwargspr'   r'   r(   r   U  s    r   c                  O   s:   t | i |}|r6|d}|d u r,| d }t||dS )Nrf   r   )r   getr   )r   r   retcoder,   r'   r'   r(   r   f  s    


r   c                 O   sb   d|v rt dd|v rJ|d d u rJ|ds8|dr>d}nd}||d< t|t| dd	|jS )
Nr7   z3stdout argument not allowed, it will be overridden.inputuniversal_newlinestext r)   T)r7   r=   check)r3   r   r   r   r7   )r=   r   r   emptyr'   r'   r(   r
   y  s    #r
   c                   @   s0   e Zd ZdddZdd ZeejZdd Z	dS )	r   Nc                 C   s   || _ || _|| _|| _d S r*   )rf   r+   r7   r.   )r/   rf   r+   r7   r.   r'   r'   r(   r0     s    zCompletedProcess.__init__c                 C   sh   d | jd | jg}| jd ur4|d | j | jd urP|d | j d t| jd|S )Nz	args={!r}zreturncode={!r}zstdout={!r}zstderr={!r}z{}({})z, )	formatrf   r+   r7   re   r.   typer$   join)r/   rf   r'   r'   r(   rO     s    



zCompletedProcess.__repr__c                 C   s    | j rt| j | j| j| jd S r*   )r+   r   rf   r7   r.   r4   r'   r'   r(   check_returncode  s    z!CompletedProcess.check_returncode)NN)
r$   r%   r&   r0   rO   classmethodtypesGenericAlias__class_getitem__r   r'   r'   r'   r(   r     s   
	
r   )r   capture_outputr=   r   c                 O   s<  | d ur&| dd urtdt|d< |r^| dd usF| dd urNtdt|d< t|d< t|i |}z|j| |d\}}W n` ty }	 z4|  tr| \|	_|	_	n|
   W Y d }	~	nd }	~	0    |   Y n0 | }
|r|
rt|
|j||dW d    n1 s"0    Y  t|j|
||S )Nstdinz/stdin and input arguments may not both be used.r7   r.   z@stdout and stderr arguments may not be used with capture_output.r   r-   r.   )r   r3   r   r   communicater   r   
_mswindowsr7   r.   r   pollr   rf   r   )r   r   r=   r   r   r   processr7   r.   excr   r'   r'   r(   r     s6    &r   c                 C   s   g }d}t tj| D ]}g }|r*|d d|v p>d|v p>| }|rN|d |D ]b}|dkrj|| qR|dkr|dt| d  g }|d qR|r|| g }|| qR|r|| |r|| |d qd|S )	NF 	"\   z\"r   )maposfsdecodere   lenr   r   )seqresult	needquoter   bs_bufcr'   r'   r(   list2cmdline  s4    




r   c              
   C   sn   zt | ddtd}d}W n0 tyH } z|j}|j}W Y d }~n
d }~0 0 |dd  dkrf|d d }||fS )NT)shellr   r.   r   r^   
)r
   r   r   r-   r+   )r,   dataexitcodeexr'   r'   r(   r   ^  s    r   c                 C   s   t | d S Nrv   )r   )r,   r'   r'   r(   r	   }  s    
r	   c               
   C   s   t sttdsdS tjdkr dS zjtd} | jdd}t|dkrHt|d	 }t	t
t|d d
}tjdkr|dkr|dkrW dS W n tttfy   Y n0 dS )Nposix_spawnFdarwinTCS_GNU_LIBC_VERSIONrv   )maxsplitr   r   .linuxglibc)r      )r   hasattrr   rY   platformconfstrsplitr   r3   tupler   rL   AttributeErrorOSError)verpartslibcversionr'   r'   r(   _use_posix_spawn  s     


r   c                   @   s  e Zd ZdZdLdddddddddd	Zd
d ZeejZ	e
dd Zejdd Zdd Zdd Zdd ZejejfddZdd Zdd ZdMddZdd Zdd  ZdNd!d"ZdOd#d$Zd%d& Zer0d'd( Zd)d* Zd+d, Z d-d. Z!de"j#e"j$e"j%fd/d0Z&d1d2 Z'd3d4 Z(d5d6 Z)d7d8 Z*d9d: Z+e+Z,n~d;d( Zd<d= Z-d>d. Z!e.j/e.j0e.j1fd?d@Z2de.j3e.j4e5j6fdAd0Z&dBdC Z7dDd2 Z'dEd6 Z)dFdG Z8dHd8 Z*dId: Z+dJdK Z,dS )Pr   Fr^   NTr   r'   )usergroupextra_groupsencodingerrorsr   umaskc          (      C   sF  t   t | _d | _d| _|d u r(d}t|ts:tdt	rP|d urt
dn8|rh|shtdt d}|d urxt
d|dkrt
d	|| _d | _d | _d | _d | _d | _|| _|| _|d ur|d urt|t|krtd
| |||\}}}}}}t	rN|dkrt| d}|dkr4t| d}|dkrNt| d}|pb|pb|pb|| _d| _d| _| jr|dkrd}d}nd}d } |d urtt dst
dnPt|t!rt"d u rt
dt"#|j$} n$t|tr|} ntd%t&|| dk rt
d|  d }!|d urtt dsBt
dnt|t!rVt
dg }!|D ]b}"t|"t!rt"d u rt
d|!'t"#|"j$ n*t|"tr|!'|" ntd%t&|"q^|!D ]}#|#dk rt
d|# qd }$|d urhtt ds
t
dnFt|t!r6t(d u r(t
dt()|j*}$nt|trH|}$ntd|$dk rht
d|$ z|dkrt+,|d|| _| jrt+j-| jd|||d| _|dkrt+,|d|| _| jrt+j-| j||d| _|dkrt+,|d|| _| jrt+j-| j||d| _| .||||||
||||	|||||||| |!|$|| W n   t/d | j| j| jfD ](}%z|%0  W n t1y   Y n0 qd| js:g }&|t2kr|&'| |t2kr|&'| |t2kr|&'| t| d r|&'| j3 |&D ]F}'z*t	rt|'t4r|'5  n
t 0|' W n t1y4   Y n0 q Y n0 d S )!NFr^   zbufsize must be an integerz0preexec_fn is not supported on Windows platformszpass_fds overriding close_fds.Tz2startupinfo is only supported on Windows platformsr   z4creationflags is only supported on Windows platformszlCannot disambiguate when both text and universal_newlines are supplied but different. Pass one or the other.g      ?rv   setregidz>The 'group' parameter is not supported on the current platformzHThe group parameter cannot be a string on systems without the grp modulez,Group must be a string or an integer, not {}z!Group ID cannot be negative, got 	setgroupszEThe 'extra_groups' parameter is not supported on the current platformz#Groups must be a list, not a stringzIItems in extra_groups cannot be strings on systems without the grp modulez9Items in extra_groups must be a string or integer, not {}setreuidz=The 'user' parameter is not supported on the current platformzGThe user parameter cannot be a string on systems without the pwd modulez#User must be a string or an integerz User ID cannot be negative, got wb)write_throughline_bufferingr   r   rb)r   r   _devnull)6rT   	threadingLock_waitpid_lock_input_communication_started
isinstancerL   	TypeErrorr   r3   warningswarnRuntimeWarningrf   r   r7   r.   pidr+   r   r   boolr   _get_handlesmsvcrtopen_osfhandlerM   	text_mode_sigint_wait_secs_closed_child_pipe_fdsr   r   strgrpgetgrnamgr_gidr   r   re   pwdgetpwnampw_uidioopenTextIOWrapper_execute_childfiltercloser   r   r   rH   rK   )(r/   rf   bufsize
executabler   r7   r.   
preexec_fn	close_fdsr   cwdenvr   startupinfocreationflagsrestore_signalsstart_new_sessionpass_fdsr   r   r   r   r   r   r   p2creadp2cwritec2preadc2pwriteerrreaderrwriter   gidgidsextra_group	gid_checkuidfto_closefdr'   r'   r(   r0     s.   	























	








zPopen.__init__c                 C   s@   d| j j d| j d| jd}t|dkr<|d d d }|S )N<z: returncode: z args: >P   L   z...>)rN   r$   r+   rf   r   )r/   obj_reprr'   r'   r(   rO     s    zPopen.__repr__c                 C   s   | j S r*   )r   r4   r'   r'   r(   r     s    zPopen.universal_newlinesc                 C   s   t || _d S r*   )r   r   )r/   r   r'   r'   r(   r     s    c                 C   s    | ||}|ddddS )Nz
r   )decodereplace)r/   r   r   r   r'   r'   r(   _translate_newlines  s    zPopen._translate_newlinesc                 C   s   | S r*   r'   r4   r'   r'   r(   	__enter__  s    zPopen.__enter__c                 C   s   | j r| j   | jr | j  z^| jr2| j  W |tkrv| jdkrlz| j| jd W n tyj   Y n0 d| _d S |   nT|tkr| jdkrz| j| jd W n ty   Y n0 d| _   Y d S |   0 d S )Nr   r   )	r7   r  r.   r   KeyboardInterruptr   _waitr   r   )r/   exc_typer9   	tracebackr'   r'   r(   __exit__  s2    




zPopen.__exit__c                 C   sT   | j s
d S | jd u r(|d| j t| d | j|d | jd u rPtd urPt|  d S )Nzsubprocess %s is still running)sourcerU   )_child_createdr+   r   ResourceWarningrX   rW   re   )r/   _maxsize_warnr'   r'   r(   rQ     s    

zPopen.__del__c                 C   s"   t | dsttjtj| _| jS )Nr   )r   r   r   devnullO_RDWRr   r4   r'   r'   r(   _get_devnull$  s    
zPopen._get_devnullc              
   C   s   |rZz| j | W nD ty&   Y n4 tyX } z|jtjkrBn W Y d }~n
d }~0 0 z| j   W nD tyz   Y n4 ty } z|jtjkrn W Y d }~n
d }~0 0 d S r*   )r   writeBrokenPipeErrorr   errnoEINVALr  )r/   r   r   r'   r'   r(   _stdin_write)  s"    zPopen._stdin_writec                 C   sV  | j r|rtd|d u r| j s| j| j| jgd dkrd }d }| jrT| | n6| jrp| j }| j  n| jr| j }| j  | 	  n|d urt
 | }nd }zz| |||\}}W nd ty(   |d urt| j| |}n| j}d| _z| j|d W n ty    Y n0  Y n0 W d| _ nd| _ 0 | j	| |d}||fS )Nz.Cannot send input after starting communicationr   r   r   T)r   r3   r   r7   r.   countr6  readr  r   _time_communicater%  minr   _remaining_timer&  r   )r/   r   r=   r7   r.   endtimesigint_timeoutstsr'   r'   r(   r   B  sH    





zPopen.communicatec                 C   s   |   S r*   )rX   r4   r'   r'   r(   r     s    z
Popen.pollc                 C   s   |d u rd S |t   S d S r*   )r9  )r/   r=  r'   r'   r(   r<    s    zPopen._remaining_timec                 C   sL   |d u rd S |st  |krHt| j||r0d|nd |r@d|nd dd S )Nr)   r   )r9  r   rf   r   )r/   r=  orig_timeout
stdout_seq
stderr_seqskip_check_and_raiser'   r'   r(   _check_timeout  s    zPopen._check_timeoutc                 C   s   |d urt  | }z| j|dW S  ty   |d urJt| j| |}n| j}d| _z| j|d W n tyx   Y n0  Y n0 d S )Nr   r   )r9  r&  r%  r;  r   r<  r   )r/   r=   r=  r>  r'   r'   r(   r     s     
z
Popen.waitc           	      C   s  t | dd }t }trX|dkr.||j |dkrB||j |dkr||j nr|dkr~|dkr~||kr~|tj| |dkr|dkr||kr|tj| |dkr|dkr||kr|tj| |d ur|tj| W d    n1 s0    Y  d| _d S )Nr   r^   T)	r   
contextlib	ExitStackr   callbackrK   r   r  r   )	r/   r  r  r  r  r  r  
devnull_fdstackr'   r'   r(   _close_pipe_fds  s$    
,zPopen._close_pipe_fdsc                 C   s~  |d u r|d u r|d u rdS d\}}d\}}d\}}	|d u rtt t j}|d u rt d d\}}
t|}t |
 nh|tkrt d d\}}t|t| }}n<|tkrt	| 
 }n$t|trt	|}nt	| }| |}|d u r*t t j}|d u rt d d\}
}t|}t |
 nn|tkrXt d d\}}t|t| }}n@|tkrrt	| 
 }n&t|trt	|}nt	| }| |}|d u rt t j}	|	d u rdt d d\}
}	t|	}	t |
 n~|tkrt d d\}}	t|t|	 }}	nP|tkr$|}	n@|tkr>t	| 
 }	n&t|trVt	|}	nt	| }	| |	}	||||||	fS )N)r^   r^   r^   r^   r^   r^   r^   r^   r   )rP   ZGetStdHandler   Z
CreatePiperH   rJ   r   r   r   Zget_osfhandler1  r   rL   fileno_make_inheritabler   r   r   )r/   r   r7   r.   r  r  r  r  r  r  _r'   r'   r(   r     sp    












zPopen._get_handlesc                 C   s&   t t  |t  ddt j}t|S )Nr   rv   )rP   ZDuplicateHandleZGetCurrentProcessZDUPLICATE_SAME_ACCESSrH   )r/   handlehr'   r'   r(   rM     s    
zPopen._make_inheritablec                 C   s   t dd |D S )Nc                 S   s,   h | ]$}|d @ d ks$t |t jkr|qS )   )rP   ZGetFileTypeZFILE_TYPE_CHAR).0rO  r'   r'   r(   	<setcomp>0  s
   z,Popen._filter_handle_list.<locals>.<setcomp>)rG   )r/   rE   r'   r'   r(   _filter_handle_list)  s    zPopen._filter_handle_listc           !      C   s  t |trnNt |tr.|
r"tdt|g}n,t |tjrR|
rFtdt|g}nt|}|d urlt|}|d u r|t }n|	 }d|||fv}|r| j
tjO  _
||_||_||_|j}t|od|v o|d }|s|r^|r^|d u ri  }|_t|dg  }|d< |r0|t|t|t|g7 }| ||d d < |r^|sZtdt d}|
r| j
tjO  _
tj|_|stjd}|stjdd	}tj|d
d}tj|st dtj|r|}n|}d!||}|d ur t|}t"#d|||| z@t$||d d t| |	|||	\}}}} W | %|||||| n| %|||||| 0 d| _&t'|| _(|| _)t*| d S )Nz$bytes args is not allowed on Windows0path-like args is not allowed when shell is truer^   rE   z?startupinfo.lpAttributeList['handle_list'] overriding close_fdsFZComSpecZ
SystemRootr   ZSystem32zcmd.exez:shell not found: neither %ComSpec% nor %SystemRoot% is setz
{} /c "{}"subprocess.PopenT)+r   r   bytesr   r   r   PathLiker   r#   rF   r?   rP   r   r@   rA   rB   rD   r   rG   r   rL   rT  r   r   r   r   r   rC   environpathr   isabsFileNotFoundErrorr   rY   auditZCreateProcessrJ  r+  rH   _handler   rJ   )!r/   rf   r  r  r  r  r  r  r  r	  r   r  r  r  r  r  r  Zunused_restore_signalsZ
unused_gidZunused_gidsZ
unused_uidZunused_umaskZunused_start_new_sessionZuse_std_handlesZattribute_listZhave_handle_listrE   ZcomspecZsystem_rootZhpZhtr   tidr'   r'   r(   r   6  s    







zPopen._execute_childc                 C   s,   | j d u r&|| jd|kr&|| j| _ | j S Nr   )r+   r^  )r/   rV   Z_WaitForSingleObjectZ_WAIT_OBJECT_0Z_GetExitCodeProcessr'   r'   r(   rX     s    
zPopen._internal_pollc                 C   s^   |d u rt j}nt|d }| jd u rXt | j|}|t jkrJt| j|t 	| j| _| jS )Ni  )
rP   ZINFINITErL   r+   WaitForSingleObjectr^  ZWAIT_TIMEOUTr   rf   GetExitCodeProcess)r/   r=   Ztimeout_millisr   r'   r'   r(   r&    s    

zPopen._waitc                 C   s   | |  |  d S r*   )re   r8  r  )r/   Zfhbufferr'   r'   r(   _readerthread  s    zPopen._readerthreadc                 C   s\  | j rBt| dsBg | _tj| j| j | jfd| _d| j_| j  | j	rt| dsg | _
tj| j| j	| j
fd| _d| j_| j  | jr| | | j d ur| j| | | j rt| j|| j	d ur| j| | | j rt| j|d }d }| j r| j}| j   | j	r0| j
}| j	  |r>|d nd }|rP|d nd }||fS )N_stdout_buff)targetrf   T_stderr_buffr   )r7   r   re  r   Threadrd  Zstdout_threaddaemonstartr.   rg  Zstderr_threadr   r6  r   r<  is_aliver   rf   r  )r/   r   r=  r@  r7   r.   r'   r'   r(   r:    sJ    










zPopen._communicatec                 C   sl   | j d urd S |tjkr"|   nF|tjkr>t| jtj n*|tjkrZt| jtj nt	d
|d S )NzUnsupported signal: {})r+   r1   SIGTERM	terminateZCTRL_C_EVENTr   r   r   ZCTRL_BREAK_EVENTr3   r   r/   sigr'   r'   r(   send_signal  s    




zPopen.send_signalc                 C   sV   | j d urd S zt| jd W n0 tyP   t| j}|tjkrF || _ Y n0 d S r   )r+   rP   ZTerminateProcessr^  PermissionErrorrb  ZSTILL_ACTIVE)r/   rcr'   r'   r(   rm    s    

zPopen.terminatec           
      C   s,  d\}}d\}}d\}}	|d u r"n@|t kr8t \}}n*|tkrJ|  }nt|trZ|}n| }|d u rln@|t krt \}}n*|tkr|  }nt|tr|}n| }|d u rnf|t krt \}}	nP|tkr|dkr|}	n
t	j
 }	n.|tkr|  }	nt|tr|}	n| }	||||||	fS )NrK  r^   )r   r   piper   r1  r   rL   rL  r   rY   
__stdout__)
r/   r   r7   r.   r  r  r  r  r  r  r'   r'   r(   r   0  sL    





c                 C   s   |d u rt j}i }|rJg }dD ]"}tt|d }|d ur|| q||d< g }|||	fD ]}|dkrX|t j|f qX|df|df|
dffD ]"\}}|dkr|t j||f q|r||d< t j|||fi || _d| _	| 
|||||	|
 d S )	N)SIGPIPESIGXFZSIGXFSZ	setsigdefr^   r   rv   r   file_actionsT)r   rY  r   r1   re   POSIX_SPAWN_CLOSEPOSIX_SPAWN_DUP2r   r   r+  rJ  )r/   rf   r  r  r
  r  r  r  r  r  r  r   sigsetsignamesignumry  r  fd2r'   r'   r(   _posix_spawnf  s8    zPopen._posix_spawnc           -         s  t |ttfr|g}n(t |tjr6|
r.td|g}nt|}|
rlttdrPdnd}|dg| } rl |d<  d u r||d  t	d ||| t
rFtj rF|d u rF|sF|sF|d u rF|dks|d	krF|dks|d	krF|dks|d	krF|sF|d u rF|d u rF|d u rF|dk rF| | ||||||||
 d S  }t \}}g }|d
k r||| t|}qZ|D ]}t| qzhz |d urg }| D ]>\}}t|}d|v rtd||d t|  qnd }t  tj r f} nt fddt|D } t|}!|!| t|| |tttt|!|||||||||||||||||| _d| _W t| nt| 0 |  |||||| t! }"t"|d}#|"|#7 }"|#rt#|"dkrqqW t| nt| 0 |"rz6t$| jd\}$}%|$| jkr<| %|% ntj&| _'W n t(yZ   Y n0 z|")dd	\}&}'}(|(* }(W n* ty   d}&d}'d+t|"}(Y n0 t,t-|&*dt.})t/|)t0r|'rt|'d}*|(dk}+|+rd}(|},n|},|*dkrt1|*}(|)|*|(|,|)|(d S )NrU  getandroidapilevelz/system/bin/shz/bin/shz-cr   rV  r^   r   rQ     =z!illegal environment variable namec                 3   s"   | ]}t jt | V  qd S r*   )r   rZ  r   fsencode)rR  dirr  r'   r(   	<genexpr>  s   z'Popen._execute_child.<locals>.<genexpr>TiP     :s   SubprocessError   0z#Bad exception data from child: {!r}ascii   noexecr   )2r   r   rW  r   rX  r   rG   r   rY   r]  _USE_POSIX_SPAWNrZ  dirnamer  rs  re   dupr  r   r  r3   r   get_exec_pathsetadd_posixsubprocess	fork_execsortedr   rL   r   r+  rJ  	bytearrayr8  r   waitpid_handle_exitstatusrZ   r+   ChildProcessErrorr   r!  r   r   builtinsr   
issubclassr   strerror)-r/   rf   r  r  r  r  r  r  r  r	  r   r  r  r  r  r  r  r
  r  r  r  r   r  
unix_shellorig_executableerrpipe_readerrpipe_writelow_fds_to_closelow_fdenv_listkrk   executable_listfds_to_keeperrpipe_datapartr   r?  exception_name	hex_errnoerr_msgchild_exception_type	errno_numchild_exec_never_callederr_filenamer'   r  r(   r     s   
	















c                 C   s$   ||r|| | _ n
||| _ d S r*   )r+   )r/   r?  waitstatus_to_exitcode_WIFSTOPPED	_WSTOPSIGr'   r'   r(   r  1  s    zPopen._handle_exitstatusc              
   C   s   | j d u r| jdsd S zzF| j d ur<| j W W | j  S || j|\}}|| jkr`| | W nD ty } z,|d ur|| _ n|j|krd| _ W Y d }~n
d }~0 0 W | j  n| j  0 | j S )NFr   )r+   r   acquirereleaser   r  r   r4  )r/   rV   _waitpid_WNOHANG_ECHILDr   r?  er'   r'   r(   rX   =  s$    	




c                 C   s<   zt | j|\}}W n ty2   | j}d}Y n0 ||fS r`  )r   r  r   r  )r/   
wait_flagsr   r?  r'   r'   r(   	_try_wait`  s    
zPopen._try_waitc                 C   sP  | j d ur| j S |d urt | }d}| jdrzX| j d urLW | j  q| tj\}}|| jkr~| 	| W | j  qW | j  n| j  0 | 
|}|dkrt| j|t|d |d}t| q&nv| j d u rJ| jP | j d urW d    qJ| d\}}|| jkr(| 	| W d    q1 s>0    Y  q| j S )NgMb@?Fr   r   g?)r+   r9  r   r  r  r  r   WNOHANGr   r  r<  r   rf   r;  timesleep)r/   r=   r=  delayr   r?  	remainingr'   r'   r(   r&  m  s>    







,c              
   C   s2  | j rT| jsTz| j   W n ty,   Y n0 |sTz| j   W n tyR   Y n0 d }d }| jsi | _| jrzg | j| j< | jrg | j| j< | jr| j| j }| jr| j| j }| | | j	rt
| j	}t }| j r|r|| j tj | jr| jjs|| jtj | jr2| jjs2|| jtj | r| |}|d urv|dk rv| j||||dd td||}	| |||| |	D ]\}
}|
j| j u r0|| j| jt  }z|  jt|
j|7  _W n* ty   ||
j |
j  Y n*0 | jt| j	kr||
j |
j  nP|
j| j| jfv rt|
jd}|sn||
j |
j  | j|
j | qq2W d    n1 s0    Y  | j | |d |d urd!|}|d urd!|}| j"r*|d ur
| #|| jj$| jj%}|d ur*| #|| jj$| jj%}||fS )Nr   T)rC  zN_check_timeout(..., skip_check_and_raise=True) failed to raise TimeoutExpired.i   r   r)   )&r   r   flushr3  r  _fileobj2outputr7   r.   _save_inputr   
memoryview_PopenSelectorregister	selectorsEVENT_WRITErI   
EVENT_READget_mapr<  rD  RuntimeErrorselectfileobj_input_offset	_PIPE_BUFr   r2  r  
unregisterr   r8  re   r   r   r   r#  r   r   )r/   r   r=  r@  r7   r.   
input_viewselectorr=   readykeyeventschunkr   r'   r'   r(   r:    s    







:





c                 C   sF   | j rB| jd u rBd| _|| _|d urB| jrB| j| j j| j j| _d S r`  )r   r   r  r   encoder   r   )r/   r   r'   r'   r(   r    s    zPopen._save_inputc                 C   s@   |    | jd urd S zt| j| W n ty:   Y n0 d S r*   )r   r+   r   r   r   ProcessLookupErrorrn  r'   r'   r(   rp    s    
c                 C   s   |  tj d S r*   )rp  r1   rl  r4   r'   r'   r(   rm  (  s    c                 C   s   |  tj d S r*   )rp  r1   SIGKILLr4   r'   r'   r(   r   -  s    z
Popen.kill)r^   NNNNNTFNNNNr   TFr'   )NN)F)N)9r$   r%   r&   r+  r0   rO   r   r   r   r   r;   r   r<   r#  r$  r)  rY   rZ   r   r   rQ   r1  r6  r   r   r<  rD  r   rJ  r   r   rM  rT  r   rP   ra  ZWAIT_OBJECT_0rb  rX   r&  rd  r:  rp  rm  r   r  r   r  
WIFSTOPPEDWSTOPSIGr  r  r  r4  ECHILDr  r  r'   r'   r'   r(   r     s   6        j	



D	 

H	
26' &

#)f r   )Kr  r4  r   r   r  r1   rY   r   r   rE  r   r9  r   r   ImportErrorr   __all__r   rP   r   ModuleNotFoundErrorr  r  r  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r   	Exceptionr   r   r   r#   rL   rH   r   r  r   rS   r  SelectSelectorrW   rT   r   r   r   rg   r   r   r   r
   objectr   r   r   r   r	   r   r  r   r'   r'   r'   r(   <module>,   s~   

P
%	

;3%EI/