U .‘cÇã@sšdZddlmZddlZddlZddlZdddgZdZdZdZdZ d d„Z d d „Z d d „Z dd„Z dd„Zdd„Zdd„Zeefdd„Zeefdd„ZdS)zPseudo terminal utilities.é)ÚselectNÚopenptyÚforkÚspawnééc Cs>z t ¡WSttfk r"YnXtƒ\}}t|ƒ}||fS)zdopenpty() -> (master_fd, slave_fd) Open a pty master/slave pair, using os.openpty() if possible.)ÚosrÚAttributeErrorÚOSErrorÚ_open_terminalÚ slave_open)Ú master_fdÚ slave_nameÚslave_fd©rú(/opt/alt/python38/lib64/python3.8/pty.pyrs  c CsLzt ¡\}}Wnttfk r(YnXt |¡}t |¡||fStƒS)z›master_open() -> (master_fd, slave_name) Open a pty master and return the fd, and the filename of the slave end. Deprecated, use openpty() instead.)rrr r ÚttynameÚcloser )r rrrrrÚ master_open"s  rc CsndD]\}dD]R}d||}zt |tj¡}Wntk rFYq YnX|d||fSqtdƒ‚dS)z1Open pty master and return (master_fd, tty_name).ZpqrstuvwxyzPQRSTZ0123456789abcdefz/dev/ptyz/dev/ttyzout of pty devicesN)rÚopenÚO_RDWRr )ÚxÚyZpty_nameÚfdrrrr 2s  r cCsrt |tj¡}zddlm}m}Wntk r:|YSXz|||dƒ|||dƒWntk rlYnX|S)z©slave_open(tty_name) -> slave_fd Open the pty slave and acquire the controlling terminal, returning opened filedescriptor. Deprecated, use openpty() instead.r)ÚioctlÚI_PUSHZptemZldterm)rrrZfcntlrrÚ ImportErrorr )Ztty_nameÚresultrrrrrr >s  r c Csðzt ¡\}}Wnttfk r(Yn4X|tkrTz t ¡Wntk rRYnX||fStƒ\}}t ¡}|tkrÞt ¡t |¡t  |t ¡t  |t ¡t  |t ¡|t kr¾t |¡t  t t ¡tj¡}t |¡n t |¡||fS)zdfork() -> (pid, master_fd) Fork and make the child a session leader with a controlling terminal.)rÚforkptyr r ÚCHILDÚsetsidrrrÚdup2Ú STDIN_FILENOÚ STDOUT_FILENOÚ STDERR_FILENOrrr)Úpidrr rZtmp_fdrrrrPs0         cCs"|rt ||¡}||d…}qdS)z#Write all the data to a descriptor.N)rÚwrite)rÚdataÚnrrrÚ_writenxs r)cCs t |d¡S)zDefault read function.i)rÚread)rrrrÚ_read~sr+cCsv|tg}t|ggƒ\}}}||krF||ƒ}|s:| |¡n t t|¡t|kr|tƒ}|sf| t¡qt||ƒqdS)zParent copy loop. Copies pty master -> standard output (master_read) standard input -> pty master (stdin_read)N)r"rÚremoverr&r#r))r Ú master_readÚ stdin_readZfdsZrfdsZwfdsZxfdsr'rrrÚ_copy‚s   r/cCsÔt|ƒtdƒkr|f}t d|¡tƒ\}}|tkrHtj|df|žŽzt t ¡}t  t ¡d}Wntj k r~d}YnXzt |||ƒWn(t k r¸|r´t t tj|¡YnXt |¡t |d¡dS)zCreate a spawned process.Úz pty.spawnrr)ÚtypeÚsysÚauditrrrÚexeclpÚttyZ tcgetattrr"ZsetrawÚerrorr/r Z tcsetattrZ TCSAFLUSHrÚwaitpid)Úargvr-r.r%r ÚmodeZrestorerrrr—s&      )Ú__doc__rrr2r5Ú__all__r"r#r$rrrr r rr)r+r/rrrrrÚs$    (