+
    Dfj+                        a  R t0 t R t^ RIt^ RIt^ RIt^ RIHt ] ^ k  ^ RIt^ RIH	t	 Rt
^ RIHt  ! R R4      t ! R R	4      t ! R
 R]4      t ! R R]4      tR#   ] d    Rt
 L=i ; i)z;
Tests for POSIX-based L{IReactorProcess} implementations.
N)Optional)processznon-POSIX platform)TestCasec                   <   a  ] tR t^t o RtR tR tR tR tRt	V t
R# )FakeFilez6
A dummy file object which records when it is closed.
c                    Wn         W n        R # N)testcasefd)selfr	   r
   s   &&&I/usr/lib/python3/dist-packages/twisted/internet/test/test_posixprocess.py__init__FakeFile.__init__    s         c                d    V P                   P                  P                  V P                  4       R # r   )r	   _filesremover
   r   s   &r   closeFakeFile.close$   s    ##DGG,r   c                    V # r    r   s   &r   	__enter__FakeFile.__enter__'   s    r   c                &    V P                  4        R # r   )r   )r   exc_type	exc_value	tracebacks   &&&&r   __exit__FakeFile.__exit__*   s    

r   )r
   r	   N)__name__
__module____qualname____firstlineno____doc__r   r   r   r   __static_attributes____classdictcell____classdict__s   @r   r   r      s#     - r   r   c                   4   a  ] tR t^.t o Rt^tR tR tRtV t	R# )FakeResourceModulez
Fake version of L{resource} which hard-codes a particular rlimit for maximum
open files.

@ivar _limit: The value to return for the hard limit of number of open files.
c                    Wn         R # r   _limitr   limits   &&r   r   FakeResourceModule.__init__8   s    r   c                F    WP                   8X  d   ^ V P                  .# ^{R.# )zH
A fake of L{resource.getrlimit} which returns a pre-determined result.
i  )RLIMIT_NOFILEr-   )r   nos   &&r   	getrlimitFakeResourceModule.getrlimit;   s)     ###t{{##Szr   r,   N)
r    r!   r"   r#   r$   r2   r   r4   r%   r&   r'   s   @r   r*   r*   .   s       M r   r*   c                      a  ] tR t^Dt o Rt]tRtRtRt	R t
R tR tR tR tR tR	 tR
 tR tR tR tR tR tR tRtV tR# )FDDetectorTestsa  
Tests for _FDDetector class in twisted.internet.process, which detects
which function to drop in place for the _listOpenFDs method.

@ivar devfs: A flag indicating whether the filesystem fake will indicate
    that /dev/fd exists.

@ivar accurateDevFDResults: A flag indicating whether the /dev/fd fake
    returns accurate open file information.

@ivar procfs: A flag indicating whether the filesystem fake will indicate
    that /proc/<pid>/fd exists.
Fc                    ^{# )z.
Fake os.getpid, always return the same thing
r   r   s   &r   getpidFDDetectorTests.getpidZ   s     r   c                   \        \        V P                  4      pV P                  '       d    VRV P	                  4       3,          8X  d   V# V P
                  '       d    VR8X  d   V P                  '       d   V# . RO# \        4       h)zl
Fake os.listdir, depending on what mode we're in to simulate behaviour.

@param arg: the directory to list
z/proc/%d/fdz/dev/fd)012)mapstrr   procfsr9   devfsaccurateDevFDResultsOSError)r   argaccurates   && r   listdirFDDetectorTests.listdir`   se     sDKK(;;;3=DKKM3C#CDO:::#*(((""ir   c           
         \        V \        \        \        R4      4      \        V P                  4      ,
          4      4      pV P                  P                  VP                  4       V# )a  
This is a mock for L{open}.  It keeps track of opened files so extra
descriptors can be returned from the mock for L{os.listdir} when used on
one of the list-of-filedescriptors directories.

A L{FakeFile} is returned which can be closed to remove the new
descriptor from the open list.
   )r   minsetranger   appendr
   )r   fnamemodefs   &&& r   openfileFDDetectorTests.openfileo   sG     T3s5;/#dkk2BBCD144 r   c                ,    R\         P                  R&   R# )zX
Make the L{resource} module unimportable for the remainder of the
current test method.
Nresource)sysmodulesr   s   &r   hideResourceModule"FDDetectorTests.hideResourceModule}   s    
 #'Jr   c                >    \        V4      \        P                  R&   R# )z
Make a L{FakeResourceModule} instance importable at the L{resource}
name.

@param limit: The value which will be returned for the hard limit of
    number of open files by the fake resource module's C{getrlimit}
    function.
rU   N)r*   rV   rW   r.   s   &&r   revealResourceModule$FDDetectorTests.revealResourceModule   s     #5U";Jr   c                    Vf    \         P                  R R# V\         P                  R&   R#   \         d     R# i ; i)z9
Restore the original resource module to L{sys.modules}.
NrU   )rV   rW   KeyError)r   values   &&r   replaceResourceModule%FDDetectorTests.replaceResourceModule   s>     =KK
+ ',CKK
#  s   . ==c                `   \         P                  ! 4       V n        V P                  V P                  n        V P                  V P                  n        V P
                  V P                  n        . ROV n        V P                  V P                  \        P                  P                  R4      4       R# )z
Set up the tests, giving ourselves a detector object to play with and
setting up its testable knobs to refer to our mocked versions.
rU   N          )r   _FDDetectordetectorrG   r9   rR   r   
addCleanupr`   rV   rW   getr   s   &r   setUpFDDetectorTests.setUp   sl    
  ++- $#{{!%22CKKOOJ4OPr   c                   a  R pR pV 3R lpVVV.S P                   n        S P                  VS P                   P                  4       4       R# )z
L{FDDetector._getImplementation} returns the first method from its
C{_implementations} list which returns results which reflect a newly
opened file descriptor.
c                      \        R 4      h)zThis does not work)
ValueErrorr   r   r   failWithExceptionBFDDetectorTests.test_selectFirstWorking.<locals>.failWithException   s    122r   c                  
    . RO# rd   rc   r   r   r   r   failWithWrongResultsEFDDetectorTests.test_selectFirstWorking.<locals>.failWithWrongResults       r   c                  *   < S P                   R ,          # ):NNN)r   r   s   r   correct8FDDetectorTests.test_selectFirstWorking.<locals>.correct   s    ;;q>!r   Nrh   _implementationsassertIs_getImplementation)r   rp   rt   rx   s   f   r   test_selectFirstWorking'FDDetectorTests.test_selectFirstWorking   sH    	3		"  *
& 	gt}}??ABr   c                    R pR pVV.V P                   n        V P                  W P                   P                  4       4       R# )z
L{FDDetector._getImplementation} returns the last method from its
C{_implementations} list if none of the implementations manage to return
results which reflect a newly opened file descriptor.
c                  
    . RO# )   )r      	   r   r   r   r   rt   =FDDetectorTests.test_selectLast.<locals>.failWithWrongResults   rv   r   c                  
    . RO# rs   r   r   r   r   failWithOtherWrongResultsBFDDetectorTests.test_selectLast.<locals>.failWithOtherWrongResults   rv   r   Nrz   )r   rt   r   s   &  r   test_selectLastFDDetectorTests.test_selectLast   s>    		 !%*
&
 	/1Q1Q1STr   c                8   \         P                  ! 4       pVP                  P                  pVP                  4        VP                  P                  pVP                  4        VP                  P                  pV P	                  W#4       V P                  W44       R# )z
Check that the identity of _listOpenFDs changes after running
_listOpenFDs the first time, but not after the second time it's run.

In other words, check that the monkey patching actually works.
N)r   rg   _listOpenFDsr    assertNotEqualassertEqual)r   rh   firstsecondthirds   &    r   !test_identityOfListOpenFDsChanges1FDDetectorTests.test_identityOfListOpenFDsChanges   sy     &&(%%..&&//%%..E*'r   c                    RV n         V P                  \        V P                  P                  4       RV n         RV n        V P                  . ROV P                  P	                  4       4       R# )z
L{_FDDetector._devFDImplementation} raises L{OSError} if there is no
I{/dev/fd} directory, otherwise it returns the basenames of its children
interpreted as integers.
FTNrc   )rB   assertRaisesrD   rh   _devFDImplementationrC   r   r   s   &r   test_devFDImplementation(FDDetectorTests.test_devFDImplementation   sQ     
'4==#E#EF
$)!DMM$F$F$HIr   c                    RV n         V P                  \        V P                  P                  4       RV n         V P                  . ROV P                  P	                  4       4       R# )z
L{_FDDetector._procFDImplementation} raises L{OSError} if there is no
I{/proc/<pid>/fd} directory, otherwise it returns the basenames of its
children interpreted as integers.
FTNrc   )rA   r   rD   rh   _procFDImplementationr   r   s   &r   test_procFDImplementation)FDDetectorTests.test_procFDImplementation   sI     '4==#F#FGDMM$G$G$IJr   c                ^   V P                  R4       V P                  \        \        R4      4      \        V P                  P                  4       4      4       V P                  R4       V P                  \        \        R4      4      \        V P                  P                  4       4      4       R# )z
L{_FDDetector._fallbackFDImplementation} uses the L{resource} module if
it is available, returning a range of integers from 0 to the
minimum of C{1024} and the hard I{NOFILE} limit.
i   i   rJ   N)r[   r   listrM   rh   _fallbackFDImplementationr   s   &r   test_resourceFDImplementation-FDDetectorTests.test_resourceFDImplementation   s~     	!!#&sd4==#J#J#LM	

 	!!$'ttDMM$K$K$MN	
r   c                    V P                  4        V P                  \        \        R4      4      \        V P                  P                  4       4      4       R# )z
L{_FDDetector._fallbackFDImplementation}, the implementation of last
resort, succeeds with a fixed range of integers from 0 to 1024 when the
L{resource} module is not importable.
rJ   N)rX   r   r   rM   rh   r   r   s   &r   test_fallbackFDImplementation-FDDetectorTests.test_fallbackFDImplementation  s>     	!ttDMM$K$K$MN	
r   )r   rC   rh   rB   rA   N)r    r!   r"   r#   r$   platformSkipskiprB   rC   rA   r9   rG   rR   rX   r[   r`   rk   r~   r   r   r   r   r   r   r%   r&   r'   s   @r   r7   r7   D   sq      DE F'	<
,
QC0U((&
J	K
$	
 	
r   r7   c                   4   a  ] tR tRt o Rt]tR tR tRt	V t
R# )FileDescriptorTestsi  z4
Tests for L{twisted.internet.process._listOpenFDs}
c           	     6   \         P                  ! 4        F)  p \        P                  ! V\        P                  4       K+  	  R#   \         dI   pT P                  \        P                  TP                  RYP                  3,          4        Rp?K}  Rp?ii ; i)z
File descriptors returned by L{_listOpenFDs} are mostly open.

This test assumes that zero-legth writes fail with EBADF on closed
file descriptors.
z2fcntl(%d, F_GETFL) failed with unexpected errno %dN)r   r   fcntlF_GETFLrD   r   errnoEBADF)r   r
   errs   &  r   test_openFDs FileDescriptorTests.test_openFDs"  sr     &&(BB. )    KKIIH99o& s   %AB=BBc                F   \        \        P                  4      p\        P                  ! 4       pV P                  VP                  4       V4       \        P                  ! VP                  4       4      pV P                  W1P                  4       8  R4        VP                  4        V P                  V\        P                  ! 4       4       \        P                  ! V4       V P                  V\        P                  ! 4       4       R#   \        P                  ! T4       i ; i)z2
L{_listOpenFDs} lists expected file descriptors.
z>Expected duplicate file descriptor to be greater than originalN)openosdevnullr   r   assertInfilenodup
assertTruer   assertNotIn)r   rQ   openfdsr
   s   &   r   test_expectedFDs$FileDescriptorTests.test_expectedFDs4  s     &&(ahhj'* VVAHHJ 	OL	

	 GGIMM"g2245 HHRLW1134 HHRLs   5D D r   N)r    r!   r"   r#   r$   r   r   r   r   r%   r&   r'   s   @r   r   r     s       D$)5 )5r   r   c                V    V ^8  d   Qh/ ^ \         9   d   \        \        ,          ;R&   # )rf   r   )__conditional_annotations__r   r@   )formats   "r   __annotate__r      s"        hsm r   )r   r$   r   r   rV   typingr   r   twisted.internetr   r   ImportErrortwisted.trial.unittestr   r   r*   r7   r   r   )r   s   @r   <module>r      sy   
  	 
   )L + & ,T
h T
nB5( B5U  ('L(s   A 	A*)A*