<s> """<STR_LIT>""" <EOL> import os , signal , errno <EOL> from twisted . python . log import msg <EOL> from twisted . trial . unittest import TestCase <EOL> from twisted . internet . fdesc import setNonBlocking <EOL> from twisted . internet . _signals import installHandler , isDefaultHandler <EOL> from twisted . internet . _signals import _extInstallHandler , _extIsDefaultHandler <EOL> from twisted . internet . _signals import _installHandlerUsingSetWakeup , _installHandlerUsingSignal , _isDefaultHandler <EOL> class SIGCHLDTestsMixin : <EOL> """<STR_LIT>""" <EOL> if getattr ( signal , '<STR_LIT>' , None ) is None : <EOL> skip = "<STR_LIT>" <EOL> def installHandler ( self , fd ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def isDefaultHandler ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def pipe ( self ) : <EOL> """<STR_LIT>""" <EOL> read , write = os . pipe ( ) <EOL> self . addCleanup ( os . close , read ) <EOL> self . addCleanup ( os . close , write ) <EOL> setNonBlocking ( read ) <EOL> setNonBlocking ( write ) <EOL> return read , write <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> handler = signal . getsignal ( signal . SIGCHLD ) <EOL> if handler != signal . SIG_DFL : <EOL> self . signalModuleHandler = handler <EOL> signal . signal ( signal . SIGCHLD , signal . SIG_DFL ) <EOL> else : <EOL> self . signalModuleHandler = None <EOL> self . oldFD = self . installHandler ( - <NUM_LIT:1> ) <EOL> if self . signalModuleHandler is not None and self . oldFD != - <NUM_LIT:1> : <EOL> msg ( "<STR_LIT>" % ( self . signalModuleHandler , self . oldFD ) ) <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> self . installHandler ( - <NUM_LIT:1> ) <EOL> signal . signal ( signal . SIGCHLD , signal . SIG_DFL ) <EOL> if self . signalModuleHandler is not None : <EOL> signal . signal ( signal . SIGCHLD , self . signalModuleHandler ) <EOL> elif self . oldFD != - <NUM_LIT:1> : <EOL> self . installHandler ( self . oldFD ) <EOL> def test_isDefaultHandler ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( self . isDefaultHandler ( ) ) <EOL> signal . signal ( signal . SIGCHLD , signal . SIG_IGN ) <EOL> self . assertFalse ( self . isDefaultHandler ( ) ) <EOL> signal . signal ( signal . SIGCHLD , signal . SIG_DFL ) <EOL> self . assertTrue ( self . isDefaultHandler ( ) ) <EOL> signal . signal ( signal . SIGCHLD , lambda * args : None ) <EOL> self . assertFalse ( self . isDefaultHandler ( ) ) <EOL> def test_returnOldFD ( self ) : <EOL> """<STR_LIT>""" <EOL> read , write = self . pipe ( ) <EOL> oldFD = self . installHandler ( write ) <EOL> self . assertEqual ( self . installHandler ( oldFD ) , write ) <EOL> def test_uninstallHandler ( self ) : <EOL> """<STR_LIT>""" <EOL> read , write = self . pipe ( ) <EOL> self . assertTrue ( self . isDefaultHandler ( ) ) <EOL> self . installHandler ( write ) <EOL> self . assertFalse ( self . isDefaultHandler ( ) ) <EOL> self . installHandler ( - <NUM_LIT:1> ) <EOL> self . assertTrue ( self . isDefaultHandler ( ) ) <EOL> def test_installHandler ( self ) : <EOL> """<STR_LIT>""" <EOL> read , write = self . pipe ( ) <EOL> self . installHandler ( write ) <EOL> exc = self . assertRaises ( OSError , os . read , read , <NUM_LIT:1> ) <EOL> self . assertEqual ( exc . errno , errno . EAGAIN ) <EOL> os . kill ( os . getpid ( ) , signal . SIGCHLD ) <EOL> self . assertEqual ( len ( os . read ( read , <NUM_LIT:5> ) ) , <NUM_LIT:1> ) <EOL> class DefaultSIGCHLDTests ( SIGCHLDTestsMixin , TestCase ) : <EOL> """<STR_LIT>""" <EOL> installHandler = staticmethod ( installHandler ) <EOL> isDefaultHandler = staticmethod ( isDefaultHandler ) <EOL> class ExtensionSIGCHLDTests ( SIGCHLDTestsMixin , TestCase ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import twisted . internet . _sigchld <EOL> except ImportError : <EOL> skip = "<STR_LIT>" <EOL> installHandler = _extInstallHandler <EOL> isDefaultHandler = _extIsDefaultHandler <EOL> class SetWakeupSIGCHLDTests ( SIGCHLDTestsMixin , TestCase ) : <EOL> """<STR_LIT>""" <EOL> if ( getattr ( signal , '<STR_LIT>' , None ) is None <EOL> or getattr ( signal , '<STR_LIT>' , None ) is None ) : <EOL> skip = "<STR_LIT>" <EOL> installHandler = staticmethod ( _installHandlerUsingSetWakeup ) <EOL> isDefaultHandler = staticmethod ( _isDefaultHandler ) <EOL> class PlainSignalModuleSIGCHLDTests ( SIGCHLDTestsMixin , TestCase ) : <EOL> """<STR_LIT>""" <EOL> installHandler = staticmethod ( _installHandlerUsingSignal ) <EOL> isDefaultHandler = staticmethod ( _isDefaultHandler ) </s>
