a »±,[{zã@shddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z z ddl Z Wne y†ddlZ Yn0ddlmZmZddlmZmZe j ¡Ze d¡pÄe d¡ZerÜdZddlmZnd Ze jdd kZerüd d „Zn(d d „ZGdd„deƒZ Gdd„deƒZ!d\a"a#dd„Z$dd„Z%dd„Z&Gdd„de'ƒZ(Gdd„de(ƒZ)dS)éN)Ú STDIN_FILENOÚCHILDé)ÚwhichÚPtyProcessErrorZsolarisZsunosF)Ú _fork_ptyTécCs t|gƒS©N)Úbytes©Úi©r ú9/usr/lib/python3.9/site-packages/ptyprocess/ptyprocess.pyÚ_byte(srcCst|ƒSr )Úchrr r r rr+sc@s eZdZdS)ÚFileNotFoundErrorN©Ú__name__Ú __module__Ú __qualname__r r r rr.órc@s eZdZdS)Ú TimeoutErrorNrr r r rr/rr)NNc Cs,tdurtdurdSz¦ddlm}m}d}dD]L}ttd|dƒ}|dus.t|dƒsVq.z | ¡}Wq.t yxYq.Yq.0q.|durŒt dƒ‚t t  |¡d|ƒ}t t  |¡d|ƒ}Wn\t t tt tjfyzdd lm}m}||}}Wnt yd \}}Yn0Yn0t|ƒat|ƒadS) zkSet constants _EOF and _INTR. This avoids doing potentially costly operations on module load. Nr)ÚVEOFÚVINTR)ÚstdinÚstdoutz__%s__ÚfilenozNo stream has a filenoé)ÚCEOFÚCINTR)ré)Ú_EOFÚ_INTRÚtermiosrrÚgetattrÚsysÚhasattrrÚ ValueErrorÚordÚ tcgetattrÚ ImportErrorÚOSErrorÚIOErrorÚerrorrrr) rrÚfdÚnameÚstreamZintrÚeofrrr r rÚ_make_eof_intr3s2   r2c Csd}zt |¡}WnVtjyh}z<|jdtjkrRt|jdd|jd|fƒ‚‚WYd}~n d}~00|r‚|dtjB|d<n|dtj@|d<zt |tj |¡WnTtyþ}z<|jdtjkrèt|jdd|jd|fƒ‚‚WYd}~n d}~00dS)Nz~setecho() may not be called on this platform (it may still be possible to enable/disable echo when spawning the child process)rú%s: %s.rr) r#r)r-ÚargsÚerrnoÚEINVALr,ÚECHOZ tcsetattrZTCSANOW)r.ÚstateÚerrmsgÚattrÚerrr r rÚ_setecho^s r<cCs0ttddƒ}t d||dd¡}t |||¡dS)NÚ TIOCSWINSZigt€ÚHHHHr)r$r#ÚstructÚpackÚfcntlÚioctl)r.ÚrowsÚcolsr=Úsr r rÚ _setwinsizevs rFc@sPeZdZdZeZer8ej  d¡Zd  d¡Z e dd„ƒZ nejZdZ e jjZ dZdZdZdZdd„ZedAd d „ƒZd d„Ze dd„ƒZe dd„ƒZdd„Zdd„ZdBdd„Zdd„Zdd„ZdCdd„Zdd „Z d!d"„Z!dDd$d%„Z"d&d'„Z#dEd(d)„Z$dFd*d+„Zd,d-„Z%d.d/„Z&d0d1„Z'd2d3„Z(dGd5d6„Z)d7d8„Z*d9d:„Z+d;d<„Z,d=d>„Z-d?d@„Z.dS)HÚ PtyProcesszThis class represents a process running in a pseudoterminal. The main constructor is the :meth:`spawn` classmethod. Úasciiz c Cs<ztjj |¡WSty6tj | dd¡¡YS0dS)NrHÚreplace)r%rÚbufferÚwriteÚAttributeErrorÚdecode)Úbr r rÚwrite_to_stdoutŠs zPtyProcess.write_to_stdoutNcCsvtƒ||_||_tj|ddd}tj|dddd}t ||¡|_d|_d|_d|_ d|_ d|_ d|_ d|_ d|_dS)NÚrbr)Ú bufferingÚwbF)rQÚclosefdçš™™™™™¹?)r2Úpidr.ÚioÚopenÚBufferedRWPairÚfileobjÚ terminatedÚclosedÚ exitstatusÚ signalstatusÚstatusÚflag_eofÚdelayaftercloseÚdelayafterterminate)ÚselfrUr.ZreadfZwritefr r rÚ__init__œszPtyProcess.__init__T©ééPc CsÚt|ttfƒstd|ƒ‚|dd…}|d}t|ƒ}|durNtdd|ƒ‚|}||d<t ¡\} } trxt   ¡\} } n t   ¡\} } | t krºzttg|¢RŽWn<tyÞ} z$| jdtjtjfvrÊ‚WYd} ~ n d} ~ 00|st +||¡nt ,|||¡Wnjt-y¸} zPd  %| jt&| ƒ¡}t'r‚| (d ¡}t )| |¡t | ¡t *tj.¡WYd} ~ n d} ~ 00|| | ƒ}||_/|durÚ||_0|durê||_1t | ¡t 2| d ¡}t | ¡t3|ƒdkr‚zL| 4dd¡\}}}t5t6| 7d¡t"ƒ}|| 7d d¡ƒ}|t-urbt8|ƒ|_Wnt"d|ƒ‚Yn0|‚z|j9|ŽWnDtyÔ} z*| jdtjtjtj:fvrÀ‚WYd} ~ n d} ~ 00|S)a%Start the given command in a child process in a pseudo terminal. This does all the fork/exec type of stuff for a pty, and returns an instance of PtyProcess. If preexec_fn is supplied, it will be called with no arguments in the child process before exec-ing the specified command. It may, for instance, set signal handlers to SIG_DFL or SIG_IGN. Dimensions of the psuedoterminal used for the subprocess can be specified as a tuple (rows, cols), or the default (24, 80) will be used. z)Expected a list or tuple for argv, got %rNrz%The command was not found or was not zexecutable: %s.Firrz{}:0:{}úutf-8z OSError:{}:{}ió:érHrIz)Subprocess failed, got bad error data: %r);Ú isinstanceÚlistÚtupleÚ TypeErrorrrÚosÚpipeÚuse_native_pty_forkÚptyÚforkrZfork_ptyrrFrr,r4r5r6ZENOTTYr<r#r-ÚcloserAZF_SETFDZ FD_CLOEXECÚminÚresourceZ getrlimitZ RLIMIT_NOFILEÚ closerangeÚchdirÚ ExceptionÚtyperÚformatÚstrÚPY3ÚencoderKÚ_exitÚexecvÚexecvper+ÚEX_OSERRÚargvÚenvÚ launch_dirÚreadÚlenÚsplitr$ÚbuiltinsrMÚintÚ setwinsizeZENXIO)Úclsr‚ÚcwdrƒÚechoZ preexec_fnZ dimensionsZcommandZcommand_with_pathZexec_err_pipe_readZexec_err_pipe_writerUr.r;Zmax_fdÚeZenameZtosendÚinstZ exec_err_dataZerrclassZerrno_sr9ÚexctypeÚ exceptionr r rÚspawn²s¦  ÿ                  "       ÿ zPtyProcess.spawncCs|t|ƒj}|jdurft|jƒg}|jdur:| d|j¡|jdurT| d|j¡d |d |¡¡Sd ||j |j ¡SdS)Nzenv=%rzcwd=%rz {}.spawn({})z, z{}(pid={}, fd={})) ryrr‚ÚreprrƒÚappendr„rzÚjoinrUr.)rbZclsnamer4r r rÚ__repr__Ts     zPtyProcess.__repr__cCst|tƒs| d¡S|S)Nrg)rjr r}©rEr r rÚ_coerce_send_stringbs  zPtyProcess._coerce_send_stringcCs|Sr r r—r r rÚ_coerce_read_stringhszPtyProcess._coerce_read_stringcCs$|js z | ¡Wn Yn0dS)aLThis makes sure that no system resources are left open. Python only garbage collects Python objects. OS file descriptors are not Python objects, so they must be handled explicitly. If the child file descriptor was opened outside of this class (passed to the constructor) then this does not close it. N)r[rs©rbr r rÚ__del__ls  zPtyProcess.__del__cCs|jS)zCThis returns the file descriptor of the pty for the child. )r.ršr r rr~szPtyProcess.filenocCsN|jsJ| ¡|j ¡t |j¡| ¡r>| |¡s>t dƒ‚d|_ d|_dS)a?This closes the connection with the child application. Note that calling close() more than once is valid. This emulates standard Python behavior with files. Set force to True if you want to make sure that the child is terminated (SIGKILL is sent if the child ignores SIGHUP and SIGINT). zCould not terminate the child.éÿÿÿÿTN) r[ÚflushrYrsÚtimeÚsleepr`ÚisaliveÚ terminaterr.©rbÚforcer r rrsƒs   zPtyProcess.closecCsdS)zWThis does nothing. It is here to support the interface for a File-like object. Nr ršr r rr•szPtyProcess.flushcCs t |j¡S)a^This returns True if the file descriptor is open and connected to a tty(-like) device, else False. On SVR4-style platforms implementing streams, such as SunOS and HP-UX, the child pty may not appear as a terminal device. This means methods such as setecho(), setwinsize(), getwinsize() may raise an IOError. )rnÚisattyr.ršr r rr¤›s zPtyProcess.isattycCsX|durt ¡|}| ¡s dS|dkr4|dur4dS|durH|t ¡}t d¡qdS)aºThis waits until the terminal ECHO flag is set False. This returns True if the echo mode is off. This returns False if the ECHO flag was not set False before the timeout. This can be used to detect when the child is waiting for a password. Usually a child application will turn off echo mode when it is waiting for the user to enter a password. For example, instead of expecting the "password:" prompt you can wait for the child to set ECHO off:: p = pexpect.spawn('ssh user@example.com') p.waitnoecho() p.sendline(mypassword) If timeout==None then this method to block until ECHO flag is False. NTrFrT)ržÚgetechorŸ)rbÚtimeoutZend_timer r rÚ waitnoecho¦s  zPtyProcess.waitnoechoc Cs†zt |j¡}WnZtjyj}z@d}|jdtjkrTt|jdd|jd|fƒ‚‚WYd}~n d}~00t|dtj @ƒ|_ |j S)aThis returns the terminal echo mode. This returns True if echo is on or False if echo is off. Child applications that are expecting you to enter a password often set ECHO False. See waitnoecho(). Not supported on platforms where ``isatty()`` returns False. z,getecho() may not be called on this platformrr3rNr) r#r)r.r-r4r5r6r,Úboolr7r)rbr:r;r9r r rr¥ÁszPtyProcess.getechocCst|j|ƒ||_dS)aZThis sets the terminal echo mode on or off. Note that anything the child sent before the echo will be lost, so you should be sure that your input buffer is empty before you call setecho(). For example, the following will work as expected:: p = pexpect.spawn('cat') # Echo is on by default. p.sendline('1234') # We expect see this twice from the child... p.expect(['1234']) # ... once from the tty echo... p.expect(['1234']) # ... and again from cat itself. p.setecho(False) # Turn off tty echo p.sendline('abcd') # We will set this only once (echoed by cat). p.sendline('wxyz') # We will set this only once (echoed by cat) p.expect(['abcd']) p.expect(['wxyz']) The following WILL NOT WORK because the lines sent before the setecho will be lost:: p = pexpect.spawn('cat') p.sendline('1234') p.setecho(False) # Turn off tty echo p.sendline('abcd') # We will set this only once (echoed by cat). p.sendline('wxyz') # We will set this only once (echoed by cat) p.expect(['1234']) p.expect(['1234']) p.expect(['abcd']) p.expect(['wxyz']) Not supported on platforms where ``isatty()`` returns False. N)r<r.r)rbr8r r rÚsetechoÓs zPtyProcess.setechoéc Cstz|j |¡}WnHttfyX}z,|jdtjkrBd|_tdƒ‚‚WYd}~n d}~00|dkrpd|_tdƒ‚|S)aRead and return at most ``size`` bytes from the pty. Can block if there is nothing to read. Raises :exc:`EOFError` if the terminal was closed. Unlike Pexpect's ``read_nonblocking`` method, this doesn't try to deal with the vagaries of EOF on platforms that do strange things, like IRIX or older Solaris systems. It handles the errno=EIO pattern used on Linux, and the empty-string return used on BSD platforms and (seemingly) on recent Solaris. rTú,End Of File (EOF). Exception style platform.Nrú/End Of File (EOF). Empty string style platform.) rYÚread1r+r,r4r5ÚEIOr_ÚEOFError)rbÚsizerEr;r r rr…÷s zPtyProcess.readc Csrz|j ¡}WnHttfyV}z,|jdtjkr@d|_tdƒ‚‚WYd}~n d}~00|dkrnd|_tdƒ‚|S)ú´Read one line from the pseudoterminal, and return it as unicode. Can block if there is nothing to read. Raises :exc:`EOFError` if the terminal was closed. rTr«Nrr¬) rYÚreadliner+r,r4r5r®r_r¯)rbrEr;r r rr²szPtyProcess.readlinecCs|j |¡}|r|j ¡|Sr )rYrKr)rbrNrÚnr r rÚ_writeb's  zPtyProcess._writebcCs|j||dS)zaWrite bytes to the pseudoterminal. Returns the number of bytes written. )r)r´)rbrErr r rrK-szPtyProcess.writec Cs’| ¡}t|ƒ}d|kr$dkrNnn&|tdƒd}t|ƒ}| |¡|fSddddddddd d d d d œ }||vrxd St||ƒ}| |¡|fS)aHelper method that wraps send() with mnemonic access for sending control character to the child (such as Ctrl-C or Ctrl-D). For example, to send Ctrl-G (ASCII 7, bell, ''):: child.sendcontrol('g') See also, sendintr() and sendeof(). éaézÚarréééééé) ú@ú`ú[ú{ú\ú|ú]ú}ú^ú~Ú_ú?)rr)Úlowerr(rr´)rbÚcharr·ZbyteÚdr r rÚ sendcontrol4s$ ú zPtyProcess.sendcontrolcCs| t¡tfS)a1This sends an EOF to the child. This sends a character which causes the pending parent output buffer to be sent to the waiting child program without waiting for end-of-line. If it is the first character of the line, the read() in the user program returns 0, which signifies end-of-file. This means to work as expected a sendeof() has to be called at the beginning of a line. This method does not send a newline. It is the responsibility of the caller to ensure the eof is sent at the beginning of a line. )r´r!ršr r rÚsendeofPs zPtyProcess.sendeofcCs| t¡tfS)znThis sends a SIGINT to the child. It does not require the SIGINT to be the first character on a line. )r´r"ršr r rÚsendintr\szPtyProcess.sendintrcCs|jS)z@This returns True if the EOF exception was ever raised. )r_ršr r rr1bszPtyProcess.eofFcCsì| ¡s dSz¨| tj¡t |j¡| ¡s4WdS| tj¡t |j¡| ¡sZWdS| tj¡t |j¡| ¡s€WdS|r°| tj ¡t |j¡| ¡sªWdSWdSWdSt yæt |j¡| ¡sÜYdSYdSYn0dS)zÿThis forces a child process to terminate. It starts nicely with SIGHUP and SIGINT. If "force" is True then moves onto SIGKILL. This returns True if the child was terminated. This returns False if the child could not be terminated. TFN) r ÚkillÚsignalÚSIGHUPržrŸraÚSIGCONTÚSIGINTÚSIGKILLr+r¢r r rr¡hs6          zPtyProcess.terminatecCsš| ¡rt |jd¡\}}n|jSt |¡|_t |¡rX||_t |¡|_d|_d|_ nrri)r$r#r?r@rArBr.Úunpack)rbrÞrEÚxr r rÚ getwinsizes zPtyProcess.getwinsizecCst|j||ƒS)a@Set the terminal window size of the child tty. This will cause a SIGWINCH signal to be sent to the child. This does not change the physical window size. It changes the size reported to TTY-aware applications like vi or curses -- applications that respond to the SIGWINCH signal. )rFr.)rbrCrDr r rrŠszPtyProcess.setwinsize)NNTNrd)T)N)rª)T)T)F)/rrrÚ__doc__r Ú string_typer|rnÚlinesepr}ZcrlfÚ staticmethodrOr%rrKÚencodingr‚rƒr„rcÚ classmethodr’r–r˜r™r›rrsrr¤r§r¥r©r…r²r´rÍrÎrÏr1r¡rÜr rÐrárŠr r r rrG€s^   þ "    $     (M rGcsVeZdZdZereZneZd ‡fdd„ Zd‡fdd„ Z ‡fd d „Z ‡fd d „Z ‡Z S)ÚPtyProcessUnicodezÖUnicode wrapper around a process running in a pseudoterminal. This class exposes a similar interface to :class:`PtyProcess`, but its read methods return unicode, and its :meth:`write` accepts unicode. rgÚstrictcs4tt|ƒ ||¡||_||_t |¡|d|_dS)N)Úerrors)ÚsuperrèrcræÚ codec_errorsÚcodecsÚgetincrementaldecoderÚdecoder)rbrUr.rærì©Ú __class__r rrc$szPtyProcessUnicode.__init__rªcs tt|ƒ |¡}|jj|ddS)aRead at most ``size`` bytes from the pty, return them as unicode. Can block if there is nothing to read. Raises :exc:`EOFError` if the terminal was closed. The size argument still refers to bytes, not unicode code points. F©Úfinal)rërèr…rïrM)rbr°rNrðr rr…*szPtyProcessUnicode.readcstt|ƒ ¡}|jj|ddS)r±Frò)rërèr²rïrM)rbrNrðr rr²5szPtyProcessUnicode.readlinecs| |j¡}tt|ƒ |¡S)zlWrite the unicode string ``s`` to the pseudoterminal. Returns the number of bytes written. )r}rærërèrK)rbrErNrðr rrK>s zPtyProcessUnicode.write)rgré)rª) rrrrâr|r{rãZunicodercr…r²rKÚ __classcell__r r rðrrès rè)*rír5rArVrnrqrurÑr?r%r#ržrˆr*Z __builtin__rrÚutilrrÚplatformrÊZ _platformÚ startswithZ _is_solarisrpÚrÚ version_infor|rr+rrr!r"r2r<rFÚobjectrGrèr r r rÚsT    þ +