B
    u9a¬-  ã               @   sÒ   d dl mZmZmZ edƒ 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ZdZerzdd„ Zndd„ Zd	d
„ Zdd„ ZG dd„ dejƒZG dd„ dejƒZdd„ ZedkrÎe ¡  dS )é    )ÚverboseÚimport_moduleÚreap_childrenZtermiosNs   I wish to buy a fish license.
s   For my pet fish, Eric.
c             C   s   t | ƒ d S )N)Úprint)Úmsg© r   ú/usr/lib/python3.7/test_pty.pyÚdebug   s    r	   c             C   s   d S )Nr   )r   r   r   r   r	      s    c             C   s0   |   d¡r|  dd¡S |   d¡r,|  dd¡S | S )Ns   
ó   
s   
)ÚendswithÚreplace)Údatar   r   r   Únormalize_output%   s
    

r   c             C   s   t j| ddd}| ¡ S )z8Read one line.  May block forever if no newline is read.ÚrbF)ÚmodeÚclosefd)ÚioÚFileIOÚreadline)ÚfdÚreaderr   r   r   Ú	_readline9   s    r   c               @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚPtyTestc             C   s>   t   t j| j¡}|  t j t j|¡ |  t jd¡ t  d¡ d S )Nr   é
   )ÚsignalÚSIGALRMÚ
handle_sigZ
addCleanupÚalarm)ÚselfZ	old_alarmr   r   r   ÚsetUpD   s    zPtyTest.setUpc             C   s   |   d¡ d S )Nzisatty hung)Úfail)r   ZsigÚframer   r   r   r   L   s    zPtyTest.handle_sigc          
   C   s‚  yLt dƒ t ¡ \}}t d||f ƒ t d|f ƒ t |¡}t d| ƒ W n tk
rj   t d¡‚Y nX |  t 	|¡d¡ t 
|¡}z^t |d¡ yt |d¡}|  d	|¡ W n0 tk
râ } z|jtjkrÒ‚ W d d }~X Y nX W d t ||¡ X t d
ƒ t |t¡ t|ƒ}|  dt|ƒ¡ t dƒ t |td d… ¡ t |tdd … ¡ t|ƒ}|  dt|ƒ¡ t |¡ t |¡ d S )NzCalling master_open()z#Got master_fd '%d', slave_name '%s'zCalling slave_open(%r)zGot slave_fd '%d'z,Pseudo-terminals (seemingly) not functional.zslave_fd is not a ttyFi   ó    zWriting to slave_fds   I wish to buy a fish license.
zWriting chunked outputé   s   For my pet fish, Eric.
)r	   ÚptyZmaster_openZ
slave_openÚOSErrorÚunittestZSkipTestZ
assertTrueÚosÚisattyÚget_blockingÚset_blockingÚreadÚassertEqualÚerrnoZEAGAINÚwriteÚTEST_STRING_1r   r   ÚTEST_STRING_2Úclose)r   Ú	master_fdZ
slave_nameZslave_fdZblockingÚs1ÚeÚs2r   r   r   Ú
test_basicO   sB    



zPtyTest.test_basicc             C   sª  t dƒ t ¡ \}}|tjkrÀt d¡s:t dƒ t d¡ t dƒ yt ¡  W nR tk
rj   t dƒ Y nJ t	k
r„   t dƒ Y n0   t dƒ t d¡ Y nX t d	ƒ t d
¡ t d¡ nÜt d| ƒ xNyt 
|d¡}W n tk
rô   P Y nX |süP tj t| dd¡dd¡ qÎW t |d¡\}}|d? }t d|||f ƒ |dkr\|  d¡ n@|d
krr|  d¡ n*|dkrˆ|  d¡ n|dkrœ|  d¡ t |¡ d S )Nzcalling pty.fork()é   zChild's fd 1 is not a tty?!é   zIn child, calling os.setsid()zGood: OSError was raised.zNo setsid() available?zAn unexpected error was raised.zos.setsid() succeeded! (bad!)é   é   z!Waiting for child (%d) to finish.éP   s   
r
   Úascii)Úencodingr   é   z&Child (%d) exited with status %d (%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$   ÚforkZCHILDr'   r(   Ú_exitÚsetsidr%   ÚAttributeErrorr+   ÚsysÚstdoutr.   Ústrr   Úwaitpidr    r1   )r   Úpidr2   r   ÚstatusZresr   r   r   Ú	test_fork€   sV    









zPtyTest.test_forkN)Ú__name__Ú
__module__Ú__qualname__r   r   r6   rI   r   r   r   r   r   C   s   1r   c               @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚSmallPtyTestsz)These tests don't spawn children or hang.c             C   s4   t j| _t j| _t j| _g | _g | _g | _	g | _
d S )N)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   Ù   s    zSmallPtyTests.setUpc          	   C   s‚   | j t_| jt_| jt_x0| jD ]&}y| ¡  W q  t	k
rD   Y q X q W x2| j
D ](}yt |¡ W qR t	k
rx   Y qRX qRW d S )N)rO   r$   rN   rQ   rP   rS   rR   rU   r1   r%   rT   r'   )r   Úfiler   r   r   r   ÚtearDownâ   s    
zSmallPtyTests.tearDownc             C   s   t  ¡ }| j |¡ |S )N)r'   ÚpiperT   Úextend)r   Zpipe_fdsr   r   r   Ú_pipeñ   s    zSmallPtyTests._pipec             C   s   t  ¡ }| j |¡ |S )N)ÚsocketÚ
socketpairrU   r[   )r   r^   r   r   r   Ú_socketpairö   s    zSmallPtyTests._socketpairc             C   s*   |   | j d¡t|ƒ¡ | j d¡g g fS )Nr   )r,   rV   ÚpopÚlenrW   )r   ÚrfdsZwfdsZxfdsr   r   r   Ú_mock_selectû   s    zSmallPtyTests._mock_selectc          	   C   s  |   ¡ \}}|t_|   ¡ \}}|t_|  ¡ }dd„ |D ƒ}t |d d¡ t |d¡ | jt_| j	 
d¡ | j 
||d g¡ | j	 
d¡ |  t¡ t |d ¡ W dQ R X t ||d gg g d¡d }|  ||d g|¡ |  t |d	¡d¡ |  t |d d	¡d¡ dS )
z6Test the normal data case on both master_fd and stdin.c             S   s   g | ]}|  ¡ ‘qS r   )Úfileno)Ú.0Úsr   r   r   ú
<listcomp>  s    z4SmallPtyTests.test__copy_to_each.<locals>.<listcomp>r7   s   from masters
   from stdinr9   r   Né   )r\   r$   rP   rN   r_   r'   r.   rc   rR   rV   ÚappendrW   ÚassertRaisesÚ
IndexErrorÚ_copyr,   r+   )r   Úread_from_stdout_fdÚmock_stdout_fdÚmock_stdin_fdÚwrite_to_stdin_fdr^   Úmastersrb   r   r   r   Útest__copy_to_each   s$    z SmallPtyTests.test__copy_to_eachc          	   C   s¬   |   ¡ \}}|t_|   ¡ \}}|t_|  ¡ }dd„ |D ƒ}|d  ¡  t |¡ | jt_| j	 
d¡ | j 
||d g¡ | j	 
d¡ |  t¡ t |d ¡ W dQ R X dS )z9Test the empty read EOF case on both master_fd and stdin.c             S   s   g | ]}|  ¡ ‘qS r   )rd   )re   rf   r   r   r   rg   #  s    z7SmallPtyTests.test__copy_eof_on_all.<locals>.<listcomp>r7   r9   r   N)r\   r$   rP   rN   r_   r1   r'   rc   rR   rV   ri   rW   rj   rk   rl   )r   rm   rn   ro   rp   r^   rq   r   r   r   Útest__copy_eof_on_all  s    
z#SmallPtyTests.test__copy_eof_on_allN)rJ   rK   rL   Ú__doc__r   rY   r\   r_   rc   rr   rs   r   r   r   r   rM   Ö   s   	rM   c               C   s
   t ƒ  d S )N)r   r   r   r   r   ÚtearDownModule4  s    ru   Ú__main__)Ztest.supportr   r   r   r-   r$   r'   rC   rR   r   r]   r   r&   r/   r0   r	   r   r   ZTestCaser   rM   ru   rJ   Úmainr   r   r   r   Ú<module>   s.   

 ^