
    Vh                         d Z ddlmZ ddlmZ ddlmZ  ej                         sddlm	Z	  G d de      Z
 e       j                  e
j                                y)	z&
Tests for L{twisted.internet.stdio}.
    )Protocol)ReactorBuilder)platform)
StandardIOc                   h    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Z ej                         rd
Zyy)StdioFilesTestszI
    L{StandardIO} supports reading and writing to filesystem files.
    c                     | j                         }t        |d      j                          t        |d      | _        | j	                  | j                  j                         y )Nwbzrb+)mktempopenclose	extraFile
addCleanup)selfpaths     P/home/dcms/DCMS/lib/python3.12/site-packages/twisted/internet/test/test_stdio.pysetUpzStdioFilesTests.setUp   sD    {{}T4 dE*,,-    c                    | j                          G fddt              }| j                         }t        |d      5 }|j	                  d       ddd       t        |d      5 } |       }t        ||j                         | j                  j                                | j                         ddd       | j                  j                  d       y# 1 sw Y   xY w# 1 sw Y   2xY w)zc
        Adding a filesystem file reader to a reactor will make sure it is
        polled.
        c                       e Zd ZdZ fdZy)4StdioFilesTests.test_addReader.<locals>.DataProtocolr   c                 p    | xj                   |z  c_         | j                   dk(  rj                          y y )N   hello!)datastop)r   r   reactors     r   dataReceivedzAStdioFilesTests.test_addReader.<locals>.DataProtocol.dataReceived&   s-    		T!	 99	)LLN *r   N)__name__
__module____qualname__r   r   r   s   r   DataProtocolr   #   s    D#r   r"   r
   r   Nrbstdinstdoutr   )buildReactorr   r   r   writer   filenor   
runReactorassertEqualr   )r   r"   r   fprotocolr   s        @r   test_addReaderzStdioFilesTests.test_addReader   s    
 ##%		#8 		# {{}$ 	GGI	 $ 		%#~Hhhj~~,,.	 OOG$		% 		2	 			% 		%s   C%AC$C!$C-c                 f   | j                          G fddt              }| j                         }t        |d      5 } |       }t	        ||j                         | j                  j                                |j                  j                  d       |j                  j                  d       |j                  j                          | j                         ddd       t        |d      5 }| j                  |j                         d	       ddd       y# 1 sw Y   ?xY w# 1 sw Y   yxY w)
zc
        Adding a filesystem file writer to a reactor will make sure it is
        polled.
        c                       e Zd Z fdZy):StdioFilesTests.test_addWriter.<locals>.DisconnectProtocolc                 &    j                          y )N)r   )r   reasonr   s     r   connectionLostzIStdioFilesTests.test_addWriter.<locals>.DisconnectProtocol.connectionLostH   s    r   N)r   r   r    r4   r!   s   r   DisconnectProtocolr1   G   s    r   r5   r
   r&   r%   r      hellos   , worldNr#   s   hello, world)r'   r   r   r   r   r)   r   	transportr(   loseConnectionr*   r+   read)r   r5   r   r,   r-   r   s        @r   test_addWriterzStdioFilesTests.test_addWriter@   s   
 ##%	 	 {{}$ 	% *+Hxxznn++-	 $$X.$$Z0--/OOG$	%  $ 	8QVVX7	8 	8!	% 	% 	8 	8s   BD1!D'D$'D0c                    | j                         }| j                         }t        |d      j                          t        |d      5 }t	        t               |j                         | j                  j                         |      }| j                  |j                  |j                                |j                  j                          | j                  |j                  |j                                ddd       y# 1 sw Y   yxY w)zq
        Removing a filesystem file reader from a reactor will make sure it is
        no longer polled.
        r
   r#   r$   N)r'   r   r   r   r   r   r)   r   assertIn_reader
getReadersstopReadingassertNotInr   r   r   r,   stdios        r   test_removeReaderz!StdioFilesTests.test_removeReader`   s    
 ##%{{}T4 $ 
	B
hhj~~,,.	E MM%--););)=>MM%%'U]]G,>,>,@A
	B 
	B 
	Bs   B,C<<Dc                    | j                         }t        | j                         d      x| _        }t	               }t        ||j                         | j                  j                         |      }|j                  j                  d       | j                  |j                  |j                                |j                  j                          | j                  |j                  |j                                y)zq
        Removing a filesystem file writer from a reactor will make sure it is
        no longer polled.
        r
   r6   r7   N)r'   r   r   r,   r   r   r)   r   r8   r(   r=   _writer
getWritersstopWritingrA   )r   r   r,   r-   rC   s        r   test_removeWriterz!StdioFilesTests.test_removeWriterv   s    
 ##% $++-.. :QXXZt~~/D/D/FPW
 	  *emmW%7%7%9:!!#(:(:(<=r   c                    | j                         }| j                         }t        |d      j                          t        |d      x| _        }t        t               |j                         | j                  j                         |      }|j                         }| j                  |j                  |       | j                  |j                  |j                                y)zk
        Calling C{removeAll} on a reactor includes descriptors that are
        filesystem files.
        r
   r#   r$   N)r'   r   r   r   r,   r   r   r)   r   	removeAllr=   r>   rA   r?   )r   r   r   r,   rC   removeds         r   test_removeAllzStdioFilesTests.test_removeAll   s    
 ##%{{}T4  $%% J((*>>((*	
 ##%emmW-(:(:(<=r   c                    | j                         }| j                         }t        |d      j                          t        |d      5 }t	        t               |j                         | j                  j                         |      }| j                  |j                  |j                                ddd       y# 1 sw Y   yxY w)zW
        C{reactor.getReaders} includes descriptors that are filesystem files.
        r
   r#   r$   N)r'   r   r   r   r   r   r)   r   r=   r>   r?   rB   s        r   test_getReaderszStdioFilesTests.test_getReaders   s     ##%{{}T4  $ 	?
hhj~~,,.	E MM%--););)=>	? 	? 	?s   A(B88Cc                    | j                         }t        | j                         d      x| _        }t	        t               |j                         | j                  j                         |      }| j                  |j                  |j                                |j                  j                          | j                  |j                  |j                                y)zW
        C{reactor.getWriters} includes descriptors that are filesystem files.
        r
   r6   N)r'   r   r   r,   r   r   r)   r   rA   rF   rG   startWritingr=   )r   r   r,   rC   s       r   test_getWriterszStdioFilesTests.test_getWriters   s     ##% $++-.. J88:..'')	
 	(:(:(<=""$emmW%7%7%9:r   zoStandardIO does not accept stdout as an argument to Windows. Testing redirection to a file is therefore harder.N)r   r   r    __doc__r   r.   r;   rD   rI   rM   rO   rR   r   	isWindowsskip r   r   r   r      sR    ."3H8@B,>(>2?(;( xA 	 r   r   N)rS   twisted.internet.protocolr   #twisted.internet.test.reactormixinsr   twisted.python.runtimer   rT   twisted.internet.stdior   r   globalsupdatemakeTestCaseClassesrV   r   r   <module>r^      sP   
 / > +x1~
n ~
B 	  446 7r   