Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Lib / test / test_signal.py
blob737f1608c9f873a633a708289b994498aeb97d27
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 -5 %(pid)d
21 sleep 2
22 kill -2 %(pid)d
23 sleep 2
24 kill -3 %(pid)d
25 ) &
26 """ % vars()
28 def handlerA(*args):
29 if verbose:
30 print "handlerA", args
32 HandlerBCalled = "HandlerBCalled" # Exception
34 def handlerB(*args):
35 if verbose:
36 print "handlerB", args
37 raise HandlerBCalled, args
39 signal.alarm(20) # Entire test lasts at most 20 sec.
40 signal.signal(5, handlerA)
41 signal.signal(2, handlerB)
42 signal.signal(3, signal.SIG_IGN)
43 signal.signal(signal.SIGALRM, signal.default_int_handler)
45 os.system(script)
47 print "starting pause() loop..."
49 try:
50 while 1:
51 if verbose:
52 print "call pause()..."
53 try:
54 signal.pause()
55 if verbose:
56 print "pause() returned"
57 except HandlerBCalled:
58 if verbose:
59 print "HandlerBCalled exception caught"
60 else:
61 pass
63 except KeyboardInterrupt:
64 if verbose:
65 print "KeyboardInterrupt (assume the alarm() went off)"
68 if hasattr(signal, "sigprocmask"):
69 class HupDelivered(Exception):
70 pass
71 def hup(signum, frame):
72 raise HupDelivered
73 def hup2(signum, frame):
74 signal.signal(signal.SIGHUP, hup)
75 return
76 signal.signal(signal.SIGHUP, hup)
78 if verbose:
79 print "blocking SIGHUP"
81 defaultmask = signal.sigprocmask(signal.SIG_BLOCK, [signal.SIGHUP])
83 if verbose:
84 print "sending SIGHUP"
86 try:
87 os.kill(pid, signal.SIGHUP)
88 except HupDelivered:
89 raise TestFailed, "HUP not blocked"
91 if signal.SIGHUP not in signal.sigpending():
92 raise TestFailed, "HUP not pending"
94 if verbose:
95 print "unblocking SIGHUP"
97 try:
98 signal.sigprocmask(signal.SIG_UNBLOCK, [signal.SIGHUP])
99 except HupDelivered:
100 pass
101 else:
102 raise TestFailed, "HUP not delivered"
104 if verbose:
105 print "testing sigsuspend"
107 signal.sigprocmask(signal.SIG_BLOCK, [signal.SIGHUP])
108 signal.signal(signal.SIGHUP, hup2)
110 if not os.fork():
111 time.sleep(2)
112 os.kill(pid, signal.SIGHUP)
113 time.sleep(2)
114 os.kill(pid, signal.SIGHUP)
115 os._exit(0)
116 else:
117 try:
118 signal.sigsuspend(defaultmask)
119 except:
120 raise TestFailed, "sigsuspend erroneously raised"
122 try:
123 signal.sigsuspend(defaultmask)
124 except HupDelivered:
125 pass
126 else:
127 raise TestFailed, "sigsupsend didn't raise"