a 5êdgú/ã@sÒddlmZmZmZedƒddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z dZ dZerzdd„Zndd„Zd d „Zd d „ZGd d„de jƒZGdd„de jƒZdd„ZedkrÎe  ¡dS)é)ÚverboseÚ import_moduleÚ reap_childrenZtermiosNóI wish to buy a fish license. óFor my pet fish, Eric. cCs t|ƒdS©N)Úprint©Úmsg©r ú)/usr/local/lib/python3.9/test/test_pty.pyÚdebugsr cCsdSrr r r r r r scCs0| d¡r| dd¡S| d¡r,| dd¡S|S)Ns ó ó )ÚendswithÚreplace)Údatar r r Únormalize_output%s    rcCstj|ddd}| ¡S)z8Read one line. May block forever if no newline is read.ÚrbF)ÚmodeÚclosefd)ÚioÚFileIOÚreadline)ÚfdÚreaderr r r Ú _readline9src@s8eZdZdd„Zdd„Zedd„ƒZdd„Zd d „Zd S) ÚPtyTestcCs`t tj|j¡}| tjtj|¡t tj|j¡}| tjtj|¡| tjd¡t d¡dS)Nré )ÚsignalÚSIGALRMÚ handle_sigZ addCleanupÚSIGHUPÚ handle_sighupÚalarm)ÚselfZ old_alarmZ old_sighupr r r ÚsetUpDs z PtyTest.setUpcCs| d¡dS)Nz isatty hung)Úfail)r%ÚsigÚframer r r r!PszPtyTest.handle_sigcCsdSrr )Úsignumr)r r r r#SszPtyTest.handle_sighupc CsŽzLtdƒt ¡\}}td||fƒtd|fƒt |¡}td|ƒWntyht d¡‚Yn0| t  |¡d¡t  |¡}zlt  |d¡zt  |d¡}|  d |¡Wn2tyâ}z|jtjkr΂WYd}~n d}~00Wt  ||¡nt  ||¡0td ƒt |t¡t|ƒ}|  d t|ƒ¡td ƒt |tdd …¡t |td d…¡t|ƒ}|  dt|ƒ¡t |¡t |¡dS)NzCalling master_open()z#Got master_fd '%d', slave_name '%s'zCalling slave_open(%r)zGot slave_fd '%d'z,Pseudo-terminals (seemingly) not functional.zslave_fd is not a ttyFiózWriting to slave_fdrzWriting chunked outputér)r ÚptyZ master_openZ slave_openÚOSErrorÚunittestZSkipTestZ assertTrueÚosÚisattyÚ get_blockingÚ set_blockingÚreadÚ assertEqualÚerrnoÚEAGAINÚwriteÚ TEST_STRING_1rrÚ TEST_STRING_2Úclose)r%Ú master_fdZ slave_nameZslave_fdÚblockingÚs1ÚeÚs2r r r Ú test_basicZsF ÿ       ÿ zPtyTest.test_basiccCs¨tdƒt ¡\}}|tjkr¼t d¡s:tdƒt d¡tdƒz t ¡WnNtyhtdƒYnHt y€tdƒYn0tdƒt d¡Yn0td ƒt d ¡t d ¡nÞtd |ƒzt  |d ¡}WntyðYqYn0|súqt j   t| dd¡dd¡qÈt |d¡\}}t |¡}td|||fƒ|dkrZ| d¡n@|d krp| d¡n*|dkr†| d¡n|d krš| d¡t |¡dS)Nzcalling pty.fork()ézChild's fd 1 is not a tty?!ézIn child, calling os.setsid()zGood: OSError was raised.zNo setsid() available?zAn unexpected error was raised.zos.setsid() succeeded! (bad!)ééz!Waiting for child (%d) to finish.éPrrÚascii)Úencodingrz+Child (%d) exited with code %d (status %d).z3Child raised an unexpected exception in os.setsid()z1pty.fork() failed to make child a session leader.z8Child spawned by pty.fork() did not have a tty as stdoutz&pty.fork() failed for unknown reasons.)r r-ÚforkZCHILDr0r1Ú_exitÚsetsidr.ÚAttributeErrorr4ÚsysÚstdoutr8ÚstrrÚwaitpidÚwaitstatus_to_exitcoder'r;)r%Úpidr<rÚstatusÚresr r r Ú test_forksV            ÿ          zPtyTest.test_forkN) Ú__name__Ú __module__Ú __qualname__r&r!Ú staticmethodr#rArUr r r r rCs   3rc@sHeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dS)Ú SmallPtyTestsz)These tests don't spawn children or hang.cCs4tj|_tj|_tj|_g|_g|_g|_ g|_ dSr) r-Ú STDIN_FILENOÚorig_stdin_filenoÚ STDOUT_FILENOÚorig_stdout_filenoÚselectÚorig_pty_selectÚfdsÚfilesÚselect_rfds_lengthsÚselect_rfds_results)r%r r r r&æszSmallPtyTests.setUpc Csv|jt_|jt_|jt_|jD]$}z | ¡Wqt y@Yq0q|j D]&}zt  |¡WqJt ynYqJ0qJdSr) r\r-r[r^r]r`r_rbr;r.rar0)r%Úfilerr r r ÚtearDownïs     zSmallPtyTests.tearDowncCst ¡}|j |¡|Sr)r0ÚpiperaÚextend)r%Zpipe_fdsr r r Ú_pipeþs zSmallPtyTests._pipecCst ¡}|j |¡|Sr)ÚsocketÚ socketpairrbrh)r%rkr r r Ú _socketpairs zSmallPtyTests._socketpaircCs*| |j d¡t|ƒ¡|j d¡ggfS)Nr)r5rcÚpopÚlenrd)r%ÚrfdsZwfdsZxfdsr r r Ú _mock_selectszSmallPtyTests._mock_selectcCs"| ¡\}}|t_| ¡\}}|t_| ¡}dd„|Dƒ}t |dd¡t |d¡|jt_|j   d¡|j   ||dg¡|j   d¡|  t ¡t |d¡Wdƒn1s¸0Yt ||dgggd¡d}| ||dg|¡| t |d ¡d¡| t |dd ¡d¡dS) z6Test the normal data case on both master_fd and stdin.cSsg|] }| ¡‘qSr ©Úfileno©Ú.0Úsr r r Ú r+z4SmallPtyTests.test__copy_to_each..rBs from masters from stdinrDrNé)rir-r]r[rlr0r8rpr_rcÚappendrdÚ assertRaisesÚ IndexErrorÚ_copyr5r4)r%Úread_from_stdout_fdÚmock_stdout_fdÚ mock_stdin_fdÚwrite_to_stdin_fdrkÚmastersror r r Útest__copy_to_each s$      ,z SmallPtyTests.test__copy_to_eachcCsÀ| ¡\}}|t_| ¡\}}|t_| ¡}dd„|Dƒ}|d ¡t |¡|jt_|j   d¡|j   ||dg¡|j   d¡|  t ¡t |d¡Wdƒn1s²0YdS)z9Test the empty read EOF case on both master_fd and stdin.cSsg|] }| ¡‘qSr rqrsr r r rv0r+z7SmallPtyTests.test__copy_eof_on_all..rBrDrN)rir-r]r[rlr;r0rpr_rcrxrdryrzr{)r%r|r}r~rrkr€r r r Útest__copy_eof_on_all)s       z#SmallPtyTests.test__copy_eof_on_allN) rVrWrXÚ__doc__r&rfrirlrprr‚r r r r rZãs rZcCs tƒdSr)rr r r r ÚtearDownModuleAsr„Ú__main__)Z test.supportrrrr6r-r0rMr_rrjrr/r9r:r rrZTestCaserrZr„rVÚmainr r r r Ús.  !^