Exceptions raised during renaming in rotating file handlers are now passed to handleE...
[python.git] / Lib / test / test_signal.py
blob042cfa93e82f1af1bb2f57862f2945d5b1d64938
1 # Test the signal module
2 from test.test_support import verbose, TestSkipped, TestFailed
3 import signal
4 import os, sys, time
6 if sys.platform[:3] in ('win', 'os2') or sys.platform=='riscos':
7 raise TestSkipped, "Can't test signal on %s" % sys.platform
9 if verbose:
10 x = '-x'
11 else:
12 x = '+x'
13 pid = os.getpid()
15 # Shell script that will send us asynchronous signals
16 script = """
18 set %(x)s
19 sleep 2
20 kill -HUP %(pid)d
21 sleep 2
22 kill -USR1 %(pid)d
23 sleep 2
24 kill -USR2 %(pid)d
25 ) &
26 """ % vars()
28 def handlerA(*args):
29 if verbose:
30 print "handlerA", args
32 class HandlerBCalled(Exception):
33 pass
35 def handlerB(*args):
36 if verbose:
37 print "handlerB", args
38 raise HandlerBCalled, args
40 signal.alarm(20) # Entire test lasts at most 20 sec.
41 hup = signal.signal(signal.SIGHUP, handlerA)
42 usr1 = signal.signal(signal.SIGUSR1, handlerB)
43 usr2 = signal.signal(signal.SIGUSR2, signal.SIG_IGN)
44 alrm = signal.signal(signal.SIGALRM, signal.default_int_handler)
46 try:
47 os.system(script)
49 print "starting pause() loop..."
51 try:
52 while 1:
53 if verbose:
54 print "call pause()..."
55 try:
56 signal.pause()
57 if verbose:
58 print "pause() returned"
59 except HandlerBCalled:
60 if verbose:
61 print "HandlerBCalled exception caught"
62 else:
63 pass
65 except KeyboardInterrupt:
66 if verbose:
67 print "KeyboardInterrupt (assume the alarm() went off)"
69 finally:
70 signal.signal(signal.SIGHUP, hup)
71 signal.signal(signal.SIGUSR1, usr1)
72 signal.signal(signal.SIGUSR2, usr2)
73 signal.signal(signal.SIGALRM, alrm)