PyXR

c:\python24\lib \ test \ test_signal.py



0001 # Test the signal module
0002 from test.test_support import verbose, TestSkipped, TestFailed
0003 import signal
0004 import os, sys, time
0005 
0006 if sys.platform[:3] in ('win', 'os2') or sys.platform=='riscos':
0007     raise TestSkipped, "Can't test signal on %s" % sys.platform
0008 
0009 if verbose:
0010     x = '-x'
0011 else:
0012     x = '+x'
0013 pid = os.getpid()
0014 
0015 # Shell script that will send us asynchronous signals
0016 script = """
0017  (
0018         set %(x)s
0019         sleep 2
0020         kill -HUP %(pid)d
0021         sleep 2
0022         kill -USR1 %(pid)d
0023         sleep 2
0024         kill -USR2 %(pid)d
0025  ) &
0026 """ % vars()
0027 
0028 def handlerA(*args):
0029     if verbose:
0030         print "handlerA", args
0031 
0032 class HandlerBCalled(Exception):
0033     pass
0034 
0035 def handlerB(*args):
0036     if verbose:
0037         print "handlerB", args
0038     raise HandlerBCalled, args
0039 
0040 signal.alarm(20)                        # Entire test lasts at most 20 sec.
0041 hup = signal.signal(signal.SIGHUP, handlerA)
0042 usr1 = signal.signal(signal.SIGUSR1, handlerB)
0043 usr2 = signal.signal(signal.SIGUSR2, signal.SIG_IGN)
0044 alrm = signal.signal(signal.SIGALRM, signal.default_int_handler)
0045 
0046 try:
0047     os.system(script)
0048 
0049     print "starting pause() loop..."
0050 
0051     try:
0052         while 1:
0053             if verbose:
0054                 print "call pause()..."
0055             try:
0056                 signal.pause()
0057                 if verbose:
0058                     print "pause() returned"
0059             except HandlerBCalled:
0060                 if verbose:
0061                     print "HandlerBCalled exception caught"
0062                 else:
0063                     pass
0064 
0065     except KeyboardInterrupt:
0066         if verbose:
0067             print "KeyboardInterrupt (assume the alarm() went off)"
0068 
0069 finally:
0070     signal.signal(signal.SIGHUP, hup)
0071     signal.signal(signal.SIGUSR1, usr1)
0072     signal.signal(signal.SIGUSR2, usr2)
0073     signal.signal(signal.SIGALRM, alrm)
0074 

Generated by PyXR 0.9.4
SourceForge.net Logo